Spaces:
Sleeping
Sleeping
carlosdimare
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -13,17 +13,27 @@ SEARCH_ENGINE_ID = "030a88810b398467c"
|
|
13 |
def web_search(query):
|
14 |
# Realiza la b煤squeda en Google
|
15 |
url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={GOOGLE_API_KEY}&cx={SEARCH_ENGINE_ID}"
|
16 |
-
response = requests.get(url)
|
17 |
-
results = response.json()
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Define la funci贸n del chatbot con navegaci贸n web
|
29 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
13 |
def web_search(query):
|
14 |
# Realiza la b煤squeda en Google
|
15 |
url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={GOOGLE_API_KEY}&cx={SEARCH_ENGINE_ID}"
|
|
|
|
|
16 |
|
17 |
+
try:
|
18 |
+
response = requests.get(url)
|
19 |
+
response.raise_for_status() # Lanza un error si la respuesta no es exitosa
|
20 |
+
results = response.json()
|
21 |
+
|
22 |
+
if "items" in results:
|
23 |
+
# Devuelve un resumen de los primeros resultados
|
24 |
+
search_results = []
|
25 |
+
for item in results["items"]:
|
26 |
+
title = item.get("title", "No title")
|
27 |
+
link = item.get("link", "")
|
28 |
+
snippet = item.get("snippet", "")
|
29 |
+
search_results.append(f"{title}: {snippet} ({link})")
|
30 |
+
return "\n".join(search_results) # Devuelve los resultados como texto
|
31 |
+
else:
|
32 |
+
return "No se encontraron resultados relevantes."
|
33 |
+
|
34 |
+
except requests.exceptions.RequestException as e:
|
35 |
+
# Maneja errores de la API, como problemas de conexi贸n
|
36 |
+
return f"Error al realizar la b煤squeda: {e}"
|
37 |
|
38 |
# Define la funci贸n del chatbot con navegaci贸n web
|
39 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|