Spaces:
Running
🔧 Fix Sample Knowledge Graphs Display Issue
Browse files✅ Problem solved:
• Fixed sample KGs not appearing as 'final' knowledge graphs
• Frontend requires window_index=None for final KGs to display
• Sample data was incorrectly setting window_index=0
🎯 Root cause:
• Frontend logic: is_final = (window_index is None && window_total is not None)
• Sample data was setting window_index=0 instead of None
• This caused KGs to not be recognized as 'final' graphs for display
⚡ Technical fixes:
1. backend/database/sample_data.py:
- Changed window_index=0 to window_index=None for new sample KGs
- Keeps window_total=1 to indicate processed final graph
2. backend/database/init_db.py:
- Added automatic migration to fix existing sample KGs
- Detects window_index=0 + window_total=1 pattern and fixes to None
- Runs on every database initialization for existing data
🚀 Expected result:
• Sample traces will now show their knowledge graphs in the UI
• 'Generate First Graph' button should disappear
• Users can immediately see and interact with sample KGs
• Automatic fix for existing HF Spaces data on next restart
|
@@ -361,7 +361,45 @@ def init_database(reset=False, force=False):
|
|
| 361 |
logger.warning(f"Failed to insert sample data (non-critical): {str(e)}")
|
| 362 |
logger.info("Database initialization completed without sample data")
|
| 363 |
else:
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
|
| 366 |
# Close connection
|
| 367 |
conn.close()
|
|
|
|
| 361 |
logger.warning(f"Failed to insert sample data (non-critical): {str(e)}")
|
| 362 |
logger.info("Database initialization completed without sample data")
|
| 363 |
else:
|
| 364 |
+
# Check and fix existing sample knowledge graphs if needed
|
| 365 |
+
logger.info("Existing data found, checking for sample data fixes...")
|
| 366 |
+
try:
|
| 367 |
+
from . import SessionLocal
|
| 368 |
+
from .models import KnowledgeGraph, Trace
|
| 369 |
+
|
| 370 |
+
session = SessionLocal()
|
| 371 |
+
try:
|
| 372 |
+
# Find sample traces
|
| 373 |
+
sample_traces = session.query(Trace).filter(
|
| 374 |
+
Trace.trace_source == "sample_data"
|
| 375 |
+
).all()
|
| 376 |
+
|
| 377 |
+
if sample_traces:
|
| 378 |
+
# Find knowledge graphs with incorrect window_index
|
| 379 |
+
trace_ids = [trace.trace_id for trace in sample_traces]
|
| 380 |
+
broken_kgs = session.query(KnowledgeGraph).filter(
|
| 381 |
+
KnowledgeGraph.trace_id.in_(trace_ids),
|
| 382 |
+
KnowledgeGraph.window_index == 0, # Should be None for final KGs
|
| 383 |
+
KnowledgeGraph.window_total == 1
|
| 384 |
+
).all()
|
| 385 |
+
|
| 386 |
+
if broken_kgs:
|
| 387 |
+
logger.info(f"Found {len(broken_kgs)} sample KGs that need fixing (window_index=0 -> None)")
|
| 388 |
+
for kg in broken_kgs:
|
| 389 |
+
kg.window_index = None # Fix to make it a final KG
|
| 390 |
+
session.commit()
|
| 391 |
+
logger.info(f"✅ Fixed {len(broken_kgs)} sample knowledge graphs to display as final KGs")
|
| 392 |
+
else:
|
| 393 |
+
logger.info("All sample knowledge graphs are correctly configured")
|
| 394 |
+
|
| 395 |
+
except Exception as e:
|
| 396 |
+
session.rollback()
|
| 397 |
+
logger.warning(f"Error checking sample data fixes: {str(e)}")
|
| 398 |
+
finally:
|
| 399 |
+
session.close()
|
| 400 |
+
|
| 401 |
+
except Exception as e:
|
| 402 |
+
logger.warning(f"Failed to check sample data fixes (non-critical): {str(e)}")
|
| 403 |
|
| 404 |
# Close connection
|
| 405 |
conn.close()
|
|
@@ -585,8 +585,8 @@ def insert_sample_data(session, force_insert=False):
|
|
| 585 |
filename=kg_data["filename"],
|
| 586 |
graph_data=kg_data["graph_data"],
|
| 587 |
trace_id=trace_ids[trace_index],
|
| 588 |
-
window_index=
|
| 589 |
-
window_total=1,
|
| 590 |
is_original=True
|
| 591 |
)
|
| 592 |
results["knowledge_graphs_inserted"] += 1
|
|
|
|
| 585 |
filename=kg_data["filename"],
|
| 586 |
graph_data=kg_data["graph_data"],
|
| 587 |
trace_id=trace_ids[trace_index],
|
| 588 |
+
window_index=None, # None for final KG
|
| 589 |
+
window_total=1, # Not None to indicate it's a processed final KG
|
| 590 |
is_original=True
|
| 591 |
)
|
| 592 |
results["knowledge_graphs_inserted"] += 1
|