Spaces:
Sleeping
Sleeping
from agent import LangGraphAgent | |
from questions import questions | |
import json | |
import os | |
# Define api_url (missing in your current code) | |
api_url = "https://agents-course-unit4-scoring.hf.space" | |
# Load existing answers if available | |
existing_answers = {} | |
if os.path.exists('answers.json'): | |
with open('answers.json', 'r') as f: | |
existing_answers_list = json.load(f) | |
existing_answers = {item["task_id"]: item["submitted_answer"] for item in existing_answers_list} | |
print(f"Loaded {len(existing_answers)} existing answers") | |
agent = LangGraphAgent() | |
answers_payload = [] | |
for item in questions: | |
task_id = item.get("task_id") | |
# Skip if already answered | |
if task_id in existing_answers: | |
print(f"Skipping already answered task: {task_id}") | |
answers_payload.append({"task_id": task_id, "submitted_answer": existing_answers[task_id]}) | |
continue | |
question_text = item.get("question") | |
file_name = item.get("file_name") | |
if not task_id or question_text is None: | |
print(f"Skipping item with missing task_id or question: {item}") | |
continue | |
try: | |
if file_name: | |
question_text += f"\n\nFile url: {api_url}/files/{task_id}" | |
try: | |
ext = file_name.split('.')[-1] | |
question_text += f"\n File extension is (.{ext} file)" | |
except: | |
pass | |
submitted_answer = agent(question_text) | |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) | |
print({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}) | |
# Save after each successful answer to prevent loss on errors | |
with open('answers.json', 'w') as f: | |
json.dump(answers_payload, f) | |
except Exception as e: | |
print(f"Error running agent on task {task_id}: {e}") | |
print(f"Completed processing {len(answers_payload)} answers") | |
with open('answers.json', 'w') as f: | |
json.dump(answers_payload, f) | |