File size: 1,701 Bytes
aed39aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}"