AliHaider1's picture
Upload folder using huggingface_hub
aed39aa verified
Raw History Blame Contribute Delete
1.7 kB
import os
import pandas as pd
from dotenv import load_dotenv
from groq import Groq
from huggingface_hub import hf_hub_download
from langchain_core.tools import tool
load_dotenv()
@tool
def download_task_file(task_id: str, file_name: str) -> str:
"""Download the file attached to a GAIA task from Hugging Face Hub and return its contents, transcript, or local path."""
try:
local_path = hf_hub_download(
repo_id="gaia-benchmark/GAIA",
filename=f"2023/validation/{file_name}",
repo_type="dataset",
token=os.getenv("HF_TOKEN"),
)
ext = os.path.splitext(file_name)[1].lower()
if ext in [".py", ".txt", ".csv", ".json", ".md"]:
with open(local_path, "r", encoding="utf-8", errors="ignore") as f:
return f"Downloaded to {local_path}.\n\nFile contents:\n{f.read()[:6000]}"
elif ext in [".xlsx", ".xls"]:
df = pd.read_excel(local_path)
return f"Downloaded to {local_path}.\n\nExcel contents:\n{df.to_string()}"
elif ext in [".mp3", ".wav", ".m4a"]:
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
with open(local_path, "rb") as audio_file:
transcription = client.audio.transcriptions.create(
file=(file_name, audio_file.read()),
model="whisper-large-v3-turbo",
)
return f"Audio transcript of {file_name}:\n{transcription.text}"
else:
return f"File downloaded to: {local_path}"
except Exception as e:
return f"Error downloading/reading file ({file_name}): {e}"