AD2000X commited on
Commit
6e1b906
Β·
verified Β·
1 Parent(s): c52071b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -80
app.py CHANGED
@@ -260,130 +260,80 @@ def run_entity_exploration():
260
  max_distance=max_distance,
261
  include_classes=True
262
  )
263
- elif not entities:
264
- st.warning("No entities found in the ontology.")
265
-
266
- if selected_entity:
267
- # Get entity information
268
- entity_info = ontology_manager.get_entity_info(selected_entity)
269
-
270
- # Display detailed information
271
- display_entity_details(entity_info, ontology_manager)
272
-
273
- # Set this entity as the central entity (for knowledge graph visualization)
274
- if st.button("View this Entity in the Knowledge Graph"):
275
- st.session_state.central_entity = selected_entity
276
- st.rerun()
277
-
278
- # Get and display entity neighbors
279
- st.subheader("Entity Neighborhood")
280
- max_distance = st.slider("Maximum Neighborhood Distance", 1, 3, 1)
281
-
282
- neighborhood = knowledge_graph.get_entity_neighborhood(
283
- selected_entity,
284
- max_distance=max_distance,
285
- include_classes=True
286
- )
287
-
288
  if neighborhood and "neighbors" in neighborhood:
289
- # Display neighbors grouped by distance
290
  for distance in range(1, max_distance+1):
291
  neighbors_at_distance = [n for n in neighborhood["neighbors"] if n["distance"] == distance]
292
-
293
  if neighbors_at_distance:
294
  with st.expander(f"Neighbors at Distance {distance} ({len(neighbors_at_distance)})"):
295
  for neighbor in neighbors_at_distance:
296
  st.markdown(f"**{neighbor['id']}** ({neighbor.get('class_type', 'unknown')})")
297
-
298
- # Display relations
299
  for relation in neighbor.get("relations", []):
300
  direction = "β†’" if relation["direction"] == "outgoing" else "←"
301
  st.markdown(f"- {direction} {relation['type']}")
302
-
303
  st.markdown("---")
304
 
305
- def run_semantic_path_visualization():
 
 
 
306
  st.title("Semantic Path Visualization")
307
-
308
- # Get all entities
309
- entities = []
310
- for class_name in ontology_manager.get_classes():
311
- entities.extend(ontology_manager.get_instances_of_class(class_name))
312
-
313
- # Remove duplicates and sort
314
  entities = sorted(set(entities))
315
-
316
- # Create two columns for selecting source and target entities
317
- col1, col2 = st.columns(2)
318
-
319
- with col1:
320
- source_entity = st.selectbox("Select Source Entity", entities, key="source")
321
-
322
- with col2:
323
- target_entity = st.selectbox("Select Target Entity", entities, key="target")
324
-
325
  if source_entity and target_entity and source_entity != target_entity:
326
- # Provide a maximum path length option
327
  max_length = st.slider("Maximum Path Length", 1, 5, 3)
328
-
329
- # Find paths
330
  paths = knowledge_graph.find_paths_between_entities(
331
- source_entity,
332
  target_entity,
333
  max_length=max_length
334
  )
335
-
336
  if paths:
337
  st.success(f"Found {len(paths)} paths!")
338
-
339
- # Create expanders for each path
340
  for i, path in enumerate(paths):
341
- # Calculate path length and relationship types
342
  path_length = len(path)
343
  rel_types = [edge["type"] for edge in path]
344
-
345
  with st.expander(f"Path {i+1} (Length: {path_length}, Relations: {', '.join(rel_types)})", expanded=(i==0)):
346
- # Create a text description of the path
347
  path_text = []
348
- entities_in_path = []
349
-
350
  for edge in path:
351
  source = edge["source"]
352
  target = edge["target"]
353
  relation = edge["type"]
354
-
355
- entities_in_path.append(source)
356
- entities_in_path.append(target)
357
-
358
- # Get entity information to get a readable name
359
  source_info = ontology_manager.get_entity_info(source)
360
  target_info = ontology_manager.get_entity_info(target)
