Update app.py
Browse files
app.py
CHANGED
|
@@ -1,147 +1,92 @@
|
|
| 1 |
-
|
| 2 |
-
import random
|
| 3 |
-
from typing import List, Tuple
|
| 4 |
|
| 5 |
-
import aiohttp
|
| 6 |
import panel as pn
|
| 7 |
-
|
| 8 |
-
from transformers import CLIPModel, CLIPProcessor
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
ICON_URLS = {
|
| 13 |
-
"brand-github": "https://github.com/holoviz/panel",
|
| 14 |
-
"brand-twitter": "https://twitter.com/Panel_Org",
|
| 15 |
-
"brand-linkedin": "https://www.linkedin.com/company/panel-org",
|
| 16 |
-
"message-circle": "https://discourse.holoviz.org/",
|
| 17 |
-
"brand-discord": "https://discord.gg/AXRHnJU6sP",
|
| 18 |
-
}
|
| 19 |
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
api_url = f"https://api.the{pet}api.com/v1/images/search"
|
| 24 |
-
async with aiohttp.ClientSession() as session:
|
| 25 |
-
async with session.get(api_url) as resp:
|
| 26 |
-
return (await resp.json())[0]["url"]
|
| 27 |
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
model = CLIPModel.from_pretrained(model_name)
|
| 35 |
-
return processor, model
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
async with session.get(image_url) as resp:
|
| 41 |
-
return Image.open(io.BytesIO(await resp.read()))
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
return class_likelihoods[0]
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
async def process_inputs(class_names: List[str], image_url: str):
|
| 60 |
-
"""
|
| 61 |
-
High level function that takes in the user inputs and returns the
|
| 62 |
-
classification results as panel objects.
|
| 63 |
-
"""
|
| 64 |
-
try:
|
| 65 |
-
main.disabled = True
|
| 66 |
-
if not image_url:
|
| 67 |
-
yield "##### ⚠️ Provide an image URL"
|
| 68 |
-
return
|
| 69 |
-
|
| 70 |
-
yield "##### ⚙ Fetching image and running model..."
|
| 71 |
-
try:
|
| 72 |
-
pil_img = await open_image_url(image_url)
|
| 73 |
-
img = pn.pane.Image(pil_img, height=400, align="center")
|
| 74 |
-
except Exception as e:
|
| 75 |
-
yield f"##### 😔 Something went wrong, please try a different URL!"
|
| 76 |
-
return
|
| 77 |
-
|
| 78 |
-
class_items = class_names.split(",")
|
| 79 |
-
class_likelihoods = get_similarity_scores(class_items, pil_img)
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
for class_item, class_likelihood in zip(class_items, class_likelihoods):
|
| 85 |
-
row_label = pn.widgets.StaticText(
|
| 86 |
-
name=class_item.strip(), value=f"{class_likelihood:.2%}", align="center"
|
| 87 |
-
)
|
| 88 |
-
row_bar = pn.indicators.Progress(
|
| 89 |
-
value=int(class_likelihood * 100),
|
| 90 |
-
sizing_mode="stretch_width",
|
| 91 |
-
bar_color="secondary",
|
| 92 |
-
margin=(0, 10),
|
| 93 |
-
design=pn.theme.Material,
|
| 94 |
-
)
|
| 95 |
-
results.append(pn.Column(row_label, row_bar))
|
| 96 |
-
yield results
|
| 97 |
-
finally:
|
| 98 |
-
main.disabled = False
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# create widgets
|
| 102 |
-
randomize_url = pn.widgets.Button(name="Randomize URL", align="end")
|
| 103 |
-
|
| 104 |
-
image_url = pn.widgets.TextInput(
|
| 105 |
-
name="Image URL to classify",
|
| 106 |
-
value=pn.bind(random_url, randomize_url),
|
| 107 |
-
)
|
| 108 |
-
class_names = pn.widgets.TextInput(
|
| 109 |
-
name="Comma separated class names",
|
| 110 |
-
placeholder="Enter possible class names, e.g. cat, dog",
|
| 111 |
-
value="cat, dog, parrot",
|
| 112 |
-
)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
pn.Row(image_url, randomize_url),
|
| 117 |
-
class_names,
|
| 118 |
-
)
|
| 119 |
|
| 120 |
-
# add interactivity
|
| 121 |
-
interactive_result = pn.panel(
|
| 122 |
-
pn.bind(process_inputs, image_url=image_url, class_names=class_names),
|
| 123 |
-
height=600,
|
| 124 |
-
)
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
footer_row.append(pn.Spacer())
|
| 133 |
-
|
| 134 |
-
# create dashboard
|
| 135 |
-
main = pn.WidgetBox(
|
| 136 |
-
input_widgets,
|
| 137 |
-
interactive_result,
|
| 138 |
-
footer_row,
|
| 139 |
)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import panel and vega datasets
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 3 |
import panel as pn
|
| 4 |
+
import vega_datasets
|
|
|
|
| 5 |
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import altair as alt
|
| 8 |
+
# import numpy as np
|
| 9 |
+
# import pprint
|
| 10 |
+
import datetime as dt
|
| 11 |
+
from vega_datasets import data
|
| 12 |
+
# import matplotlib.pyplot as plt
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
df2=pd.read_csv("https://raw.githubusercontent.com/dallascard/SI649_public/main/altair_hw3/approval_topline.csv")
|
| 16 |
|
| 17 |
+
df2['timestamp']=pd.to_datetime(df2['timestamp'])
|
| 18 |
+
df2=pd.melt(df2, id_vars=['president', 'subgroup', 'timestamp'], value_vars=['approve','disapprove']).rename(columns={'variable':'choice', 'value':'rate'})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
+
# Enable Panel extensions
|
| 22 |
+
# pn.extension()
|
| 23 |
+
# pn.extension('vega', 'tabulator')
|
| 24 |
+
pn.extension(design='bootstrap')
|
| 25 |
+
pn.extension('vega')
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
template = pn.template.BootstrapTemplate(
|
| 28 |
+
title='SI649 Altair3',
|
| 29 |
+
)
|
| 30 |
|
| 31 |
+
# Define a function to create and return a plot
|
| 32 |
+
def create_plot(subgroup, date_range, moving_av_window):
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Apply any required transformations to the data in pandas)
|
| 35 |
+
df2_approve = df2[df2['choice'] == 'approve']
|
| 36 |
+
filtered_df = df2_approve[df2_approve['subgroup'] == subgroup]
|
| 37 |
+
filtered_df = filtered_df[(filtered_df['timestamp'].dt.date >= date_range[0]) & (filtered_df['timestamp'].dt.date <= date_range[1])]
|
| 38 |
+
filtered_df['mov_avg'] = filtered_df['rate'].rolling(window=moving_av_window).mean().shift(-moving_av_window//2)
|
| 39 |
|
| 40 |
+
# Line chart
|
| 41 |
+
line_chart = alt.Chart(filtered_df).mark_line(color='red', size=2).encode(
|
| 42 |
+
x='timestamp:T',
|
| 43 |
+
y='mov_avg:Q'
|
| 44 |
)
|
| 45 |
+
|
| 46 |
+
# Scatter plot with individual polls
|
| 47 |
+
scatter_plot = alt.Chart(filtered_df).mark_point(color='grey', size=2, opacity=0.7).encode(
|
| 48 |
+
x='timestamp:T',
|
| 49 |
+
y='rate:Q'
|
| 50 |
)
|
| 51 |
+
|
| 52 |
+
# Put them togetehr
|
| 53 |
+
plot = scatter_plot + line_chart
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Return the combined chart
|
| 56 |
+
return pn.pane.Vega(plot)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# # Create the selection widget
|
| 60 |
+
select = pn.widgets.Select(name='Select', options=['All polls', 'Adults', 'Voters'])
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# # Create the slider for the date range
|
| 64 |
+
date_range_slider = pn.widgets.DateRangeSlider(
|
| 65 |
+
name='Date Range Slider',
|
| 66 |
+
start=df2['timestamp'].dt.date.min(), end=df2['timestamp'].dt.date.max(),
|
| 67 |
+
value=(df2['timestamp'].dt.date.min(), df2['timestamp'].dt.date.max()),
|
| 68 |
+
step=1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
+
|
| 72 |
+
# # Create the slider for the moving average window
|
| 73 |
+
moving_av_slider = pn.widgets.IntSlider(name='Moving Average Window', start=1, end=100, value=1)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# Bind the widgets to the create_plot function
|
| 77 |
+
final = pn.Row(pn.bind(create_plot,
|
| 78 |
+
subgroup=select,
|
| 79 |
+
date_range=date_range_slider,
|
| 80 |
+
moving_av_window=moving_av_slider))
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# # Combine everything in a Panel Column to create an app
|
| 84 |
+
maincol=pn.Column()
|
| 85 |
+
maincol.append(final)
|
| 86 |
+
maincol.append(select)
|
| 87 |
+
maincol.append(date_range_slider)
|
| 88 |
+
maincol.append(moving_av_slider)
|
| 89 |
+
template.main.append(maincol)
|
| 90 |
+
|
| 91 |
+
# # set the app to be servable
|
| 92 |
+
template.serverable(title='SI649 Altair3')
|