Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ from Gradio_UI import GradioUI
|
|
| 15 |
|
| 16 |
# Weather search tool using DuckDuckGo
|
| 17 |
@tool
|
|
|
|
| 18 |
def get_weather_info(city: str) -> str:
|
| 19 |
"""Searches for current weather information for a given city using DuckDuckGo.
|
| 20 |
|
|
@@ -25,21 +26,30 @@ def get_weather_info(city: str) -> str:
|
|
| 25 |
A string containing weather information for the specified city
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
search_results = search_tool(search_query)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
if 'snippet' in result:
|
| 36 |
-
weather_info += f"\n{i+1}. {result['snippet']}"
|
| 37 |
-
return weather_info
|
| 38 |
-
else:
|
| 39 |
-
return f"Could not find weather information for {city}. Please check the city name and try again."
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
return f"Error
|
| 43 |
|
| 44 |
@tool
|
| 45 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 15 |
|
| 16 |
# Weather search tool using DuckDuckGo
|
| 17 |
@tool
|
| 18 |
+
@tool
|
| 19 |
def get_weather_info(city: str) -> str:
|
| 20 |
"""Searches for current weather information for a given city using DuckDuckGo.
|
| 21 |
|
|
|
|
| 26 |
A string containing weather information for the specified city
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
+
from duckduckgo_search import DDGS
|
| 30 |
+
|
|
|
|
| 31 |
|
| 32 |
+
with DDGS() as ddgs:
|
| 33 |
+
|
| 34 |
+
results = list(ddgs.text(f"{city} current weather temperature", max_results=3))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
if results:
|
| 37 |
+
weather_info = f"Weather information for {city}:\n"
|
| 38 |
+
for i, result in enumerate(results):
|
| 39 |
+
title = result.get('title', '')
|
| 40 |
+
body = result.get('body', '')
|
| 41 |
+
weather_info += f"\n{i+1}. {title}\n {body}\n"
|
| 42 |
+
return weather_info
|
| 43 |
+
else:
|
| 44 |
+
return f"No weather information found for {city}"
|
| 45 |
+
|
| 46 |
+
except ImportError:
|
| 47 |
+
# Fallback do DuckDuckGoSearchTool
|
| 48 |
+
search_tool = DuckDuckGoSearchTool()
|
| 49 |
+
result = search_tool(f"{city} weather")
|
| 50 |
+
return f"Weather in {city}: {result}" if result else f"No weather data for {city}"
|
| 51 |
except Exception as e:
|
| 52 |
+
return f"Error: {str(e)}"
|
| 53 |
|
| 54 |
@tool
|
| 55 |
def get_current_time_in_timezone(timezone: str) -> str:
|