Spaces:
Running
Running
Refactor app.py and update requirements.txt
Browse files- .ipynb_checkpoints/app-checkpoint.ipynb +0 -0
- app.ipynb +0 -0
- app.py +38 -132
- requirements.txt +0 -1
.ipynb_checkpoints/app-checkpoint.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
app.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
app.py
CHANGED
@@ -1,147 +1,53 @@
|
|
1 |
-
|
2 |
-
import random
|
3 |
-
from typing import List, Tuple
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
from transformers import CLIPModel, CLIPProcessor
|
9 |
-
|
10 |
-
pn.extension(design="bootstrap", sizing_mode="stretch_width")
|
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 |
-
async def random_url(_):
|
22 |
-
pet = random.choice(["cat", "dog"])
|
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 |
-
processor_name: str, model_name: str
|
32 |
-
) -> Tuple[CLIPProcessor, CLIPModel]:
|
33 |
-
processor = CLIPProcessor.from_pretrained(processor_name)
|
34 |
-
model = CLIPModel.from_pretrained(model_name)
|
35 |
-
return processor, model
|
36 |
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
async with aiohttp.ClientSession() as session:
|
40 |
-
async with session.get(image_url) as resp:
|
41 |
-
return Image.open(io.BytesIO(await resp.read()))
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
def
|
45 |
-
|
46 |
-
|
47 |
-
)
|
48 |
-
|
49 |
-
text=class_items,
|
50 |
-
images=[image],
|
51 |
-
return_tensors="pt", # pytorch tensors
|
52 |
)
|
53 |
-
outputs = model(**inputs)
|
54 |
-
logits_per_image = outputs.logits_per_image
|
55 |
-
class_likelihoods = logits_per_image.softmax(dim=1).detach().numpy()
|
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 |
-
# build the results column
|
82 |
-
results = pn.Column("##### 🎉 Here are the results!", img)
|
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 |
-
#
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
)
|
125 |
|
126 |
-
#
|
127 |
-
|
128 |
-
for icon, url in ICON_URLS.items():
|
129 |
-
href_button = pn.widgets.Button(icon=icon, width=35, height=35)
|
130 |
-
href_button.js_on_click(code=f"window.open('{url}')")
|
131 |
-
footer_row.append(href_button)
|
132 |
-
footer_row.append(pn.Spacer())
|
133 |
|
134 |
-
#
|
135 |
-
|
136 |
-
input_widgets,
|
137 |
-
interactive_result,
|
138 |
-
footer_row,
|
139 |
-
)
|
140 |
|
141 |
-
|
142 |
-
pn.template.BootstrapTemplate(
|
143 |
-
title=title,
|
144 |
-
main=main,
|
145 |
-
main_max_width="min(50%, 698px)",
|
146 |
-
header_background="#F08080",
|
147 |
-
).servable(title=title)
|
|
|
1 |
+
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
|
|
|
|
|
2 |
|
3 |
+
# %% auto 0
|
4 |
+
__all__ = ['csv_file', 'data', 'variable_widget', 'window_widget', 'sigma_widget', 'bound_plot', 'first_app', 'transform_data',
|
5 |
+
'create_plot']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# %% app.ipynb 1
|
8 |
+
import panel as pn
|
9 |
+
import hvplot.pandas
|
10 |
+
import pandas as pd
|
11 |
+
import numpy as np
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
+
# %% app.ipynb 2
|
16 |
+
pn.extension(design='material')
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
csv_file = ("https://raw.githubusercontent.com/holoviz/panel/main/examples/assets/occupancy.csv")
|
19 |
+
data = pd.read_csv(csv_file, parse_dates=["date"], index_col="date")
|
20 |
|
21 |
+
data.tail()
|
|
|
|
|
|
|
22 |
|
23 |
+
# %% app.ipynb 3
|
24 |
+
def transform_data(variable, window, sigma):
|
25 |
+
''' Calculates the rolling average and the outliers '''
|
26 |
+
avg = data[variable].rolling(window=window).mean()
|
27 |
+
residual = data[variable] - avg
|
28 |
+
std = residual.rolling(window=window).std()
|
29 |
+
outliers = np.abs(residual) > std * sigma
|
30 |
+
return avg, avg[outliers]
|
31 |
|
32 |
+
def create_plot(variable="Temperature", window=30, sigma=10):
|
33 |
+
''' Plots the rolling average and the outliers '''
|
34 |
+
avg, highlight = transform_data(variable, window, sigma)
|
35 |
+
return avg.hvplot(height=300, width=400, legend=False) * highlight.hvplot.scatter(
|
36 |
+
color="orange", padding=0.1, legend=False
|
|
|
|
|
|
|
37 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# %% app.ipynb 4
|
40 |
+
create_plot(variable='Temperature', window=20, sigma=10)
|
|
|
|
|
|
|
41 |
|
42 |
+
# %% app.ipynb 5
|
43 |
+
variable_widget = pn.widgets.Select(name="variable", value="Temperature", options=list(data.columns))
|
44 |
+
window_widget = pn.widgets.IntSlider(name="window", value=30, start=1, end=60)
|
45 |
+
sigma_widget = pn.widgets.IntSlider(name="sigma", value=10, start=0, end=20)
|
|
|
46 |
|
47 |
+
# %% app.ipynb 6
|
48 |
+
bound_plot = pn.bind(create_plot, variable=variable_widget, window=window_widget, sigma=sigma_widget)
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
# %% app.ipynb 7
|
51 |
+
first_app = pn.Column(variable_widget, window_widget, sigma_widget, bound_plot)
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
first_app.servable()
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -2,5 +2,4 @@ panel
|
|
2 |
jupyter
|
3 |
transformers
|
4 |
numpy
|
5 |
-
torch
|
6 |
aiohttp
|
|
|
2 |
jupyter
|
3 |
transformers
|
4 |
numpy
|
|
|
5 |
aiohttp
|