Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,22 +8,52 @@ from tools.final_answer import FinalAnswerTool
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
|
|
|
|
|
|
|
11 |
@tool
|
12 |
-
def tool_ricerca_panini(city:str) -> str:
|
13 |
-
|
14 |
-
|
15 |
Args:
|
16 |
-
city:
|
|
|
|
|
|
|
17 |
"""
|
18 |
try:
|
19 |
-
#
|
20 |
query = f"miglior panino {city} Italia"
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
except Exception as e:
|
26 |
-
return f"Errore
|
|
|
27 |
|
28 |
@tool
|
29 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
+
|
12 |
+
from smolagents import tool
|
13 |
+
import requests
|
14 |
+
from bs4 import BeautifulSoup
|
15 |
+
|
16 |
@tool
|
17 |
+
def tool_ricerca_panini(city: str) -> str:
|
18 |
+
"""Un tool che ricerca il miglior panino in una città italiana utilizzando una query su DuckDuckGo.
|
19 |
+
|
20 |
Args:
|
21 |
+
city: Una stringa rappresentante una città in Italia (es. 'Milano', 'Torino').
|
22 |
+
|
23 |
+
Returns:
|
24 |
+
Una stringa che descrive il miglior panino trovato nella città specificata.
|
25 |
"""
|
26 |
try:
|
27 |
+
# Costruisci la query di ricerca
|
28 |
query = f"miglior panino {city} Italia"
|
29 |
+
url = "https://html.duckduckgo.com/html/"
|
30 |
+
params = {"q": query}
|
31 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
32 |
+
response = requests.post(url, data=params, headers=headers)
|
33 |
+
|
34 |
+
if response.status_code != 200:
|
35 |
+
return f"Errore nella ricerca: codice {response.status_code}"
|
36 |
+
|
37 |
+
# Analizza la risposta HTML con BeautifulSoup
|
38 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
39 |
+
# Cerca i risultati della ricerca; i risultati sono generalmente link con classe "result__a"
|
40 |
+
results = []
|
41 |
+
for result in soup.find_all("a", class_="result__a"):
|
42 |
+
title = result.get_text()
|
43 |
+
# Proviamo a trovare un elemento descrittivo vicino al link (se presente)
|
44 |
+
snippet_elem = result.find_next("a", class_="result__snippet")
|
45 |
+
snippet = snippet_elem.get_text() if snippet_elem else "Nessun dettaglio disponibile"
|
46 |
+
results.append({"title": title, "body": snippet})
|
47 |
+
|
48 |
+
if results:
|
49 |
+
top_result = results[0]
|
50 |
+
return (f"Il miglior panino a {city.title()} sembra essere: {top_result['title']}. "
|
51 |
+
f"Dettagli: {top_result['body']}")
|
52 |
+
else:
|
53 |
+
return f"Nessun risultato trovato per {city.title()}."
|
54 |
except Exception as e:
|
55 |
+
return f"Errore durante la ricerca per {city.title()}: {str(e)}"
|
56 |
+
|
57 |
|
58 |
@tool
|
59 |
def get_current_time_in_timezone(timezone: str) -> str:
|