YeeJun02 commited on
Commit
43b82ee
·
verified ·
1 Parent(s): 3e78df7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -3
app.py CHANGED
@@ -4,6 +4,7 @@ import datetime
4
  import pytz
5
  import math
6
  import os
 
7
  from deep_translator import GoogleTranslator
8
 
9
  # ============ TOOLS ============
@@ -21,6 +22,43 @@ def translator_tool(text: str, target_language: str) -> str:
21
  except Exception as e:
22
  return f"Translation error: {str(e)}. Make sure the language name is correct."
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  @tool
25
  def web_search_tool(query: str) -> str:
26
  """Search the web for current information using DuckDuckGo.
@@ -130,9 +168,9 @@ model = InferenceClientModel(
130
 
131
  agent = CodeAgent(
132
  model=model,
133
- tools=[web_search_tool, time_tool, calculator_tool, translator_tool],
134
  max_steps=5,
135
- additional_authorized_imports=['math', 'datetime', 'pytz', 'deep_translator']
136
  )
137
 
138
  # ============ GRADIO UI ============
@@ -156,7 +194,8 @@ demo = gr.ChatInterface(
156
  "Convert 150 pounds to kg",
157
  "Calculate the square root of 144 plus 50",
158
  "Translate 'Hello, how are you today?' into Japanese",
159
- "Search for the latest news on AI agents"
 
160
  ],
161
  )
162
 
 
4
  import pytz
5
  import math
6
  import os
7
+ import requests
8
  from deep_translator import GoogleTranslator
9
 
10
  # ============ TOOLS ============
 
22
  except Exception as e:
23
  return f"Translation error: {str(e)}. Make sure the language name is correct."
24
 
25
+ @tool
26
+ def weather_tool(location: str) -> str:
27
+ """Get the current weather for any location worldwide.
28
+
29
+ Args:
30
+ location: The name of the city or place (e.g., 'London' or 'Tokyo').
31
+ """
32
+ try:
33
+ # 1. Geocoding: Convert city name to coordinates using a free service
34
+ geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=1&language=en&format=json"
35
+ geo_res = requests.get(geo_url).json()
36
+
37
+ if not geo_res.get('results'):
38
+ return f"I couldn't find coordinates for '{location}'. Please check the spelling."
39
+
40
+ data = geo_res['results'][0]
41
+ lat, lon = data['latitude'], data['longitude']
42
+ city_full = f"{data.get('name')}, {data.get('country')}"
43
+
44
+ # 2. Weather: Get current data using coordinates
45
+ weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current=temperature_2m,relative_humidity_2m,apparent_temperature,weather_code,wind_speed_10m"
46
+ w_res = requests.get(weather_url).json()
47
+
48
+ curr = w_res['current']
49
+ temp = curr['temperature_2m']
50
+ feels = curr['apparent_temperature']
51
+ hum = curr['relative_humidity_2m']
52
+ wind = curr['wind_speed_10m']
53
+
54
+ return (f"🌤️ Weather for {city_full}:\n"
55
+ f"• Temperature: {temp}°C (Feels like {feels}°C)\n"
56
+ f"• Humidity: {hum}%\n"
57
+ f"• Wind Speed: {wind} km/h")
58
+ except Exception as e:
59
+ return f"Weather Error: {str(e)}"
60
+
61
+
62
  @tool
63
  def web_search_tool(query: str) -> str:
64
  """Search the web for current information using DuckDuckGo.
 
168
 
169
  agent = CodeAgent(
170
  model=model,
171
+ tools=[web_search_tool, time_tool, calculator_tool, translator_tool, weather_tool],
172
  max_steps=5,
173
+ additional_authorized_imports=['math', 'datetime', 'pytz', 'deep_translator', 'requests']
174
  )
175
 
176
  # ============ GRADIO UI ============
 
194
  "Convert 150 pounds to kg",
195
  "Calculate the square root of 144 plus 50",
196
  "Translate 'Hello, how are you today?' into Japanese",
197
+ "Search for the latest news on AI agents",
198
+ "What's the weather in Paris right now?", "How's the weather in New York compared to Tokyo?"
199
  ],
200
  )
201