Added Multi Threading to # 3. Run your Agent (Claude)
Browse files
app.py
CHANGED
|
@@ -118,22 +118,49 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 118 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 119 |
|
| 120 |
# 3. Run your Agent
|
|
|
|
|
|
|
| 121 |
results_log = []
|
| 122 |
answers_payload = []
|
| 123 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 124 |
-
|
|
|
|
| 125 |
task_id = item.get("task_id")
|
| 126 |
question_text = item.get("question")
|
| 127 |
if not task_id or question_text is None:
|
| 128 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 129 |
-
|
| 130 |
try:
|
| 131 |
submitted_answer = agent(question_text)
|
| 132 |
-
|
| 133 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 134 |
except Exception as e:
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
if not answers_payload:
|
| 139 |
print("Agent did not produce any answers to submit.")
|
|
|
|
| 118 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 119 |
|
| 120 |
# 3. Run your Agent
|
| 121 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 122 |
+
|
| 123 |
results_log = []
|
| 124 |
answers_payload = []
|
| 125 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 126 |
+
|
| 127 |
+
def process_question(item):
|
| 128 |
task_id = item.get("task_id")
|
| 129 |
question_text = item.get("question")
|
| 130 |
if not task_id or question_text is None:
|
| 131 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 132 |
+
return None, None, None
|
| 133 |
try:
|
| 134 |
submitted_answer = agent(question_text)
|
| 135 |
+
return task_id, question_text, submitted_answer
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 138 |
+
return task_id, question_text, f"AGENT ERROR: {e}"
|
| 139 |
+
|
| 140 |
+
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 141 |
+
futures = {executor.submit(process_question, item): item for item in questions_data}
|
| 142 |
+
for future in as_completed(futures):
|
| 143 |
+
task_id, question_text, submitted_answer = future.result()
|
| 144 |
+
if task_id is None:
|
| 145 |
+
continue
|
| 146 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 147 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 148 |
+
# results_log = []
|
| 149 |
+
# answers_payload = []
|
| 150 |
+
# print(f"Running agent on {len(questions_data)} questions...")
|
| 151 |
+
# for item in questions_data:
|
| 152 |
+
# task_id = item.get("task_id")
|
| 153 |
+
# question_text = item.get("question")
|
| 154 |
+
# if not task_id or question_text is None:
|
| 155 |
+
# print(f"Skipping item with missing task_id or question: {item}")
|
| 156 |
+
# continue
|
| 157 |
+
# try:
|
| 158 |
+
# submitted_answer = agent(question_text)
|
| 159 |
+
# answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 160 |
+
# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 161 |
+
# except Exception as e:
|
| 162 |
+
# print(f"Error running agent on task {task_id}: {e}")
|
| 163 |
+
# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 164 |
|
| 165 |
if not answers_payload:
|
| 166 |
print("Agent did not produce any answers to submit.")
|