Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,9 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
@@ -18,6 +21,74 @@ def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
import requests
|
| 8 |
+
from bs4 import BeautifulSoup
|
| 9 |
+
from smolagents import tool
|
| 10 |
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
|
|
|
| 21 |
"""
|
| 22 |
return "What magic will you build ?"
|
| 23 |
|
| 24 |
+
@tool
|
| 25 |
+
def get_random_joke() -> str:
|
| 26 |
+
"""A tool that fetches a random joke from a public joke API.
|
| 27 |
+
Args:
|
| 28 |
+
None
|
| 29 |
+
"""
|
| 30 |
+
try:
|
| 31 |
+
response = requests.get("https://official-joke-api.appspot.com/random_joke", timeout=5)
|
| 32 |
+
if response.status_code == 200:
|
| 33 |
+
data = response.json()
|
| 34 |
+
setup = data.get("setup", "")
|
| 35 |
+
punchline = data.get("punchline", "")
|
| 36 |
+
return f"{setup} - {punchline}"
|
| 37 |
+
else:
|
| 38 |
+
return f"Failed to fetch a joke. HTTP status code: {response.status_code}"
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"An error occurred: {str(e)}"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@tool
|
| 44 |
+
def question_about_car_km77(query: str) -> str:
|
| 45 |
+
"""
|
| 46 |
+
A tool that queries https://www.km77.com/ to find information related to a user's question
|
| 47 |
+
about a specific car or topic.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
query (str): The question or keywords the user wants to search for on km77.
|
| 51 |
+
|
| 52 |
+
Returns:
|
| 53 |
+
str: A short text containing top search results or an error message if none are found.
|
| 54 |
+
"""
|
| 55 |
+
try:
|
| 56 |
+
# Hacemos una búsqueda en la sección del buscador de km77
|
| 57 |
+
search_url = f"https://www.km77.com/buscador/?q={query}"
|
| 58 |
+
response = requests.get(search_url, timeout=10)
|
| 59 |
+
|
| 60 |
+
if response.status_code != 200:
|
| 61 |
+
return f"Error accediendo a km77. Código de estado HTTP: {response.status_code}"
|
| 62 |
+
|
| 63 |
+
# Usamos BeautifulSoup para parsear el HTML
|
| 64 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 65 |
+
|
| 66 |
+
# Seleccionamos algunos enlaces de la página de resultados
|
| 67 |
+
# (el selector CSS podría variar si km77 cambia su estructura)
|
| 68 |
+
results = soup.select("div.col-xs-12.col-md-8 a")
|
| 69 |
+
if not results:
|
| 70 |
+
return "No se encontraron resultados para tu búsqueda en km77."
|
| 71 |
+
|
| 72 |
+
# Tomamos los primeros 5 resultados y construimos una respuesta
|
| 73 |
+
answer_lines = []
|
| 74 |
+
for r in results[:5]:
|
| 75 |
+
title = r.get_text(strip=True) # texto del enlace
|
| 76 |
+
link = r.get('href') # URL del enlace
|
| 77 |
+
if link.startswith('/'):
|
| 78 |
+
# Ajustar link relativo al dominio principal
|
| 79 |
+
link = f"https://www.km77.com{link}"
|
| 80 |
+
answer_lines.append(f"- {title}: {link}")
|
| 81 |
+
|
| 82 |
+
answer = (
|
| 83 |
+
f"**Resultados de km77 para** '{query}':\n\n"
|
| 84 |
+
+ "\n".join(answer_lines)
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
return answer
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return f"Ocurrió un error al realizar la búsqueda: {str(e)}"
|
| 91 |
+
|
| 92 |
@tool
|
| 93 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 94 |
"""A tool that fetches the current local time in a specified timezone.
|