smainye commited on
Commit
82611be
·
verified ·
1 Parent(s): b51bd91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -35
app.py CHANGED
@@ -9,47 +9,34 @@ from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def fetch_weather(city: str) -> str:
13
- """Fetches current weather data for any city"""
 
 
14
  api_key = OPEN_WEATHER_TOKEN # Replace with your own key
15
 
16
  try:
17
- # First get coordinates for the city
18
- geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={api_key}"
19
- geo_response = requests.get(geo_url)
20
- geo_response.raise_for_status()
 
21
 
22
- geo_data = geo_response.json()
23
- if not geo_data:
24
- return f"City '{city}' not found"
25
-
26
- lat = geo_data[0]['lat']
27
- lon = geo_data[0]['lon']
28
-
29
- # Then get weather data
30
- weather_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}&units=metric"
31
- weather_response = requests.get(weather_url)
32
- weather_response.raise_for_status()
33
-
34
- data = weather_response.json()
35
-
36
- # Extract weather information
37
- temperature = data['main']['temp']
38
- feels_like = data['main']['feels_like']
39
- humidity = data['main']['humidity']
40
- weather_desc = data['weather'][0]['description'].title()
41
- wind_speed = data['wind']['speed']
42
-
43
- return (
44
- f"🌤️ Weather in {city.title()}:\n\n"
45
- f"🌡️ Temperature: {temperature}°C (Feels like {feels_like}°C)\n"
46
- f"☁️ Conditions: {weather_desc}\n"
47
- f"💧 Humidity: {humidity}%\n"
48
- f"🌬️ Wind: {wind_speed} m/s"
49
  )
50
-
 
51
  except requests.exceptions.RequestException as e:
52
- return f"⚠️ Error: {str(e)}"
 
 
53
 
54
  @tool
55
  def get_current_time_in_timezone(timezone: str) -> str:
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
+ def get_nairobi_weather() -> str:
13
+ """Fetches and returns current weather data for Nairobi"""
14
+ # Nairobi coordinates
15
+ lat, lon = -1.286389, 36.817223
16
  api_key = OPEN_WEATHER_TOKEN # Replace with your own key
17
 
18
  try:
19
+ # Fetch weather data
20
+ url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}&units=metric"
21
+ response = requests.get(url)
22
+ response.raise_for_status()
23
+ data = response.json()
24
 
25
+ # Format the weather report
26
+ report = (
27
+ f"🌍 Location: Nairobi, Kenya\n"
28
+ f"🌡️ Temperature: {data['main']['temp']}°C (Feels like {data['main']['feels_like']}°C)\n"
29
+ f"☁️ Conditions: {data['weather'][0]['description'].title()}\n"
30
+ f"💧 Humidity: {data['main']['humidity']}%\n"
31
+ f"🌬️ Wind: {data['wind']['speed']} m/s\n"
32
+ f"📊 Pressure: {data['main']['pressure']} hPa"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  )
34
+ return report
35
+
36
  except requests.exceptions.RequestException as e:
37
+ return f"⚠️ Error fetching data: {str(e)}"
38
+ except KeyError:
39
+ return "⚠️ Error processing weather data"
40
 
41
  @tool
42
  def get_current_time_in_timezone(timezone: str) -> str: