File size: 4,570 Bytes
03aebad
9363094
fe25c9a
9363094
 
 
 
fe25c9a
 
cdbcd7d
9363094
 
 
cdbcd7d
9363094
 
 
 
cdbcd7d
9363094
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe25c9a
cdbcd7d
9363094
 
 
cdbcd7d
9363094
 
 
 
cdbcd7d
9363094
8c99152
9363094
 
 
 
 
 
 
 
 
 
 
fe25c9a
cdbcd7d
9363094
 
 
cdbcd7d
9363094
 
 
 
cdbcd7d
7b1f7dd
8c99152
9363094
 
 
 
 
 
 
 
 
 
 
 
 
 
7df3234
f03d005
26f5620
9363094
 
 
26f5620
9363094
 
 
 
 
ab6c455
8c99152
9363094
 
ab6c455
9363094
 
 
 
 
ab6c455
9363094
ffb9aa5
cdbcd7d
9363094
 
 
 
 
 
 
 
 
 
cdbcd7d
9363094
 
 
 
 
 
 
 
 
23ba2f5
9363094
 
 
dd8df2c
9363094
 
 
 
 
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
import os
import tempfile

import requests
from google import genai
from google.genai import types
from smolagents import tool


@tool
def download_file_of_task_id(task_id: str, file_name: str) -> str:
    """
    Download a file associated with a specific task ID and save it to a temporary location.
    Args:
        task_id (str): The unique identifier of the task associated with the file to download.
        file_name (str): The name to assign to the downloaded file.
    Returns:
        str: Path to the downloaded file or an error message if the download fails.
    """

    try:
        # Create temporary file
        temp_dir = tempfile.gettempdir()
        filepath = os.path.join(temp_dir, file_name)

        # Download the file
        response = requests.get(f"https://agents-course-unit4-scoring.hf.space/files/{task_id}",
                                stream=True)
        response.raise_for_status()

        # Save the file
        with open(filepath, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)

        return filepath
    except Exception as e:
        return f"Error downloading file: {e!s}"


@tool
def analyze_audio_file(path_file_audio: str, query: str) -> str:
    """
    Analyzes an MP3 audio file to answer a specific query.
    Args:
        path_file_audio (str): Path to the MP3 audio file to be analyzed.
        query (str): Question or query to analyze the content of the audio file.
    Returns:
        str: The result of the analysis of audio.
    """

    client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))

    myfile = client.files.upload(file=path_file_audio)

    response = client.models.generate_content(
        model=os.getenv("GOOGLE_MODEL_ID"),
        contents=[f"Carefully analyze the audio to answer the question correctly.\n\n The question is {query}",
                  myfile]
    )

    return response.text


@tool
def analyze_youtube_video(url_youtube_video: str, query: str) -> str:
    """
    Analyzes a YouTube video using the provided query.
    Args:
        url_youtube_video (str): URL of the YouTube video to analyze.
        query (str): Query or question to analyze the content of the video.
    Returns:
        str: Result of the video analysis.
    """

    client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))

    response = client.models.generate_content(
        model=f"models/{os.getenv('GOOGLE_MODEL_ID')}",
        contents=types.Content(
            parts=[
                types.Part(
                    file_data=types.FileData(file_uri=url_youtube_video)
                ),
                types.Part(text=f"Carefully analyze each frame of the video to answer the question correctly.\n\n The question is {query}")
            ]
        )
    )

    return response.text


@tool
def analyze_image_file(path_file_image: str, query: str) -> str:
    """
    Analyzes an image file to answer a specific query.
    Args:
        path_file_image (str): Path to the image file to be analyzed.
        query (str): Question or query to analyze the content of the image file.
    Returns:
        str: The result of the analysis of audio.
    """

    client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))

    myfile = client.files.upload(file=path_file_image)

    response = client.models.generate_content(
        model=os.getenv('GOOGLE_MODEL_ID'),
        contents=[myfile,
                  f"Carefully analyze the image file and think to answer the question correctly.\n\n The question is {query}"]
    )

    return response.text


@tool
def analyze_xlsx_file(file_path: str, query: str) -> str:
    """
    Analyze an Excel file using pandas and answer a question about it.
    Args:
        file_path: Path to the Excel file
        query: Question about the data
    Returns:
        Analysis result or error message
    """
    
    try:
        import pandas as pd

        # Read the Excel file
        df = pd.read_excel(file_path)

        # Run various analyses based on the query
        result = f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
        result += f"Columns: {', '.join(df.columns)}\n\n"

        # Add summary statistics
        result += "Summary statistics:\n"
        result += str(df.describe())

        return result
    except ImportError:
        return "Error: pandas and openpyxl are not installed. Please install them with 'pip install pandas openpyxl'."
    except Exception as e:
        return f"Error analyzing Excel file: {e!s}"