File size: 2,025 Bytes
06e83d9
 
 
2f004f0
 
 
 
 
 
 
 
 
 
 
 
06e83d9
 
 
2f004f0
06e83d9
 
2f004f0
 
 
 
 
 
 
06e83d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f004f0
 
 
 
06e83d9
2f004f0
 
 
06e83d9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)