Spaces:
Runtime error
Runtime error
MarcSkovMadsen
commited on
Commit
β’
04ce1c2
1
Parent(s):
7e2c9a6
Update app.py
Browse filescytoscape example
app.py
CHANGED
@@ -1,147 +1,70 @@
|
|
1 |
-
import io
|
2 |
-
import random
|
3 |
-
from typing import List, Tuple
|
4 |
-
|
5 |
-
import aiohttp
|
6 |
import panel as pn
|
7 |
-
|
8 |
-
from
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
"
|
14 |
-
"
|
15 |
-
"
|
16 |
-
"
|
17 |
-
"
|
18 |
}
|
19 |
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
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 |
-
@pn.cache
|
30 |
-
def load_processor_model(
|
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 |
-
async def open_image_url(image_url: str) -> Image:
|
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 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 |
-
|
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 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
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 |
-
input_widgets = pn.Column(
|
115 |
-
"##### π Click randomize or paste a URL to start classifying!",
|
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 |
-
# add footer
|
127 |
-
footer_row = pn.Row(pn.Spacer(), align="center")
|
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 |
-
# create dashboard
|
135 |
-
main = pn.WidgetBox(
|
136 |
-
input_widgets,
|
137 |
-
interactive_result,
|
138 |
-
footer_row,
|
139 |
-
)
|
140 |
-
|
141 |
-
title = "Panel Demo - Image Classification"
|
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 |
import panel as pn
|
2 |
+
import param
|
3 |
+
from panel.reactive import ReactiveHTML
|
4 |
+
|
5 |
+
layouts = {
|
6 |
+
"cose": {"name": "cose"},
|
7 |
+
"grid": {"name": "grid"},
|
8 |
+
"circle": {"name": "circle"},
|
9 |
+
"concentric": {"name": "concentric"},
|
10 |
+
"breadthfirst": {"name": "breadthfirst"},
|
11 |
+
"random": {"name": "random"},
|
12 |
+
"preset": {"name": "preset"},
|
13 |
}
|
14 |
|
15 |
|
16 |
+
class Cytoscape(ReactiveHTML):
|
17 |
+
layout_names = param.ObjectSelector(
|
18 |
+
default="cose", doc="Layout Options to choose from.", objects=list(layouts.keys())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
)
|
20 |
+
graph_layouts = param.Dict(default=layouts)
|
21 |
+
style = param.List(doc="Use to set the styles of the nodes/edges")
|
22 |
+
data = param.List(doc="Use to send node's data/attributes to Cytoscape")
|
23 |
+
|
24 |
+
_template = """
|
25 |
+
<div id="pnx-container">
|
26 |
+
<div id="cy" style="position: relative; width: 200px; height: 200px; border: 1px solid"></div>
|
27 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
"""
|
29 |
+
__javascript__ = ["https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.25.0/cytoscape.umd.js"]
|
30 |
+
|
31 |
+
_scripts = {
|
32 |
+
"after_layout": """
|
33 |
+
self.create()
|
34 |
+
""",
|
35 |
+
"create": """
|
36 |
+
state.cy = cytoscape({
|
37 |
+
container: cy,
|
38 |
+
layout: {name: 'cose'},
|
39 |
+
elements: [{data:{id:'A', label:'A'}},{data:{id:'B', label:'B'}}, {data:{source:'A', target:'B'}}],
|
40 |
+
zoom: 1,
|
41 |
+
pan: {x: 0, y: 0},
|
42 |
+
});
|
43 |
+
state.cy.on('click', function (evt) {
|
44 |
+
console.log(evt.originalEvent, evt)
|
45 |
+
state.cy.resize();
|
46 |
+
});
|
47 |
+
""",
|
48 |
+
"remove": """
|
49 |
+
state.cy.destroy()
|
50 |
+
delete state.cy
|
51 |
+
""",
|
52 |
+
"layout_names": """
|
53 |
+
console.log("layout_names")
|
54 |
+
const layout = state.cy.layout(data.graph_layouts[data.layout_names])
|
55 |
+
layout.run()
|
56 |
+
""",
|
57 |
+
}
|
58 |
+
|
59 |
+
_extension_name = "cytoscape"
|
60 |
+
|
61 |
+
|
62 |
+
pn.extension("cytoscape", template="fast")
|
63 |
+
|
64 |
+
my_graph = Cytoscape(width=800, height=400)
|
65 |
+
|
66 |
+
widget_container = pn.Column(pn.pane.Markdown("## Title"))
|
67 |
+
app = pn.Column(widget_container).servable(target="main")
|
68 |
+
widget_container.append(pn.Row(my_graph.param.layout_names, my_graph))
|
69 |
+
|
70 |
+
app.servable()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|