Spaces:
Sleeping
Sleeping
import os | |
import tempfile | |
import requests | |
from google import genai | |
from google.genai import types | |
from smolagents import 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}" | |
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 | |
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 | |
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 | |
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}" |