File size: 2,134 Bytes
80494ac
 
 
 
 
 
 
 
0774b2e
80494ac
0774b2e
 
80494ac
0774b2e
 
 
 
9b5b26a
0774b2e
8c01ffb
80494ac
ae7a494
80494ac
 
 
 
 
13d500a
8c01ffb
80494ac
 
8c01ffb
80494ac
 
9b5b26a
80494ac
 
0774b2e
80494ac
 
 
 
 
 
 
8fe992b
 
0774b2e
 
 
 
 
 
 
 
 
 
 
 
 
 
80494ac
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
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 personalizzato per ottenere l'ora attuale in qualsiasi città
@tool
def get_current_time(city: str) -> str:
    """Restituisce l'ora attuale per una città specificata dall'utente."""
    try:
        # Ricerca della timezone basata sulla città
        timezone = pytz.timezone(pytz.country_timezones.get(city.lower(), 'UTC'))
        local_time = datetime.datetime.now(timezone).strftime("%Y-%m-%d %H:%M:%S")
        return f"L'ora attuale a {city.capitalize()} è: {local_time}"
    except Exception as e:
        return f"Errore nel recupero dell'ora per {city}: {str(e)}"

final_answer = FinalAnswerTool()

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None,
)

# Importa lo strumento di generazione immagini dal tool Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)
    
agent = CodeAgent(
    model=model,
    tools=[final_answer, get_current_time],  # Aggiunto il nuovo tool per qualsiasi città
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

# Funzione per interpretare la richiesta e restituire l'ora della città specificata
@tool
def interpret_and_respond(query: str) -> str:
    """Interpreta la richiesta dell'utente e restituisce l'ora per una città specificata."""
    words = query.lower().split()
    if "time" in words or "ora" in words:
        for word in words:
            if word in pytz.all_timezones:
                return get_current_time(word)
        return "Per favore, specifica una città."
    return "Mi dispiace, non ho capito la richiesta. Puoi riformularla?"

agent.tools.append(interpret_and_respond)

GradioUI(agent).launch()