File size: 2,834 Bytes
51e4c5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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