File size: 8,264 Bytes
4c12777
 
 
 
 
 
8b9c57a
4c12777
 
 
 
 
 
 
 
 
 
 
 
c001877
 
4c12777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155e351
4c12777
239b7d0
4c12777
c001877
4c12777
 
 
 
 
 
 
 
 
 
 
c001877
 
 
 
 
 
4c12777
 
 
c001877
4c12777
 
 
 
 
c001877
4c12777
c001877
4c12777
c001877
 
 
 
 
 
 
4c12777
 
8b9c57a
4c12777
c001877
 
 
 
4c12777
 
 
 
 
8b9c57a
 
 
 
 
4c12777
8b9c57a
 
4c12777
 
 
 
 
 
239b7d0
4c12777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b9c57a
4c12777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b9c57a
4c12777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c001877
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
app.py β€” HuggingFace Agents Course Unit 4 Final Assignment
Uses Google Gemini (gemini-2.0-flash) as the agent model.

Deploy to HF Spaces:
  - SDK: Gradio
  - Add secret: Gemini

Get your free Gemini API key at: https://aistudio.google.com/apikey
Scoring API docs: https://agents-course-unit4-scoring.hf.space/docs
Course leaderboard: https://huggingface.co/spaces/agents-course/Students_leaderboard
"""

import os
import re
import time

import gradio as gr
import requests
from google import genai
from google.genai import types

# ── Constants ─────────────────────────────────────────────────────────────────

DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
MODEL = "gemini-2.0-flash"

SYSTEM_PROMPT = (
    "You are a precise AI assistant. Answer the question as concisely as possible. "
    "Your final answer must be a number, as few words as possible, or a comma-separated "
    "list of numbers and/or strings. "
    "Do not include units (e.g. $, %) unless explicitly asked. "
    "Do not use articles or abbreviations for place names. "
    "Write numbers as digits, not words, unless asked otherwise. "
    "Reply with ONLY the answer β€” no explanation, no preamble."
)

# ── Agent ─────────────────────────────────────────────────────────────────────

def run_agent(question: str, task_id: str, api_url: str) -> str:
    """Call Gemini to answer one GAIA question. Attaches file if one exists."""
    api_key = os.environ.get("Gemini", "")
    if not api_key:
        return "[ERROR] Gemini secret is not set in this Space."

    client = genai.Client(api_key=api_key)

    # Build message parts β€” try to fetch any associated file
    parts = []
    try:
        file_resp = requests.get(f"{api_url}/files/{task_id}", timeout=10)
        if file_resp.status_code == 200 and file_resp.content:
            import base64
            content_type = file_resp.headers.get("content-type", "")

            if "pdf" in content_type or "image" in content_type:
                media_type = content_type.split(";")[0].strip()
                parts.append(types.Part(
                    inline_data=types.Blob(
                        mime_type=media_type,
                        data=base64.standard_b64encode(file_resp.content).decode(),
                    )
                ))
            else:
                try:
                    text_content = file_resp.content.decode("utf-8", errors="replace")
                    parts.append(types.Part(text=f"[Attached file content]\n{text_content}\n"))
                except Exception:
                    pass
    except Exception:
        pass  # No file or fetch failed β€” continue without it

    parts.append(types.Part(text=question))

    config = types.GenerateContentConfig(
        system_instruction=SYSTEM_PROMPT,
        tools=[types.Tool(google_search=types.GoogleSearch())],
        safety_settings=[
            types.SafetySetting(category="HARM_CATEGORY_HARASSMENT",        threshold="BLOCK_NONE"),
            types.SafetySetting(category="HARM_CATEGORY_HATE_SPEECH",       threshold="BLOCK_NONE"),
            types.SafetySetting(category="HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold="BLOCK_NONE"),
            types.SafetySetting(category="HARM_CATEGORY_DANGEROUS_CONTENT", threshold="BLOCK_NONE"),
        ],
    )

    for attempt in range(5):
        try:
            response = client.models.generate_content(
                model=MODEL,
                contents=parts,
                config=config,
            )
            answer = response.text.strip()
            answer = re.sub(r"(?i)^final answer:\s*", "", answer).strip()
            return answer
        except Exception as e:
            msg = str(e)
            if "429" in msg or "RESOURCE_EXHAUSTED" in msg:
                wait = 30 * (attempt + 1)  # 30s, 60s, 90s, 120s, 150s
                time.sleep(wait)
            elif attempt == 4:
                return f"[ERROR] {e}"
            else:
                time.sleep(5)

    return "[ERROR] All retries failed."


# ── Gradio logic ──────────────────────────────────────────────────────────────

def run_and_submit(profile: gr.OAuthProfile | None, *args):
    """Fetch questions β†’ run agent β†’ submit answers β†’ return results."""

    if profile is None:
        return "Please log in with your Hugging Face account first.", None

    username = profile.username
    space_id = os.environ.get("SPACE_ID", "")
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "N/A"
    api_url = DEFAULT_API_URL

    # Fetch questions
    status_lines = [f"Logged in as: **{username}**", "Fetching questions…"]
    try:
        resp = requests.get(f"{api_url}/questions", timeout=15)
        resp.raise_for_status()
        questions = resp.json()
    except Exception as e:
        return f"Failed to fetch questions: {e}", None

    if not questions:
        return "No questions returned by the API.", None

    status_lines.append(f"Fetched {len(questions)} questions. Running agent…")

    # Run agent on each question
    results_log = []
    answers_payload = []

    for i, item in enumerate(questions, 1):
        task_id = item.get("task_id", "")
        question = item.get("question", "")

        answer = run_agent(question, task_id, api_url)
        time.sleep(4)  # stay under free-tier RPM limit (15 req/min)

        answers_payload.append({"task_id": task_id, "submitted_answer": answer})
        results_log.append({
            "Task ID": task_id,
            "Question": question[:80] + ("…" if len(question) > 80 else ""),
            "Answer": answer,
        })
        status_lines.append(f"[{i}/{len(questions)}] {task_id} β†’ {answer[:60]}")

    # Submit answers
    status_lines.append("Submitting answers…")
    submit_payload = {
        "username": username,
        "agent_code": agent_code,
        "answers": answers_payload,
    }

    try:
        submit_resp = requests.post(f"{api_url}/submit", json=submit_payload, timeout=60)
        submit_resp.raise_for_status()
        result = submit_resp.json()

        score = result.get("score", "N/A")
        correct = result.get("correct_count", "?")
        total = result.get("total_attempted", len(questions))
        message = result.get("message", "")

        status_lines.append(f"\n**Score: {score}% ({correct}/{total} correct)**\n{message}")
    except Exception as e:
        status_lines.append(f"Submission failed: {e}")

    return "\n\n".join(status_lines), results_log


# ── Gradio UI ─────────────────────────────────────────────────────────────────

with gr.Blocks(title="GAIA Agent β€” Unit 4 Final Assignment") as demo:
    gr.Markdown(
        """
        # GAIA Agent β€” HF Agents Course Unit 4
        This agent fetches GAIA benchmark questions, answers them using **Gemini 2.0 Flash**
        with web search, and submits your score to the
        [course leaderboard](https://huggingface.co/spaces/agents-course/Students_leaderboard).

        **Before running:** make sure `Gemini` is set as a secret in your Space settings.
        Get a free key at [aistudio.google.com/apikey](https://aistudio.google.com/apikey).
        """
    )

    login_btn = gr.LoginButton()

    run_btn = gr.Button("Run Evaluation & Submit", variant="primary")

    status_box = gr.Markdown(label="Status")

    results_table = gr.Dataframe(
        headers=["Task ID", "Question", "Answer"],
        datatype=["str", "str", "str"],
        label="Results",
        wrap=True,
    )

    run_btn.click(
        fn=run_and_submit,
        inputs=[login_btn],
        outputs=[status_box, results_table],
    )

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