Update tools/attachment_reader.py
Browse files- tools/attachment_reader.py +58 -18
tools/attachment_reader.py
CHANGED
|
@@ -42,35 +42,75 @@ def _filename_from_response(response: requests.Response, task_id: str) -> str:
|
|
| 42 |
return f"{task_id}{extension}"
|
| 43 |
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
url = f"{api_url}/files/{task_id}"
|
| 52 |
try:
|
| 53 |
response = requests.get(url, timeout=timeout)
|
| 54 |
-
if response.status_code =
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
task_dir = os.path.join(ATTACHMENTS_DIR, str(task_id))
|
| 61 |
-
os.makedirs(task_dir, exist_ok=True)
|
| 62 |
-
path = os.path.join(task_dir, _filename_from_response(response, task_id))
|
| 63 |
-
|
| 64 |
-
with open(path, "wb") as f:
|
| 65 |
-
f.write(response.content)
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
return path
|
| 69 |
except Exception as e:
|
| 70 |
-
print(f"
|
| 71 |
return None
|
| 72 |
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
# =========================================================
|
| 75 |
# Reader tool
|
| 76 |
# =========================================================
|
|
|
|
| 42 |
return f"{task_id}{extension}"
|
| 43 |
|
| 44 |
|
| 45 |
+
def _save_bytes(task_id: str, filename: str, data: bytes) -> str:
|
| 46 |
+
task_dir = os.path.join(ATTACHMENTS_DIR, str(task_id))
|
| 47 |
+
os.makedirs(task_dir, exist_ok=True)
|
| 48 |
+
path = os.path.join(task_dir, filename)
|
| 49 |
+
with open(path, "wb") as f:
|
| 50 |
+
f.write(data)
|
| 51 |
+
return path
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _download_from_scoring_api(api_url: str, task_id: str, timeout: int = 30) -> str | None:
|
| 55 |
+
"""Try the scoring API's /files/{task_id} endpoint. Returns a path or None."""
|
| 56 |
url = f"{api_url}/files/{task_id}"
|
| 57 |
try:
|
| 58 |
response = requests.get(url, timeout=timeout)
|
| 59 |
+
if response.status_code != 200 or not response.content:
|
| 60 |
+
print(
|
| 61 |
+
f"[attachment] {url} -> HTTP {response.status_code} "
|
| 62 |
+
f"{response.text[:150]!r}"
|
| 63 |
+
)
|
| 64 |
return None
|
| 65 |
+
path = _save_bytes(task_id, _filename_from_response(response, task_id), response.content)
|
| 66 |
+
print(f"[attachment] downloaded from scoring API: {path}")
|
| 67 |
+
return path
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"[attachment] scoring API download failed for {task_id}: {e}")
|
| 70 |
+
return None
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
def _download_from_gaia_dataset(task_id: str, file_name: str) -> str | None:
|
| 74 |
+
"""
|
| 75 |
+
Fallback: fetch the file straight from the gated GAIA dataset on the Hub.
|
| 76 |
+
Needs HF_TOKEN in the environment (a Space secret) and that your account
|
| 77 |
+
has accepted the terms at https://huggingface.co/datasets/gaia-benchmark/GAIA
|
| 78 |
+
"""
|
| 79 |
+
token = os.environ.get("HF_TOKEN")
|
| 80 |
+
if not token:
|
| 81 |
+
print("[attachment] HF_TOKEN not set; cannot use the GAIA dataset fallback.")
|
| 82 |
+
return None
|
| 83 |
+
try:
|
| 84 |
+
from huggingface_hub import hf_hub_download
|
| 85 |
+
|
| 86 |
+
cached = hf_hub_download(
|
| 87 |
+
repo_id="gaia-benchmark/GAIA",
|
| 88 |
+
filename=f"2023/validation/{file_name}",
|
| 89 |
+
repo_type="dataset",
|
| 90 |
+
token=token,
|
| 91 |
+
)
|
| 92 |
+
with open(cached, "rb") as f:
|
| 93 |
+
path = _save_bytes(task_id, os.path.basename(file_name), f.read())
|
| 94 |
+
print(f"[attachment] downloaded from GAIA dataset: {path}")
|
| 95 |
return path
|
| 96 |
except Exception as e:
|
| 97 |
+
print(f"[attachment] GAIA dataset fallback failed for {file_name}: {e}")
|
| 98 |
return None
|
| 99 |
|
| 100 |
|
| 101 |
+
def download_attachment(api_url: str, task_id: str, file_name: str | None = None,
|
| 102 |
+
timeout: int = 30) -> str | None:
|
| 103 |
+
"""
|
| 104 |
+
Gets the attachment for a task. Tries the scoring API first and, if that
|
| 105 |
+
fails and the task's file_name is known, falls back to the GAIA dataset.
|
| 106 |
+
Returns the local file path, or None. Never raises.
|
| 107 |
+
"""
|
| 108 |
+
path = _download_from_scoring_api(api_url, task_id, timeout)
|
| 109 |
+
if path is None and file_name:
|
| 110 |
+
path = _download_from_gaia_dataset(task_id, file_name)
|
| 111 |
+
return path
|
| 112 |
+
|
| 113 |
+
|
| 114 |
# =========================================================
|
| 115 |
# Reader tool
|
| 116 |
# =========================================================
|