Spaces:
Sleeping
Sleeping
File size: 3,891 Bytes
ac5097d f8a2b06 cd7f0f2 ac5097d cd7f0f2 50e6d45 ac5097d 50e6d45 ac5097d 3c81608 50e6d45 ac5097d 50e6d45 ac5097d 50e6d45 cd7f0f2 ac5097d cd7f0f2 ac5097d 3c81608 cd7f0f2 ac5097d cd7f0f2 ac5097d cd7f0f2 ac5097d 117e0fe ac5097d 3c81608 ac5097d 3c81608 ac5097d 3c81608 ac5097d 117e0fe 3c81608 | 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import os
import requests
from smolagents import tool
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
DOWNLOAD_DIR = "/tmp/gaia_files"
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
@tool
def download_gaia_file(task_id: str) -> str:
"""
Downloads the file attached to a GAIA benchmark question, if any, and
saves it to local disk so it can be inspected with read_file_content.
Args:
task_id: The task_id of the current question, used to fetch the
file associated with it from the scoring server.
Returns:
The local file path of the downloaded file, or a short message
saying that no file is attached to this task.
"""
url = f"{DEFAULT_API_URL}/files/{task_id}"
try:
response = requests.get(url, timeout=30)
if response.status_code == 404:
return "No file is attached to this task."
response.raise_for_status()
except requests.exceptions.RequestException as e:
return f"Error downloading file: {e}"
filename = task_id
content_disposition = response.headers.get("content-disposition", "")
if "filename=" in content_disposition:
filename = content_disposition.split("filename=")[-1].strip('"; ')
else:
content_type = response.headers.get("content-type", "")
if "spreadsheet" in content_type or "excel" in content_type:
filename = f"{task_id}.xlsx"
elif "csv" in content_type:
filename = f"{task_id}.csv"
elif "audio" in content_type:
filename = f"{task_id}.mp3"
elif "image" in content_type:
filename = f"{task_id}.png"
file_path = os.path.join(DOWNLOAD_DIR, filename)
with open(file_path, "wb") as f:
f.write(response.content)
return file_path
@tool
def read_file_content(file_path: str) -> str:
"""
Reads a local file and returns its content as text. Handles plain text,
code, JSON, CSV, and Excel files. For file types it cannot parse it
returns a short message instead of raising an error.
Args:
file_path: The local path of the file to read, typically the path
returned by download_gaia_file.
Returns:
A text representation of the file's content, or an explanatory
message if the file cannot be read.
"""
if not os.path.exists(file_path):
return f"File not found: {file_path}"
ext = os.path.splitext(file_path)[1].lower()
try:
if ext in (".xlsx", ".xls"):
import pandas as pd
sheets = pd.read_excel(file_path, sheet_name=None)
chunks = [f"Sheet: {name}\n{df.to_string()}" for name, df in sheets.items()]
return "\n\n".join(chunks)
if ext == ".csv":
import pandas as pd
df = pd.read_csv(file_path)
return df.to_string()
if ext in (".txt", ".py", ".json", ".md", ".xml", ".html", ".csv"):
with open(file_path, "r", encoding="utf-8", errors="replace") as f:
return f.read()
if ext in (".png", ".jpg", ".jpeg", ".gif"):
return (
f"This is an image file at {file_path}. Use the Python "
"interpreter with PIL to inspect pixel data if needed, or "
"describe what analysis is required."
)
if ext in (".mp3", ".wav"):
return (
f"This is an audio file at {file_path}. Transcription tools "
"are not available; note this limitation if the question "
"cannot be answered without listening to it."
)
# Fall back to a best-effort text read for unknown extensions.
with open(file_path, "r", encoding="utf-8", errors="replace") as f:
return f.read()
except Exception as e:
return f"Error reading file: {e}" |