Hanlu2024 commited on
Commit
a03a59f
·
verified ·
1 Parent(s): 5bcbd8e

Update app.py

Browse files

add new feature

Files changed (1) hide show
  1. app.py +49 -8
app.py CHANGED
@@ -7,16 +7,57 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  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 my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +96,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer,my_custom_tool], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
7
 
8
  from Gradio_UI import GradioUI
9
 
10
+ # Below is an example of a custom weather tool
11
  @tool
12
+ def get_weather_info(city: str) -> str:
13
+ """Fetches current weather information for a specified city using the Open-Meteo API (no API key required).
 
14
  Args:
15
+ city: The name of the city to get weather information for (e.g., 'London', 'New York', 'Tokyo').
 
16
  """
17
+ try:
18
+ # First, get coordinates for the city using the geocoding API
19
+ geocoding_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"
20
+ geo_response = requests.get(geocoding_url)
21
+ geo_data = geo_response.json()
22
+
23
+ if 'results' not in geo_data or len(geo_data['results']) == 0:
24
+ return f"Error: Could not find coordinates for city '{city}'. Please check the city name."
25
+
26
+ # Extract latitude and longitude
27
+ latitude = geo_data['results'][0]['latitude']
28
+ longitude = geo_data['results'][0]['longitude']
29
+ city_name = geo_data['results'][0]['name']
30
+ country = geo_data['results'][0].get('country', 'Unknown')
31
+
32
+ # Get weather data
33
+ weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current_weather=true&temperature_unit=celsius"
34
+ weather_response = requests.get(weather_url)
35
+ weather_data = weather_response.json()
36
+
37
+ if 'current_weather' not in weather_data:
38
+ return f"Error: Could not fetch weather data for {city_name}."
39
+
40
+ current = weather_data['current_weather']
41
+ temperature = current['temperature']
42
+ windspeed = current['windspeed']
43
+ weather_code = current['weathercode']
44
+
45
+ # Map weather codes to descriptions
46
+ weather_descriptions = {
47
+ 0: "Clear sky", 1: "Mainly clear", 2: "Partly cloudy", 3: "Overcast",
48
+ 45: "Foggy", 48: "Depositing rime fog", 51: "Light drizzle", 53: "Moderate drizzle",
49
+ 55: "Dense drizzle", 61: "Slight rain", 63: "Moderate rain", 65: "Heavy rain",
50
+ 71: "Slight snow", 73: "Moderate snow", 75: "Heavy snow", 80: "Slight rain showers",
51
+ 81: "Moderate rain showers", 82: "Violent rain showers", 95: "Thunderstorm",
52
+ 96: "Thunderstorm with slight hail", 99: "Thunderstorm with heavy hail"
53
+ }
54
+
55
+ weather_desc = weather_descriptions.get(weather_code, "Unknown weather")
56
+
57
+ return f"Weather in {city_name}, {country}:\n- Temperature: {temperature}°C\n- Conditions: {weather_desc}\n- Wind Speed: {windspeed} km/h"
58
+
59
+ except Exception as e:
60
+ return f"Error fetching weather for '{city}': {str(e)}"
61
 
62
  @tool
63
  def get_current_time_in_timezone(timezone: str) -> str:
 
96
 
97
  agent = CodeAgent(
98
  model=model,
99
+ tools=[get_weather_info, get_current_time_in_timezone, final_answer], ## add your tools here (don't remove final answer)
100
  max_steps=6,
101
  verbosity_level=1,
102
  grammar=None,