First_agent / app.py
lchumaceiro's picture
Update app.py
0dd5ee2 verified
raw
history blame
3.18 kB
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
import time
import os
from tools.final_answer import FinalAnswerTool
from huggingface_hub import InferenceClient, login
from pydub.generators import WhiteNoise
from pydub import AudioSegment
import gradio as gr
from Gradio_UI import GradioUI
# Load Hugging Face token
hf_token = os.getenv("HF_TOKEN") # Fetch secret from Hugging Face Space
if hf_token:
login(hf_token)
else:
print("⚠️ Hugging Face API token is missing! Set HF_TOKEN in Space secrets.")
# ✅ Sound generation tool with structured docstring for smolagents
@tool
def generate_sound(sound_type: str, duration: int) -> str:
"""Generates a simple sound based on the specified type and duration.
Args:
sound_type: Type of sound to generate (e.g., 'rain', 'white_noise').
duration: Duration of the sound in seconds.
Returns:
Path to the generated audio file.
"""
try:
duration_ms = duration * 1000 # Convert to milliseconds
if sound_type == "rain":
sound = WhiteNoise().to_audio_segment(duration=duration_ms).low_pass_filter(5000)
else:
return f"Unsupported sound type: {sound_type}"
output_path = f"/tmp/{sound_type}_{duration}s.wav"
sound.export(output_path, format="wav")
return output_path
except Exception as e:
return f"Error generating sound: {str(e)}"
# ✅ Time zone tool
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""Fetches the current local time in a specified timezone."""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
# ✅ Load the final answer tool
final_answer = FinalAnswerTool()
# ✅ Model configuration
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
token=hf_token
)
# ✅ Load external tools
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# ✅ Load prompt templates
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# ✅ Define the agent (Removed `output_modality`)
agent = CodeAgent(
model=model,
tools=[final_answer, generate_sound, image_generation_tool], # Included image generation tool
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
# ✅ Start the UI with processing time display
def launch_with_processing_time():
def wrapped_launch():
start_time = time.time()
print("Processing request...")
GradioUI(agent).launch()
end_time = time.time()
print(f"Processing completed in {end_time - start_time:.2f} seconds")
wrapped_launch()
launch_with_processing_time()