Spaces:
Runtime error
Runtime error
not sure what the error is
Browse files- pages/2_Concepts_comparison.py +11 -27
pages/2_Concepts_comparison.py
CHANGED
@@ -107,14 +107,7 @@ with output_col_1:
|
|
107 |
print(np.round(similarity[0][0], 3))
|
108 |
edges.append((concept_ids[i], concept_ids[j], np.round(similarity[0][0], 3)))
|
109 |
|
110 |
-
|
111 |
-
# G = nx.Graph()
|
112 |
-
|
113 |
-
# # Add edges with weights to the graph
|
114 |
-
# for edge in edges:
|
115 |
-
# node1, node2, weight = edge
|
116 |
-
# G.add_edge(node1, node2, weight=weight)
|
117 |
-
|
118 |
net = Network(height="750px", width="100%",)
|
119 |
for e in edges:
|
120 |
src = e[0]
|
@@ -124,7 +117,6 @@ with output_col_1:
|
|
124 |
net.add_node(src, src, title=src)
|
125 |
net.add_node(dst, dst, title=dst)
|
126 |
net.add_edge(src, dst, value=w, title=src + ' to ' + dst + ' similarity ' +str(w))
|
127 |
-
print(net)
|
128 |
|
129 |
# Generate network with specific layout settings
|
130 |
net.repulsion(
|
@@ -158,36 +150,28 @@ with output_col_2:
|
|
158 |
#st.write('Concept vector', separation_vector)
|
159 |
header_col_2.write(f'Concepts {", ".join(concept_ids)} - Latent space CLIP')# - Nodes {",".join(list(imp_nodes))}')
|
160 |
|
161 |
-
|
162 |
for c1 in concept_ids:
|
163 |
for c2 in concept_ids:
|
164 |
if c1 != c2:
|
165 |
print(f'Similarity between {c1} and {c2}')
|
166 |
similarity = cosine_similarity(vectors[c1].reshape(1, -1), vectors[c2].reshape(1, -1))
|
167 |
print(np.round(similarity[0][0], 3))
|
168 |
-
|
169 |
-
|
170 |
-
# # Create an empty graph
|
171 |
-
# G = nx.Graph()
|
172 |
|
173 |
-
# # Add edges with weights to the graph
|
174 |
-
# for edge in edges:
|
175 |
-
# node1, node2, weight = edge
|
176 |
-
# G.add_edge(node1, node2, weight=weight)
|
177 |
|
178 |
-
|
179 |
-
for e in
|
180 |
src = e[0]
|
181 |
dst = e[1]
|
182 |
w = e[2]
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
print(net)
|
188 |
|
189 |
# Generate network with specific layout settings
|
190 |
-
|
191 |
node_distance=420,
|
192 |
central_gravity=0.33,
|
193 |
spring_length=110,
|
@@ -198,13 +182,13 @@ with output_col_2:
|
|
198 |
# Save and read graph as HTML file (on Streamlit Sharing)
|
199 |
try:
|
200 |
path = '/tmp'
|
201 |
-
|
202 |
HtmlFile = open(f'{path}/pyvis_graph_clip.html', 'r', encoding='utf-8')
|
203 |
|
204 |
# Save and read graph as HTML file (locally)
|
205 |
except:
|
206 |
path = '/html_files'
|
207 |
-
|
208 |
HtmlFile = open(f'{path}/pyvis_graph_clip.html', 'r', encoding='utf-8')
|
209 |
|
210 |
# Load HTML file in HTML component for display on Streamlit page
|
|
|
107 |
print(np.round(similarity[0][0], 3))
|
108 |
edges.append((concept_ids[i], concept_ids[j], np.round(similarity[0][0], 3)))
|
109 |
|
110 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
net = Network(height="750px", width="100%",)
|
112 |
for e in edges:
|
113 |
src = e[0]
|
|
|
117 |
net.add_node(src, src, title=src)
|
118 |
net.add_node(dst, dst, title=dst)
|
119 |
net.add_edge(src, dst, value=w, title=src + ' to ' + dst + ' similarity ' +str(w))
|
|
|
120 |
|
121 |
# Generate network with specific layout settings
|
122 |
net.repulsion(
|
|
|
150 |
#st.write('Concept vector', separation_vector)
|
151 |
header_col_2.write(f'Concepts {", ".join(concept_ids)} - Latent space CLIP')# - Nodes {",".join(list(imp_nodes))}')
|
152 |
|
153 |
+
edges_clip = []
|
154 |
for c1 in concept_ids:
|
155 |
for c2 in concept_ids:
|
156 |
if c1 != c2:
|
157 |
print(f'Similarity between {c1} and {c2}')
|
158 |
similarity = cosine_similarity(vectors[c1].reshape(1, -1), vectors[c2].reshape(1, -1))
|
159 |
print(np.round(similarity[0][0], 3))
|
160 |
+
edges_clip.append((c1, c2, np.round(similarity[0][0], 3)))
|
|
|
|
|
|
|
161 |
|
|
|
|
|
|
|
|
|
162 |
|
163 |
+
net_clip = Network(height="750px", width="100%",)
|
164 |
+
for e in edges_clip:
|
165 |
src = e[0]
|
166 |
dst = e[1]
|
167 |
w = e[2]
|
168 |
|
169 |
+
net_clip.add_node(src, src, title=src)
|
170 |
+
net_clip.add_node(dst, dst, title=dst)
|
171 |
+
net_clip.add_edge(src, dst, value=w, title=src + ' to ' + dst + ' similarity ' +str(w))
|
|
|
172 |
|
173 |
# Generate network with specific layout settings
|
174 |
+
net_clip.repulsion(
|
175 |
node_distance=420,
|
176 |
central_gravity=0.33,
|
177 |
spring_length=110,
|
|
|
182 |
# Save and read graph as HTML file (on Streamlit Sharing)
|
183 |
try:
|
184 |
path = '/tmp'
|
185 |
+
net_clip.save_graph(f'{path}/pyvis_graph_clip.html')
|
186 |
HtmlFile = open(f'{path}/pyvis_graph_clip.html', 'r', encoding='utf-8')
|
187 |
|
188 |
# Save and read graph as HTML file (locally)
|
189 |
except:
|
190 |
path = '/html_files'
|
191 |
+
net_clip.save_graph(f'{path}/pyvis_graph_clip.html')
|
192 |
HtmlFile = open(f'{path}/pyvis_graph_clip.html', 'r', encoding='utf-8')
|
193 |
|
194 |
# Load HTML file in HTML component for display on Streamlit page
|