aniketkno commited on
Commit
fc1bcb0
·
verified ·
1 Parent(s): ae7a494
Files changed (1) hide show
  1. app.py +80 -0
app.py CHANGED
@@ -33,6 +33,86 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
 
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
+ @tool
37
+ def get_weather_forecast(city_name):
38
+ """A tool that fetches the current weather of a specified city.
39
+ Args:
40
+ city: A string representing a valid city (e.g., 'Bangalore').
41
+ """
42
+
43
+ latitude, longitude = get_coordinates_no_api_key(city_name)
44
+
45
+ base_url = "https://api.open-meteo.com/v1/forecast" # No API key needed for this version
46
+ params = {
47
+ "latitude": latitude,
48
+ "longitude": longitude,
49
+ "hourly": "temperature_2m,precipitation,wind_speed",
50
+ "daily": "temperature_2m_max,temperature_2m_min,precipitation_sum",
51
+ "forecast_days": 7,
52
+ "timezone": "auto"
53
+ }
54
+
55
+ try:
56
+ response = requests.get(base_url, params=params) # No headers needed
57
+ response.raise_for_status()
58
+ weather_data = response.json()
59
+ return weather_data
60
+ except requests.exceptions.RequestException as e:
61
+ print(f"Error fetching weather data: {e}")
62
+ return None
63
+ except json.JSONDecodeError as e:
64
+ print(f"Error decoding JSON response: {e}")
65
+ return None
66
+
67
+ def get_coordinates_no_api_key(city_name):
68
+ """Gets coordinates using OpenStreetMap's Nominatim (no API key, but with limitations)."""
69
+
70
+ # This approach is less reliable and might have rate limits.
71
+ # It's suitable for basic use cases but not for production.
72
+
73
+ geocoding_url = "https://nominatim.openstreetmap.org/search" # No API Key needed
74
+ params = {
75
+ "q": city_name,
76
+ "format": "json",
77
+ "limit": 1
78
+ }
79
+
80
+ try:
81
+ response = requests.get(geocoding_url, params=params)
82
+ response.raise_for_status()
83
+ geocoding_data = response.json()
84
+
85
+ if geocoding_data: # Check if any results were found
86
+ latitude = float(geocoding_data[0]["lat"])
87
+ longitude = float(geocoding_data[0]["lon"])
88
+ return latitude, longitude
89
+ else:
90
+ print(f"Could not find coordinates for {city_name}")
91
+ return None, None
92
+
93
+ except requests.exceptions.RequestException as e:
94
+ print(f"Error during geocoding: {e}")
95
+ return None, None
96
+ except (KeyError, IndexError, ValueError) as e: # Handle more potential errors
97
+ print(f"Error parsing geocoding response: {e}")
98
+ return None, None
99
+
100
+
101
+
102
+ def get_weather(city: str) -> str:
103
+ """A tool that fetches the current weather of a specified city.
104
+ Args:
105
+ city: A string representing a valid city (e.g., 'Bangalore').
106
+ """
107
+ try:
108
+ response = requests.get('https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m&models=bom_access_global')
109
+ weatherJSON = response.json()
110
+ cityName = weatherJSON.get('location').get('name')
111
+ cityTemp = weatherJSON.get('current').get('temp_c')
112
+ cityCondition = weatherJSON.get('current').get('condition').get('text')
113
+ return f"The current Temperature in {cityName} is {cityTemp} and conditions are {cityCondition}"
114
+ except Exception as e:
115
+ return f"Error fetching weather conditions for {city}"
116
 
117
  final_answer = FinalAnswerTool()
118