Spaces:
Sleeping
Sleeping
Renzo
wip: added tool to convert audio to text and improve fetch file and save to temp location
c42da51
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 | |