gugukaka commited on
Commit
2e4d0ba
·
verified ·
1 Parent(s): 12c0855

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -12
app.py CHANGED
@@ -12,14 +12,6 @@ import requests
12
  import pytz
13
  import yaml
14
 
15
- @tool
16
- def my_custom_tool(arg1: str, arg2: int) -> str:
17
- """A tool that does nothing yet
18
- Args:
19
- arg1: the first argument
20
- arg2: the second argument
21
- """
22
- return "What magic will you build ?"
23
 
24
  @tool
25
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -34,6 +26,57 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  except Exception as e:
35
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  final_answer = FinalAnswerTool()
38
 
39
  model = HfApiModel(
@@ -43,7 +86,6 @@ model = HfApiModel(
43
  custom_role_conversions=None,
44
  )
45
 
46
- # Load image generation tool correctly (only once)
47
  image_generation_tool = load_tool("m-ric/text-to-image", trust_remote_code=True)
48
 
49
  with open("prompts.yaml", 'r') as stream:
@@ -53,9 +95,10 @@ agent = CodeAgent(
53
  model=model,
54
  tools=[
55
  final_answer,
56
- image_generation_tool, # ✅ image tool
57
- get_current_time_in_timezone, # ✅ custom tools
58
- my_custom_tool,
 
59
  ],
60
  max_steps=6,
61
  verbosity_level=1,
 
12
  import pytz
13
  import yaml
14
 
 
 
 
 
 
 
 
 
15
 
16
  @tool
17
  def get_current_time_in_timezone(timezone: str) -> str:
 
26
  except Exception as e:
27
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
28
 
29
+
30
+ @tool
31
+ def get_current_weather(location: str) -> str:
32
+ """A tool that fetches the current weather for a given city/location.
33
+ Args:
34
+ location: The name of the city or location (e.g., 'Ho Chi Minh City').
35
+ """
36
+ try:
37
+ # Geocode
38
+ geo_response = requests.get(
39
+ f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=1"
40
+ ).json()
41
+
42
+ if not geo_response.get("results"):
43
+ return f"Could not find location: {location}"
44
+
45
+ result = geo_response["results"][0]
46
+ lat, lon = result["latitude"], result["longitude"]
47
+ city_name = result["name"]
48
+ country = result.get("country", "")
49
+
50
+ # Fetch weather
51
+ weather_response = requests.get(
52
+ f"https://api.open-meteo.com/v1/forecast?"
53
+ f"latitude={lat}&longitude={lon}"
54
+ f"&current_weather=true"
55
+ ).json()
56
+ current = weather_response["current_weather"]
57
+
58
+ weather_codes = {
59
+ 0: "Clear sky", 1: "Mainly clear", 2: "Partly cloudy", 3: "Overcast",
60
+ 45: "Foggy", 48: "Icy fog", 51: "Light drizzle", 53: "Moderate drizzle",
61
+ 61: "Slight rain", 63: "Moderate rain", 65: "Heavy rain",
62
+ 71: "Slight snow", 73: "Moderate snow", 75: "Heavy snow",
63
+ 80: "Slight showers", 81: "Moderate showers", 82: "Violent showers",
64
+ 95: "Thunderstorm", 96: "Thunderstorm with hail",
65
+ }
66
+ description = weather_codes.get(current["weathercode"], "Unknown")
67
+
68
+ return (
69
+ f"Weather in {city_name}, {country}:\n"
70
+ f" 🌡️ Temperature : {current['temperature']}°C\n"
71
+ f" 💨 Wind Speed : {current['windspeed']} km/h\n"
72
+ f" 🧭 Wind Direction : {current['winddirection']}°\n"
73
+ f" 🌤️ Condition : {description}\n"
74
+ )
75
+
76
+ except Exception as e:
77
+ return f"Error fetching weather for '{location}': {str(e)}"
78
+
79
+
80
  final_answer = FinalAnswerTool()
81
 
82
  model = HfApiModel(
 
86
  custom_role_conversions=None,
87
  )
88
 
 
89
  image_generation_tool = load_tool("m-ric/text-to-image", trust_remote_code=True)
90
 
91
  with open("prompts.yaml", 'r') as stream:
 
95
  model=model,
96
  tools=[
97
  final_answer,
98
+ image_generation_tool,
99
+ get_current_time_in_timezone,
100
+ get_current_weather, # ✅ new weather tool
101
+ DuckDuckGoSearchTool(),
102
  ],
103
  max_steps=6,
104
  verbosity_level=1,