Added threading
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Import our custom tools from their modules
|
| 8 |
from huggingface_hub import login
|
|
@@ -155,55 +157,85 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 155 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 156 |
|
| 157 |
# 3. Run your Agent
|
| 158 |
-
# from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 159 |
-
|
| 160 |
-
# results_log = []
|
| 161 |
-
# answers_payload = []
|
| 162 |
-
# print(f"Running agent on {len(questions_data)} questions...")
|
| 163 |
-
|
| 164 |
-
# def process_question(item):
|
| 165 |
-
# task_id = item.get("task_id")
|
| 166 |
-
# question_text = item.get("question")
|
| 167 |
-
# if not task_id or question_text is None:
|
| 168 |
-
# print(f"Skipping item with missing task_id or question: {item}")
|
| 169 |
-
# return None, None, None
|
| 170 |
-
# try:
|
| 171 |
-
# submitted_answer = agent(question_text)
|
| 172 |
-
# return task_id, question_text, submitted_answer
|
| 173 |
-
# except Exception as e:
|
| 174 |
-
# print(f"Error running agent on task {task_id}: {e}")
|
| 175 |
-
# return task_id, question_text, f"AGENT ERROR: {e}"
|
| 176 |
-
|
| 177 |
-
# with ThreadPoolExecutor(max_workers=5) as executor:
|
| 178 |
-
# futures = {executor.submit(process_question, item): item for item in questions_data}
|
| 179 |
-
# for future in as_completed(futures):
|
| 180 |
-
# task_id, question_text, submitted_answer = future.result()
|
| 181 |
-
# if task_id is None:
|
| 182 |
-
# continue
|
| 183 |
-
# answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 184 |
-
# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 185 |
|
|
|
|
|
|
|
| 186 |
results_log = []
|
| 187 |
answers_payload = []
|
| 188 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
task_id = item.get("task_id")
|
| 191 |
question_text = item.get("question")
|
| 192 |
if not task_id or question_text is None:
|
| 193 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
try:
|
| 196 |
-
# submitted_answer = agent(question_text)
|
| 197 |
question_with_context = f"""Task ID: {task_id}
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
submitted_answer = agent(question_with_context)
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
| 204 |
except Exception as e:
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
if not answers_payload:
|
| 209 |
print("Agent did not produce any answers to submit.")
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import json
|
| 7 |
+
import threading
|
| 8 |
|
| 9 |
# Import our custom tools from their modules
|
| 10 |
from huggingface_hub import login
|
|
|
|
| 157 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 158 |
|
| 159 |
# 3. Run your Agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError
|
| 162 |
+
cache_lock = threading.Lock()
|
| 163 |
results_log = []
|
| 164 |
answers_payload = []
|
| 165 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 166 |
+
|
| 167 |
+
CACHE_FILE = "answer_cache.json"
|
| 168 |
+
cache = json.load(open(CACHE_FILE)) if os.path.exists(CACHE_FILE) else {}
|
| 169 |
+
|
| 170 |
+
def process_question(item):
|
| 171 |
task_id = item.get("task_id")
|
| 172 |
question_text = item.get("question")
|
| 173 |
if not task_id or question_text is None:
|
| 174 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 175 |
+
return None, None, None
|
| 176 |
+
|
| 177 |
+
# Return cached answer if available
|
| 178 |
+
if task_id in cache:
|
| 179 |
+
print(f"Cache hit for task {task_id}")
|
| 180 |
+
return task_id, question_text, cache[task_id]
|
| 181 |
+
|
| 182 |
try:
|
|
|
|
| 183 |
question_with_context = f"""Task ID: {task_id}
|
| 184 |
+
If this question refers to an attached file, download it first from:
|
| 185 |
+
https://agents-course-unit4-scoring.hf.space/files/{task_id}
|
| 186 |
+
{question_text}"""
|
| 187 |
+
submitted_answer = agent(question_with_context)
|
| 188 |
+
# Save to cache
|
| 189 |
+
with cache_lock:
|
| 190 |
+
cache[task_id] = submitted_answer
|
| 191 |
+
json.dump(cache, open(CACHE_FILE, "w"))
|
| 192 |
+
return task_id, question_text, submitted_answer
|
| 193 |
except Exception as e:
|
| 194 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 195 |
+
return task_id, question_text, f"AGENT ERROR: {e}"
|
| 196 |
+
|
| 197 |
+
with ThreadPoolExecutor(max_workers=3) as executor:
|
| 198 |
+
futures = {executor.submit(process_question, item): item for item in questions_data}
|
| 199 |
+
for future in as_completed(futures):
|
| 200 |
+
item = futures[future]
|
| 201 |
+
task_id = item.get("task_id")
|
| 202 |
+
question_text = item.get("question")
|
| 203 |
+
try:
|
| 204 |
+
result_task_id, result_question, submitted_answer = future.result(timeout=120)
|
| 205 |
+
if result_task_id is None:
|
| 206 |
+
continue
|
| 207 |
+
answers_payload.append({"task_id": result_task_id, "submitted_answer": submitted_answer})
|
| 208 |
+
results_log.append({"Task ID": result_task_id, "Question": result_question, "Submitted Answer": submitted_answer})
|
| 209 |
+
except TimeoutError:
|
| 210 |
+
print(f"Task {task_id} timed out after 120s, skipping.")
|
| 211 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": "TIMEOUT"})
|
| 212 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "TIMEOUT"})
|
| 213 |
+
except Exception as e:
|
| 214 |
+
print(f"Task {task_id} raised an exception: {e}")
|
| 215 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"})
|
| 216 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 217 |
+
|
| 218 |
+
# results_log = []
|
| 219 |
+
# answers_payload = []
|
| 220 |
+
# print(f"Running agent on {len(questions_data)} questions...")
|
| 221 |
+
# for item in questions_data:
|
| 222 |
+
# task_id = item.get("task_id")
|
| 223 |
+
# question_text = item.get("question")
|
| 224 |
+
# if not task_id or question_text is None:
|
| 225 |
+
# print(f"Skipping item with missing task_id or question: {item}")
|
| 226 |
+
# continue
|
| 227 |
+
# try:
|
| 228 |
+
# # submitted_answer = agent(question_text)
|
| 229 |
+
# question_with_context = f"""Task ID: {task_id}
|
| 230 |
+
# If this question refers to an attached file, download it first from:
|
| 231 |
+
# https://agents-course-unit4-scoring.hf.space/files/{task_id}
|
| 232 |
+
# {question_text}"""
|
| 233 |
+
# submitted_answer = agent(question_with_context) # for Excel and audio questions
|
| 234 |
+
# answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 235 |
+
# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 236 |
+
# except Exception as e:
|
| 237 |
+
# print(f"Error running agent on task {task_id}: {e}")
|
| 238 |
+
# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 239 |
|
| 240 |
if not answers_payload:
|
| 241 |
print("Agent did not produce any answers to submit.")
|