File size: 8,440 Bytes
9505e38
859769f
6c9d559
9505e38
 
6c9d559
9505e38
 
 
859769f
 
 
9505e38
6c9d559
9505e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c9d559
9505e38
6c9d559
 
 
 
9505e38
6c9d559
 
9505e38
6c9d559
9505e38
6c9d559
9505e38
6c9d559
 
9505e38
6c9d559
9505e38
6c9d559
 
 
 
 
 
 
 
9505e38
6c9d559
 
 
9505e38
6c9d559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9505e38
 
 
6c9d559
 
 
 
 
 
 
 
 
 
 
9505e38
6c9d559
 
 
 
 
9505e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859769f
9505e38
 
859769f
9505e38
859769f
 
9505e38
859769f
9505e38
 
 
 
 
859769f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9505e38
859769f
 
9505e38
 
 
 
 
 
859769f
9505e38
 
859769f
9505e38
859769f
 
9505e38
859769f
9505e38
 
859769f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9505e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import os
import pandas as pd
import re
import requests
import tempfile
import uuid

from langchain_core.tools import tool

from google import genai
from google.genai import types

# from smolagents import tool
from typing import Optional, Dict, Union


@tool
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
    """
    Save content to a temporary file and return the path.
    Useful for processing files from the GAIA API.

    Args:
        content: The content to save to the file
        filename: Optional filename, will generate a random name if not provided

    Returns:
        Path to the saved file
    """
    temp_dir = tempfile.gettempdir()
    if filename is None:
        temp_file = tempfile.NamedTemporaryFile(delete=False)
        filepath = temp_file.name
    else:
        filepath = os.path.join(temp_dir, filename)

    # Write content to the file
    with open(filepath, "w") as f:
        f.write(content)

    return f"File saved to {filepath}. You can read this file to process its contents."


# File Download Tool
@tool
def download_file_from_url(
    url: str, directory: str
) -> Dict[str, Union[str, None]]:
    """Downloads a file from a URL and saves it to a directory.
    Args:
        url (str): the URL to download the file from.
        directory (str): the directory to save the file to.
    Returns:
        Dict[str, Union[str, None]]: A dictionary containing the file type and path.
    """

    try:
        response = requests.get(url, stream=True, timeout=10)
        response.raise_for_status()

        content_type = response.headers.get("content-type", "").lower()

        # Try to get filename from headers
        filename = None
        cd = response.headers.get("content-disposition", "")
        match = re.search(r"filename\*=UTF-8\'\'(.+)", cd) or re.search(
            r'filename="?([^"]+)"?', cd
        )
        if match:
            filename = match.group(1)

        # If not in headers, try URL
        if not filename:
            filename = os.path.basename(url.split("?")[0])

        # Fallback to generated filename
        if not filename:
            extension = {
                "image/jpeg": ".jpg",
                "image/png": ".png",
                "image/gif": ".gif",
                "audio/wav": ".wav",
                "audio/mpeg": ".mp3",
                "video/mp4": ".mp4",
                "text/plain": ".txt",
                "text/csv": ".csv",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
                "application/vnd.ms-excel": ".xls",
                "application/octet-stream": ".bin",
            }.get(content_type, ".bin")
            filename = f"downloaded_{uuid.uuid4().hex[:8]}{extension}"

        os.makedirs(directory, exist_ok=True)
        file_path = os.path.join(directory, filename)

        with open(file_path, "wb") as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)

        # shutil.copy(file_path, os.getcwd())

        if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
            return {"type": content_type, "path": file_path}
        else:
            return {
                "type": "error",
                "path": None,
                "error": "Failed to save file",
            }

    except Exception as e:
        return {
            "type": "error",
            "path": None,
            "error": f"Error downloading file: {str(e)}",
        }


