Spaces:
Runtime error
Runtime error
File size: 1,834 Bytes
ddbbdbf |
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 |
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"
) |