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}"