wu981526092 commited on
Commit
e2bfb4b
·
1 Parent(s): aa7cc4d

Simplify sample data loading with direct SQL to fix HF Spaces connection issues

Browse files
Files changed (1) hide show
  1. backend/database/__init__.py +122 -261
backend/database/__init__.py CHANGED
@@ -80,278 +80,139 @@ def test_database_connection():
80
  return False
81
 
82
  def add_sample_data_for_hf():
83
- """Add sample data for HF Spaces using SQLAlchemy."""
84
  if not os.getenv("SPACE_ID"):
85
  return # Only run on HF Spaces
86
 
87
- # First test database connection
88
- if not test_database_connection():
89
- print(" Database connection failed, skipping sample data")
90
- return
91
 
92
- import json
93
- import uuid
94
- import hashlib
95
- from backend.database.models import Trace, KnowledgeGraph, Entity, Relation
96
-
97
- # Use SessionLocal instead of scoped Session for better control
98
- session = SessionLocal()
99
  try:
100
- # Ensure tables exist first
101
- Base.metadata.create_all(bind=engine)
102
-
103
- # Check if data already exists
104
- existing_traces = session.query(Trace).count()
105
- existing_kgs = session.query(KnowledgeGraph).count()
106
-
107
- print(f"🔍 HF Spaces environment check:")
108
- print(f" • SPACE_ID: {os.getenv('SPACE_ID')}")
109
- print(f" • Database URL: {DATABASE_URL}")
110
- print(f" • Existing traces: {existing_traces}")
111
- print(f" • Existing KGs: {existing_kgs}")
112
-
113
- if existing_traces > 0 or existing_kgs > 0:
114
- print("📊 Sample data already exists, skipping...")
115
- return # Data already exists
116
-
117
- print("🎯 HF Spaces: Adding sample data to in-memory database...")
118
-
119
- # Sample trace content - Multi-agent customer service scenario
120
- sample_trace_content = '''[
121
- {
122
- "role": "user",
123
- "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.",
124
- "timestamp": "2024-08-31T10:00:00Z",
125
- "metadata": {"priority": "high", "emotion": "frustrated"}
126
- },
127
- {
128
- "role": "assistant",
129
- "name": "RouterAgent",
130
- "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.",
131
- "timestamp": "2024-08-31T10:00:15Z",
132
- "actions": [{"type": "route", "target": "OrderTrackingAgent", "priority": "high"}]
133
- },
134
- {
135
- "role": "assistant",
136
- "name": "OrderTrackingAgent",
137
- "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.",
138
- "timestamp": "2024-08-31T10:01:00Z",
139
- "actions": [
140
- {"type": "database_query", "query": "SELECT * FROM orders WHERE order_id = '12345'"},
141
- {"type": "api_call", "endpoint": "shipping_tracker", "response": "in_transit"}
142
- ],
143
- "findings": {
144
- "issue": "Weather delay at distribution center",
145
- "current_status": "In transit",
146
- "estimated_delivery": "2024-08-31T14:00:00Z"
147
- }
148
- },
149
- {
150
- "role": "assistant",
151
- "name": "CompensationAgent",
152
- "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.",
153
- "timestamp": "2024-08-31T10:02:00Z",
154
- "actions": [
155
- {"type": "credit_account", "amount": 10, "reason": "delivery_delay"},
156
- {"type": "add_benefit", "type": "free_expedited_shipping"},
157
- {"type": "escalate", "department": "operations", "issue": "delayed_delivery_communication"}
158
- ],
159
- "error": "Failed to process account credit - payment system temporarily unavailable"
160
- },
161
- {
162
- "role": "assistant",
163
- "name": "SupervisorAgent",
164
- "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.",
165
- "timestamp": "2024-08-31T10:03:00Z",
166
- "actions": [
167
- {"type": "manual_flag", "account_credit": 10},
168
- {"type": "technical_escalation", "system": "payment_processor"},
169
- {"type": "follow_up_schedule", "timeframe": "24_hours"}
170
- ],
171
- "optimization_opportunity": "Implement payment system redundancy to prevent credit processing failures"
172
- },
173
- {
174
- "role": "user",
175
- "content": "Thank you for the quick response and compensation. I appreciate you taking care of this issue promptly.",
176
- "timestamp": "2024-08-31T10:04:00Z",
177
- "sentiment": "satisfied"
178
- },
179
- {
180
- "role": "assistant",
181
- "name": "QualityAgent",
182
- "content": "Session completed successfully. Customer satisfaction restored. Identified system improvement: need better proactive communication about shipping delays and payment system reliability backup.",
183
- "timestamp": "2024-08-31T10:05:00Z",
184
- "session_analysis": {
185
- "resolution_time": "5 minutes",
186
- "customer_satisfaction": "high",
187
- "agents_involved": 4,
188
- "system_errors": 1,
189
- "optimization_recommendations": [
190
- "Implement proactive delay notifications",
191
- "Add payment system redundancy",
192
- "Improve agent handoff protocols"
193
- ]
194
- }
195
- }
196
- ]'''
197
-
198
- # Generate trace metadata
199
- trace_id = str(uuid.uuid4())
200
- content_hash = hashlib.sha256(sample_trace_content.encode()).hexdigest()
201
-
202
- # Create and save sample trace
203
- sample_trace = Trace(
204
- trace_id=trace_id,
205
- filename="sample_customer_service.json",
206
- title="Multi-Agent Customer Service Resolution",
207
- description="Demonstration of multi-agent system handling customer complaint with error handling and optimization opportunities",
208
- content=sample_trace_content,
209
- content_hash=content_hash,
210
- uploader="AgentGraph Demo",
211
- trace_type="multi_agent",
212
- trace_source="sample",
213
- character_count=len(sample_trace_content),
214
- turn_count=6,
215
- status="processed",
216
- tags=json.dumps(["sample", "customer_service", "multi_agent", "error_handling", "optimization"]),
217
- trace_metadata=json.dumps({
218
- "scenario": "customer_service",
219
- "agents": ["RouterAgent", "OrderTrackingAgent", "CompensationAgent", "SupervisorAgent", "QualityAgent"],
220
- "domain": "e_commerce",
221
- "complexity": "high"
222
- })
223
- )
224
-
225
- session.add(sample_trace)
226
- session.flush() # Get the trace ID
227
-
228
- # Create sample knowledge graph
229
- sample_kg = KnowledgeGraph(
230
- filename="sample_customer_service_kg.json",
231
- creator="AgentGraph Demo",
232
- entity_count=15,
233
- relation_count=18,
234
- namespace="customer_service_demo",
235
- system_name="Multi-Agent Customer Service System",
236
- 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.",
237
- status="completed",
238
- trace_id=trace_id,
239
- window_index=0,
240
- window_total=1,
241
- processing_run_id="sample_demo_run"
242
- )
243
-
244
- session.add(sample_kg)
245
- session.flush() # Get the KG ID
246
-
247
- # Sample entities with rich properties
248
- entities_data = [
249
- # Agents
250
- ("agent_1", "agent", "RouterAgent", {"role": "traffic_routing", "specialization": "request_classification", "priority_handling": True}),
251
- ("agent_2", "agent", "OrderTrackingAgent", {"role": "order_management", "specialization": "shipping_tracking", "data_sources": ["internal_db", "shipping_apis"]}),
252
- ("agent_3", "agent", "CompensationAgent", {"role": "customer_retention", "specialization": "compensation_authorization", "max_credit_limit": 50}),
253
- ("agent_4", "agent", "SupervisorAgent", {"role": "escalation_handling", "specialization": "system_error_recovery", "override_authority": True}),
254
- ("agent_5", "agent", "QualityAgent", {"role": "quality_assurance", "specialization": "session_analysis", "improvement_tracking": True}),
255
-
256
- # Systems and Tools
257
- ("system_1", "system", "OrderDatabase", {"type": "database", "function": "order_storage", "performance": "high"}),
258
- ("system_2", "system", "ShippingTracker", {"type": "external_api", "function": "package_tracking", "reliability": "99.5%"}),
259
- ("system_3", "system", "PaymentProcessor", {"type": "financial_system", "function": "account_credits", "status": "temporarily_unavailable"}),
260
-
261
- # Issues and Problems
262
- ("issue_1", "issue", "DeliveryDelay", {"severity": "medium", "cause": "weather_conditions", "impact": "customer_satisfaction"}),
263
- ("issue_2", "issue", "PaymentSystemFailure", {"severity": "high", "cause": "system_unavailability", "impact": "compensation_processing"}),
264
 
265
- # Actions and Processes
266
- ("action_1", "action", "RouteRequest", {"type": "traffic_management", "success_rate": "98%"}),
267
- ("action_2", "action", "TrackPackage", {"type": "information_retrieval", "data_sources": 2}),
268
- ("action_3", "action", "AuthorizeCredit", {"type": "financial_transaction", "approval_required": True}),
269
- ("action_4", "action", "EscalateIssue", {"type": "process_escalation", "department": "operations"}),
270
-
271
- # Improvements and Optimizations
272
- ("improvement_1", "improvement", "ProactiveNotifications", {"priority": "high", "implementation_effort": "medium", "expected_impact": "reduce_complaints_by_30%"})
273
- ]
274
-
275
- # Add entities
276
- for entity_id, entity_type, name, properties in entities_data:
277
- entity = Entity(
278
- graph_id=sample_kg.id,
279
- entity_id=entity_id,
280
- type=entity_type,
281
- name=name,
282
- properties=json.dumps(properties),
283
- knowledge_graph_namespace="customer_service_demo"
284
- )
285
- session.add(entity)
286
-
287
- # Sample relations showing complex interactions
288
- relations_data = [
289
- # Agent interactions
290
- ("rel_1", "agent_1", "routes_to", "agent_2", {"context": "order_inquiry", "priority": "high", "success": True}),
291
- ("rel_2", "agent_2", "collaborates_with", "agent_3", {"context": "customer_compensation", "coordination": "automated"}),
292
- ("rel_3", "agent_3", "escalates_to", "agent_4", {"context": "system_error", "escalation_reason": "payment_failure"}),
293
- ("rel_4", "agent_4", "coordinates_with", "agent_5", {"context": "quality_improvement", "outcome": "optimization_identified"}),
294
-
295
- # System interactions
296
- ("rel_5", "agent_2", "queries", "system_1", {"query_type": "order_lookup", "response_time": "0.5s", "success": True}),
297
- ("rel_6", "agent_2", "calls", "system_2", {"api_endpoint": "track_package", "response_time": "1.2s", "success": True}),
298
- ("rel_7", "agent_3", "attempts_transaction", "system_3", {"transaction_type": "credit", "amount": 10, "success": False}),
299
-
300
- # Problem identification and resolution
301
- ("rel_8", "agent_2", "identifies", "issue_1", {"detection_method": "system_query", "severity_assessed": "medium"}),
302
- ("rel_9", "agent_3", "encounters", "issue_2", {"error_handling": "automatic_escalation", "recovery_action": "manual_override"}),
303
- ("rel_10", "agent_4", "resolves", "issue_2", {"resolution_method": "manual_flag", "permanent_fix": False}),
304
-
305
- # Action execution
306
- ("rel_11", "agent_1", "executes", "action_1", {"execution_time": "15s", "outcome": "successful_routing"}),
307
- ("rel_12", "agent_2", "performs", "action_2", {"data_retrieved": True, "accuracy": "100%"}),
308
- ("rel_13", "agent_3", "initiates", "action_3", {"authorization_level": "standard", "blocked_by": "system_error"}),
309
- ("rel_14", "agent_4", "triggers", "action_4", {"escalation_department": "operations", "follow_up_required": True}),
310
-
311
- # Improvement opportunities
312
- ("rel_15", "agent_5", "identifies", "improvement_1", {"analysis_method": "session_review", "confidence": "high"}),
313
- ("rel_16", "issue_1", "leads_to", "improvement_1", {"causal_relationship": "direct", "prevention_potential": "high"}),
314
- ("rel_17", "issue_2", "exposes", "system_3", {"vulnerability_type": "single_point_of_failure", "risk_level": "high"}),
315
- ("rel_18", "improvement_1", "would_prevent", "issue_1", {"prevention_mechanism": "early_warning", "effectiveness": "85%"})
316
- ]
317
-
318
- # Add relations
319
- for relation_id, from_entity, relation_type, to_entity, properties in relations_data:
320
- relation = Relation(
321
- graph_id=sample_kg.id,
322
- relation_id=relation_id,
323
- from_entity_id=from_entity,
324
- relation_type=relation_type,
325
- to_entity_id=to_entity,
326
- properties=json.dumps(properties),
327
- knowledge_graph_namespace="customer_service_demo"
328
- )
329
- session.add(relation)
330
-
331
- # Commit all data
332
- session.commit()
333
-
334
- # Verify data was added
335
- final_traces = session.query(Trace).count()
336
- final_kgs = session.query(KnowledgeGraph).count()
337
- final_entities = session.query(Entity).count()
338
- final_relations = session.query(Relation).count()
339
-
340
- print(" Sample data added successfully to HF Spaces in-memory database!")
341
- print(f" • Traces added: {final_traces}")
342
- print(f" • Knowledge graphs added: {final_kgs}")
343
- print(f" • Entities added: {final_entities}")
344
- print(f" • Relations added: {final_relations}")
345
- print(f" • Scenario: Multi-Agent Customer Service with error handling and optimization")
346
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
  except Exception as e:
