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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -65,34 +65,34 @@ async def smart_agent(questions_data: List[Dict[str, Any]]) -> List[Dict[str, An
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):
98
  """
 
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):
98
  """