Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import io
|
|
|
|
| 4 |
import math
|
|
|
|
| 5 |
import traceback
|
| 6 |
from pathlib import Path
|
| 7 |
from typing import Any, Dict, List, Optional, Tuple
|
|
@@ -11,7 +13,7 @@ import requests
|
|
| 11 |
import pandas as pd
|
| 12 |
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
-
DEBUG_ATTACH = True #
|
| 15 |
|
| 16 |
|
| 17 |
# -----------------------------
|
|
@@ -22,10 +24,7 @@ def _http_get(url: str, timeout: int = 30, stream: bool = False) -> requests.Res
|
|
| 22 |
url,
|
| 23 |
timeout=timeout,
|
| 24 |
stream=stream,
|
| 25 |
-
headers={
|
| 26 |
-
"User-Agent": "Mozilla/5.0 (HF Space agent)",
|
| 27 |
-
"Accept": "*/*",
|
| 28 |
-
},
|
| 29 |
)
|
| 30 |
|
| 31 |
|
|
@@ -58,12 +57,11 @@ def sanitize_answer(ans: str) -> str:
|
|
| 58 |
return ""
|
| 59 |
t = str(ans).strip()
|
| 60 |
t = re.sub(r"(?i)\bFINAL ANSWER\b\s*[:\-]*\s*", "", t).strip()
|
| 61 |
-
|
| 62 |
-
return t
|
| 63 |
|
| 64 |
|
| 65 |
# -----------------------------
|
| 66 |
-
#
|
| 67 |
# -----------------------------
|
| 68 |
def _collect_strings(x: Any) -> List[str]:
|
| 69 |
out: List[str] = []
|
|
@@ -114,21 +112,21 @@ def _normalize_to_full_url(s: str, api_url: str) -> Optional[str]:
|
|
| 114 |
return s
|
| 115 |
if s.startswith("/"):
|
| 116 |
return api_url.rstrip("/") + s
|
| 117 |
-
|
|
|
|
| 118 |
return api_url.rstrip("/") + "/" + s
|
| 119 |
return None
|
| 120 |
|
| 121 |
|
| 122 |
def extract_file_urls_from_item(item: Dict[str, Any], api_url: str) -> List[str]:
|
| 123 |
-
all_strings = _collect_strings(item)
|
| 124 |
urls: List[str] = []
|
| 125 |
-
for s in
|
| 126 |
u = _normalize_to_full_url(s, api_url)
|
| 127 |
if u:
|
| 128 |
urls.append(u)
|
| 129 |
-
|
| 130 |
seen = set()
|
| 131 |
-
out
|
| 132 |
for u in urls:
|
| 133 |
if u not in seen:
|
| 134 |
out.append(u)
|
|
@@ -137,9 +135,11 @@ def extract_file_urls_from_item(item: Dict[str, Any], api_url: str) -> List[str]
|
|
| 137 |
|
| 138 |
|
| 139 |
def extract_filenames_from_question(q: str) -> List[str]:
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
out = []
|
| 144 |
for n in names:
|
| 145 |
n = n.strip().strip('"').strip("'")
|
|
@@ -150,12 +150,13 @@ def extract_filenames_from_question(q: str) -> List[str]:
|
|
| 150 |
res = []
|
| 151 |
for x in out:
|
| 152 |
if x not in seen:
|
| 153 |
-
res.append(x)
|
|
|
|
| 154 |
return res
|
| 155 |
|
| 156 |
|
| 157 |
# -----------------------------
|
| 158 |
-
# Download
|
| 159 |
# -----------------------------
|
| 160 |
def _save_stream_to_tmp(resp: requests.Response, file_tag: str) -> Optional[Path]:
|
| 161 |
try:
|
|
@@ -197,68 +198,154 @@ def _try_download_urls(urls: List[str], tag: str) -> Tuple[Optional[Path], List[
|
|
| 197 |
return p, debug_lines
|
| 198 |
except Exception as e:
|
| 199 |
debug_lines.append(f"ERR {url} :: {type(e).__name__}: {e}")
|
| 200 |
-
continue
|
| 201 |
return None, debug_lines
|
| 202 |
|
| 203 |
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
| 205 |
"""
|
| 206 |
-
|
| 207 |
"""
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
f"{api_url}/
|
| 212 |
-
f"{api_url}/
|
| 213 |
-
f"{api_url}/files/{fid}?download=1",
|
| 214 |
-
f"{api_url}/file/{fid}",
|
| 215 |
-
f"{api_url}/file/{fid}/download",
|
| 216 |
-
f"{api_url}/download/{fid}",
|
| 217 |
-
f"{api_url}/download?file_id={fid}",
|
| 218 |
-
f"{api_url}/get_file/{fid}",
|
| 219 |
-
f"{api_url}/asset/{fid}",
|
| 220 |
-
f"{api_url}/assets/{fid}",
|
| 221 |
-
f"{api_url}/static/{fid}",
|
| 222 |
-
# ✅ more attachment-ish patterns
|
| 223 |
-
f"{api_url}/attachments/{fid}",
|
| 224 |
-
f"{api_url}/attachments/{fid}/download",
|
| 225 |
-
f"{api_url}/attachment/{fid}",
|
| 226 |
-
f"{api_url}/attachment/{fid}/download",
|
| 227 |
-
f"{api_url}/media/{fid}",
|
| 228 |
-
f"{api_url}/media/{fid}/download",
|
| 229 |
-
f"{api_url}/raw/{fid}",
|
| 230 |
-
f"{api_url}/raw/{fid}/download",
|
| 231 |
-
# query styles
|
| 232 |
-
f"{api_url}/files?file_id={fid}",
|
| 233 |
-
f"{api_url}/file?file_id={fid}",
|
| 234 |
-
f"{api_url}/attachments?file_id={fid}",
|
| 235 |
-
f"{api_url}/media?file_id={fid}",
|
| 236 |
]
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
|
| 240 |
-
def
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
|
| 260 |
# -----------------------------
|
| 261 |
-
#
|
| 262 |
# -----------------------------
|
| 263 |
def solve_reversed_sentence(q: str) -> Optional[str]:
|
| 264 |
if "rewsna eht sa" in q and '"tfel"' in q:
|
|
@@ -291,6 +378,9 @@ def solve_polish_actor(q: str) -> Optional[str]:
|
|
| 291 |
return None
|
| 292 |
|
| 293 |
|
|
|
|
|
|
|
|
|
|
| 294 |
def solve_excel_food_sales(file_path: Path) -> Optional[str]:
|
| 295 |
try:
|
| 296 |
xl = pd.read_excel(file_path, sheet_name=None)
|
|
@@ -362,23 +452,10 @@ def solve_python_final_numeric(file_path: Path) -> Optional[str]:
|
|
| 362 |
return None
|
| 363 |
|
| 364 |
safe_builtins = {
|
| 365 |
-
"print": print,
|
| 366 |
-
"
|
| 367 |
-
"
|
| 368 |
-
"
|
| 369 |
-
"min": min,
|
| 370 |
-
"max": max,
|
| 371 |
-
"abs": abs,
|
| 372 |
-
"round": round,
|
| 373 |
-
"enumerate": enumerate,
|
| 374 |
-
"zip": zip,
|
| 375 |
-
"list": list,
|
| 376 |
-
"dict": dict,
|
| 377 |
-
"set": set,
|
| 378 |
-
"tuple": tuple,
|
| 379 |
-
"float": float,
|
| 380 |
-
"int": int,
|
| 381 |
-
"str": str,
|
| 382 |
}
|
| 383 |
safe_globals = {"__builtins__": safe_builtins, "math": math}
|
| 384 |
|
|
@@ -395,9 +472,7 @@ def solve_python_final_numeric(file_path: Path) -> Optional[str]:
|
|
| 395 |
return None
|
| 396 |
|
| 397 |
nums = re.findall(r"[-+]?\d+(?:\.\d+)?", out)
|
| 398 |
-
|
| 399 |
-
return None
|
| 400 |
-
return nums[-1]
|
| 401 |
except Exception:
|
| 402 |
return None
|
| 403 |
|
|
@@ -407,87 +482,150 @@ def solve_python_final_numeric(file_path: Path) -> Optional[str]:
|
|
| 407 |
# -----------------------------
|
| 408 |
class BasicAgent:
|
| 409 |
def __init__(self, api_url: str):
|
| 410 |
-
self.api_url = api_url
|
| 411 |
-
|
|
|
|
| 412 |
|
| 413 |
def __call__(self, question: str, item: Dict[str, Any]) -> Tuple[str, str]:
|
| 414 |
q = (question or "").strip()
|
| 415 |
ql = q.lower()
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
# deterministic
|
| 419 |
-
for fn in [
|
| 420 |
-
solve_reversed_sentence,
|
| 421 |
-
solve_non_commutative_subset,
|
| 422 |
-
solve_botany_vegetables,
|
| 423 |
-
solve_mercedes_sosa,
|
| 424 |
-
solve_polish_actor,
|
| 425 |
-
]:
|
| 426 |
try:
|
| 427 |
ans = fn(q)
|
| 428 |
if ans:
|
| 429 |
-
return sanitize_answer(ans),
|
| 430 |
except Exception:
|
| 431 |
pass
|
| 432 |
|
| 433 |
is_attachment_task = any(k in ql for k in ["attached excel", "attached python", "attached a file", "i've attached", ".mp3", ".xlsx", ".py"])
|
| 434 |
if not is_attachment_task:
|
| 435 |
-
return "",
|
| 436 |
|
| 437 |
-
#
|
| 438 |
filenames = extract_filenames_from_question(q)
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
|
|
|
| 447 |
|
| 448 |
-
#
|
| 449 |
urls = extract_file_urls_from_item(item, api_url=self.api_url)
|
| 450 |
for u in urls:
|
| 451 |
-
fp,
|
| 452 |
-
|
| 453 |
-
debug += "\n".join(dbg) + "\n"
|
| 454 |
if fp:
|
| 455 |
ans = self._solve_from_file(q, fp)
|
| 456 |
if ans:
|
| 457 |
-
return sanitize_answer(ans),
|
| 458 |
|
| 459 |
-
#
|
| 460 |
file_ids = extract_file_ids_from_item(item)
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
if fp:
|
| 466 |
ans = self._solve_from_file(q, fp)
|
| 467 |
if ans:
|
| 468 |
-
return sanitize_answer(ans),
|
| 469 |
-
|
| 470 |
-
# 3) fallback: task_id as file_id
|
| 471 |
-
task_id = item.get("task_id")
|
| 472 |
-
if isinstance(task_id, str) and task_id:
|
| 473 |
-
fp, dbg = download_scoring_file(task_id, api_url=self.api_url)
|
| 474 |
-
if DEBUG_ATTACH:
|
| 475 |
-
debug += "\n".join(dbg) + "\n"
|
| 476 |
-
if fp:
|
| 477 |
-
ans = self._solve_from_file(q, fp)
|
| 478 |
-
if ans:
|
| 479 |
-
return sanitize_answer(ans), debug
|
| 480 |
|
| 481 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
|
| 483 |
def _solve_from_file(self, q: str, fp: Path) -> Optional[str]:
|
| 484 |
suf = fp.suffix.lower()
|
| 485 |
ql = q.lower()
|
|
|
|
| 486 |
if ("attached excel file" in ql) or (suf in [".xlsx", ".xls"]):
|
| 487 |
return solve_excel_food_sales(fp)
|
|
|
|
| 488 |
if ("attached python code" in ql) or (suf in [".py", ".txt"]):
|
| 489 |
return solve_python_final_numeric(fp)
|
| 490 |
-
|
|
|
|
| 491 |
return None
|
| 492 |
|
| 493 |
|
|
@@ -508,11 +646,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None = None):
|
|
| 508 |
submit_url = f"{api_url}/submit"
|
| 509 |
|
| 510 |
agent = BasicAgent(api_url=api_url)
|
| 511 |
-
agent_code =
|
| 512 |
-
f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 513 |
-
if space_id
|
| 514 |
-
else "https://huggingface.co/spaces/UNKNOWN/tree/main"
|
| 515 |
-
)
|
| 516 |
|
| 517 |
r = requests.get(questions_url, timeout=45)
|
| 518 |
r.raise_for_status()
|
|
@@ -534,30 +668,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None = None):
|
|
| 534 |
|
| 535 |
if isinstance(submitted_answer, str) and submitted_answer.strip() == "":
|
| 536 |
skipped += 1
|
| 537 |
-
results_log.append({
|
| 538 |
-
"Task ID": task_id,
|
| 539 |
-
"Question": question_text,
|
| 540 |
-
"Submitted Answer": "SKIPPED",
|
| 541 |
-
"Debug": debug if DEBUG_ATTACH else ""
|
| 542 |
-
})
|
| 543 |
continue
|
| 544 |
|
| 545 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 546 |
-
results_log.append({
|
| 547 |
-
"Task ID": task_id,
|
| 548 |
-
"Question": question_text,
|
| 549 |
-
"Submitted Answer": submitted_answer,
|
| 550 |
-
"Debug": debug if DEBUG_ATTACH else ""
|
| 551 |
-
})
|
| 552 |
|
| 553 |
if not answers_payload:
|
| 554 |
-
return "⚠️ 全部 SKIPPED(
|
| 555 |
|
| 556 |
-
submission_data = {
|
| 557 |
-
"username": username.strip(),
|
| 558 |
-
"agent_code": agent_code,
|
| 559 |
-
"answers": answers_payload,
|
| 560 |
-
}
|
| 561 |
|
| 562 |
r2 = requests.post(submit_url, json=submission_data, timeout=180)
|
| 563 |
r2.raise_for_status()
|
|
@@ -584,20 +704,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None = None):
|
|
| 584 |
# -----------------------------
|
| 585 |
with gr.Blocks() as demo:
|
| 586 |
gr.Markdown("# Basic Agent Evaluation Runner (No Paid Model)")
|
| 587 |
-
gr.Markdown(
|
| 588 |
-
"""
|
| 589 |
-
1) Login
|
| 590 |
-
2) Click **Run Evaluation & Submit All Answers**
|
| 591 |
-
|
| 592 |
-
✅ This version adds **Debug** column for attachment tasks, showing tried URLs and status codes.
|
| 593 |
-
"""
|
| 594 |
-
)
|
| 595 |
-
|
| 596 |
gr.LoginButton()
|
| 597 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 598 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=14, interactive=False)
|
| 599 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 600 |
-
|
| 601 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 602 |
|
| 603 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import io
|
| 4 |
+
import json
|
| 5 |
import math
|
| 6 |
+
import base64
|
| 7 |
import traceback
|
| 8 |
from pathlib import Path
|
| 9 |
from typing import Any, Dict, List, Optional, Tuple
|
|
|
|
| 13 |
import pandas as pd
|
| 14 |
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
+
DEBUG_ATTACH = True # 想安靜就 False
|
| 17 |
|
| 18 |
|
| 19 |
# -----------------------------
|
|
|
|
| 24 |
url,
|
| 25 |
timeout=timeout,
|
| 26 |
stream=stream,
|
| 27 |
+
headers={"User-Agent": "Mozilla/5.0", "Accept": "*/*"},
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
|
|
|
|
| 57 |
return ""
|
| 58 |
t = str(ans).strip()
|
| 59 |
t = re.sub(r"(?i)\bFINAL ANSWER\b\s*[:\-]*\s*", "", t).strip()
|
| 60 |
+
return t.strip().strip('"').strip("'").strip()
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
# -----------------------------
|
| 64 |
+
# Utils: collect strings
|
| 65 |
# -----------------------------
|
| 66 |
def _collect_strings(x: Any) -> List[str]:
|
| 67 |
out: List[str] = []
|
|
|
|
| 112 |
return s
|
| 113 |
if s.startswith("/"):
|
| 114 |
return api_url.rstrip("/") + s
|
| 115 |
+
# relative-ish
|
| 116 |
+
if s.startswith(("files/", "file/", "static/", "assets/", "attachments/", "media/", "raw/", "api/")):
|
| 117 |
return api_url.rstrip("/") + "/" + s
|
| 118 |
return None
|
| 119 |
|
| 120 |
|
| 121 |
def extract_file_urls_from_item(item: Dict[str, Any], api_url: str) -> List[str]:
|
|
|
|
| 122 |
urls: List[str] = []
|
| 123 |
+
for s in _collect_strings(item):
|
| 124 |
u = _normalize_to_full_url(s, api_url)
|
| 125 |
if u:
|
| 126 |
urls.append(u)
|
| 127 |
+
# dedup
|
| 128 |
seen = set()
|
| 129 |
+
out = []
|
| 130 |
for u in urls:
|
| 131 |
if u not in seen:
|
| 132 |
out.append(u)
|
|
|
|
| 135 |
|
| 136 |
|
| 137 |
def extract_filenames_from_question(q: str) -> List[str]:
|
| 138 |
+
names = re.findall(
|
| 139 |
+
r"attached (?:a file called|the recipe as|as)\s+([A-Za-z0-9 _\-\.\(\)]+?\.(?:mp3|xlsx|xls|py|txt))",
|
| 140 |
+
q,
|
| 141 |
+
flags=re.I,
|
| 142 |
+
)
|
| 143 |
out = []
|
| 144 |
for n in names:
|
| 145 |
n = n.strip().strip('"').strip("'")
|
|
|
|
| 150 |
res = []
|
| 151 |
for x in out:
|
| 152 |
if x not in seen:
|
| 153 |
+
res.append(x)
|
| 154 |
+
seen.add(x)
|
| 155 |
return res
|
| 156 |
|
| 157 |
|
| 158 |
# -----------------------------
|
| 159 |
+
# Download & save helpers
|
| 160 |
# -----------------------------
|
| 161 |
def _save_stream_to_tmp(resp: requests.Response, file_tag: str) -> Optional[Path]:
|
| 162 |
try:
|
|
|
|
| 198 |
return p, debug_lines
|
| 199 |
except Exception as e:
|
| 200 |
debug_lines.append(f"ERR {url} :: {type(e).__name__}: {e}")
|
|
|
|
| 201 |
return None, debug_lines
|
| 202 |
|
| 203 |
|
| 204 |
+
# -----------------------------
|
| 205 |
+
# OpenAPI discovery
|
| 206 |
+
# -----------------------------
|
| 207 |
+
def discover_openapi_paths(api_url: str) -> List[str]:
|
| 208 |
"""
|
| 209 |
+
If openapi exists, extract candidate path templates that look like file download endpoints.
|
| 210 |
"""
|
| 211 |
+
probes = [
|
| 212 |
+
f"{api_url}/openapi.json",
|
| 213 |
+
f"{api_url}/openapi",
|
| 214 |
+
f"{api_url}/swagger.json",
|
| 215 |
+
f"{api_url}/api/openapi.json",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
]
|
| 217 |
+
candidates: List[str] = []
|
| 218 |
+
for p in probes:
|
| 219 |
+
try:
|
| 220 |
+
r = _http_get(p, timeout=20, stream=False)
|
| 221 |
+
if r.status_code != 200:
|
| 222 |
+
continue
|
| 223 |
+
data = r.json()
|
| 224 |
+
paths = data.get("paths", {}) if isinstance(data, dict) else {}
|
| 225 |
+
for path in paths.keys():
|
| 226 |
+
low = path.lower()
|
| 227 |
+
if any(k in low for k in ["file", "files", "attachment", "download", "asset", "media"]):
|
| 228 |
+
candidates.append(path)
|
| 229 |
+
except Exception:
|
| 230 |
+
continue
|
| 231 |
+
|
| 232 |
+
# dedup
|
| 233 |
+
seen = set()
|
| 234 |
+
out = []
|
| 235 |
+
for x in candidates:
|
| 236 |
+
if x not in seen:
|
| 237 |
+
out.append(x)
|
| 238 |
+
seen.add(x)
|
| 239 |
+
return out
|
| 240 |
|
| 241 |
|
| 242 |
+
def build_openapi_url_candidates(api_url: str, fid: str, openapi_paths: List[str]) -> List[str]:
|
| 243 |
+
urls = []
|
| 244 |
+
for path in openapi_paths:
|
| 245 |
+
# Replace common params
|
| 246 |
+
u = path
|
| 247 |
+
u = u.replace("{file_id}", fid).replace("{fileId}", fid).replace("{id}", fid).replace("{attachment_id}", fid)
|
| 248 |
+
if "{" in u and "}" in u:
|
| 249 |
+
# still has unknown template vars
|
| 250 |
+
continue
|
| 251 |
+
if not u.startswith("/"):
|
| 252 |
+
u = "/" + u
|
| 253 |
+
urls.append(api_url.rstrip("/") + u)
|
| 254 |
+
return urls
|
| 255 |
|
| 256 |
|
| 257 |
+
# -----------------------------
|
| 258 |
+
# Base64-in-item extraction (IMPORTANT)
|
| 259 |
+
# -----------------------------
|
| 260 |
+
_B64_KEYS = {
|
| 261 |
+
"data", "content", "blob", "bytes", "file_bytes", "filebytes", "b64", "base64",
|
| 262 |
+
"attachment", "file", "payload"
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
def looks_like_base64(s: str) -> bool:
|
| 266 |
+
if not isinstance(s, str):
|
| 267 |
+
return False
|
| 268 |
+
t = s.strip()
|
| 269 |
+
if len(t) < 200:
|
| 270 |
+
return False
|
| 271 |
+
# allow data:...;base64,....
|
| 272 |
+
if t.startswith("data:") and "base64," in t:
|
| 273 |
+
return True
|
| 274 |
+
# base64 charset check (loose)
|
| 275 |
+
if re.fullmatch(r"[A-Za-z0-9+/=\s]+", t) is None:
|
| 276 |
+
return False
|
| 277 |
+
# must have padding-ish or length multiple-ish (loose)
|
| 278 |
+
return True
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
def decode_base64_to_file(b64s: str, filename_hint: str) -> Optional[Path]:
|
| 282 |
+
try:
|
| 283 |
+
t = b64s.strip()
|
| 284 |
+
if t.startswith("data:") and "base64," in t:
|
| 285 |
+
t = t.split("base64,", 1)[1]
|
| 286 |
+
|
| 287 |
+
raw = base64.b64decode(t, validate=False)
|
| 288 |
+
if not raw or _looks_like_html(raw[:400]):
|
| 289 |
+
return None
|
| 290 |
+
|
| 291 |
+
out_dir = Path("/tmp/gaia_files")
|
| 292 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 293 |
+
|
| 294 |
+
# infer suffix by magic if missing
|
| 295 |
+
name = filename_hint or "attachment"
|
| 296 |
+
if "." not in name:
|
| 297 |
+
# quick magic guesses
|
| 298 |
+
if raw[:2] == b"PK":
|
| 299 |
+
name += ".xlsx"
|
| 300 |
+
elif raw[:3] == b"ID3" or raw[:2] == b"\xff\xfb":
|
| 301 |
+
name += ".mp3"
|
| 302 |
+
elif raw[:1] == b"#" or b"import" in raw[:200]:
|
| 303 |
+
name += ".py"
|
| 304 |
+
else:
|
| 305 |
+
name += ".bin"
|
| 306 |
+
|
| 307 |
+
path = out_dir / name
|
| 308 |
+
with open(path, "wb") as f:
|
| 309 |
+
f.write(raw)
|
| 310 |
+
return path
|
| 311 |
+
except Exception:
|
| 312 |
+
return None
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
def extract_base64_files_from_item(item: Any, filename_hint: str) -> Tuple[List[Path], List[str]]:
|
| 316 |
+
"""
|
| 317 |
+
Deep-scan dict/list for base64 strings under likely keys.
|
| 318 |
+
"""
|
| 319 |
+
found_paths: List[Path] = []
|
| 320 |
+
debug: List[str] = []
|
| 321 |
+
|
| 322 |
+
def walk(x: Any, key_hint: str = ""):
|
| 323 |
+
if isinstance(x, dict):
|
| 324 |
+
for k, v in x.items():
|
| 325 |
+
kh = f"{key_hint}.{k}" if key_hint else str(k)
|
| 326 |
+
# if key suggests file-like, attempt decode when value is b64-ish string
|
| 327 |
+
if isinstance(v, str) and (k.lower() in _B64_KEYS or "base64" in k.lower() or "b64" in k.lower()):
|
| 328 |
+
if looks_like_base64(v):
|
| 329 |
+
p = decode_base64_to_file(v, filename_hint)
|
| 330 |
+
if p:
|
| 331 |
+
found_paths.append(p)
|
| 332 |
+
debug.append(f"BASE64_OK at {kh} -> {p.name} ({p.stat().st_size} bytes)")
|
| 333 |
+
else:
|
| 334 |
+
debug.append(f"BASE64_FAIL at {kh}")
|
| 335 |
+
walk(v, kh)
|
| 336 |
+
elif isinstance(x, list):
|
| 337 |
+
for i, y in enumerate(x):
|
| 338 |
+
walk(y, f"{key_hint}[{i}]")
|
| 339 |
+
else:
|
| 340 |
+
# not traversable
|
| 341 |
+
return
|
| 342 |
+
|
| 343 |
+
walk(item)
|
| 344 |
+
return found_paths, debug
|
| 345 |
|
| 346 |
|
| 347 |
# -----------------------------
|
| 348 |
+
# Rule solvers (known correct)
|
| 349 |
# -----------------------------
|
| 350 |
def solve_reversed_sentence(q: str) -> Optional[str]:
|
| 351 |
if "rewsna eht sa" in q and '"tfel"' in q:
|
|
|
|
| 378 |
return None
|
| 379 |
|
| 380 |
|
| 381 |
+
# -----------------------------
|
| 382 |
+
# Attachment solvers
|
| 383 |
+
# -----------------------------
|
| 384 |
def solve_excel_food_sales(file_path: Path) -> Optional[str]:
|
| 385 |
try:
|
| 386 |
xl = pd.read_excel(file_path, sheet_name=None)
|
|
|
|
| 452 |
return None
|
| 453 |
|
| 454 |
safe_builtins = {
|
| 455 |
+
"print": print, "range": range, "len": len, "sum": sum,
|
| 456 |
+
"min": min, "max": max, "abs": abs, "round": round,
|
| 457 |
+
"enumerate": enumerate, "zip": zip, "list": list, "dict": dict,
|
| 458 |
+
"set": set, "tuple": tuple, "float": float, "int": int, "str": str,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
}
|
| 460 |
safe_globals = {"__builtins__": safe_builtins, "math": math}
|
| 461 |
|
|
|
|
| 472 |
return None
|
| 473 |
|
| 474 |
nums = re.findall(r"[-+]?\d+(?:\.\d+)?", out)
|
| 475 |
+
return nums[-1] if nums else None
|
|
|
|
|
|
|
| 476 |
except Exception:
|
| 477 |
return None
|
| 478 |
|
|
|
|
| 482 |
# -----------------------------
|
| 483 |
class BasicAgent:
|
| 484 |
def __init__(self, api_url: str):
|
| 485 |
+
self.api_url = api_url.rstrip("/")
|
| 486 |
+
self.openapi_paths = discover_openapi_paths(self.api_url)
|
| 487 |
+
print("BasicAgent initialized. openapi_paths:", len(self.openapi_paths))
|
| 488 |
|
| 489 |
def __call__(self, question: str, item: Dict[str, Any]) -> Tuple[str, str]:
|
| 490 |
q = (question or "").strip()
|
| 491 |
ql = q.lower()
|
| 492 |
+
debug_lines: List[str] = []
|
| 493 |
+
|
| 494 |
+
# deterministic answers
|
| 495 |
+
for fn in [solve_reversed_sentence, solve_non_commutative_subset, solve_botany_vegetables, solve_mercedes_sosa, solve_polish_actor]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
try:
|
| 497 |
ans = fn(q)
|
| 498 |
if ans:
|
| 499 |
+
return sanitize_answer(ans), ""
|
| 500 |
except Exception:
|
| 501 |
pass
|
| 502 |
|
| 503 |
is_attachment_task = any(k in ql for k in ["attached excel", "attached python", "attached a file", "i've attached", ".mp3", ".xlsx", ".py"])
|
| 504 |
if not is_attachment_task:
|
| 505 |
+
return "", ""
|
| 506 |
|
| 507 |
+
# filename hint
|
| 508 |
filenames = extract_filenames_from_question(q)
|
| 509 |
+
filename_hint = filenames[0] if filenames else "attachment"
|
| 510 |
+
|
| 511 |
+
# 1) try base64 inside item (NEW)
|
| 512 |
+
paths, dbg = extract_base64_files_from_item(item, filename_hint=filename_hint)
|
| 513 |
+
debug_lines.extend(dbg)
|
| 514 |
+
for fp in paths:
|
| 515 |
+
ans = self._solve_from_file(q, fp)
|
| 516 |
+
if ans:
|
| 517 |
+
return sanitize_answer(ans), "\n".join(debug_lines) if DEBUG_ATTACH else ""
|
| 518 |
|
| 519 |
+
# 2) try URLs found in item
|
| 520 |
urls = extract_file_urls_from_item(item, api_url=self.api_url)
|
| 521 |
for u in urls:
|
| 522 |
+
fp, dbg2 = _try_download_urls([u], tag=filename_hint)
|
| 523 |
+
debug_lines.extend(dbg2)
|
|
|
|
| 524 |
if fp:
|
| 525 |
ans = self._solve_from_file(q, fp)
|
| 526 |
if ans:
|
| 527 |
+
return sanitize_answer(ans), "\n".join(debug_lines) if DEBUG_ATTACH else ""
|
| 528 |
|
| 529 |
+
# 3) try ids in item
|
| 530 |
file_ids = extract_file_ids_from_item(item)
|
| 531 |
+
# fallback: task_id also as id
|
| 532 |
+
tid = item.get("task_id")
|
| 533 |
+
if isinstance(tid, str) and tid:
|
| 534 |
+
file_ids.append(tid)
|
| 535 |
+
|
| 536 |
+
# dedup ids
|
| 537 |
+
seen = set()
|
| 538 |
+
file_ids2 = []
|
| 539 |
+
for x in file_ids:
|
| 540 |
+
if x and x not in seen:
|
| 541 |
+
file_ids2.append(x); seen.add(x)
|
| 542 |
+
|
| 543 |
+
for fid in file_ids2:
|
| 544 |
+
fp, dbg3 = self._download_by_id(fid, filename_hint)
|
| 545 |
+
debug_lines.extend(dbg3)
|
| 546 |
if fp:
|
| 547 |
ans = self._solve_from_file(q, fp)
|
| 548 |
if ans:
|
| 549 |
+
return sanitize_answer(ans), "\n".join(debug_lines) if DEBUG_ATTACH else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
|
| 551 |
+
# last: add item keys snapshot (so you can see where file is)
|
| 552 |
+
if DEBUG_ATTACH:
|
| 553 |
+
try:
|
| 554 |
+
keys = sorted(list(item.keys()))
|
| 555 |
+
debug_lines.append("ITEM_KEYS: " + ", ".join(keys))
|
| 556 |
+
except Exception:
|
| 557 |
+
pass
|
| 558 |
+
|
| 559 |
+
return "", "\n".join(debug_lines).strip() if DEBUG_ATTACH else ""
|
| 560 |
+
|
| 561 |
+
def _download_by_id(self, fid: str, tag: str) -> Tuple[Optional[Path], List[str]]:
|
| 562 |
+
fid = fid.strip()
|
| 563 |
+
api = self.api_url
|
| 564 |
+
|
| 565 |
+
candidates = []
|
| 566 |
+
|
| 567 |
+
# openapi discovered paths
|
| 568 |
+
candidates += build_openapi_url_candidates(api, fid, self.openapi_paths)
|
| 569 |
+
|
| 570 |
+
# common REST-ish
|
| 571 |
+
candidates += [
|
| 572 |
+
f"{api}/files/{fid}",
|
| 573 |
+
f"{api}/files/{fid}/download",
|
| 574 |
+
f"{api}/file/{fid}",
|
| 575 |
+
f"{api}/download/{fid}",
|
| 576 |
+
f"{api}/get_file/{fid}",
|
| 577 |
+
f"{api}/assets/{fid}",
|
| 578 |
+
f"{api}/static/{fid}",
|
| 579 |
+
f"{api}/attachments/{fid}",
|
| 580 |
+
f"{api}/media/{fid}",
|
| 581 |
+
f"{api}/raw/{fid}",
|
| 582 |
+
]
|
| 583 |
+
|
| 584 |
+
# API prefix (very common)
|
| 585 |
+
candidates += [
|
| 586 |
+
f"{api}/api/files/{fid}",
|
| 587 |
+
f"{api}/api/files/{fid}/download",
|
| 588 |
+
f"{api}/api/file/{fid}",
|
| 589 |
+
f"{api}/api/download/{fid}",
|
| 590 |
+
f"{api}/api/attachments/{fid}",
|
| 591 |
+
f"{api}/api/media/{fid}",
|
| 592 |
+
]
|
| 593 |
+
|
| 594 |
+
# HF/Gradio-style file serving (common on Spaces)
|
| 595 |
+
candidates += [
|
| 596 |
+
f"{api}/file={fid}",
|
| 597 |
+
f"{api}/gradio_api/file={fid}",
|
| 598 |
+
f"{api}/gradio_api/file={fid}&download=1",
|
| 599 |
+
]
|
| 600 |
+
|
| 601 |
+
# query styles
|
| 602 |
+
candidates += [
|
| 603 |
+
f"{api}/download?file_id={fid}",
|
| 604 |
+
f"{api}/api/download?file_id={fid}",
|
| 605 |
+
f"{api}/files?file_id={fid}",
|
| 606 |
+
f"{api}/api/files?file_id={fid}",
|
| 607 |
+
]
|
| 608 |
+
|
| 609 |
+
# dedup preserve order
|
| 610 |
+
seen = set()
|
| 611 |
+
cand2 = []
|
| 612 |
+
for u in candidates:
|
| 613 |
+
if u not in seen:
|
| 614 |
+
cand2.append(u); seen.add(u)
|
| 615 |
+
|
| 616 |
+
return _try_download_urls(cand2, tag)
|
| 617 |
|
| 618 |
def _solve_from_file(self, q: str, fp: Path) -> Optional[str]:
|
| 619 |
suf = fp.suffix.lower()
|
| 620 |
ql = q.lower()
|
| 621 |
+
|
| 622 |
if ("attached excel file" in ql) or (suf in [".xlsx", ".xls"]):
|
| 623 |
return solve_excel_food_sales(fp)
|
| 624 |
+
|
| 625 |
if ("attached python code" in ql) or (suf in [".py", ".txt"]):
|
| 626 |
return solve_python_final_numeric(fp)
|
| 627 |
+
|
| 628 |
+
# mp3 tasks are intentionally skipped (no audio pipeline)
|
| 629 |
return None
|
| 630 |
|
| 631 |
|
|
|
|
| 646 |
submit_url = f"{api_url}/submit"
|
| 647 |
|
| 648 |
agent = BasicAgent(api_url=api_url)
|
| 649 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "https://huggingface.co/spaces/UNKNOWN/tree/main"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
|
| 651 |
r = requests.get(questions_url, timeout=45)
|
| 652 |
r.raise_for_status()
|
|
|
|
| 668 |
|
| 669 |
if isinstance(submitted_answer, str) and submitted_answer.strip() == "":
|
| 670 |
skipped += 1
|
| 671 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "SKIPPED", "Debug": debug})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
continue
|
| 673 |
|
| 674 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 675 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer, "Debug": debug})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 676 |
|
| 677 |
if not answers_payload:
|
| 678 |
+
return "⚠️ 全部 SKIPPED(目前沒有穩定可解題,或附件仍抓不到)。", pd.DataFrame(results_log)
|
| 679 |
|
| 680 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
|
| 682 |
r2 = requests.post(submit_url, json=submission_data, timeout=180)
|
| 683 |
r2.raise_for_status()
|
|
|
|
| 704 |
# -----------------------------
|
| 705 |
with gr.Blocks() as demo:
|
| 706 |
gr.Markdown("# Basic Agent Evaluation Runner (No Paid Model)")
|
| 707 |
+
gr.Markdown("✅ This version tries **base64-in-item** + **openapi discovery** + more HF Space download patterns.\n\nDebug column shows what happened.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 708 |
gr.LoginButton()
|
| 709 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 710 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=14, interactive=False)
|
| 711 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
|
| 712 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 713 |
|
| 714 |
if __name__ == "__main__":
|