File size: 6,352 Bytes
10e9b7d
386943e
 
 
10e9b7d
3c4371f
386943e
 
 
 
 
 
 
 
 
 
 
 
10e9b7d
e80aab9
3db6293
386943e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
386943e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31243f4
 
386943e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c4371f
386943e
 
 
 
 
 
 
3c4371f
7e4a06b
386943e
e80aab9
31243f4
 
 
 
3c4371f
eccf8e4
386943e
7d65c66
386943e
7d65c66
386943e
 
 
 
31243f4
386943e
 
 
31243f4
 
386943e
31243f4
386943e
 
 
 
 
31243f4
 
386943e
e80aab9
386943e
 
 
 
 
e80aab9
386943e
 
 
 
 
 
 
 
 
e80aab9
386943e
7d65c66
386943e
e80aab9
 
386943e
e80aab9
386943e
0ee0419
386943e
 
 
e80aab9
7e4a06b
31243f4
386943e
7d65c66
386943e
e80aab9
 
3c4371f
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import re
import tempfile

import gradio as gr
import pandas as pd
import requests
from smolagents import (
    CodeAgent,
    DuckDuckGoSearchTool,
    VisitWebpageTool,
    tool,
)

try:
    from smolagents import InferenceClientModel as HFModel
except ImportError:  # older smolagents versions
    from smolagents import HfApiModel as HFModel

# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
MODEL_ID = os.getenv("MODEL_ID", "Qwen/Qwen2.5-Coder-32B-Instruct")


# --- Tools ---
@tool
def get_task_file(task_id: str) -> str:
    """Download the file attached to a task (if any) and return its content or local path.

    Args:
        task_id: The task_id of the question that has an attached file.
    """
    try:
        r = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=60)
        r.raise_for_status()
    except Exception as e:
        return f"No file could be downloaded for this task: {e}"

    disposition = r.headers.get("content-disposition", "")
    match = re.search(r'filename="?([^";]+)"?', disposition)
    name = match.group(1) if match else f"{task_id}.bin"
    path = os.path.join(tempfile.gettempdir(), name)
    with open(path, "wb") as f:
        f.write(r.content)

    lower = name.lower()
    try:
        if lower.endswith((".xlsx", ".xls")):
            sheets = pd.read_excel(path, sheet_name=None)
            return "\n\n".join(
                f"Sheet '{s}':\n{df.to_string()}" for s, df in sheets.items()
            )[:20000]
        if lower.endswith((".txt", ".csv", ".py", ".json", ".md")):
            return r.content.decode("utf-8", errors="replace")[:20000]
    except Exception as e:
        return f"File saved at {path} but could not be parsed: {e}"
    return (
        f"File saved at {path} (type: {name.split('.')[-1]}). "
        "It is a binary file (image/audio/other); handle it with Python code."
    )


SYSTEM_HINT = """You are solving GAIA-style questions. Rules:
- Use tools (web search, visiting pages, reading attached files, Python) to find the answer.
- Your final answer must be ONLY the answer: a number, a short string, or a comma-separated list.
- Do NOT write 'FINAL ANSWER', explanations, units unless asked, or full sentences.
- Numbers: no thousands separators, no currency/percent symbols unless asked.
- Strings: no articles (a, the) and no abbreviations unless asked.
- Lists: comma-separated, in the order requested.
- If the question mentions an attached file, call get_task_file with the given task_id.
"""


# --- Agent ---
class BasicAgent:
    def __init__(self):
        model = HFModel(model_id=MODEL_ID, token=os.getenv("HF_TOKEN"))
        self.agent = CodeAgent(
            tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), get_task_file],
            model=model,
            additional_authorized_imports=[
                "pandas", "numpy", "requests", "re", "json",
                "datetime", "math", "bs4", "collections", "itertools",
            ],
            max_steps=12,
        )

    def __call__(self, question: str, task_id: str = "", has_file: bool = False) -> str:
        prompt = f"{SYSTEM_HINT}\n\ntask_id: {task_id}\n"
        if has_file:
            prompt += "This task has an attached file (use get_task_file).\n"
        prompt += f"\nQuestion: {question}"
        answer = str(self.agent.run(prompt)).strip()
        answer = re.sub(r"(?i)^\s*final answer\s*:\s*", "", answer)
        return answer.strip().rstrip(".") if len(answer) > 1 else answer


# --- Run & submit ---
def run_and_submit_all(profile: gr.OAuthProfile | None):
    space_id = os.getenv("SPACE_ID")
    if not profile:
        return "Please log in to Hugging Face with the button.", None
    username = profile.username

    api_url = DEFAULT_API_URL
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"

    try:
        agent = BasicAgent()
    except Exception as e:
        return f"Error initializing agent: {e}", None

    try:
        response = requests.get(f"{api_url}/questions", timeout=30)
        response.raise_for_status()
        questions = response.json()
    except Exception as e:
        return f"Error fetching questions: {e}", None

    results_log, answers_payload = [], []
    for item in questions:
        task_id = item.get("task_id")
        question = item.get("question")
        has_file = bool(item.get("file_name"))
        if not task_id or question is None:
            continue
        try:
            answer = agent(question, task_id=task_id, has_file=has_file)
        except Exception as e:
            answer = f"AGENT ERROR: {e}"
        answers_payload.append({"task_id": task_id, "submitted_answer": answer})
        results_log.append(
            {"Task ID": task_id, "Question": question, "Submitted Answer": answer}
        )

    if not answers_payload:
        return "Agent produced no answers.", pd.DataFrame(results_log)

    submission = {
        "username": username.strip(),
        "agent_code": agent_code,
        "answers": answers_payload,
    }
    try:
        resp = requests.post(f"{api_url}/submit", json=submission, timeout=120)
        resp.raise_for_status()
        data = resp.json()
        status = (
            f"Submission successful!\n"
            f"User: {data.get('username')}\n"
            f"Score: {data.get('score')}% "
            f"({data.get('correct_count')}/{data.get('total_attempted')} correct)\n"
            f"Message: {data.get('message')}"
        )
        return status, pd.DataFrame(results_log)
    except Exception as e:
        return f"Submission failed: {e}", pd.DataFrame(results_log)


# --- Gradio UI ---
with gr.Blocks() as demo:
    gr.Markdown("# Agents Course - Final Assignment")
    gr.Markdown(
        "1. Log in with your Hugging Face account.\n"
        "2. Click the button to run the agent on all questions and submit.\n"
        "This can take several minutes."
    )
    gr.LoginButton()
    run_button = gr.Button("Run Evaluation & Submit All Answers")
    status_output = gr.Textbox(label="Run Status / Submission Result", lines=5)
    results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
    run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])

if __name__ == "__main__":
    demo.launch(debug=True, share=False)