Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
| def download_file(task_id: str, filename: str) -> str: | |
| """ | |
| Downloads a file associated with the given task_id and saves it to the specified filename. | |
| Args: | |
| task_id (str): The task identifier used to fetch the file. | |
| filename (str): The desired filename to save the file as. | |
| Returns: | |
| str: The absolute path to the saved file. | |
| """ | |
| api_url = DEFAULT_API_URL | |
| file_url = f"{api_url}/files/{task_id}" | |
| folder = 'data' | |
| print(f"📡 Fetching file from: {file_url}") | |
| try: | |
| response = requests.get(file_url, timeout=15) | |
| response.raise_for_status() | |
| # Save binary content to the given filename | |
| fpath = os.path.join(folder, filename) | |
| with open(fpath, "wb") as f: | |
| f.write(response.content) | |
| abs_path = os.path.abspath(fpath) | |
| print(f"✅ File saved as: {abs_path}") | |
| return abs_path | |
| except requests.exceptions.RequestException as e: | |
| error_msg = f"❌ Failed to download file for task {task_id}: {e}" | |
| print(error_msg) | |
| raise RuntimeError(error_msg) |