cowrycode commited on
Commit
5f25fa9
·
verified ·
1 Parent(s): beabdf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -64,20 +64,34 @@ async def smart_agent(questions_data: List[Dict[str, Any]]) -> List[Dict[str, An
64
  llm=llm,
65
  )
66
 
67
- answers_payload = []
68
- for item in questions_data:
69
- task_id = item.get("task_id")
70
- question_text = item.get("question")
71
- if not task_id or question_text is None:
72
- print(f"Skipping item with missing task_id or question: {item}")
73
- continue
74
- try:
75
- answer = await llamaindex_agent.run(question_text) # await coroutine
76
- answers_payload.append({"task_id": task_id, "submitted_answer": answer})
77
- print(f"Answered Task {task_id}: {answer[:50]}...")
78
- except Exception as e:
79
- answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"})
80
- print(f"Error on Task {task_id}: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  return answers_payload
82
 
83
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
64
  llm=llm,
65
  )
66
 
67
+ async def process_question(item):
68
+ task_id = item.get("task_id")
69
+ question_text = item.get("question")
70
+ if not task_id or not question_text:
71
+ print(f"Skipping invalid item: {item}")
72
+ return {"task_id": task_id, "submitted_answer": "Invalid question."}
73
+
74
+ try:
75
+ # Retry up to 3 times if connection closes
76
+ for attempt in range(3):
77
+ try:
78
+ response = await llamaindex_agent.run(question_text)
79
+ print(f"Answered Task {task_id}: {response[:50]}...")
80
+ return {"task_id": task_id, "submitted_answer": response}
81
+ except Exception as e:
82
+ if "client has been closed" in str(e) and attempt < 2:
83
+ print(f"Retrying task {task_id} (attempt {attempt + 2})...")
84
+ await asyncio.sleep(1)
85
+ continue
86
+ raise e
87
+ except Exception as e:
88
+ print(f"Error on Task {task_id}: {e}")
89
+ return {"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"}
90
+
91
+ tasks = [process_question(item) for item in questions_data]
92
+ answers_payload = await asyncio.gather(*tasks)
93
+
94
+ print("✅ All tasks completed.")
95
  return answers_payload
96
 
97
  def run_and_submit_all( profile: gr.OAuthProfile | None):