Update app.py
Browse files
app.py
CHANGED
@@ -126,6 +126,7 @@ def create_graph(entities: list[str], relationships: list[tuple[str, str, str]])
|
|
126 |
A networkx Graph object.
|
127 |
"""
|
128 |
logging.debug(f"create_graph called with entities: {entities}, relationships: {relationships}")
|
|
|
129 |
try:
|
130 |
G = nx.Graph()
|
131 |
G.add_nodes_from(entities)
|
@@ -138,6 +139,9 @@ def create_graph(entities: list[str], relationships: list[tuple[str, str, str]])
|
|
138 |
else:
|
139 |
logging.warning(f"Edge ({subject}, {object_}) not found in the graph.")
|
140 |
|
|
|
|
|
|
|
141 |
return G
|
142 |
except Exception as e:
|
143 |
error_message = f"Error in create_graph: {str(e)}"
|
@@ -155,6 +159,10 @@ import networkx as nx
|
|
155 |
def create_bokeh_plot(graph: nx.Graph, layout_type: str):
|
156 |
"""Creates a Bokeh plot of the given networkx graph."""
|
157 |
try:
|
|
|
|
|
|
|
|
|
158 |
if layout_type == 'spring':
|
159 |
pos = nx.spring_layout(graph, seed=42)
|
160 |
elif layout_type == 'fruchterman_reingold':
|
@@ -201,70 +209,73 @@ def create_bokeh_plot(graph: nx.Graph, layout_type: str):
|
|
201 |
def create_plotly_plot(graph: nx.Graph, layout_type: str):
|
202 |
"""Creates a Plotly plot of the given networkx graph."""
|
203 |
try:
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
|
|
|
|
|
|
268 |
layout=go.Layout(
|
269 |
title='Knowledge Graph',
|
270 |
titlefont_size=16,
|
@@ -280,7 +291,7 @@ def create_plotly_plot(graph: nx.Graph, layout_type: str):
|
|
280 |
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
|
281 |
)
|
282 |
|
283 |
-
|
284 |
|
285 |
except Exception as e:
|
286 |
error_message = f"Error creating Plotly plot: {str(e)}"
|
@@ -410,9 +421,11 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
|
410 |
|
411 |
def process_and_update(text: str, entity_types: str, predicates: str, layout_type: str, visualization_type: str):
|
412 |
G, fig, output = process_text(text, entity_types, predicates, layout_type, visualization_type)
|
|
|
413 |
return G, fig, output
|
414 |
|
415 |
def update_graph_wrapper(G: nx.Graph, layout_type: str, visualization_type: str):
|
|
|
416 |
if G is not None:
|
417 |
fig, _ = update_graph(G, layout_type, visualization_type)
|
418 |
return fig
|
|
|
126 |
A networkx Graph object.
|
127 |
"""
|
128 |
logging.debug(f"create_graph called with entities: {entities}, relationships: {relationships}")
|
129 |
+
print(f"create_graph input:\nentities: {entities}\nrelationships: {relationships}")
|
130 |
try:
|
131 |
G = nx.Graph()
|
132 |
G.add_nodes_from(entities)
|
|
|
139 |
else:
|
140 |
logging.warning(f"Edge ({subject}, {object_}) not found in the graph.")
|
141 |
|
142 |
+
if not nx.is_graph(G): # Add this check
|
143 |
+
logging.error("Error: create_graph did not create a valid networkx graph")
|
144 |
+
return nx.Graph() # Return an empty graph
|
145 |
return G
|
146 |
except Exception as e:
|
147 |
error_message = f"Error in create_graph: {str(e)}"
|
|
|
159 |
def create_bokeh_plot(graph: nx.Graph, layout_type: str):
|
160 |
"""Creates a Bokeh plot of the given networkx graph."""
|
161 |
try:
|
162 |
+
if not nx.is_graph(graph): # Add this check
|
163 |
+
logging.error("Error: create_bokeh_plot received an invalid networkx graph")
|
164 |
+
return None # Or return a placeholder plot
|
165 |
+
|
166 |
if layout_type == 'spring':
|
167 |
pos = nx.spring_layout(graph, seed=42)
|
168 |
elif layout_type == 'fruchterman_reingold':
|
|
|
209 |
def create_plotly_plot(graph: nx.Graph, layout_type: str):
|
210 |
"""Creates a Plotly plot of the given networkx graph."""
|
211 |
try:
|
212 |
+
if not nx.is_graph(graph): # Add this check
|
213 |
+
logging.error("Error: create_plotly_plot received an invalid networkx graph")
|
214 |
+
return None # Or return a placeholder plot
|
215 |
+
import plotly.graph_objects as go
|
216 |
+
|
217 |
+
if layout_type == 'spring':
|
218 |
+
pos = nx.spring_layout(graph, seed=42)
|
219 |
+
elif layout_type == 'fruchterman_reingold':
|
220 |
+
pos = nx.fruchterman_reingold_layout(graph, seed=42)
|
221 |
+
elif layout_type == 'circular':
|
222 |
+
pos = nx.circular_layout(graph)
|
223 |
+
elif layout_type == 'random':
|
224 |
+
pos = nx.random_layout(graph, seed=42)
|
225 |
+
elif layout_type == 'spectral':
|
226 |
+
pos = nx.spectral_layout(graph)
|
227 |
+
elif layout_type == 'shell':
|
228 |
+
pos = nx.shell_layout(graph)
|
229 |
+
else:
|
230 |
+
pos = nx.spring_layout(graph, seed=42) # Default layout
|
231 |
+
|
232 |
+
edge_x = []
|
233 |
+
edge_y = []
|
234 |
+
for edge in graph.edges():
|
235 |
+
x0, y0 = pos[edge[0]]
|
236 |
+
x1, y1 = pos[edge[1]]
|
237 |
+
edge_x.append(x0)
|
238 |
+
edge_y.append(y0)
|
239 |
+
edge_x.append(x1)
|
240 |
+
edge_y.append(y1)
|
241 |
+
edge_x.append(None)
|
242 |
+
edge_y.append(None)
|
243 |
+
|
244 |
+
edge_trace = go.Scatter(
|
245 |
+
x=edge_x, y=edge_y,
|
246 |
+
line=dict(width=0.5, color='#888'),
|
247 |
+
hoverinfo='none',
|
248 |
+
mode='lines')
|
249 |
+
|
250 |
+
node_x = []
|
251 |
+
node_y = []
|
252 |
+
for node in graph.nodes():
|
253 |
+
x, y = pos[node]
|
254 |
+
node_x.append(x)
|
255 |
+
node_y.append(y)
|
256 |
+
|
257 |
+
node_trace = go.Scatter(
|
258 |
+
x=node_x, y=node_y,
|
259 |
+
mode='markers',
|
260 |
+
hoverinfo='text',
|
261 |
+
marker=dict(
|
262 |
+
showscale=False,
|
263 |
+
colorscale='YlGnBu',
|
264 |
+
reversescale=True,
|
265 |
+
color=[],
|
266 |
+
size=10,
|
267 |
+
line_width=2))
|
268 |
+
|
269 |
+
node_adjacencies = []
|
270 |
+
node_text = []
|
271 |
+
for node, adjacencies in enumerate(graph.adjacency()):
|
272 |
+
node_adjacencies.append(len(adjacencies[1]))
|
273 |
+
node_text.append(f"{adjacencies[0]} (# of connections: {len(adjacencies[1])})")
|
274 |
+
|
275 |
+
node_trace.marker.color = node_adjacencies
|
276 |
+
node_trace.text = node_text
|
277 |
+
|
278 |
+
fig = go.Figure(data=[edge_trace, node_trace],
|
279 |
layout=go.Layout(
|
280 |
title='Knowledge Graph',
|
281 |
titlefont_size=16,
|
|
|
291 |
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
|
292 |
)
|
293 |
|
294 |
+
return fig
|
295 |
|
296 |
except Exception as e:
|
297 |
error_message = f"Error creating Plotly plot: {str(e)}"
|
|
|
421 |
|
422 |
def process_and_update(text: str, entity_types: str, predicates: str, layout_type: str, visualization_type: str):
|
423 |
G, fig, output = process_text(text, entity_types, predicates, layout_type, visualization_type)
|
424 |
+
print(f"process_and_update: G = {G}") # Debug
|
425 |
return G, fig, output
|
426 |
|
427 |
def update_graph_wrapper(G: nx.Graph, layout_type: str, visualization_type: str):
|
428 |
+
print(f"update_graph_wrapper: G = {G}") # Debug
|
429 |
if G is not None:
|
430 |
fig, _ = update_graph(G, layout_type, visualization_type)
|
431 |
return fig
|