Spaces:
Sleeping
Sleeping
restored default model
Browse files
app.py
CHANGED
|
@@ -14,15 +14,16 @@ from smolagents import (
|
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
-
# (Keep Constants as is)
|
| 18 |
# --- Constants ---
|
| 19 |
-
DEFAULT_API_URL =
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Format instructions appended to every question
|
| 22 |
# so that the agent returns exact-match-friendly
|
| 23 |
# answers via final_answer().
|
| 24 |
ANSWER_FORMAT_INSTRUCTIONS = """
|
| 25 |
-
|
| 26 |
IMPORTANT FORMAT INSTRUCTIONS:
|
| 27 |
Your final_answer must be as concise as possible:
|
| 28 |
- If the answer is a number, return ONLY the number
|
|
@@ -34,18 +35,15 @@ Your final_answer must be as concise as possible:
|
|
| 34 |
- If the answer is a comma separated list, apply
|
| 35 |
the rules above to each element.
|
| 36 |
Do NOT include explanations in your final_answer,
|
| 37 |
-
just the bare answer.
|
| 38 |
-
"""
|
| 39 |
|
| 40 |
|
| 41 |
-
# --- Basic Agent Definition ---
|
| 42 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 43 |
# --------------------------------------------------
|
| 44 |
# Custom tool: download a GAIA task file
|
| 45 |
# --------------------------------------------------
|
| 46 |
class GaiaFileFetcherTool(Tool):
|
| 47 |
"""Downloads the file attached to a GAIA task."""
|
| 48 |
-
|
| 49 |
name = "fetch_task_file"
|
| 50 |
description = (
|
| 51 |
"Downloads the file attached to a GAIA task "
|
|
@@ -62,24 +60,24 @@ class GaiaFileFetcherTool(Tool):
|
|
| 62 |
}
|
| 63 |
}
|
| 64 |
output_type = "string"
|
| 65 |
-
|
| 66 |
def __init__(self, api_url: str, **kwargs):
|
| 67 |
super().__init__(**kwargs)
|
| 68 |
self.api_url = api_url
|
| 69 |
-
|
| 70 |
def forward(self, task_id: str) -> str:
|
| 71 |
import requests as _req
|
| 72 |
import tempfile as _tmp
|
| 73 |
import mimetypes as _mt
|
| 74 |
-
|
| 75 |
url = f"{self.api_url}/files/{task_id}"
|
| 76 |
resp = _req.get(url, timeout=30)
|
| 77 |
resp.raise_for_status()
|
| 78 |
-
|
| 79 |
# Derive a sensible extension from headers
|
| 80 |
ct = resp.headers.get("Content-Type", "")
|
| 81 |
ext = _mt.guess_extension(ct.split(";")[0]) or ""
|
| 82 |
-
|
| 83 |
cd = resp.headers.get(
|
| 84 |
"Content-Disposition", ""
|
| 85 |
)
|
|
@@ -87,34 +85,46 @@ class GaiaFileFetcherTool(Tool):
|
|
| 87 |
if "filename=" in cd:
|
| 88 |
fname = cd.split("filename=")[-1]
|
| 89 |
fname = fname.strip('"').strip("'")
|
| 90 |
-
|
| 91 |
if not fname:
|
| 92 |
fname = f"{task_id}{ext}"
|
| 93 |
-
|
| 94 |
path = os.path.join(
|
| 95 |
_tmp.gettempdir(), fname
|
| 96 |
)
|
| 97 |
with open(path, "wb") as f:
|
| 98 |
f.write(resp.content)
|
| 99 |
return path
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
class BasicAgent:
|
| 102 |
def __init__(self):
|
| 103 |
print("BasicAgent initialized.")
|
|
|
|
| 104 |
model = InferenceClientModel(
|
| 105 |
-
model_id=
|
|
|
|
|
|
|
| 106 |
token=os.getenv("HF_TOKEN"),
|
|
|
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
|
| 109 |
self.file_tool = GaiaFileFetcherTool(
|
| 110 |
api_url=DEFAULT_API_URL,
|
| 111 |
)
|
| 112 |
-
|
| 113 |
self.agent = CodeAgent(
|
| 114 |
model=model,
|
| 115 |
tools=[
|
| 116 |
DuckDuckGoSearchTool(),
|
| 117 |
-
WikipediaSearchTool(
|
|
|
|
|
|
|
| 118 |
VisitWebpageTool(),
|
| 119 |
self.file_tool,
|
| 120 |
],
|
|
@@ -156,91 +166,163 @@ class BasicAgent:
|
|
| 156 |
return raw.strip()
|
| 157 |
|
| 158 |
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
"""
|
| 161 |
-
Fetches all questions, runs the
|
| 162 |
-
and displays
|
| 163 |
"""
|
| 164 |
-
|
| 165 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 166 |
|
| 167 |
if profile:
|
| 168 |
username = f"{profile.username}"
|
| 169 |
print(f"User logged in: {username}")
|
| 170 |
else:
|
| 171 |
print("User not logged in.")
|
| 172 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
api_url = DEFAULT_API_URL
|
| 175 |
questions_url = f"{api_url}/questions"
|
| 176 |
submit_url = f"{api_url}/submit"
|
| 177 |
|
| 178 |
-
# 1. Instantiate Agent
|
| 179 |
try:
|
| 180 |
agent = BasicAgent()
|
| 181 |
except Exception as e:
|
| 182 |
print(f"Error instantiating agent: {e}")
|
| 183 |
return f"Error initializing agent: {e}", None
|
| 184 |
-
|
| 185 |
-
agent_code =
|
|
|
|
|
|
|
|
|
|
| 186 |
print(agent_code)
|
| 187 |
|
| 188 |
# 2. Fetch Questions
|
| 189 |
-
print(
|
|
|
|
|
|
|
| 190 |
try:
|
| 191 |
-
response = requests.get(
|
|
|
|
|
|
|
| 192 |
response.raise_for_status()
|
| 193 |
questions_data = response.json()
|
| 194 |
if not questions_data:
|
| 195 |
print("Fetched questions list is empty.")
|
| 196 |
-
return
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
except requests.exceptions.RequestException as e:
|
| 199 |
print(f"Error fetching questions: {e}")
|
| 200 |
return f"Error fetching questions: {e}", None
|
| 201 |
except requests.exceptions.JSONDecodeError as e:
|
| 202 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 203 |
print(f"Response text: {response.text[:500]}")
|
| 204 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
except Exception as e:
|
| 206 |
-
print(
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
# 3. Run
|
| 210 |
results_log = []
|
| 211 |
answers_payload = []
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
| 214 |
task_id = item.get("task_id")
|
| 215 |
question_text = item.get("question")
|
| 216 |
if not task_id or question_text is None:
|
| 217 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 218 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
try:
|
| 220 |
-
submitted_answer = agent(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
answers_payload.append(
|
| 222 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
)
|
| 224 |
results_log.append(
|
| 225 |
{
|
| 226 |
"Task ID": task_id,
|
| 227 |
"Question": question_text,
|
| 228 |
-
"Submitted Answer":
|
|
|
|
|
|
|
| 229 |
}
|
| 230 |
)
|
| 231 |
except Exception as e:
|
| 232 |
-
print(
|
|
|
|
|
|
|
| 233 |
results_log.append(
|
| 234 |
{
|
| 235 |
"Task ID": task_id,
|
| 236 |
"Question": question_text,
|
| 237 |
-
"Submitted Answer":
|
|
|
|
|
|
|
| 238 |
}
|
| 239 |
)
|
| 240 |
|
| 241 |
if not answers_payload:
|
| 242 |
-
print(
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
# 4. Prepare Submission
|
| 246 |
submission_data = {
|
|
@@ -248,107 +330,147 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 248 |
"agent_code": agent_code,
|
| 249 |
"answers": answers_payload,
|
| 250 |
}
|
| 251 |
-
status_update =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
print(status_update)
|
| 253 |
|
| 254 |
# 5. Submit
|
| 255 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 256 |
try:
|
| 257 |
-
response = requests.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
response.raise_for_status()
|
| 259 |
result_data = response.json()
|
| 260 |
final_status = (
|
| 261 |
f"Submission Successful!\n"
|
| 262 |
f"User: {result_data.get('username')}\n"
|
| 263 |
-
f"Overall Score:
|
| 264 |
-
f"
|
| 265 |
-
f"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
)
|
| 267 |
print("Submission successful.")
|
| 268 |
results_df = pd.DataFrame(results_log)
|
| 269 |
return final_status, results_df
|
|
|
|
| 270 |
except requests.exceptions.HTTPError as e:
|
| 271 |
-
error_detail =
|
|
|
|
|
|
|
|
|
|
| 272 |
try:
|
| 273 |
error_json = e.response.json()
|
| 274 |
-
error_detail +=
|
|
|
|
|
|
|
|
|
|
| 275 |
except requests.exceptions.JSONDecodeError:
|
| 276 |
-
error_detail +=
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
print(status_message)
|
| 279 |
results_df = pd.DataFrame(results_log)
|
| 280 |
return status_message, results_df
|
|
|
|
| 281 |
except requests.exceptions.Timeout:
|
| 282 |
-
status_message =
|
|
|
|
|
|
|
| 283 |
print(status_message)
|
| 284 |
results_df = pd.DataFrame(results_log)
|
| 285 |
return status_message, results_df
|
|
|
|
| 286 |
except requests.exceptions.RequestException as e:
|
| 287 |
-
status_message =
|
|
|
|
|
|
|
| 288 |
print(status_message)
|
| 289 |
results_df = pd.DataFrame(results_log)
|
| 290 |
return status_message, results_df
|
|
|
|
| 291 |
except Exception as e:
|
| 292 |
-
status_message =
|
|
|
|
|
|
|
|
|
|
| 293 |
print(status_message)
|
| 294 |
results_df = pd.DataFrame(results_log)
|
| 295 |
return status_message, results_df
|
| 296 |
|
| 297 |
|
| 298 |
-
# ---
|
|
|
|
|
|
|
| 299 |
with gr.Blocks() as demo:
|
| 300 |
-
gr.Markdown("#
|
| 301 |
gr.Markdown(
|
| 302 |
"""
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 313 |
"""
|
| 314 |
)
|
| 315 |
|
| 316 |
gr.LoginButton()
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
status_output = gr.Textbox(
|
| 321 |
-
label="Run Status / Submission Result",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
)
|
| 323 |
-
# Removed max_rows=10 from DataFrame constructor
|
| 324 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 325 |
|
| 326 |
-
run_button.click(
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
if __name__ == "__main__":
|
| 329 |
-
print(
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 337 |
-
else:
|
| 338 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 339 |
|
| 340 |
-
if
|
| 341 |
-
print(f"✅
|
| 342 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 343 |
-
print(
|
| 344 |
-
f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
|
| 345 |
-
)
|
| 346 |
else:
|
| 347 |
-
print(
|
| 348 |
-
"ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
|
| 349 |
-
)
|
| 350 |
|
| 351 |
-
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
-
print("
|
| 354 |
-
|
|
|
|
|
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
|
|
|
|
| 17 |
# --- Constants ---
|
| 18 |
+
DEFAULT_API_URL = (
|
| 19 |
+
"https://agents-course-unit4-scoring.hf.space"
|
| 20 |
+
)
|
| 21 |
|
| 22 |
# Format instructions appended to every question
|
| 23 |
# so that the agent returns exact-match-friendly
|
| 24 |
# answers via final_answer().
|
| 25 |
ANSWER_FORMAT_INSTRUCTIONS = """
|
| 26 |
+
|
| 27 |
IMPORTANT FORMAT INSTRUCTIONS:
|
| 28 |
Your final_answer must be as concise as possible:
|
| 29 |
- If the answer is a number, return ONLY the number
|
|
|
|
| 35 |
- If the answer is a comma separated list, apply
|
| 36 |
the rules above to each element.
|
| 37 |
Do NOT include explanations in your final_answer,
|
| 38 |
+
just the bare answer."""
|
|
|
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
| 41 |
# --------------------------------------------------
|
| 42 |
# Custom tool: download a GAIA task file
|
| 43 |
# --------------------------------------------------
|
| 44 |
class GaiaFileFetcherTool(Tool):
|
| 45 |
"""Downloads the file attached to a GAIA task."""
|
| 46 |
+
|
| 47 |
name = "fetch_task_file"
|
| 48 |
description = (
|
| 49 |
"Downloads the file attached to a GAIA task "
|
|
|
|
| 60 |
}
|
| 61 |
}
|
| 62 |
output_type = "string"
|
| 63 |
+
|
| 64 |
def __init__(self, api_url: str, **kwargs):
|
| 65 |
super().__init__(**kwargs)
|
| 66 |
self.api_url = api_url
|
| 67 |
+
|
| 68 |
def forward(self, task_id: str) -> str:
|
| 69 |
import requests as _req
|
| 70 |
import tempfile as _tmp
|
| 71 |
import mimetypes as _mt
|
| 72 |
+
|
| 73 |
url = f"{self.api_url}/files/{task_id}"
|
| 74 |
resp = _req.get(url, timeout=30)
|
| 75 |
resp.raise_for_status()
|
| 76 |
+
|
| 77 |
# Derive a sensible extension from headers
|
| 78 |
ct = resp.headers.get("Content-Type", "")
|
| 79 |
ext = _mt.guess_extension(ct.split(";")[0]) or ""
|
| 80 |
+
|
| 81 |
cd = resp.headers.get(
|
| 82 |
"Content-Disposition", ""
|
| 83 |
)
|
|
|
|
| 85 |
if "filename=" in cd:
|
| 86 |
fname = cd.split("filename=")[-1]
|
| 87 |
fname = fname.strip('"').strip("'")
|
| 88 |
+
|
| 89 |
if not fname:
|
| 90 |
fname = f"{task_id}{ext}"
|
| 91 |
+
|
| 92 |
path = os.path.join(
|
| 93 |
_tmp.gettempdir(), fname
|
| 94 |
)
|
| 95 |
with open(path, "wb") as f:
|
| 96 |
f.write(resp.content)
|
| 97 |
return path
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# --------------------------------------------------
|
| 101 |
+
# Agent wrapper
|
| 102 |
+
# --------------------------------------------------
|
| 103 |
class BasicAgent:
|
| 104 |
def __init__(self):
|
| 105 |
print("BasicAgent initialized.")
|
| 106 |
+
|
| 107 |
model = InferenceClientModel(
|
| 108 |
+
model_id=(
|
| 109 |
+
"Qwen/Qwen2.5-72B-Instruct"
|
| 110 |
+
),
|
| 111 |
token=os.getenv("HF_TOKEN"),
|
| 112 |
+
# If you have HF PRO, try a faster
|
| 113 |
+
# provider like "novita" or "hyperbolic"
|
| 114 |
+
# provider="novita",
|
| 115 |
)
|
| 116 |
|
| 117 |
self.file_tool = GaiaFileFetcherTool(
|
| 118 |
api_url=DEFAULT_API_URL,
|
| 119 |
)
|
| 120 |
+
|
| 121 |
self.agent = CodeAgent(
|
| 122 |
model=model,
|
| 123 |
tools=[
|
| 124 |
DuckDuckGoSearchTool(),
|
| 125 |
+
WikipediaSearchTool(
|
| 126 |
+
user_agent="GaiaAgent/1.0"
|
| 127 |
+
),
|
| 128 |
VisitWebpageTool(),
|
| 129 |
self.file_tool,
|
| 130 |
],
|
|
|
|
| 166 |
return raw.strip()
|
| 167 |
|
| 168 |
|
| 169 |
+
# --------------------------------------------------
|
| 170 |
+
# Gradio: run all & submit
|
| 171 |
+
# --------------------------------------------------
|
| 172 |
+
def run_and_submit_all(
|
| 173 |
+
profile: gr.OAuthProfile | None,
|
| 174 |
+
):
|
| 175 |
"""
|
| 176 |
+
Fetches all questions, runs the agent,
|
| 177 |
+
submits answers, and displays results.
|
| 178 |
"""
|
| 179 |
+
space_id = os.getenv("SPACE_ID")
|
|
|
|
| 180 |
|
| 181 |
if profile:
|
| 182 |
username = f"{profile.username}"
|
| 183 |
print(f"User logged in: {username}")
|
| 184 |
else:
|
| 185 |
print("User not logged in.")
|
| 186 |
+
return (
|
| 187 |
+
"Please Login to Hugging Face "
|
| 188 |
+
"with the button.",
|
| 189 |
+
None,
|
| 190 |
+
)
|
| 191 |
|
| 192 |
api_url = DEFAULT_API_URL
|
| 193 |
questions_url = f"{api_url}/questions"
|
| 194 |
submit_url = f"{api_url}/submit"
|
| 195 |
|
| 196 |
+
# 1. Instantiate Agent
|
| 197 |
try:
|
| 198 |
agent = BasicAgent()
|
| 199 |
except Exception as e:
|
| 200 |
print(f"Error instantiating agent: {e}")
|
| 201 |
return f"Error initializing agent: {e}", None
|
| 202 |
+
|
| 203 |
+
agent_code = (
|
| 204 |
+
f"https://huggingface.co/spaces/"
|
| 205 |
+
f"{space_id}/tree/main"
|
| 206 |
+
)
|
| 207 |
print(agent_code)
|
| 208 |
|
| 209 |
# 2. Fetch Questions
|
| 210 |
+
print(
|
| 211 |
+
f"Fetching questions from: {questions_url}"
|
| 212 |
+
)
|
| 213 |
try:
|
| 214 |
+
response = requests.get(
|
| 215 |
+
questions_url, timeout=15
|
| 216 |
+
)
|
| 217 |
response.raise_for_status()
|
| 218 |
questions_data = response.json()
|
| 219 |
if not questions_data:
|
| 220 |
print("Fetched questions list is empty.")
|
| 221 |
+
return (
|
| 222 |
+
"Fetched questions list is empty "
|
| 223 |
+
"or invalid format.",
|
| 224 |
+
None,
|
| 225 |
+
)
|
| 226 |
+
print(
|
| 227 |
+
f"Fetched {len(questions_data)} "
|
| 228 |
+
f"questions."
|
| 229 |
+
)
|
| 230 |
except requests.exceptions.RequestException as e:
|
| 231 |
print(f"Error fetching questions: {e}")
|
| 232 |
return f"Error fetching questions: {e}", None
|
| 233 |
except requests.exceptions.JSONDecodeError as e:
|
| 234 |
+
print(
|
| 235 |
+
"Error decoding JSON from questions "
|
| 236 |
+
f"endpoint: {e}"
|
| 237 |
+
)
|
| 238 |
print(f"Response text: {response.text[:500]}")
|
| 239 |
+
return (
|
| 240 |
+
"Error decoding server response "
|
| 241 |
+
f"for questions: {e}",
|
| 242 |
+
None,
|
| 243 |
+
)
|
| 244 |
except Exception as e:
|
| 245 |
+
print(
|
| 246 |
+
"Unexpected error fetching "
|
| 247 |
+
f"questions: {e}"
|
| 248 |
+
)
|
| 249 |
+
return (
|
| 250 |
+
"Unexpected error fetching "
|
| 251 |
+
f"questions: {e}",
|
| 252 |
+
None,
|
| 253 |
+
)
|
| 254 |
|
| 255 |
+
# 3. Run Agent on each question
|
| 256 |
results_log = []
|
| 257 |
answers_payload = []
|
| 258 |
+
total = len(questions_data)
|
| 259 |
+
print(f"Running agent on {total} questions...")
|
| 260 |
+
|
| 261 |
+
for i, item in enumerate(questions_data):
|
| 262 |
task_id = item.get("task_id")
|
| 263 |
question_text = item.get("question")
|
| 264 |
if not task_id or question_text is None:
|
| 265 |
+
print(
|
| 266 |
+
"Skipping item with missing "
|
| 267 |
+
f"task_id or question: {item}"
|
| 268 |
+
)
|
| 269 |
continue
|
| 270 |
+
|
| 271 |
+
# Check if the question has a file
|
| 272 |
+
file_name = item.get("file_name", "")
|
| 273 |
+
has_file = bool(file_name)
|
| 274 |
+
|
| 275 |
+
print(
|
| 276 |
+
f"[{i+1}/{total}] Task {task_id}"
|
| 277 |
+
f"{' (has file)' if has_file else ''}"
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
try:
|
| 281 |
+
submitted_answer = agent(
|
| 282 |
+
question_text,
|
| 283 |
+
task_id,
|
| 284 |
+
has_file,
|
| 285 |
+
)
|
| 286 |
answers_payload.append(
|
| 287 |
+
{
|
| 288 |
+
"task_id": task_id,
|
| 289 |
+
"submitted_answer": (
|
| 290 |
+
submitted_answer
|
| 291 |
+
),
|
| 292 |
+
}
|
| 293 |
)
|
| 294 |
results_log.append(
|
| 295 |
{
|
| 296 |
"Task ID": task_id,
|
| 297 |
"Question": question_text,
|
| 298 |
+
"Submitted Answer": (
|
| 299 |
+
submitted_answer
|
| 300 |
+
),
|
| 301 |
}
|
| 302 |
)
|
| 303 |
except Exception as e:
|
| 304 |
+
print(
|
| 305 |
+
f"Error on task {task_id}: {e}"
|
| 306 |
+
)
|
| 307 |
results_log.append(
|
| 308 |
{
|
| 309 |
"Task ID": task_id,
|
| 310 |
"Question": question_text,
|
| 311 |
+
"Submitted Answer": (
|
| 312 |
+
f"AGENT ERROR: {e}"
|
| 313 |
+
),
|
| 314 |
}
|
| 315 |
)
|
| 316 |
|
| 317 |
if not answers_payload:
|
| 318 |
+
print(
|
| 319 |
+
"Agent did not produce any answers."
|
| 320 |
+
)
|
| 321 |
+
return (
|
| 322 |
+
"Agent did not produce any answers "
|
| 323 |
+
"to submit.",
|
| 324 |
+
pd.DataFrame(results_log),
|
| 325 |
+
)
|
| 326 |
|
| 327 |
# 4. Prepare Submission
|
| 328 |
submission_data = {
|
|
|
|
| 330 |
"agent_code": agent_code,
|
| 331 |
"answers": answers_payload,
|
| 332 |
}
|
| 333 |
+
status_update = (
|
| 334 |
+
f"Agent finished. Submitting "
|
| 335 |
+
f"{len(answers_payload)} answers for "
|
| 336 |
+
f"user '{username}'..."
|
| 337 |
+
)
|
| 338 |
print(status_update)
|
| 339 |
|
| 340 |
# 5. Submit
|
| 341 |
+
print(
|
| 342 |
+
f"Submitting {len(answers_payload)} "
|
| 343 |
+
f"answers to: {submit_url}"
|
| 344 |
+
)
|
| 345 |
try:
|
| 346 |
+
response = requests.post(
|
| 347 |
+
submit_url,
|
| 348 |
+
json=submission_data,
|
| 349 |
+
timeout=60,
|
| 350 |
+
)
|
| 351 |
response.raise_for_status()
|
| 352 |
result_data = response.json()
|
| 353 |
final_status = (
|
| 354 |
f"Submission Successful!\n"
|
| 355 |
f"User: {result_data.get('username')}\n"
|
| 356 |
+
f"Overall Score: "
|
| 357 |
+
f"{result_data.get('score', 'N/A')}% "
|
| 358 |
+
f"({result_data.get('correct_count', '?')}"
|
| 359 |
+
f"/{result_data.get('total_attempted', '?')}"
|
| 360 |
+
f" correct)\n"
|
| 361 |
+
f"Message: "
|
| 362 |
+
f"{result_data.get('message', 'N/A')}"
|
| 363 |
)
|
| 364 |
print("Submission successful.")
|
| 365 |
results_df = pd.DataFrame(results_log)
|
| 366 |
return final_status, results_df
|
| 367 |
+
|
| 368 |
except requests.exceptions.HTTPError as e:
|
| 369 |
+
error_detail = (
|
| 370 |
+
"Server responded with status "
|
| 371 |
+
f"{e.response.status_code}."
|
| 372 |
+
)
|
| 373 |
try:
|
| 374 |
error_json = e.response.json()
|
| 375 |
+
error_detail += (
|
| 376 |
+
" Detail: "
|
| 377 |
+
f"{error_json.get('detail', e.response.text)}"
|
| 378 |
+
)
|
| 379 |
except requests.exceptions.JSONDecodeError:
|
| 380 |
+
error_detail += (
|
| 381 |
+
f" Response: "
|
| 382 |
+
f"{e.response.text[:500]}"
|
| 383 |
+
)
|
| 384 |
+
status_message = (
|
| 385 |
+
f"Submission Failed: {error_detail}"
|
| 386 |
+
)
|
| 387 |
print(status_message)
|
| 388 |
results_df = pd.DataFrame(results_log)
|
| 389 |
return status_message, results_df
|
| 390 |
+
|
| 391 |
except requests.exceptions.Timeout:
|
| 392 |
+
status_message = (
|
| 393 |
+
"Submission Failed: Request timed out."
|
| 394 |
+
)
|
| 395 |
print(status_message)
|
| 396 |
results_df = pd.DataFrame(results_log)
|
| 397 |
return status_message, results_df
|
| 398 |
+
|
| 399 |
except requests.exceptions.RequestException as e:
|
| 400 |
+
status_message = (
|
| 401 |
+
f"Submission Failed: Network error - {e}"
|
| 402 |
+
)
|
| 403 |
print(status_message)
|
| 404 |
results_df = pd.DataFrame(results_log)
|
| 405 |
return status_message, results_df
|
| 406 |
+
|
| 407 |
except Exception as e:
|
| 408 |
+
status_message = (
|
| 409 |
+
"Unexpected error during "
|
| 410 |
+
f"submission: {e}"
|
| 411 |
+
)
|
| 412 |
print(status_message)
|
| 413 |
results_df = pd.DataFrame(results_log)
|
| 414 |
return status_message, results_df
|
| 415 |
|
| 416 |
|
| 417 |
+
# --------------------------------------------------
|
| 418 |
+
# Gradio UI
|
| 419 |
+
# --------------------------------------------------
|
| 420 |
with gr.Blocks() as demo:
|
| 421 |
+
gr.Markdown("# GAIA Agent Evaluation Runner")
|
| 422 |
gr.Markdown(
|
| 423 |
"""
|
| 424 |
+
**Instructions:**
|
| 425 |
+
1. Clone this space and customise the agent.
|
| 426 |
+
2. Log in with the button below.
|
| 427 |
+
3. Click **Run Evaluation & Submit All Answers**.
|
| 428 |
+
|
| 429 |
+
---
|
| 430 |
+
*Processing all 20 questions will take several
|
| 431 |
+
minutes. The agent uses web search, Wikipedia,
|
| 432 |
+
page fetching, and file download tools.*
|
|
|
|
| 433 |
"""
|
| 434 |
)
|
| 435 |
|
| 436 |
gr.LoginButton()
|
| 437 |
+
run_button = gr.Button(
|
| 438 |
+
"Run Evaluation & Submit All Answers"
|
| 439 |
+
)
|
| 440 |
status_output = gr.Textbox(
|
| 441 |
+
label="Run Status / Submission Result",
|
| 442 |
+
lines=5,
|
| 443 |
+
interactive=False,
|
| 444 |
+
)
|
| 445 |
+
results_table = gr.DataFrame(
|
| 446 |
+
label="Questions and Agent Answers",
|
| 447 |
+
wrap=True,
|
| 448 |
)
|
|
|
|
|
|
|
| 449 |
|
| 450 |
+
run_button.click(
|
| 451 |
+
fn=run_and_submit_all,
|
| 452 |
+
outputs=[status_output, results_table],
|
| 453 |
+
)
|
| 454 |
|
| 455 |
if __name__ == "__main__":
|
| 456 |
+
print(
|
| 457 |
+
"\n" + "-" * 30
|
| 458 |
+
+ " App Starting "
|
| 459 |
+
+ "-" * 30
|
| 460 |
+
)
|
| 461 |
+
space_host = os.getenv("SPACE_HOST")
|
| 462 |
+
space_id = os.getenv("SPACE_ID")
|
|
|
|
|
|
|
|
|
|
| 463 |
|
| 464 |
+
if space_host:
|
| 465 |
+
print(f"✅ SPACE_HOST: {space_host}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
else:
|
| 467 |
+
print("ℹ️ SPACE_HOST not found.")
|
|
|
|
|
|
|
| 468 |
|
| 469 |
+
if space_id:
|
| 470 |
+
print(f"✅ SPACE_ID: {space_id}")
|
| 471 |
+
else:
|
| 472 |
+
print("ℹ️ SPACE_ID not found.")
|
| 473 |
|
| 474 |
+
print("-" * 74 + "\n")
|
| 475 |
+
print("Launching Gradio Interface...")
|
| 476 |
+
demo.launch(debug=True, share=False)
|