Final_Assignment_Template / files_util.py
DATAD2's picture
Upload 9 files
51e4c5e verified
Raw History Blame Contribute Delete
2.83 kB
import os
import shutil
from typing import List, Optional
import requests
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
def format_user_message(question: str, file_paths: Optional[List[str]] = None) -> str:
question = (question or "").strip()
if not file_paths:
return question
lines = [question, "", "Attached local files (use tools with these paths):"]
for path in file_paths:
if not path:
continue
name = os.path.basename(path)
exists = os.path.exists(path)
size = os.path.getsize(path) if exists else 0
lines.append(f"- {name}: {path} (exists={exists}, bytes={size})")
return "\n".join(lines)
def download_scoring_file(
task_id: str,
file_name: Optional[str] = None,
dest_dir: str = ".",
api_url: str = DEFAULT_API_URL,
timeout: int = 60,
) -> Optional[str]:
"""Download a GAIA attachment from the course scoring API."""
if not file_name:
return None
os.makedirs(dest_dir, exist_ok=True)
dest_path = os.path.join(dest_dir, os.path.basename(file_name))
url = f"{api_url.rstrip('/')}/files/{task_id}"
try:
response = requests.get(url, timeout=timeout)
if response.status_code != 200:
print(f"Scoring API file {task_id} -> HTTP {response.status_code}: {response.text[:200]}")
return download_gaia_dataset_file(file_name, dest_path)
with open(dest_path, "wb") as handle:
handle.write(response.content)
print(f"Saved attachment {file_name} -> {dest_path}")
return dest_path
except requests.RequestException as exc:
print(f"Scoring API file download failed for {task_id}: {exc}")
return download_gaia_dataset_file(file_name, dest_path)
def download_gaia_dataset_file(file_name: str, dest_path: str) -> Optional[str]:
"""Fallback when /files/{task_id} is 404: try the gated GAIA dataset on the Hub."""
try:
from huggingface_hub import hf_hub_download
except ImportError:
return None
token = os.getenv("HUGGINGFACEHUB_API_TOKEN") or os.getenv("HF_TOKEN")
candidates = [
f"2023/validation/{file_name}",
f"2023/test/{file_name}",
file_name,
]
for filename in candidates:
try:
cached = hf_hub_download(
repo_id="gaia-benchmark/GAIA",
filename=filename,
repo_type="dataset",
token=token,
)
os.makedirs(os.path.dirname(dest_path) or ".", exist_ok=True)
shutil.copy2(cached, dest_path)
print(f"Copied GAIA dataset file {filename} -> {dest_path}")
return dest_path
except Exception as exc:
print(f"Hub download missed {filename}: {exc}")
return None