ddavydov commited on
Commit
dc7eb56
·
verified ·
1 Parent(s): 40390d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
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., 'Paris').
16
  """
17
- try:
18
- api_key = os.getenv("OPENWEATHERMAP_API_KEY")
19
- if not api_key:
20
- return "OPENWEATHERMAP_API_KEY key not found"
21
 
22
- response = requests.get(
23
- f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
24
- )
 
25
  data = response.json()
26
- if data.get("cod") != 200:
27
- return f"Could not fetch weather: {data.get('message', 'Unknown error')}"
28
- temp = data['main']['temp']
29
- condition = data['weather'][0]['description']
 
 
30
  return f"Current weather in {city}: {temp}°C, {condition}."
 
31
  except Exception as e:
32
- return f"Error fetching weather: {str(e)}"
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], ## add your tools here (don't remove 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,