File size: 1,464 Bytes
1bbca12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import requests
import os

class FileParserTool:
    def __init__(self):
        self.name = "file_parser"
        self.description = "Downloads and parses CSV or text files for GAIA tasks."
        self.inputs = {
            "task_id": {"type": "string", "description": "GAIA task ID"},
            "file_type": {"type": "string", "description": "File type (csv, txt, default: csv)"}
        }
        self.output_type = str

    async def aparse(self, task_id: str, file_type: str = "csv") -> str:
        try:
            url = f"https://api.gaia-benchmark.com/files/{task_id}"
            response = await requests.get(url)
            if response.status_code == 200:
                file_path = f"temp_{task_id}.{file_type}"
                with open(file_path, "wb") as f:
                    f.write(response.content)
                if file_type == "csv":
                    df = pd.read_csv(file_path)
                    return df.to_string()
                elif file_type == "txt":
                    with open(file_path, "r") as f:
                        return f.read()
                else:
                    return f"Unsupported file type: {file_type}"
            return f"Error downloading file for task ID {task_id}"
        except Exception as e:
            return f"Error: {str(e)}"
        finally:
            if os.path.exists(file_path):
                os.remove(file_path)

file_parser_tool = FileParserTool()