Spaces:
Running
🔧 Fix Sample Data Loading for HF Spaces
Browse files✅ Root Cause Identified:
• Database inconsistency: sample data added to file DB, app used in-memory DB
• In init_db.py: used sqlite3 connection to file database
• In __init__.py: used SQLAlchemy with in-memory database for HF Spaces
• Result: sample data invisible to application
⚡ Complete Fix Implementation:
• Created add_sample_data_for_hf() using SQLAlchemy models
• Uses same in-memory database engine as the application
• Proper session management with rollback on errors
• Only runs on HF Spaces (SPACE_ID environment variable)
• Integrated into app startup sequence after database initialization
🎯 Technical improvements:
• Consistent database access patterns across codebase
• Proper SQLAlchemy model usage (Trace, KnowledgeGraph, Entity, Relation)
• Transaction safety with commit/rollback handling
• Clean separation: file DB for local dev, memory DB + samples for HF
🚀 Expected result:
• Dashboard should show 1 trace and 1 knowledge graph on HF Spaces
• 15 entities and 18 relations demonstrating multi-agent coordination
• Perfect customer service scenario with error handling examples
• Users immediately see AgentGraph's capabilities
Files changed (2)
75 unmodified lines76777879808182838485868775 unmodified lines# 🗄️ Initialize database on startuptry:from backend.database.init_db import init_databaseinit_database(reset=False, force=False)# Show database type infoif os.getenv("SPACE_ID"):logger.info("🔒 HF Spaces: Using in-memory database for user privacy")logger.info("📝 Note: Data will be cleared when container restarts")else:logger.info("💾 Local development: Using persistent database")75 unmodified lines76777879808182838485868788899091929375 unmodified lines# 🗄️ Initialize database on startuptry:from backend.database.init_db import init_databasefrom backend.database import init_db, add_sample_data_for_hfinit_database(reset=False, force=False)init_db() # Create tables using SQLAlchemy# Show database type infoif os.getenv("SPACE_ID"):logger.info("🔒 HF Spaces: Using in-memory database for user privacy")logger.info("📝 Note: Data will be cleared when container restarts")# Add sample data for HF Spacesadd_sample_data_for_hf()else:logger.info("💾 Local development: Using persistent database")
59 unmodified lines6061626364656667686959 unmodified lines"""Initialize the database by creating all tables."""Base.metadata.create_all(bind=engine)__all__ = ['get_db','models','init_db','save_knowledge_graph','update_knowledge_graph_status','get_knowledge_graph',59 unmodified lines6061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631759 unmodified lines"""Initialize the database by creating all tables."""Base.metadata.create_all(bind=engine)def add_sample_data_for_hf():"""Add sample data for HF Spaces using SQLAlchemy."""if not os.getenv("SPACE_ID"):return # Only run on HF Spacesimport jsonimport uuidimport hashlibfrom backend.database.models import Trace, KnowledgeGraph, Entity, Relationsession = Session()try:# Check if data already existsexisting_traces = session.query(Trace).count()existing_kgs = session.query(KnowledgeGraph).count()if existing_traces > 0 or existing_kgs > 0:return # Data already existsprint("🎯 HF Spaces: Adding sample data to in-memory database...")# Sample trace content - Multi-agent customer service scenariosample_trace_content = '''[{"role": "user","content": "I need help with my order #12345. It was supposed to arrive yesterday but I still haven't received it. This is very frustrating as I need it for an important meeting tomorrow.","timestamp": "2024-08-31T10:00:00Z","metadata": {"priority": "high", "emotion": "frustrated"}},{"role": "assistant","name": "RouterAgent","content": "I understand your frustration about the delayed order. Let me route this to our order tracking specialist who can provide detailed information about your shipment.","timestamp": "2024-08-31T10:00:15Z","actions": [{"type": "route", "target": "OrderTrackingAgent", "priority": "high"}]},{"role": "assistant","name": "OrderTrackingAgent","content": "I'll look up order #12345 immediately. Checking our tracking system... I found the issue - there was a delay at the distribution center due to weather conditions. Your package is currently in transit and should arrive by 2 PM today.","timestamp": "2024-08-31T10:01:00Z","actions": [{"type": "database_query", "query": "SELECT * FROM orders WHERE order_id = '12345'"},{"type": "api_call", "endpoint": "shipping_tracker", "response": "in_transit"}],"findings": {"issue": "Weather delay at distribution center","current_status": "In transit","estimated_delivery": "2024-08-31T14:00:00Z"}},{"role": "assistant","name": "CompensationAgent","content": "Given this delay caused inconvenience, I'm authorizing a $10 credit to your account and providing free expedited shipping on your next order. I'm also escalating this to ensure better communication about delays in the future.","timestamp": "2024-08-31T10:02:00Z","actions": [{"type": "credit_account", "amount": 10, "reason": "delivery_delay"},{"type": "add_benefit", "type": "free_expedited_shipping"},{"type": "escalate", "department": "operations", "issue": "delayed_delivery_communication"}],"error": "Failed to process account credit - payment system temporarily unavailable"},{"role": "assistant","name": "SupervisorAgent","content": "I see there was an error processing the account credit. I'm manually flagging your account for the $10 credit and expedited shipping benefit. Our technical team will resolve the payment system issue. You should see the credit within 24 hours.","timestamp": "2024-08-31T10:03:00Z","actions": [{"type": "manual_flag", "account_credit": 10},{"type": "technical_escalation", "system": "payment_processor"},{"type": "follow_up_schedule", "timeframe": "24_hours"}],"optimization_opportunity": "Implement payment system redundancy to prevent credit processing failures"},{"role": "user","content": "Thank you for the quick response and compensation. I appreciate you taking care of this issue promptly.","timestamp": "2024-08-31T10:04:00Z","sentiment": "satisfied"},{"role": "assistant","name": "QualityAgent","content": "Session completed successfully. Customer satisfaction restored. Identified system improvement: need better proactive communication about shipping delays and payment system reliability backup.","timestamp": "2024-08-31T10:05:00Z","session_analysis": {"resolution_time": "5 minutes","customer_satisfaction": "high","agents_involved": 4,"system_errors": 1,"optimization_recommendations": ["Implement proactive delay notifications","Add payment system redundancy","Improve agent handoff protocols"]}}]'''# Generate trace metadatatrace_id = str(uuid.uuid4())content_hash = hashlib.sha256(sample_trace_content.encode()).hexdigest()# Create and save sample tracesample_trace = Trace(trace_id=trace_id,filename="sample_customer_service.json",title="Multi-Agent Customer Service Resolution",description="Demonstration of multi-agent system handling customer complaint with error handling and optimization opportunities",content=sample_trace_content,content_hash=content_hash,uploader="AgentGraph Demo",trace_type="multi_agent",trace_source="sample",character_count=len(sample_trace_content),turn_count=6,status="processed",tags=json.dumps(["sample", "customer_service", "multi_agent", "error_handling", "optimization"]),trace_metadata=json.dumps({"scenario": "customer_service","agents": ["RouterAgent", "OrderTrackingAgent", "CompensationAgent", "SupervisorAgent", "QualityAgent"],"domain": "e_commerce","complexity": "high"}))session.add(sample_trace)session.flush() # Get the trace ID# Create sample knowledge graphsample_kg = KnowledgeGraph(filename="sample_customer_service_kg.json",creator="AgentGraph Demo",entity_count=15,relation_count=18,namespace="customer_service_demo",system_name="Multi-Agent Customer Service System",system_summary="An intelligent customer service system featuring multiple specialized agents working together to resolve customer issues, handle errors, and identify optimization opportunities. The system demonstrates sophisticated agent coordination, error recovery mechanisms, and continuous improvement processes.",status="completed",trace_id=trace_id,window_index=0,window_total=1,processing_run_id="sample_demo_run")session.add(sample_kg)session.flush() # Get the KG ID# Sample entities with rich propertiesentities_data = [# Agents("agent_1", "agent", "RouterAgent", {"role": "traffic_routing", "specialization": "request_classification", "priority_handling": True}),("agent_2", "agent", "OrderTrackingAgent", {"role": "order_management", "specialization": "shipping_tracking", "data_sources": ["internal_db", "shipping_apis"]}),("agent_3", "agent", "CompensationAgent", {"role": "customer_retention", "specialization": "compensation_authorization", "max_credit_limit": 50}),("agent_4", "agent", "SupervisorAgent", {"role": "escalation_handling", "specialization": "system_error_recovery", "override_authority": True}),("agent_5", "agent", "QualityAgent", {"role": "quality_assurance", "specialization": "session_analysis", "improvement_tracking": True}),# Systems and Tools("system_1", "system", "OrderDatabase", {"type": "database", "function": "order_storage", "performance": "high"}),("system_2", "system", "ShippingTracker", {"type": "external_api", "function": "package_tracking", "reliability": "99.5%"}),("system_3", "system", "PaymentProcessor", {"type": "financial_system", "function": "account_credits", "status": "temporarily_unavailable"}),# Issues and Problems("issue_1", "issue", "DeliveryDelay", {"severity": "medium", "cause": "weather_conditions", "impact": "customer_satisfaction"}),("issue_2", "issue", "PaymentSystemFailure", {"severity": "high", "cause": "system_unavailability", "impact": "compensation_processing"}),# Actions and Processes("action_1", "action", "RouteRequest", {"type": "traffic_management", "success_rate": "98%"}),("action_2", "action", "TrackPackage", {"type": "information_retrieval", "data_sources": 2}),("action_3", "action", "AuthorizeCredit", {"type": "financial_transaction", "approval_required": True}),("action_4", "action", "EscalateIssue", {"type": "process_escalation", "department": "operations"}),# Improvements and Optimizations("improvement_1", "improvement", "ProactiveNotifications", {"priority": "high", "implementation_effort": "medium", "expected_impact": "reduce_complaints_by_30%"})]# Add entitiesfor entity_id, entity_type, name, properties in entities_data:entity = Entity(graph_id=sample_kg.id,entity_id=entity_id,type=entity_type,name=name,properties=json.dumps(properties),knowledge_graph_namespace="customer_service_demo")session.add(entity)# Sample relations showing complex interactionsrelations_data = [# Agent interactions("rel_1", "agent_1", "routes_to", "agent_2", {"context": "order_inquiry", "priority": "high", "success": True}),("rel_2", "agent_2", "collaborates_with", "agent_3", {"context": "customer_compensation", "coordination": "automated"}),("rel_3", "agent_3", "escalates_to", "agent_4", {"context": "system_error", "escalation_reason": "payment_failure"}),("rel_4", "agent_4", "coordinates_with", "agent_5", {"context": "quality_improvement", "outcome": "optimization_identified"}),# System interactions("rel_5", "agent_2", "queries", "system_1", {"query_type": "order_lookup", "response_time": "0.5s", "success": True}),("rel_6", "agent_2", "calls", "system_2", {"api_endpoint": "track_package", "response_time": "1.2s", "success": True}),("rel_7", "agent_3", "attempts_transaction", "system_3", {"transaction_type": "credit", "amount": 10, "success": False}),# Problem identification and resolution("rel_8", "agent_2", "identifies", "issue_1", {"detection_method": "system_query", "severity_assessed": "medium"}),("rel_9", "agent_3", "encounters", "issue_2", {"error_handling": "automatic_escalation", "recovery_action": "manual_override"}),("rel_10", "agent_4", "resolves", "issue_2", {"resolution_method": "manual_flag", "permanent_fix": False}),# Action execution("rel_11", "agent_1", "executes", "action_1", {"execution_time": "15s", "outcome": "successful_routing"}),("rel_12", "agent_2", "performs", "action_2", {"data_retrieved": True, "accuracy": "100%"}),("rel_13", "agent_3", "initiates", "action_3", {"authorization_level": "standard", "blocked_by": "system_error"}),("rel_14", "agent_4", "triggers", "action_4", {"escalation_department": "operations", "follow_up_required": True}),# Improvement opportunities("rel_15", "agent_5", "identifies", "improvement_1", {"analysis_method": "session_review", "confidence": "high"}),("rel_16", "issue_1", "leads_to", "improvement_1", {"causal_relationship": "direct", "prevention_potential": "high"}),("rel_17", "issue_2", "exposes", "system_3", {"vulnerability_type": "single_point_of_failure", "risk_level": "high"}),("rel_18", "improvement_1", "would_prevent", "issue_1", {"prevention_mechanism": "early_warning", "effectiveness": "85%"})]# Add relationsfor relation_id, from_entity, relation_type, to_entity, properties in relations_data:relation = Relation(graph_id=sample_kg.id,relation_id=relation_id,from_entity_id=from_entity,relation_type=relation_type,to_entity_id=to_entity,properties=json.dumps(properties),knowledge_graph_namespace="customer_service_demo")session.add(relation)# Commit all datasession.commit()print("✅ Sample data added successfully to HF Spaces in-memory database!")print(f" • 1 sample trace: Multi-Agent Customer Service Resolution")print(f" • 1 knowledge graph with {len(entities_data)} entities and {len(relations_data)} relations")print(f" • Demonstrates: Multi-agent coordination, error handling, optimization opportunities")except Exception as e:print(f"❌ Failed to add sample data: {e}")session.rollback()finally:session.close()__all__ = ['get_db','models','init_db','add_sample_data_for_hf','save_knowledge_graph','update_knowledge_graph_status','get_knowledge_graph',