TouradAi commited on
Commit
16a6aef
·
verified ·
1 Parent(s): 992655d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -7,28 +7,41 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- WEATHER_API_KEY = "52305581a21f4c9f93d144513250109"
11
 
 
 
 
 
 
 
12
 
13
  @tool
14
  def get_weather(city:str)-> str:
15
- """ A tool that fetches the current weather for a given city.
 
 
16
  Args:
17
- city (str): The name of the city (e.g., "Paris").
 
18
  Returns:
19
- str: A description of the weather.
20
  """
21
-
22
  try:
23
- url = f"http://api.weatherapi.com/v1/current.json?key={WEATHER_API_KEY}&q={city}"
 
 
 
 
24
  response = requests.get(url)
25
- if response.status.code == 200:
26
- data = response.json()
27
- temp = data["current"]["temp_c"]
28
- condition = data["current"]["condtion"]["text"]
29
- return f"The current weather in {city} is {temp}°C, {condition}"
30
- else:
31
- return f"Error fetching weather for {city} (status: {response.status.code})"
 
 
32
  except Exception as e:
33
  return f"Error: {str(e)}"
34
 
 
7
 
8
  from Gradio_UI import GradioUI
9
 
 
10
 
11
+ CITY_COORDINATES = {
12
+ "Paris": (48.8566, 2.3522),
13
+ "Dakar": (14.6928, -17.4467),
14
+ "New York": (40.7128, -74.0060),
15
+ "Tokyo": (35.6895, 139.6917),
16
+ }
17
 
18
  @tool
19
  def get_weather(city:str)-> str:
20
+ """
21
+ Fetch the current weather for a given city using Open-Meteo.
22
+
23
  Args:
24
+ city (str): Name of the city (must be in CITY_COORDINATES)
25
+
26
  Returns:
27
+ str: Text summary of the current temperature and wind speed.
28
  """
 
29
  try:
30
+ if city not in CITY_COORDINATES:
31
+ return f"City '{city}' not found in coordinates table."
32
+
33
+ lat, lon = CITY_COORDINATES[city]
34
+ url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current_weather=true"
35
  response = requests.get(url)
36
+ if response.status_code != 200:
37
+ return f"Error fetching weather for {city} (status: {response.status_code})"
38
+
39
+ data = response.json()
40
+ weather = data["current_weather"]
41
+ temp = weather["temperature"]
42
+ wind = weather["windspeed"]
43
+ return f"The current weather in {city}: {temp}°C, wind {wind} km/h"
44
+
45
  except Exception as e:
46
  return f"Error: {str(e)}"
47