Spaces:
Runtime error
Runtime error
MarcSkovMadsen
commited on
Commit
•
9beecf3
1
Parent(s):
82b27f7
Insert code
Browse files
app.py
CHANGED
@@ -1,147 +1,131 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
from
|
4 |
|
5 |
-
import
|
6 |
import panel as pn
|
7 |
-
|
8 |
-
from
|
9 |
-
|
10 |
-
pn.
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
)
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
def get_similarity_scores(class_items: List[str], image: Image) -> List[float]:
|
45 |
-
processor, model = load_processor_model(
|
46 |
-
"openai/clip-vit-base-patch32", "openai/clip-vit-base-patch32"
|
47 |
-
)
|
48 |
-
inputs = processor(
|
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 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
)
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
)
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
from concurrent.futures import ThreadPoolExecutor
|
3 |
+
from contextlib import contextmanager
|
4 |
|
5 |
+
import numpy as np
|
6 |
import panel as pn
|
7 |
+
import param
|
8 |
+
from asyncio import wrap_future
|
9 |
+
|
10 |
+
class ProgressExtMod(pn.viewable.Viewer):
|
11 |
+
"""A custom component for easy progress reporting"""
|
12 |
+
|
13 |
+
completed = param.Integer(default=0)
|
14 |
+
bar_color = param.String(default="info")
|
15 |
+
num_tasks = param.Integer(default=100, bounds=(1, None))
|
16 |
+
|
17 |
+
# @param.depends('completed', 'num_tasks')
|
18 |
+
@property
|
19 |
+
def value(self) -> int:
|
20 |
+
"""Returns the progress value
|
21 |
+
|
22 |
+
Returns:
|
23 |
+
int: The progress value
|
24 |
+
"""
|
25 |
+
return int(100 * (self.completed / self.num_tasks))
|
26 |
+
|
27 |
+
def reset(self):
|
28 |
+
"""Resets the value and message"""
|
29 |
+
# Please note the order matters as the Widgets updates two times. One for each change
|
30 |
+
self.completed = 0
|
31 |
+
|
32 |
+
def __panel__(self):
|
33 |
+
return self.view
|
34 |
+
|
35 |
+
@param.depends("completed", "bar_color")
|
36 |
+
def view(self):
|
37 |
+
"""View the widget
|
38 |
+
Returns:
|
39 |
+
pn.viewable.Viewable: Add this to your app to see the progress reported
|
40 |
+
"""
|
41 |
+
if self.value:
|
42 |
+
return pn.widgets.Progress(
|
43 |
+
active=True, value=self.value, align="center", sizing_mode="stretch_width"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
)
|
45 |
+
return None
|
46 |
+
|
47 |
+
@contextmanager
|
48 |
+
def increment(self):
|
49 |
+
"""Increments the value
|
50 |
+
|
51 |
+
Can be used as context manager or decorator
|
52 |
+
|
53 |
+
Yields:
|
54 |
+
None: Nothing is yielded
|
55 |
+
"""
|
56 |
+
self.completed += 1
|
57 |
+
yield
|
58 |
+
if self.completed == self.num_tasks:
|
59 |
+
self.reset()
|
60 |
+
|
61 |
+
executor = ThreadPoolExecutor(max_workers=2) # pylint: disable=consider-using-with
|
62 |
+
progress = ProgressExtMod()
|
63 |
+
|
64 |
+
|
65 |
+
class AsyncComponent(pn.viewable.Viewer):
|
66 |
+
"""A component that demonstrates how to run a Blocking Background task asynchronously
|
67 |
+
in Panel"""
|
68 |
+
|
69 |
+
select = param.Selector(objects=range(10))
|
70 |
+
slider = param.Number(2, bounds=(0, 10))
|
71 |
+
|
72 |
+
run_blocking_task = param.Event(label="RUN")
|
73 |
+
result = param.Number(0)
|
74 |
+
view = param.Parameter()
|
75 |
+
|
76 |
+
def __init__(self, **params):
|
77 |
+
super().__init__(**params)
|
78 |
+
|
79 |
+
self._layout = pn.Column(
|
80 |
+
pn.pane.Markdown("## Blocking Task Running in Background"),
|
81 |
+
pn.Param(
|
82 |
+
self,
|
83 |
+
parameters=["run_blocking_task", "result"],
|
84 |
+
widgets={"result": {"disabled": True}, "run_blocking_task": {"button_type": "primary"}},
|
85 |
+
show_name=False,
|
86 |
+
),
|
87 |
+
progress,
|
88 |
+
pn.pane.Markdown("## Other, Non-Blocked Tasks"),
|
89 |
+
pn.Param(
|
90 |
+
self,
|
91 |
+
parameters=["select", "slider"],
|
92 |
+
widgets={"text": {"disabled": True}},
|
93 |
+
show_name=False,
|
94 |
+
),
|
95 |
+
self.text
|
96 |
+
)
|
97 |
+
|
98 |
+
def __panel__(self):
|
99 |
+
return self._layout
|
100 |
+
|
101 |
+
@param.depends("slider", "select")
|
102 |
+
def text(self):
|
103 |
+
if self.select:
|
104 |
+
select = self.select
|
105 |
+
else:
|
106 |
+
select = 0
|
107 |
+
return f"{select} + {self.slider} = {select + self.slider}"
|
108 |
+
|
109 |
+
@pn.depends("run_blocking_task", watch=True)
|
110 |
+
async def _run_blocking_tasks(self, num_tasks=10):
|
111 |
+
"""Runs background tasks num_tasks times"""
|
112 |
+
num_tasks = 20
|
113 |
+
progress.num_tasks = num_tasks
|
114 |
+
for _ in range(num_tasks):
|
115 |
+
future = executor.submit(self._run_blocking_task)
|
116 |
+
result = await wrap_future(future)
|
117 |
+
self._update(result)
|
118 |
+
|
119 |
+
@progress.increment()
|
120 |
+
def _update(self, number):
|
121 |
+
self.result += number
|
122 |
+
|
123 |
+
@staticmethod
|
124 |
+
def _run_blocking_task():
|
125 |
+
time.sleep(np.random.randint(1, 2))
|
126 |
+
return 5
|
127 |
+
|
128 |
+
if __name__.startswith("bokeh"):
|
129 |
+
pn.extension(template="fast")
|
130 |
+
pn.pane.Markdown(__doc__).servable()
|
131 |
+
AsyncComponent().servable() # pylint: disable=no-value-for-parameter
|