Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,24 +12,27 @@ from Gradio_UI import GradioUI
|
|
| 12 |
def get_weather(city: str) -> str:
|
| 13 |
"""Fetches current weather information for a specified city.
|
| 14 |
Args:
|
| 15 |
-
city: Name of the city (e.g., '
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return "OPENWEATHERMAP_API_KEY key not found"
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
data = response.json()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
return f"Current weather in {city}: {temp}°C, {condition}."
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
return f"
|
| 33 |
|
| 34 |
@tool
|
| 35 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -68,7 +71,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 68 |
|
| 69 |
agent = CodeAgent(
|
| 70 |
model=model,
|
| 71 |
-
tools=[get_current_time_in_timezone, get_weather, final_answer],
|
| 72 |
max_steps=6,
|
| 73 |
verbosity_level=1,
|
| 74 |
grammar=None,
|
|
|
|
| 12 |
def get_weather(city: str) -> str:
|
| 13 |
"""Fetches current weather information for a specified city.
|
| 14 |
Args:
|
| 15 |
+
city: Name of the city (e.g., 'London')
|
| 16 |
"""
|
| 17 |
+
api_key = os.getenv("OPENWEATHERMAP_API_KEY")
|
| 18 |
+
if not api_key:
|
| 19 |
+
return "API key not found. Make sure OPENWEATHERMAP_API_KEY is set in Hugging Face secrets."
|
|
|
|
| 20 |
|
| 21 |
+
try:
|
| 22 |
+
url = f"http://api.openweathermap.org/data/2.5/weather"
|
| 23 |
+
params = {"q": city, "appid": api_key, "units": "metric"}
|
| 24 |
+
response = requests.get(url, params=params)
|
| 25 |
data = response.json()
|
| 26 |
+
|
| 27 |
+
if response.status_code != 200:
|
| 28 |
+
return f"Error: {data.get('message', 'Unknown error')}"
|
| 29 |
+
|
| 30 |
+
temp = data["main"]["temp"]
|
| 31 |
+
condition = data["weather"][0]["description"]
|
| 32 |
return f"Current weather in {city}: {temp}°C, {condition}."
|
| 33 |
+
|
| 34 |
except Exception as e:
|
| 35 |
+
return f"An error occurred while fetching weather data: {str(e)}"
|
| 36 |
|
| 37 |
@tool
|
| 38 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 71 |
|
| 72 |
agent = CodeAgent(
|
| 73 |
model=model,
|
| 74 |
+
tools=[get_current_time_in_timezone, get_weather, final_answer],
|
| 75 |
max_steps=6,
|
| 76 |
verbosity_level=1,
|
| 77 |
grammar=None,
|