Added threading
Browse files2 unmodified lines345678146 unmodified lines1551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092 unmodified linesimport requestsimport inspectimport pandas as pd# Import our custom tools from their modulesfrom huggingface_hub import login146 unmodified linesreturn f"An unexpected error occurred fetching questions: {e}", None# 3. Run your Agent# from concurrent.futures import ThreadPoolExecutor, as_completed# results_log = []# answers_payload = []# print(f"Running agent on {len(questions_data)} questions...")# def process_question(item):# task_id = item.get("task_id")# question_text = item.get("question")# if not task_id or question_text is None:# print(f"Skipping item with missing task_id or question: {item}")# return None, None, None# try:# submitted_answer = agent(question_text)# return task_id, question_text, submitted_answer# except Exception as e:# print(f"Error running agent on task {task_id}: {e}")# return task_id, question_text, f"AGENT ERROR: {e}"# with ThreadPoolExecutor(max_workers=5) as executor:# futures = {executor.submit(process_question, item): item for item in questions_data}# for future in as_completed(futures):# task_id, question_text, submitted_answer = future.result()# if task_id is None:# continue# answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})results_log = []answers_payload = []print(f"Running agent on {len(questions_data)} questions...")for item in questions_data:task_id = item.get("task_id")question_text = item.get("question")if not task_id or question_text is None:print(f"Skipping item with missing task_id or question: {item}")continuetry:# submitted_answer = agent(question_text)question_with_context = f"""Task ID: {task_id}If this question refers to an attached file, download it first from:https://agents-course-unit4-scoring.hf.space/files/{task_id}{question_text}"""submitted_answer = agent(question_with_context) # for Excel and audio questionsanswers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})except Exception as e:print(f"Error running agent on task {task_id}: {e}")results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})if not answers_payload:print("Agent did not produce any answers to submit.")2 unmodified lines345678910146 unmodified lines1571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412 unmodified linesimport requestsimport inspectimport pandas as pdimport jsonimport threading# Import our custom tools from their modulesfrom huggingface_hub import login146 unmodified linesreturn f"An unexpected error occurred fetching questions: {e}", None# 3. Run your Agentfrom concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutErrorcache_lock = threading.Lock()results_log = []answers_payload = []print(f"Running agent on {len(questions_data)} questions...")CACHE_FILE = "answer_cache.json"cache = json.load(open(CACHE_FILE)) if os.path.exists(CACHE_FILE) else {}def process_question(item):task_id = item.get("task_id")question_text = item.get("question")if not task_id or question_text is None:print(f"Skipping item with missing task_id or question: {item}")return None, None, None# Return cached answer if availableif task_id in cache:print(f"Cache hit for task {task_id}")return task_id, question_text, cache[task_id]try:question_with_context = f"""Task ID: {task_id}If this question refers to an attached file, download it first from:https://agents-course-unit4-scoring.hf.space/files/{task_id}{question_text}"""submitted_answer = agent(question_with_context)# Save to cachewith cache_lock:cache[task_id] = submitted_answerjson.dump(cache, open(CACHE_FILE, "w"))return task_id, question_text, submitted_answerexcept Exception as e:print(f"Error running agent on task {task_id}: {e}")return task_id, question_text, f"AGENT ERROR: {e}"with ThreadPoolExecutor(max_workers=3) as executor:futures = {executor.submit(process_question, item): item for item in questions_data}for future in as_completed(futures):item = futures[future]task_id = item.get("task_id")question_text = item.get("question")try:result_task_id, result_question, submitted_answer = future.result(timeout=120)if result_task_id is None:continueanswers_payload.append({"task_id": result_task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": result_task_id, "Question": result_question, "Submitted Answer": submitted_answer})except TimeoutError:print(f"Task {task_id} timed out after 120s, skipping.")answers_payload.append({"task_id": task_id, "submitted_answer": "TIMEOUT"})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "TIMEOUT"})except Exception as e:print(f"Task {task_id} raised an exception: {e}")answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT ERROR: {e}"})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})# results_log = []# answers_payload = []# print(f"Running agent on {len(questions_data)} questions...")# for item in questions_data:# task_id = item.get("task_id")# question_text = item.get("question")# if not task_id or question_text is None:# print(f"Skipping item with missing task_id or question: {item}")# continue# try:# # submitted_answer = agent(question_text)# question_with_context = f"""Task ID: {task_id}# If this question refers to an attached file, download it first from:# https://agents-course-unit4-scoring.hf.space/files/{task_id}# {question_text}"""# submitted_answer = agent(question_with_context) # for Excel and audio questions# answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})# except Exception as e:# print(f"Error running agent on task {task_id}: {e}")# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})if not answers_payload:print("Agent did not produce any answers to submit.")
app.py+70-38