bvantuan's picture
gemini-2.0-flash-lite for CodeAgent
ddbbdbf
Raw
History Blame Contribute Delete
1.83 kB
from typing import Dict, List, Tuple
import re
import tempfile
from pathlib import Path
import pandas as pd
import requests
from pandas import DataFrame
# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
QUESTIONS_URL = f"{DEFAULT_API_URL}/questions"
SUBMIT_URL = f"{DEFAULT_API_URL}/submit"
FILE_PATH = f"{DEFAULT_API_URL}/files/"
def process_file(task_id: str, question_text: str) -> str:
"""
Attempt to download a file associated with a task from the API.
- If the file exists (HTTP 200), it is saved to a temp directory and the local file path is returned.
- If no file is found (HTTP 404), returns None.
- For all other HTTP errors, the exception is propagated to the caller.
"""
file_url = f"{FILE_PATH}{task_id}"
try:
response = requests.get(file_url, timeout=30)
response.raise_for_status()
except requests.exceptions.RequestException as exc:
print(f"Exception in download_file>> {str(exc)}")
return question_text # Unable to get the file
# Determine filename from 'Content-Disposition' header, fallback to task_id
content_disposition = response.headers.get("content-disposition", "")
filename = task_id
match = re.search(r'filename="([^"]+)"', content_disposition)
if match:
filename = match.group(1)
# Save file in a temp directory
temp_storage_dir = Path(tempfile.gettempdir()) / "gaia_cached_files"
temp_storage_dir.mkdir(parents=True, exist_ok=True)
file_path = temp_storage_dir / filename
file_path.write_bytes(response.content)
return (
f"{question_text}\n\n"
f"---\n"
f"A file was downloaded for this task and saved locally at:\n"
f"{str(file_path)}\n"
f"---\n\n"
)