Superbrida commited on
Commit
0774b2e
·
verified ·
1 Parent(s): 80494ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -6,16 +6,17 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
- # Tool personalizzato per ottenere l'ora attuale a Milano
10
  @tool
11
- def get_current_time_milan() -> str:
12
- """Restituisce l'ora attuale a Milano."""
13
  try:
14
- tz = pytz.timezone('Europe/Rome') # Milano è nella timezone di Roma
15
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
16
- return f"L'ora attuale a Milano è: {local_time}"
 
17
  except Exception as e:
18
- return f"Errore nel recupero dell'ora per Milano: {str(e)}"
19
 
20
  final_answer = FinalAnswerTool()
21
 
@@ -34,7 +35,7 @@ with open("prompts.yaml", 'r') as stream:
34
 
35
  agent = CodeAgent(
36
  model=model,
37
- tools=[final_answer, get_current_time_milan], # Aggiunto il nuovo tool
38
  max_steps=6,
39
  verbosity_level=1,
40
  grammar=None,
@@ -44,4 +45,18 @@ agent = CodeAgent(
44
  prompt_templates=prompt_templates
45
  )
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  GradioUI(agent).launch()
 
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
+ # Tool personalizzato per ottenere l'ora attuale in qualsiasi città
10
  @tool
11
+ def get_current_time(city: str) -> str:
12
+ """Restituisce l'ora attuale per una città specificata dall'utente."""
13
  try:
14
+ # Ricerca della timezone basata sulla città
15
+ timezone = pytz.timezone(pytz.country_timezones.get(city.lower(), 'UTC'))
16
+ local_time = datetime.datetime.now(timezone).strftime("%Y-%m-%d %H:%M:%S")
17
+ return f"L'ora attuale a {city.capitalize()} è: {local_time}"
18
  except Exception as e:
19
+ return f"Errore nel recupero dell'ora per {city}: {str(e)}"
20
 
21
  final_answer = FinalAnswerTool()
22
 
 
35
 
36
  agent = CodeAgent(
37
  model=model,
38
+ tools=[final_answer, get_current_time], # Aggiunto il nuovo tool per qualsiasi città
39
  max_steps=6,
40
  verbosity_level=1,
41
  grammar=None,
 
45
  prompt_templates=prompt_templates
46
  )
47
 
48
+ # Funzione per interpretare la richiesta e restituire l'ora della città specificata
49
+ @tool
50
+ def interpret_and_respond(query: str) -> str:
51
+ """Interpreta la richiesta dell'utente e restituisce l'ora per una città specificata."""
52
+ words = query.lower().split()
53
+ if "time" in words or "ora" in words:
54
+ for word in words:
55
+ if word in pytz.all_timezones:
56
+ return get_current_time(word)
57
+ return "Per favore, specifica una città."
58
+ return "Mi dispiace, non ho capito la richiesta. Puoi riformularla?"
59
+
60
+ agent.tools.append(interpret_and_respond)
61
+
62
  GradioUI(agent).launch()