Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from tools.final_answer import FinalAnswerTool
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
import requests
|
|
|
|
| 11 |
|
| 12 |
@tool
|
| 13 |
def get_location() -> str:
|
|
@@ -25,37 +26,35 @@ def get_location() -> str:
|
|
| 25 |
return f"Error retrieving location: {str(e)}"
|
| 26 |
|
| 27 |
@tool
|
| 28 |
-
def
|
| 29 |
-
"""Gets the current weather for a given location.
|
| 30 |
|
| 31 |
Args:
|
| 32 |
-
location: The
|
| 33 |
|
| 34 |
Returns:
|
| 35 |
A string containing the current weather conditions for the specified location.
|
| 36 |
"""
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
"appid": api_key,
|
| 44 |
-
"units": "metric" # Use "imperial" for Fahrenheit
|
| 45 |
}
|
| 46 |
|
| 47 |
-
response = requests.get(
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return f"The current weather in {location} is {temp}°C with {description}."
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
-
return f"
|
| 59 |
|
| 60 |
@tool
|
| 61 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
import requests
|
| 11 |
+
from bs4 import BeautifulSoup
|
| 12 |
|
| 13 |
@tool
|
| 14 |
def get_location() -> str:
|
|
|
|
| 26 |
return f"Error retrieving location: {str(e)}"
|
| 27 |
|
| 28 |
@tool
|
| 29 |
+
def get_weather_no_api(location: str) -> str:
|
| 30 |
+
"""Gets the current weather for a given location without using an API key.
|
| 31 |
|
| 32 |
Args:
|
| 33 |
+
location: The city name or location.
|
| 34 |
|
| 35 |
Returns:
|
| 36 |
A string containing the current weather conditions for the specified location.
|
| 37 |
"""
|
| 38 |
try:
|
| 39 |
+
# Format the location for the search query
|
| 40 |
+
search_query = location.replace(" ", "+")
|
| 41 |
+
url = f"https://www.google.com/search?q=weather+{search_query}"
|
| 42 |
|
| 43 |
+
headers = {
|
| 44 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
+
response = requests.get(url, headers=headers)
|
| 48 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 49 |
+
|
| 50 |
+
# Extract weather information
|
| 51 |
+
temp = soup.find("span", class_="wob_t").text
|
| 52 |
+
condition = soup.find("div", class_="wob_dcp").text
|
| 53 |
+
|
| 54 |
+
return f"The current weather in {location} is {temp}°C with {condition}."
|
|
|
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
+
return f"Could not retrieve weather for {location}. Error: {str(e)}"
|
| 58 |
|
| 59 |
@tool
|
| 60 |
def get_current_time_in_timezone(timezone: str) -> str:
|