File size: 1,777 Bytes
07a4cd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
import requests
import os

# ✅ Your Hugging Face credentials
USERNAME = "jcleee"
AGENT_CODE_URL = "https://huggingface.co/spaces/jcleee/First_agent_template/tree/main"
BASE_URL = "https://agents-course-unit4-scoring.hf.space"

def download_files(task_id, filenames):
    for fname in filenames:
        url = f"{BASE_URL}/files/{task_id}"
        response = requests.get(url)
        os.makedirs("downloads", exist_ok=True)
        with open(f"downloads/{fname}", "wb") as f:
            f.write(response.content)

def load_questions():
    response = requests.get(f"{BASE_URL}/questions")
    response.raise_for_status()
    return response.json()

def run_agent_on_question(question_text, agent):
    output = agent.run(task=question_text, reset=True)
    for step in output:
        if hasattr(step, "final_answer"):
            return step.final_answer
        elif hasattr(step, "tool_calls"):
            for call in step.tool_calls:
                if call.name == "final_answer":
                    return call.arguments.get("answer")
    return None

def submit_answers(agent):
    questions = load_questions()
    answers = []

    for q in questions:
        print(f"Running task: {q['task_id']}")

        if q["files"]:
            download_files(q["task_id"], q["files"])

        answer = run_agent_on_question(q["question"], agent)
        print("Answer:", answer)

        answers.append({
            "task_id": q["task_id"],
            "submitted_answer": str(answer or "null")
        })

    payload = {
        "username": USERNAME,
        "agent_code": AGENT_CODE_URL,
        "answers": answers
    }

    response = requests.post(f"{BASE_URL}/submit", json=payload)
    print("Submitted! Status:", response.status_code)
    print(response.json())