@tool
def extract_text_from_image(image_path: str) -> str:
    """
    Extract text from an image using pytesseract (if available).

    Args:
        image_path: Path to the image file

    Returns:
        Extracted text or error message
    """
    try:
        # Try to import pytesseract
        import pytesseract
        from PIL import Image

        # Open the image
        image = Image.open(image_path)

        # Extract text
        text = pytesseract.image_to_string(image)

        return f"Extracted text from image:\n\n{text}"
    except ImportError:
        return "Error: pytesseract is not installed. Please install it with 'pip install pytesseract' and ensure Tesseract OCR is installed on your system."
    except Exception as e:
        return f"Error extracting text from image: {str(e)}"


# CSV Analysis Tool
@tool
def analyze_csv_file(file_path: str, query: str) -> str:
    """Analyzes a CSV file and answers questions about its contents using Gemini.
    Args:
        file_path (str): the path to the CSV file to analyze.
        query (str): the question to answer about the CSV file.
    Returns:
        str: The result of the analysis.
    """
    try:
        # Read the CSV file
        df = pd.read_csv(file_path)

        # Initialize Gemini
        client = genai.Client(api_key=os.getenv("GEMINI_KEY"))
        model = "models/gemini-1.5-flash-8b"

        # Convert DataFrame to a string representation
        df_str = df.to_string()

        # Create a prompt for Gemini
        prompt = f"""Analyze this CSV data and provide insights:
Dimensions: {len(df)} rows × {len(df.columns)} columns
Data:
{df_str}
Please provide:
1. A summary of the data structure and content
2. Key patterns and insights
3. Potential data quality issues
4. Suggestions for analysis
User Query: {query}
Please format your response in a clear, structured way with sections and bullet points."""

        # Get analysis from Gemini
        response = client.models.generate_content(
            model=model,
            contents=types.Content(
                parts=[
                    types.Part(text=df_str),
                    types.Part(text=prompt),
                ]
            ),
        )

        result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n\n"
        result += response.text

        return result
    except Exception as e:
        return f"Error analyzing CSV file: {str(e)}"


# Excel Analysis Tool
@tool
def analyze_excel_file(file_path: str, query: str) -> str:
    """Analyzes an Excel file and answers questions about its contents using Gemini.
    Args:
        file_path (str): the path to the Excel file to analyze.
        query (str): the question to answer about the Excel file.
    Returns:
        str: The result of the analysis.
    """
    try:
        # Read all sheets from the Excel file
        excel_file = pd.ExcelFile(file_path)
        sheet_names = excel_file.sheet_names

        # Initialize Gemini
        client = genai.Client(api_key=os.getenv("GEMINI_KEY"))
        model = "models/gemini-1.5-flash-8b"

        result = f"Excel file loaded with {len(sheet_names)} sheets: {', '.join(sheet_names)}\n\n"

        # Analyze each sheet
        for sheet_name in sheet_names:
            df = pd.read_excel(file_path, sheet_name=sheet_name)

            # Convert DataFrame to a string representation
            df_str = df.to_string()

            # Create a prompt for Gemini
            prompt = f"""Analyze this Excel sheet data and provide insights:
Sheet Name: {sheet_name}
Dimensions: {len(df)} rows × {len(df.columns)} columns
Data:
{df_str}
Please provide:
1. A summary of the data structure and content
2. Key patterns and insights
3. Potential data quality issues
4. Suggestions for analysis
User Query: {query}
Please format your response in a clear, structured way with sections and bullet points."""

            # Get analysis from Gemini
            response = client.models.generate_content(
                model=model,
                contents=types.Content(
                    parts=[types.Part(text=df_str), types.Part(text=prompt)]
                ),
            )

            result += f"=== Sheet: {sheet_name} ===\n"
            result += response.text + "\n"
            result += "=" * 50 + "\n\n"

        return result
    except Exception as e:
        return f"Error analyzing Excel file: {str(e)}"


@tool
def read_file(filepath: str) -> str:
    """Reads the content of a text file.
    Args:
        filepath (str): the path to the file to read.
    Returns:
        str: The content of the file.
    """
    try:
        with open(filepath, "r", encoding="utf-8") as file:
            content = file.read()
        return content
    except FileNotFoundError:
        return f"File not found: {filepath}"
    except IOError as e:
        return f"Error reading file: {str(e)}"