Update app.py
Browse files
app.py
CHANGED
|
@@ -5,197 +5,689 @@ import inspect
|
|
| 5 |
import pandas as pd
|
| 6 |
import spaces
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@spaces.GPU(duration=1)
|
| 9 |
def _zerogpu_probe():
|
| 10 |
return True
|
| 11 |
|
| 12 |
-
|
| 13 |
-
# ---
|
|
|
|
|
|
|
|
|
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
# -----
|
|
|
|
|
|
|
|
|
|
| 18 |
class BasicAgent:
|
| 19 |
def __init__(self):
|
| 20 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def __call__(self, question: str) -> str:
|
| 22 |
-
print(
|
| 23 |
-
|
| 24 |
-
print(
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
| 29 |
-
Fetches all questions, runs the
|
| 30 |
-
and displays the results.
|
| 31 |
"""
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
if profile:
|
| 36 |
-
username= f"{profile.username}"
|
| 37 |
print(f"User logged in: {username}")
|
| 38 |
else:
|
| 39 |
print("User not logged in.")
|
| 40 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
api_url = DEFAULT_API_URL
|
| 43 |
questions_url = f"{api_url}/questions"
|
| 44 |
submit_url = f"{api_url}/submit"
|
| 45 |
|
| 46 |
-
#
|
|
|
|
|
|
|
|
|
|
| 47 |
try:
|
| 48 |
agent = BasicAgent()
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
print(f"Error instantiating agent: {e}")
|
| 51 |
-
return f"Error initializing agent: {e}", None
|
| 52 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
| 53 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 54 |
-
print(agent_code)
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
print(f"Fetching questions from: {questions_url}")
|
|
|
|
| 58 |
try:
|
| 59 |
-
response = requests.get(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
response.raise_for_status()
|
|
|
|
| 61 |
questions_data = response.json()
|
|
|
|
| 62 |
if not questions_data:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
except requests.exceptions.RequestException as e:
|
| 67 |
print(f"Error fetching questions: {e}")
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
except requests.exceptions.JSONDecodeError as e:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
-
print(
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
# 3. Run your Agent
|
| 78 |
results_log = []
|
| 79 |
answers_payload = []
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
task_id = item.get("task_id")
|
| 83 |
question_text = item.get("question")
|
|
|
|
| 84 |
if not task_id or question_text is None:
|
| 85 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 86 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
try:
|
| 88 |
-
submitted_answer = agent(
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
if not answers_payload:
|
| 96 |
-
print(
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
# 4. Prepare Submission
|
| 100 |
-
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 101 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 102 |
print(status_update)
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
try:
|
| 107 |
-
response = requests.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
response.raise_for_status()
|
|
|
|
| 109 |
result_data = response.json()
|
|
|
|
| 110 |
final_status = (
|
| 111 |
-
|
| 112 |
-
f"User:
|
| 113 |
-
f"
|
| 114 |
-
f"
|
| 115 |
-
f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
)
|
|
|
|
| 117 |
print("Submission successful.")
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
except requests.exceptions.HTTPError as e:
|
| 121 |
-
error_detail =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
try:
|
| 123 |
error_json = e.response.json()
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
except requests.exceptions.JSONDecodeError:
|
| 126 |
-
error_detail +=
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
print(status_message)
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
except requests.exceptions.Timeout:
|
| 132 |
-
status_message =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
print(status_message)
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
except requests.exceptions.RequestException as e:
|
| 137 |
-
status_message =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
print(status_message)
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
except Exception as e:
|
| 142 |
-
status_message =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
print(status_message)
|
| 144 |
-
results_df = pd.DataFrame(results_log)
|
| 145 |
-
return status_message, results_df
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
# --- Build Gradio Interface using Blocks ---
|
| 149 |
with gr.Blocks() as demo:
|
| 150 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 151 |
gr.Markdown(
|
| 152 |
"""
|
| 153 |
**Instructions:**
|
| 154 |
|
| 155 |
-
1.
|
| 156 |
-
2.
|
| 157 |
-
3.
|
|
|
|
| 158 |
|
| 159 |
---
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
| 163 |
"""
|
| 164 |
)
|
| 165 |
|
| 166 |
gr.LoginButton()
|
| 167 |
|
| 168 |
-
run_button = gr.Button(
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
status_output = gr.Textbox(
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
run_button.click(
|
| 175 |
fn=run_and_submit_all,
|
| 176 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 177 |
)
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
if __name__ == "__main__":
|
| 180 |
-
print(
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
if space_host_startup:
|
| 186 |
-
print(
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
else:
|
| 189 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 192 |
-
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 193 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 194 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 195 |
else:
|
| 196 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
import spaces
|
| 7 |
|
| 8 |
+
from smolagents import (
|
| 9 |
+
CodeAgent,
|
| 10 |
+
InferenceClientModel,
|
| 11 |
+
DuckDuckGoSearchTool,
|
| 12 |
+
VisitWebpageTool,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# ---------------------------------------------------------
|
| 17 |
+
# ZeroGPU startup requirement
|
| 18 |
+
# ---------------------------------------------------------
|
| 19 |
+
|
| 20 |
@spaces.GPU(duration=1)
|
| 21 |
def _zerogpu_probe():
|
| 22 |
return True
|
| 23 |
|
| 24 |
+
|
| 25 |
+
# ---------------------------------------------------------
|
| 26 |
+
# Constants
|
| 27 |
+
# ---------------------------------------------------------
|
| 28 |
+
|
| 29 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 30 |
|
| 31 |
+
|
| 32 |
+
# ---------------------------------------------------------
|
| 33 |
+
# GAIA Agent
|
| 34 |
+
# ---------------------------------------------------------
|
| 35 |
+
|
| 36 |
class BasicAgent:
|
| 37 |
def __init__(self):
|
| 38 |
+
print("Initializing GAIA agent...")
|
| 39 |
+
|
| 40 |
+
# Read the Hugging Face token securely from Space Secrets.
|
| 41 |
+
token = os.getenv("HF_TOKEN")
|
| 42 |
+
|
| 43 |
+
if not token:
|
| 44 |
+
raise RuntimeError(
|
| 45 |
+
"HF_TOKEN secret is not configured. "
|
| 46 |
+
"Add HF_TOKEN under Space Settings -> Variables and secrets."
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Small reasoning model available through HF Inference Providers.
|
| 50 |
+
self.model = InferenceClientModel(
|
| 51 |
+
model_id="Qwen/Qwen3-4B-Thinking-2507",
|
| 52 |
+
provider="auto",
|
| 53 |
+
token=token,
|
| 54 |
+
temperature=0.0,
|
| 55 |
+
max_tokens=2048,
|
| 56 |
+
timeout=120,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# CodeAgent can reason, execute Python, search the web,
|
| 60 |
+
# and inspect webpages.
|
| 61 |
+
self.agent = CodeAgent(
|
| 62 |
+
tools=[
|
| 63 |
+
DuckDuckGoSearchTool(
|
| 64 |
+
max_results=8,
|
| 65 |
+
),
|
| 66 |
+
VisitWebpageTool(
|
| 67 |
+
max_output_length=30000,
|
| 68 |
+
),
|
| 69 |
+
],
|
| 70 |
+
model=self.model,
|
| 71 |
+
max_steps=6,
|
| 72 |
+
additional_authorized_imports=[
|
| 73 |
+
"math",
|
| 74 |
+
"re",
|
| 75 |
+
"json",
|
| 76 |
+
"datetime",
|
| 77 |
+
"statistics",
|
| 78 |
+
"collections",
|
| 79 |
+
"itertools",
|
| 80 |
+
],
|
| 81 |
+
instructions="""
|
| 82 |
+
You are solving GAIA benchmark questions.
|
| 83 |
+
|
| 84 |
+
Your job is to determine the exact answer to the user's question.
|
| 85 |
+
|
| 86 |
+
Work carefully and verify factual information.
|
| 87 |
+
|
| 88 |
+
Use web search when the question requires current, historical,
|
| 89 |
+
geographical, biographical, scientific, cultural, or other factual
|
| 90 |
+
information that should be verified.
|
| 91 |
+
|
| 92 |
+
Use the webpage-reading tool when search results point to useful sources.
|
| 93 |
+
|
| 94 |
+
Use Python when calculations, counting, date arithmetic, conversions,
|
| 95 |
+
logical operations, or other computational reasoning would improve
|
| 96 |
+
accuracy.
|
| 97 |
+
|
| 98 |
+
You may perform multiple searches or calculations before answering.
|
| 99 |
+
|
| 100 |
+
IMPORTANT:
|
| 101 |
+
The evaluation system uses exact-match scoring.
|
| 102 |
+
|
| 103 |
+
Your final response MUST contain only the requested answer.
|
| 104 |
+
|
| 105 |
+
Do not explain your reasoning.
|
| 106 |
+
Do not provide citations.
|
| 107 |
+
Do not add introductory text.
|
| 108 |
+
Do not add concluding text.
|
| 109 |
+
Do not write "FINAL ANSWER".
|
| 110 |
+
Do not write "Final answer".
|
| 111 |
+
Do not write "The answer is".
|
| 112 |
+
Do not surround the answer with Markdown formatting.
|
| 113 |
+
Do not use a code block unless the question explicitly asks for code.
|
| 114 |
+
|
| 115 |
+
Follow the exact format requested by the question.
|
| 116 |
+
|
| 117 |
+
Preserve appropriate:
|
| 118 |
+
- capitalization
|
| 119 |
+
- punctuation
|
| 120 |
+
- spelling
|
| 121 |
+
- names
|
| 122 |
+
- dates
|
| 123 |
+
- units
|
| 124 |
+
- decimal places
|
| 125 |
+
- comma-separated lists
|
| 126 |
+
- ordering
|
| 127 |
+
|
| 128 |
+
If the question requests multiple items, return only those items in the
|
| 129 |
+
requested format.
|
| 130 |
+
|
| 131 |
+
Before producing the final response, verify that it directly answers
|
| 132 |
+
the question and contains nothing unnecessary.
|
| 133 |
+
""",
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
print("GAIA agent initialized successfully.")
|
| 137 |
+
|
| 138 |
def __call__(self, question: str) -> str:
|
| 139 |
+
print("\n" + "=" * 80)
|
| 140 |
+
print("Agent received question:")
|
| 141 |
+
print(question)
|
| 142 |
+
print("=" * 80)
|
| 143 |
+
|
| 144 |
+
try:
|
| 145 |
+
result = self.agent.run(question)
|
| 146 |
+
|
| 147 |
+
answer = str(result).strip()
|
| 148 |
+
|
| 149 |
+
# -------------------------------------------------
|
| 150 |
+
# Defensive cleanup for exact-match scoring
|
| 151 |
+
# -------------------------------------------------
|
| 152 |
+
|
| 153 |
+
prefixes = [
|
| 154 |
+
"FINAL ANSWER:",
|
| 155 |
+
"FINAL ANSWER :",
|
| 156 |
+
"Final Answer:",
|
| 157 |
+
"Final Answer :",
|
| 158 |
+
"Final answer:",
|
| 159 |
+
"Final answer :",
|
| 160 |
+
"Answer:",
|
| 161 |
+
"Answer :",
|
| 162 |
+
"The answer is ",
|
| 163 |
+
"The answer is: ",
|
| 164 |
+
]
|
| 165 |
|
| 166 |
+
for prefix in prefixes:
|
| 167 |
+
if answer.startswith(prefix):
|
| 168 |
+
answer = answer[len(prefix):].strip()
|
| 169 |
+
break
|
| 170 |
+
|
| 171 |
+
# Remove accidental Markdown code fences/backticks.
|
| 172 |
+
if answer.startswith("```") and answer.endswith("```"):
|
| 173 |
+
answer = answer[3:-3].strip()
|
| 174 |
+
|
| 175 |
+
# Remove a language identifier if one appears.
|
| 176 |
+
first_line, separator, remainder = answer.partition("\n")
|
| 177 |
+
if separator and first_line.strip().lower() in {
|
| 178 |
+
"text",
|
| 179 |
+
"plaintext",
|
| 180 |
+
"python",
|
| 181 |
+
}:
|
| 182 |
+
answer = remainder.strip()
|
| 183 |
+
|
| 184 |
+
answer = answer.strip("`").strip()
|
| 185 |
+
|
| 186 |
+
print(f"Final submitted answer: {answer}")
|
| 187 |
+
return answer
|
| 188 |
+
|
| 189 |
+
except Exception as e:
|
| 190 |
+
print(f"Agent execution error: {type(e).__name__}: {e}")
|
| 191 |
+
raise
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
# ---------------------------------------------------------
|
| 195 |
+
# Evaluation and Submission
|
| 196 |
+
# ---------------------------------------------------------
|
| 197 |
+
|
| 198 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 199 |
"""
|
| 200 |
+
Fetches all evaluation questions, runs the agent on each question,
|
| 201 |
+
submits all generated answers, and displays the results.
|
| 202 |
"""
|
| 203 |
+
|
| 204 |
+
# -----------------------------------------------------
|
| 205 |
+
# Determine Hugging Face username and Space URL
|
| 206 |
+
# -----------------------------------------------------
|
| 207 |
+
|
| 208 |
+
space_id = os.getenv("SPACE_ID")
|
| 209 |
|
| 210 |
if profile:
|
| 211 |
+
username = f"{profile.username}"
|
| 212 |
print(f"User logged in: {username}")
|
| 213 |
else:
|
| 214 |
print("User not logged in.")
|
| 215 |
+
return (
|
| 216 |
+
"Please Login to Hugging Face with the button.",
|
| 217 |
+
None,
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
if not space_id:
|
| 221 |
+
print("SPACE_ID environment variable is missing.")
|
| 222 |
+
return (
|
| 223 |
+
"SPACE_ID environment variable is missing.",
|
| 224 |
+
None,
|
| 225 |
+
)
|
| 226 |
|
| 227 |
api_url = DEFAULT_API_URL
|
| 228 |
questions_url = f"{api_url}/questions"
|
| 229 |
submit_url = f"{api_url}/submit"
|
| 230 |
|
| 231 |
+
# -----------------------------------------------------
|
| 232 |
+
# 1. Instantiate agent
|
| 233 |
+
# -----------------------------------------------------
|
| 234 |
+
|
| 235 |
try:
|
| 236 |
agent = BasicAgent()
|
| 237 |
+
|
| 238 |
except Exception as e:
|
| 239 |
print(f"Error instantiating agent: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
+
return (
|
| 242 |
+
f"Error initializing agent: {e}",
|
| 243 |
+
None,
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
# Public link to the submitted agent code.
|
| 247 |
+
agent_code = (
|
| 248 |
+
f"https://huggingface.co/spaces/"
|
| 249 |
+
f"{space_id}/tree/main"
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
print(f"Agent code URL: {agent_code}")
|
| 253 |
+
|
| 254 |
+
# -----------------------------------------------------
|
| 255 |
+
# 2. Fetch questions
|
| 256 |
+
# -----------------------------------------------------
|
| 257 |
+
|
| 258 |
print(f"Fetching questions from: {questions_url}")
|
| 259 |
+
|
| 260 |
try:
|
| 261 |
+
response = requests.get(
|
| 262 |
+
questions_url,
|
| 263 |
+
timeout=30,
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
response.raise_for_status()
|
| 267 |
+
|
| 268 |
questions_data = response.json()
|
| 269 |
+
|
| 270 |
if not questions_data:
|
| 271 |
+
print("Fetched questions list is empty.")
|
| 272 |
+
|
| 273 |
+
return (
|
| 274 |
+
"Fetched questions list is empty or invalid format.",
|
| 275 |
+
None,
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
print(
|
| 279 |
+
f"Fetched {len(questions_data)} questions."
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
except requests.exceptions.RequestException as e:
|
| 283 |
print(f"Error fetching questions: {e}")
|
| 284 |
+
|
| 285 |
+
return (
|
| 286 |
+
f"Error fetching questions: {e}",
|
| 287 |
+
None,
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
except requests.exceptions.JSONDecodeError as e:
|
| 291 |
+
print(
|
| 292 |
+
f"Error decoding JSON response "
|
| 293 |
+
f"from questions endpoint: {e}"
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
print(
|
| 297 |
+
f"Response text: {response.text[:500]}"
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
+
return (
|
| 301 |
+
f"Error decoding server response "
|
| 302 |
+
f"for questions: {e}",
|
| 303 |
+
None,
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
except Exception as e:
|
| 307 |
+
print(
|
| 308 |
+
f"Unexpected error fetching questions: {e}"
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
return (
|
| 312 |
+
f"An unexpected error occurred "
|
| 313 |
+
f"fetching questions: {e}",
|
| 314 |
+
None,
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
# -----------------------------------------------------
|
| 318 |
+
# 3. Run agent on every question
|
| 319 |
+
# -----------------------------------------------------
|
| 320 |
|
|
|
|
| 321 |
results_log = []
|
| 322 |
answers_payload = []
|
| 323 |
+
|
| 324 |
+
print(
|
| 325 |
+
f"Running agent on "
|
| 326 |
+
f"{len(questions_data)} questions..."
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
for index, item in enumerate(
|
| 330 |
+
questions_data,
|
| 331 |
+
start=1,
|
| 332 |
+
):
|
| 333 |
task_id = item.get("task_id")
|
| 334 |
question_text = item.get("question")
|
| 335 |
+
|
| 336 |
if not task_id or question_text is None:
|
| 337 |
+
print(
|
| 338 |
+
"Skipping item with missing "
|
| 339 |
+
f"task_id or question: {item}"
|
| 340 |
+
)
|
| 341 |
continue
|
| 342 |
+
|
| 343 |
+
print("\n" + "#" * 80)
|
| 344 |
+
print(
|
| 345 |
+
f"QUESTION {index}/"
|
| 346 |
+
f"{len(questions_data)}"
|
| 347 |
+
)
|
| 348 |
+
print(f"Task ID: {task_id}")
|
| 349 |
+
print("#" * 80)
|
| 350 |
+
|
| 351 |
try:
|
| 352 |
+
submitted_answer = agent(
|
| 353 |
+
question_text
|
| 354 |
+
)
|
| 355 |
+
|
| 356 |
+
submitted_answer = str(
|
| 357 |
+
submitted_answer
|
| 358 |
+
).strip()
|
| 359 |
+
|
| 360 |
+
answers_payload.append(
|
| 361 |
+
{
|
| 362 |
+
"task_id": task_id,
|
| 363 |
+
"submitted_answer":
|
| 364 |
+
submitted_answer,
|
| 365 |
+
}
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
results_log.append(
|
| 369 |
+
{
|
| 370 |
+
"Task ID": task_id,
|
| 371 |
+
"Question": question_text,
|
| 372 |
+
"Submitted Answer":
|
| 373 |
+
submitted_answer,
|
| 374 |
+
}
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
except Exception as e:
|
| 378 |
+
print(
|
| 379 |
+
f"Error running agent on "
|
| 380 |
+
f"task {task_id}: {e}"
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
+
results_log.append(
|
| 384 |
+
{
|
| 385 |
+
"Task ID": task_id,
|
| 386 |
+
"Question": question_text,
|
| 387 |
+
"Submitted Answer":
|
| 388 |
+
f"AGENT ERROR: {e}",
|
| 389 |
+
}
|
| 390 |
+
)
|
| 391 |
+
|
| 392 |
+
# -----------------------------------------------------
|
| 393 |
+
# Ensure at least one valid answer exists
|
| 394 |
+
# -----------------------------------------------------
|
| 395 |
|
| 396 |
if not answers_payload:
|
| 397 |
+
print(
|
| 398 |
+
"Agent did not produce any answers "
|
| 399 |
+
"to submit."
|
| 400 |
+
)
|
| 401 |
+
|
| 402 |
+
return (
|
| 403 |
+
"Agent did not produce any answers "
|
| 404 |
+
"to submit.",
|
| 405 |
+
pd.DataFrame(results_log),
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
# -----------------------------------------------------
|
| 409 |
+
# 4. Prepare submission
|
| 410 |
+
# -----------------------------------------------------
|
| 411 |
+
|
| 412 |
+
submission_data = {
|
| 413 |
+
"username": username.strip(),
|
| 414 |
+
"agent_code": agent_code,
|
| 415 |
+
"answers": answers_payload,
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
status_update = (
|
| 419 |
+
f"Agent finished. "
|
| 420 |
+
f"Submitting {len(answers_payload)} "
|
| 421 |
+
f"answers for user '{username}'..."
|
| 422 |
+
)
|
| 423 |
|
|
|
|
|
|
|
|
|
|
| 424 |
print(status_update)
|
| 425 |
|
| 426 |
+
# -----------------------------------------------------
|
| 427 |
+
# 5. Submit answers
|
| 428 |
+
# -----------------------------------------------------
|
| 429 |
+
|
| 430 |
+
print(
|
| 431 |
+
f"Submitting "
|
| 432 |
+
f"{len(answers_payload)} answers "
|
| 433 |
+
f"to: {submit_url}"
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
try:
|
| 437 |
+
response = requests.post(
|
| 438 |
+
submit_url,
|
| 439 |
+
json=submission_data,
|
| 440 |
+
timeout=120,
|
| 441 |
+
)
|
| 442 |
+
|
| 443 |
response.raise_for_status()
|
| 444 |
+
|
| 445 |
result_data = response.json()
|
| 446 |
+
|
| 447 |
final_status = (
|
| 448 |
+
"Submission Successful!\n"
|
| 449 |
+
f"User: "
|
| 450 |
+
f"{result_data.get('username')}\n"
|
| 451 |
+
f"Overall Score: "
|
| 452 |
+
f"{result_data.get('score', 'N/A')}% "
|
| 453 |
+
f"("
|
| 454 |
+
f"{result_data.get('correct_count', '?')}/"
|
| 455 |
+
f"{result_data.get('total_attempted', '?')} "
|
| 456 |
+
f"correct)\n"
|
| 457 |
+
f"Message: "
|
| 458 |
+
f"{result_data.get('message', 'No message received.')}"
|
| 459 |
)
|
| 460 |
+
|
| 461 |
print("Submission successful.")
|
| 462 |
+
print(final_status)
|
| 463 |
+
|
| 464 |
+
results_df = pd.DataFrame(
|
| 465 |
+
results_log
|
| 466 |
+
)
|
| 467 |
+
|
| 468 |
+
return (
|
| 469 |
+
final_status,
|
| 470 |
+
results_df,
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
except requests.exceptions.HTTPError as e:
|
| 474 |
+
error_detail = (
|
| 475 |
+
f"Server responded with status "
|
| 476 |
+
f"{e.response.status_code}."
|
| 477 |
+
)
|
| 478 |
+
|
| 479 |
try:
|
| 480 |
error_json = e.response.json()
|
| 481 |
+
|
| 482 |
+
error_detail += (
|
| 483 |
+
" Detail: "
|
| 484 |
+
f"{error_json.get('detail', e.response.text)}"
|
| 485 |
+
)
|
| 486 |
+
|
| 487 |
except requests.exceptions.JSONDecodeError:
|
| 488 |
+
error_detail += (
|
| 489 |
+
f" Response: "
|
| 490 |
+
f"{e.response.text[:500]}"
|
| 491 |
+
)
|
| 492 |
+
|
| 493 |
+
status_message = (
|
| 494 |
+
f"Submission Failed: "
|
| 495 |
+
f"{error_detail}"
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
print(status_message)
|
| 499 |
+
|
| 500 |
+
results_df = pd.DataFrame(
|
| 501 |
+
results_log
|
| 502 |
+
)
|
| 503 |
+
|
| 504 |
+
return (
|
| 505 |
+
status_message,
|
| 506 |
+
results_df,
|
| 507 |
+
)
|
| 508 |
+
|
| 509 |
except requests.exceptions.Timeout:
|
| 510 |
+
status_message = (
|
| 511 |
+
"Submission Failed: "
|
| 512 |
+
"The request timed out."
|
| 513 |
+
)
|
| 514 |
+
|
| 515 |
print(status_message)
|
| 516 |
+
|
| 517 |
+
results_df = pd.DataFrame(
|
| 518 |
+
results_log
|
| 519 |
+
)
|
| 520 |
+
|
| 521 |
+
return (
|
| 522 |
+
status_message,
|
| 523 |
+
results_df,
|
| 524 |
+
)
|
| 525 |
+
|
| 526 |
except requests.exceptions.RequestException as e:
|
| 527 |
+
status_message = (
|
| 528 |
+
"Submission Failed: "
|
| 529 |
+
f"Network error - {e}"
|
| 530 |
+
)
|
| 531 |
+
|
| 532 |
print(status_message)
|
| 533 |
+
|
| 534 |
+
results_df = pd.DataFrame(
|
| 535 |
+
results_log
|
| 536 |
+
)
|
| 537 |
+
|
| 538 |
+
return (
|
| 539 |
+
status_message,
|
| 540 |
+
results_df,
|
| 541 |
+
)
|
| 542 |
+
|
| 543 |
except Exception as e:
|
| 544 |
+
status_message = (
|
| 545 |
+
"An unexpected error occurred "
|
| 546 |
+
f"during submission: {e}"
|
| 547 |
+
)
|
| 548 |
+
|
| 549 |
print(status_message)
|
|
|
|
|
|
|
| 550 |
|
| 551 |
+
results_df = pd.DataFrame(
|
| 552 |
+
results_log
|
| 553 |
+
)
|
| 554 |
+
|
| 555 |
+
return (
|
| 556 |
+
status_message,
|
| 557 |
+
results_df,
|
| 558 |
+
)
|
| 559 |
+
|
| 560 |
+
|
| 561 |
+
# ---------------------------------------------------------
|
| 562 |
+
# Gradio Interface
|
| 563 |
+
# ---------------------------------------------------------
|
| 564 |
|
|
|
|
| 565 |
with gr.Blocks() as demo:
|
| 566 |
+
gr.Markdown(
|
| 567 |
+
"# GAIA Agent Evaluation Runner"
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
gr.Markdown(
|
| 571 |
"""
|
| 572 |
**Instructions:**
|
| 573 |
|
| 574 |
+
1. Log in to your Hugging Face account using the button below.
|
| 575 |
+
2. Click **Run Evaluation & Submit All Answers**.
|
| 576 |
+
3. The agent will fetch the evaluation questions, research and
|
| 577 |
+
answer them, and submit the results to the course API.
|
| 578 |
|
| 579 |
---
|
| 580 |
+
|
| 581 |
+
**Important:**
|
| 582 |
+
|
| 583 |
+
The evaluation uses exact-match scoring, so the agent is
|
| 584 |
+
configured to return concise final answers without explanations.
|
| 585 |
"""
|
| 586 |
)
|
| 587 |
|
| 588 |
gr.LoginButton()
|
| 589 |
|
| 590 |
+
run_button = gr.Button(
|
| 591 |
+
"Run Evaluation & Submit All Answers"
|
| 592 |
+
)
|
| 593 |
|
| 594 |
+
status_output = gr.Textbox(
|
| 595 |
+
label="Run Status / Submission Result",
|
| 596 |
+
lines=5,
|
| 597 |
+
interactive=False,
|
| 598 |
+
)
|
| 599 |
+
|
| 600 |
+
results_table = gr.DataFrame(
|
| 601 |
+
label="Questions and Agent Answers",
|
| 602 |
+
wrap=True,
|
| 603 |
+
)
|
| 604 |
|
| 605 |
run_button.click(
|
| 606 |
fn=run_and_submit_all,
|
| 607 |
+
outputs=[
|
| 608 |
+
status_output,
|
| 609 |
+
results_table,
|
| 610 |
+
],
|
| 611 |
)
|
| 612 |
|
| 613 |
+
|
| 614 |
+
# ---------------------------------------------------------
|
| 615 |
+
# Startup
|
| 616 |
+
# ---------------------------------------------------------
|
| 617 |
+
|
| 618 |
if __name__ == "__main__":
|
| 619 |
+
print(
|
| 620 |
+
"\n"
|
| 621 |
+
+ "-" * 30
|
| 622 |
+
+ " App Starting "
|
| 623 |
+
+ "-" * 30
|
| 624 |
+
)
|
| 625 |
+
|
| 626 |
+
# Check Space runtime information.
|
| 627 |
+
space_host_startup = os.getenv(
|
| 628 |
+
"SPACE_HOST"
|
| 629 |
+
)
|
| 630 |
+
|
| 631 |
+
space_id_startup = os.getenv(
|
| 632 |
+
"SPACE_ID"
|
| 633 |
+
)
|
| 634 |
|
| 635 |
if space_host_startup:
|
| 636 |
+
print(
|
| 637 |
+
"SPACE_HOST found: "
|
| 638 |
+
f"{space_host_startup}"
|
| 639 |
+
)
|
| 640 |
+
|
| 641 |
+
print(
|
| 642 |
+
"Runtime URL should be: "
|
| 643 |
+
f"https://{space_host_startup}.hf.space"
|
| 644 |
+
)
|
| 645 |
+
|
| 646 |
else:
|
| 647 |
+
print(
|
| 648 |
+
"SPACE_HOST environment variable "
|
| 649 |
+
"not found (running locally?)."
|
| 650 |
+
)
|
| 651 |
+
|
| 652 |
+
if space_id_startup:
|
| 653 |
+
print(
|
| 654 |
+
"SPACE_ID found: "
|
| 655 |
+
f"{space_id_startup}"
|
| 656 |
+
)
|
| 657 |
+
|
| 658 |
+
print(
|
| 659 |
+
"Repo URL: "
|
| 660 |
+
"https://huggingface.co/spaces/"
|
| 661 |
+
f"{space_id_startup}"
|
| 662 |
+
)
|
| 663 |
+
|
| 664 |
+
print(
|
| 665 |
+
"Repo Tree URL: "
|
| 666 |
+
"https://huggingface.co/spaces/"
|
| 667 |
+
f"{space_id_startup}/tree/main"
|
| 668 |
+
)
|
| 669 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 670 |
else:
|
| 671 |
+
print(
|
| 672 |
+
"SPACE_ID environment variable "
|
| 673 |
+
"not found (running locally?). "
|
| 674 |
+
"Repo URL cannot be determined."
|
| 675 |
+
)
|
| 676 |
|
| 677 |
+
print(
|
| 678 |
+
"-" * (
|
| 679 |
+
60
|
| 680 |
+
+ len(" App Starting ")
|
| 681 |
+
)
|
| 682 |
+
+ "\n"
|
| 683 |
+
)
|
| 684 |
+
|
| 685 |
+
print(
|
| 686 |
+
"Launching Gradio Interface "
|
| 687 |
+
"for GAIA Agent Evaluation..."
|
| 688 |
+
)
|
| 689 |
|
| 690 |
+
demo.launch(
|
| 691 |
+
debug=True,
|
| 692 |
+
share=False,
|
| 693 |
+
)
|