Spaces:
Sleeping
Sleeping
File size: 964 Bytes
c42da51 |
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 |
import httpx
import tempfile
import mimetypes
import os
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
def fetch_file_content(task_id: str, temp: bool = False) -> dict:
"""
Downloads file content for the given task_id.
Returns a dict with:
- content: bytes of the file
- path: filesystem path to a temp file if temp=True, else None
"""
url = f"{DEFAULT_API_URL}/files/{task_id}"
resp = httpx.get(url, timeout=15, follow_redirects=True)
resp.raise_for_status()
content = resp.content
result = {"content": content, "path": None}
if temp:
ctype = resp.headers.get("content-type", "")
ext = mimetypes.guess_extension(ctype) or os.path.splitext(task_id)[1] or ""
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=ext)
tmp.write(content)
tmp.close()
result["path"] = tmp.file.name
print("[fetch_file_content]", result)
return result
|