348
  print(f"❌ Failed to add sample data: {e}")
349
  import traceback
350
  print("Full error traceback:")
351
  traceback.print_exc()
352
- session.rollback()
353
- finally:
354
- session.close()
355
 
356
  __all__ = [
357
  'get_db',
 
80
  return False
81
 
82
  def add_sample_data_for_hf():
83
+ """Add simple sample data for HF Spaces using direct SQL."""
84
  if not os.getenv("SPACE_ID"):
85
  return # Only run on HF Spaces
86
 
87
+ print(f"🔍 HF Spaces environment check:")
88
+ print(f" • SPACE_ID: {os.getenv('SPACE_ID')}")
89
+ print(f" Database URL: {DATABASE_URL}")
 
90
 
 
 
 
 
 
 
 
91
  try:
92
+ # Use direct connection to avoid session issues
93
+ with engine.connect() as conn:
94
+ # Begin transaction for atomic operations
95
+ trans = conn.begin()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
+ try:
98
+ # Check if data already exists
99
+ result = conn.execute("SELECT COUNT(*) FROM traces").fetchone()
100
+ existing_traces = result[0] if result else 0
101
+
102
+ result = conn.execute("SELECT COUNT(*) FROM knowledge_graphs").fetchone()
103
+ existing_kgs = result[0] if result else 0
104
+
105
+ print(f" • Existing traces: {existing_traces}")
106
+ print(f" • Existing KGs: {existing_kgs}")
107
+
108
+ if existing_traces > 0 or existing_kgs > 0:
109
+ print("📊 Sample data already exists, skipping...")
110
+ return
111
+
112
+ print("🎯 Adding sample data using direct SQL...")
113
+
114
+ import json
115
+ import uuid
116
+ import hashlib
117
+ from datetime import datetime
118
+
119
+ # Simple trace content
120
+ sample_trace_content = '''[
121
+ {"role": "user", "content": "I need help with my delayed order #12345. This is frustrating!", "timestamp": "2024-08-31T10:00:00Z"},
122
+ {"role": "assistant", "name": "RouterAgent", "content": "Let me route you to our order specialist.", "timestamp": "2024-08-31T10:00:15Z"},
123
+ {"role": "assistant", "name": "OrderAgent", "content": "I found the issue - weather delay. Your package arrives today at 2 PM.", "timestamp": "2024-08-31T10:01:00Z"},
124
+ {"role": "assistant", "name": "CompensationAgent", "content": "I'll authorize a $10 credit for the inconvenience.", "timestamp": "2024-08-31T10:02:00Z", "error": "Payment system unavailable"},
125
+ {"role": "assistant", "name": "SupervisorAgent", "content": "I'll manually flag your account for the credit. Technical team notified.", "timestamp": "2024-08-31T10:03:00Z"},
126
+ {"role": "user", "content": "Thank you for the quick resolution!", "timestamp": "2024-08-31T10:04:00Z", "sentiment": "satisfied"}
127
+ ]'''
128
+
129
+ # Generate IDs
130
+ trace_id = str(uuid.uuid4())
131
+ content_hash = hashlib.sha256(sample_trace_content.encode()).hexdigest()
132
+ now = datetime.utcnow()
133
+
134
+ # Insert trace
135
+ conn.execute(
136
+ """INSERT INTO traces (trace_id, filename, title, description, content, content_hash,
137
+ upload_timestamp, update_timestamp, uploader, trace_type, trace_source,
138
+ character_count, turn_count, status, tags, trace_metadata)
139
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
140
+ (trace_id, "sample_demo.json", "Multi-Agent Customer Service Demo",
141
+ "Demo showing agent coordination and error handling", sample_trace_content, content_hash,
142
+ now, now, "AgentGraph Demo", "multi_agent", "sample", len(sample_trace_content), 6,
143
+ "processed", '["demo", "customer_service", "multi_agent"]',
144
+ '{"scenario": "customer_service", "agents": ["RouterAgent", "OrderAgent", "CompensationAgent", "SupervisorAgent"]}')
145
+ )
146
+
147
+ # Insert knowledge graph
148
+ conn.execute(
149
+ """INSERT INTO knowledge_graphs (filename, creator, entity_count, relation_count, namespace,
150
+ system_name, system_summary, status, trace_id, window_index, window_total, processing_run_id)
151
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
152
+ ("demo_kg.json", "AgentGraph Demo", 5, 4, "demo", "Customer Service System",
153
+ "Multi-agent customer service system with error handling and coordination",
154
+ "completed", trace_id, 0, 1, "demo_run")
155
+ )
156
+
157
+ # Get KG ID
158
+ kg_result = conn.execute("SELECT id FROM knowledge_graphs WHERE trace_id = ?", (trace_id,))
159
+ kg_id = kg_result.fetchone()[0]
160
+
161
+ # Insert sample entities
162
+ entities = [
163
+ ("agent_1", "agent", "RouterAgent", '{"role": "routing", "priority_handling": true}'),
164
+ ("agent_2", "agent", "OrderAgent", '{"role": "order_tracking", "data_sources": ["db", "api"]}'),
165
+ ("agent_3", "agent", "CompensationAgent", '{"role": "compensation", "max_credit": 50}'),
166
+ ("agent_4", "agent", "SupervisorAgent", '{"role": "escalation", "override_authority": true}'),
167
+ ("issue_1", "issue", "PaymentSystemFailure", '{"severity": "high", "impact": "service_disruption"}')
168
+ ]
169
+
170
+ for entity_id, entity_type, name, properties in entities:
171
+ conn.execute(
172
+ """INSERT INTO entities (graph_id, entity_id, type, name, properties, knowledge_graph_namespace)
173
+ VALUES (?, ?, ?, ?, ?, ?)""",
174
+ (kg_id, entity_id, entity_type, name, properties, "demo")
175
+ )
176
+
177
+ # Insert sample relations
178
+ relations = [
179
+ ("rel_1", "agent_1", "routes_to", "agent_2", '{"priority": "high", "success": true}'),
180
+ ("rel_2", "agent_2", "escalates_to", "agent_3", '{"reason": "compensation_needed"}'),
181
+ ("rel_3", "agent_3", "escalates_to", "agent_4", '{"reason": "system_error"}'),
182
+ ("rel_4", "agent_4", "resolves", "issue_1", '{"method": "manual_override"}')
183
+ ]
184
+
185
+ for relation_id, from_entity, relation_type, to_entity, properties in relations:
186
+ conn.execute(
187
+ """INSERT INTO relations (graph_id, relation_id, from_entity_id, relation_type, to_entity_id,
188
+ properties, knowledge_graph_namespace) VALUES (?, ?, ?, ?, ?, ?, ?)""",
189
+ (kg_id, relation_id, from_entity, relation_type, to_entity, properties, "demo")
190
+ )
191
+
192
+ # Commit transaction
193
+ trans.commit()
194
+
195
+ # Verify data
196
+ final_traces = conn.execute("SELECT COUNT(*) FROM traces").fetchone()[0]
197
+ final_kgs = conn.execute("SELECT COUNT(*) FROM knowledge_graphs").fetchone()[0]
198
+ final_entities = conn.execute("SELECT COUNT(*) FROM entities").fetchone()[0]
199
+ final_relations = conn.execute("SELECT COUNT(*) FROM relations").fetchone()[0]
200
+
201
+ print("✅ Sample data added successfully!")
202
+ print(f" • Traces: {final_traces}")
203
+ print(f" • Knowledge graphs: {final_kgs}")
204
+ print(f" • Entities: {final_entities}")
205
+ print(f" • Relations: {final_relations}")
206
+
207
+ except Exception as e:
208
+ trans.rollback()
209
+ raise e
210
+
211
  except Exception as e:
212
  print(f"❌ Failed to add sample data: {e}")
213
  import traceback
214
  print("Full error traceback:")
215
  traceback.print_exc()
 
 
 
216
 
217
  __all__ = [
218
  'get_db',