361
-
362
- source_name = source
363
- if "properties" in source_info and "name" in source_info["properties"]:
364
- source_name = source_info["properties"]["name"]
365
-
366
- target_name = target
367
- if "properties" in target_info and "name" in target_info["properties"]:
368
- target_name = target_info["properties"]["name"]
369
-
370
  path_text.append(f"{source_name} ({source}) **{relation}** {target_name} ({target})")
371
-
372
- # Display path description
373
  st.markdown(" β†’ ".join(path_text))
374
-
375
- # Prepare path visualization
376
  path_info = {
377
  "source": source_entity,
378
  "target": target_entity,
379
  "path": path,
380
  "text": " β†’ ".join(path_text)
381
  }
382
-
383
- # Display path visualization
384
  visualize_path(path_info, ontology_manager)
385
  else:
386
  st.warning(f"No paths of length {max_length} or shorter were found between these entities.")
 
 
 
387
 
388
  def run_reasoning_trace():
389
  st.title("Reasoning Trace Visualization")
 
260
  max_distance=max_distance,
261
  include_classes=True
262
  )
263
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  if neighborhood and "neighbors" in neighborhood:
 
265
  for distance in range(1, max_distance+1):
266
  neighbors_at_distance = [n for n in neighborhood["neighbors"] if n["distance"] == distance]
267
+
268
  if neighbors_at_distance:
269
  with st.expander(f"Neighbors at Distance {distance} ({len(neighbors_at_distance)})"):
270
  for neighbor in neighbors_at_distance:
271
  st.markdown(f"**{neighbor['id']}** ({neighbor.get('class_type', 'unknown')})")
 
 
272
  for relation in neighbor.get("relations", []):
273
  direction = "β†’" if relation["direction"] == "outgoing" else "←"
274
  st.markdown(f"- {direction} {relation['type']}")
 
275
  st.markdown("---")
276
 
277
+ elif not entities:
278
+ st.warning("No entities found in the ontology.")
279
+
280
+ def render_semantic_path_tab():
281
  st.title("Semantic Path Visualization")
282
+
283
+ entities = [
284
+ node for node, attr in ontology_manager.graph.nodes(data=True)
285
+ if attr.get("type") == "instance"
286
+ ]
 
 
287
  entities = sorted(set(entities))
288
+
289
+ source_entity = st.selectbox("Select Source Entity", entities, key="source_entity") if entities else None
290
+ target_entity = st.selectbox("Select Target Entity", entities, key="target_entity") if entities else None
291
+
 
 
 
 
 
 
292
  if source_entity and target_entity and source_entity != target_entity:
 
293
  max_length = st.slider("Maximum Path Length", 1, 5, 3)
294
+
 
295
  paths = knowledge_graph.find_paths_between_entities(
296
+ source_entity,
297
  target_entity,
298
  max_length=max_length
299
  )
300
+
301
  if paths:
302
  st.success(f"Found {len(paths)} paths!")
 
 
303
  for i, path in enumerate(paths):
 
304
  path_length = len(path)
305
  rel_types = [edge["type"] for edge in path]
306
+
307
  with st.expander(f"Path {i+1} (Length: {path_length}, Relations: {', '.join(rel_types)})", expanded=(i==0)):
 
308
  path_text = []
 
 
309
  for edge in path:
310
  source = edge["source"]
311
  target = edge["target"]
312
  relation = edge["type"]
313
+
 
 
 
 
314
  source_info = ontology_manager.get_entity_info(source)
315
  target_info = ontology_manager.get_entity_info(target)
316
+
317
+ source_name = source_info.get("properties", {}).get("name", source)
318
+ target_name = target_info.get("properties", {}).get("name", target)
319
+
 
 
 
 
 
320
  path_text.append(f"{source_name} ({source}) **{relation}** {target_name} ({target})")
321
+
 
322
  st.markdown(" β†’ ".join(path_text))
323
+
 
324
  path_info = {
325
  "source": source_entity,
326
  "target": target_entity,
327
  "path": path,
328
  "text": " β†’ ".join(path_text)
329
  }
330
+
 
331
  visualize_path(path_info, ontology_manager)
332
  else:
333
  st.warning(f"No paths of length {max_length} or shorter were found between these entities.")
334
+ elif not entities:
335
+ st.warning("No entities available for semantic path selection.")
336
+
337
 
338
  def run_reasoning_trace():
339
  st.title("Reasoning Trace Visualization")