Added fix to conncurrency issues
Browse files
app.py
CHANGED
|
@@ -69,13 +69,6 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 69 |
username= f"{profile.username}"
|
| 70 |
print(f"User logged in: {username}")
|
| 71 |
|
| 72 |
-
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 73 |
-
try:
|
| 74 |
-
agent = BasicAgent()
|
| 75 |
-
except Exception as e:
|
| 76 |
-
print(f"Error instantiating agent: {e}")
|
| 77 |
-
return f"Error initializing agent: {e}", None
|
| 78 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
| 79 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 80 |
print(agent_code)
|
| 81 |
|
|
@@ -117,8 +110,14 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 117 |
results_log = []
|
| 118 |
answers_payload = []
|
| 119 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
task_id = item.get("task_id")
|
| 123 |
question_text = item.get("question")
|
| 124 |
if not task_id or question_text is None:
|
|
@@ -134,7 +133,7 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 134 |
print(f"Error running agent on task {task_id}: {e}")
|
| 135 |
return None, {"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"}
|
| 136 |
|
| 137 |
-
tasks = [process_question(item
|
| 138 |
processed_results = await asyncio.gather(*tasks)
|
| 139 |
|
| 140 |
for payload, log_entry in processed_results:
|
|
|
|
| 69 |
username= f"{profile.username}"
|
| 70 |
print(f"User logged in: {username}")
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 73 |
print(agent_code)
|
| 74 |
|
|
|
|
| 110 |
results_log = []
|
| 111 |
answers_payload = []
|
| 112 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 113 |
+
|
| 114 |
+
# Note: If BasicAgent initialization is very lightweight, this is fine.
|
| 115 |
+
# If it's heavy (e.g., downloads large models every time),
|
| 116 |
+
# then ensuring BasicAgent and its components are stateless/reentrant (Option 2)
|
| 117 |
+
# would be more efficient, but also more complex to verify.
|
| 118 |
+
async def process_question(item):
|
| 119 |
+
# Instantiate a new agent for each question to ensure isolation
|
| 120 |
+
agent_instance = BasicAgent()
|
| 121 |
task_id = item.get("task_id")
|
| 122 |
question_text = item.get("question")
|
| 123 |
if not task_id or question_text is None:
|
|
|
|
| 133 |
print(f"Error running agent on task {task_id}: {e}")
|
| 134 |
return None, {"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"}
|
| 135 |
|
| 136 |
+
tasks = [process_question(item) for item in questions_data]
|
| 137 |
processed_results = await asyncio.gather(*tasks)
|
| 138 |
|
| 139 |
for payload, log_entry in processed_results:
|