Spoon-assassin commited on
Commit
5b41d86
·
verified ·
1 Parent(s): a90113c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -7
app.py CHANGED
@@ -4,20 +4,113 @@ import requests
4
  import pytz
5
  import yaml
6
  import os
 
7
  from tools.final_answer import FinalAnswerTool
8
 
9
  from Gradio_UI import GradioUI
10
 
11
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  @tool
13
- def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
14
- #Keep this format for the description / args / args description but feel free to modify the tool
15
- """A tool that does nothing yet
 
16
  Args:
17
- arg1: the first argument
18
- arg2: the second argument
 
 
 
 
19
  """
20
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  @tool
23
  def get_current_time_in_timezone(timezone: str) -> str:
 
4
  import pytz
5
  import yaml
6
  import os
7
+ from geopy.geocoders import Nominatim
8
  from tools.final_answer import FinalAnswerTool
9
 
10
  from Gradio_UI import GradioUI
11
 
12
+ def date_to_friendly_format(date_string):
13
+ """
14
+ Converts a date string to a more readable
15
+ """
16
+ date_obj = datetime.strptime(date_string, "%Y-%m-%d")
17
+ friendly_date = date_obj.strftime("%a %b %d %Y")
18
+ return friendly_date
19
+
20
+ def meteo_weathercode_to_emoji(weathercode):
21
+ """
22
+ Converts a weather code to a corresponding emoji.
23
+ """
24
+ emoji = {
25
+ 0: "☀️",
26
+ 1: "🌤️",
27
+ 2: "⛅️",
28
+ 3: "☁️",
29
+ 45: "🌫️",
30
+ 48: "🌫️",
31
+ 51: "🌦️",
32
+ 53: "🌦️",
33
+ 55: "🌧️",
34
+ 56: "🌧️",
35
+ 57: "🌧️",
36
+ 61: "🌧️",
37
+ 63: "🌧️",
38
+ 65: "🌧️",
39
+ 66: "🌧️",
40
+ 67: "🌧️",
41
+ 71: "🌩️",
42
+ 73: "🌩️",
43
+ 75: "🌩️",
44
+ 77: "⛈️",
45
+ 80: "🌦️",
46
+ 81: "🌦️",
47
+ 82: "🌦️",
48
+ 85: "🌧️",
49
+ 86: "🌧️",
50
+ 95: "🌫️",
51
+ 96: "🌫️",
52
+ 99: "🌫️"
53
+ }
54
+ return emoji.get(weathercode, "❓")
55
+
56
+ def get_city_coordinates(city_name: str) -> float, float:
57
+ """
58
+ Finds the GPS coordinates (latitude, longitude) of a city.
59
+
60
+ Args:
61
+ city_name: A string representing the name of a city (e.g., 'London').
62
+
63
+ Returns:
64
+ A tuple containing the latitude and longitude (floats), or None if the city is not found.
65
+ """
66
+ geolocator = Nominatim(user_agent="city_coordinates_finder_andyh") # Important: Provide a user agent
67
+ location = geolocator.geocode(city_name)
68
+
69
+ if location:
70
+ latitude = location.latitude
71
+ longitude = location.longitude
72
+ return latitude, longitude
73
+ else:
74
+ print(f"Error: Could not find coordinates for '{city_name}'.") # Indicate failure
75
+ return None
76
+
77
  @tool
78
+ def get_weather_forecast(city: str, days: str=3):
79
+ """
80
+ Retrieves a weather forecast for a city over the next few days.
81
+
82
  Args:
83
+ city: a sting of the name of the city to get weather
84
+ days: The number of days to forecast (int, default is 3).
85
+
86
+ Returns:
87
+ A string with the following days forcast
88
+ Prints an error message if the API request fails.
89
  """
90
+
91
+ latitude, longitude = get_city_coordinates(city)
92
+
93
+ url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=weathercode,temperature_2m_max,temperature_2m_min,precipitation_sum&forecast_days={days}"
94
+
95
+ try:
96
+ response = requests.get(url)
97
+ response.raise_for_status()
98
+ forecast_data = response.json()
99
+ if "daily" in forecast_data:
100
+ output = []
101
+ daily_date = forecast_data["daily"]
102
+ for weather in zip(daily_date["time"], daily_date["weathercode"], daily_date["temperature_2m_max"], daily_date["temperature_2m_min"], daily_date["precipitation_sum"]):
103
+ output.append(f'{date_to_friendly_format(weather[0])}: ({meteo_weathercode_to_emoji(weather[1])}) High: {weather[2]}°C, Low: {weather[3]}°C, Rain: {weather[4]}mm')
104
+ return "\n".join(output)
105
+ else:
106
+ print("Error: No 'daily' data found in the API response.")
107
+ return None
108
+ except requests.exceptions.RequestException as e:
109
+ print(f"Error fetching forecast data: {e}")
110
+ return None
111
+ except (ValueError, KeyError) as e:
112
+ print(f"Error processing forecast data: {e}")
113
+ return None
114
 
115
  @tool
116
  def get_current_time_in_timezone(timezone: str) -> str: