cjb97 commited on
Commit
70cddf8
·
1 Parent(s): 7dc53ba

Remove duplicate weather tool definition from app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -50
app.py CHANGED
@@ -1,58 +1,9 @@
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool
2
- from smolagents.tools import tool
3
- import datetime
4
- import requests
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from tools.weather import get_weather
8
  from Gradio_UI import GradioUI
9
- import os # Added for accessing environment variables
10
- from typing import Any, Optional
11
- import re
12
-
13
- @tool("get_weather") # Add the tool name explicitly
14
- def get_weather(location: str) -> str:
15
- """Get the current weather for a specified location.
16
- Args:
17
- location: A string representing a city (e.g., 'New York', 'Paris').
18
- Returns:
19
- str: A string containing the current weather information.
20
- """
21
- # Validate that location contains only allowed characters: letters, digits, spaces, and hyphens
22
- if not re.fullmatch(r'[A-Za-z0-9\s-]+', location):
23
- return "Error: Location contains invalid characters. Only letters, digits, spaces, and hyphens are allowed."
24
- try:
25
- # Get the API key from environment variables
26
- api_key = os.getenv('OPENWEATHERMAP_API_KEY')
27
- if not api_key:
28
- return "Error: OPENWEATHERMAP_API_KEY not set in environment variables."
29
-
30
- # First, geocode the location to get latitude and longitude
31
- geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={location}&limit=1&appid={api_key}"
32
- geo_response = requests.get(geo_url)
33
- geo_data = geo_response.json()
34
- if not geo_data:
35
- return f"Error: Could not geocode location '{location}'."
36
- lat = geo_data[0].get('lat')
37
- lon = geo_data[0].get('lon')
38
- if lat is None or lon is None:
39
- return f"Error: Latitude or longitude not found for location '{location}'."
40
-
41
- # Call the OpenWeatherMap weather API 2.5 endpoint
42
- weather_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}&units=imperial"
43
- weather_response = requests.get(weather_url)
44
- weather_data = weather_response.json()
45
-
46
- if weather_response.status_code == 200:
47
- temp = weather_data.get('main', {}).get('temp', 'N/A')
48
- weather_desc = weather_data.get('weather', [{}])[0].get('description', 'N/A')
49
- humidity = weather_data.get('main', {}).get('humidity', 'N/A')
50
- wind_speed = weather_data.get('wind', {}).get('speed', 'N/A')
51
- return f"The current weather in {location} is {temp}°F with {weather_desc}. Humidity: {humidity}%, Wind speed: {wind_speed} mph."
52
- else:
53
- return f"Error fetching weather for {location}: {weather_data.get('message', 'Unknown error')}"
54
- except Exception as e:
55
- return f"Error fetching weather for {location}: {str(e)}"
56
 
57
  # Initialize tools
58
  final_answer = FinalAnswerTool()
 
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool
 
 
 
2
  import yaml
3
  from tools.final_answer import FinalAnswerTool
4
  from tools.weather import get_weather
5
  from Gradio_UI import GradioUI
6
+ import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Initialize tools
9
  final_answer = FinalAnswerTool()