Superbrida's picture
Update app.py
7310dd0 verified
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# TOOL PERSONALIZZATI
@tool
def my_custom_tool(arg1: str, arg2: int) -> str:
"""Un tool di esempio che al momento non fa nulla di utile.
Args:
arg1: Il primo argomento.
arg2: Il secondo argomento.
Returns:
Una stringa di esempio.
"""
return "What magic will you build?"
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""Ottiene l'orario corrente in un fuso orario specificato.
Args:
timezone: Il fuso orario richiesto (es. 'Europe/Rome').
Returns:
Una stringa con l'orario locale.
"""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"L'ora locale in {timezone} è: {local_time}"
except Exception as e:
return f"Errore nel recupero dell'orario per '{timezone}': {str(e)}"
@tool
def web_search(query: str) -> str:
"""Effettua una ricerca sul web utilizzando DuckDuckGo e restituisce i risultati principali.
Args:
query: La stringa di ricerca.
Returns:
Una stringa con i risultati principali della ricerca.
"""
try:
search_tool = DuckDuckGoSearchTool()
results = search_tool.run(query)
return f"Risultati per '{query}':\n{results}"
except Exception as e:
return f"Errore nella ricerca web: {str(e)}"
# STRUMENTO DI RISPOSTA FINALE
final_answer = FinalAnswerTool()
# CONFIGURAZIONE DEL MODELLO AI
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)
# IMPORT TOOL PER GENERAZIONE IMMAGINI
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# CARICAMENTO TEMPLATE DI PROMPT
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# CREAZIONE DELL'AGENTE AI
agent = CodeAgent(
model=model,
tools=[final_answer, web_search, get_current_time_in_timezone, my_custom_tool],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name="AI Helper",
description="Un assistente AI con ricerca web e strumenti utili.",
prompt_templates=prompt_templates
)
# AVVIO DELL'INTERFACCIA GRADIO
GradioUI(agent).launch()