jaidesign commited on
Commit
8346f0e
·
verified ·
1 Parent(s): 97ba0b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -19
app.py CHANGED
@@ -1,10 +1,12 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
 
 
 
2
  import datetime
3
  import requests
4
  import re
5
  import pytz
6
  import yaml
7
- from tools.final_answer import FinalAnswerTool
8
 
9
  from Gradio_UI import GradioUI
10
 
@@ -31,46 +33,67 @@ def get_stock_price(stock_symbol: str) -> str:
31
 
32
 
33
  @tool
34
- def get_current_time_in_timezone(timezone: str) -> str:
35
- """A tool that fetches the current local time in a specified timezone.
36
 
37
  Args:
38
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
39
 
40
  Returns:
41
- A string displaying the local date and time in the specified timezone.
42
  """
43
  try:
44
- # Validate timezone
45
- if timezone not in pytz.all_timezones:
46
- return f" Error: '{timezone}' is not a valid timezone. Please provide a correct timezone (e.g., 'America/New_York')."
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  # Get the current time in the specified timezone
49
- tz = pytz.timezone(timezone)
50
  local_time = datetime.datetime.now(tz)
51
 
52
  # Format the output with day, date, and time
53
  formatted_time = local_time.strftime("%A, %Y-%m-%d %H:%M:%S %Z")
54
 
55
- return f"The current local time in **{timezone}** is: **{formatted_time}**"
56
 
57
  except Exception as e:
58
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
59
 
60
-
61
  @tool
62
- def get_weather(location: str = "auto") -> str:
63
- """Fetches the current weather for a specified location without using an API key.
64
 
65
  Args:
66
  location: A string representing a valid city name (e.g., 'New York').
67
- Defaults to 'auto' to detect the location automatically.
68
 
69
  Returns:
70
- A string describing the weather conditions.
71
  """
72
 
73
- BASE_URL = f"https://wttr.in/{location}?format=%C+%t+%h"
 
 
 
 
 
 
 
 
74
 
75
  try:
76
  response = requests.get(BASE_URL)
@@ -78,8 +101,12 @@ def get_weather(location: str = "auto") -> str:
78
 
79
  # Split the response into separate parts
80
  weather_data = response.text.strip().split("|")
81
-
82
- return f"The current Weather in {location} is: {weather_data[0]}, Temperature: {weather_data[1]}, Humidity: {weather_data[2]}"
 
 
 
 
83
 
84
  except requests.exceptions.RequestException as e:
85
  return f"Error fetching weather data: {str(e)}"
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ from geopy.geocoders import Nominatim
3
+ from timezonefinder import TimezoneFinder
4
+ from tools.final_answer import FinalAnswerTool
5
  import datetime
6
  import requests
7
  import re
8
  import pytz
9
  import yaml
 
10
 
11
  from Gradio_UI import GradioUI
12
 
 
33
 
34
 
35
  @tool
36
+ def get_current_time_in_location(location: str) -> str:
37
+ """A tool that fetches the current local time for a specified location (city or zip code).
38
 
39
  Args:
40
+ location: A string representing a city or zip code (e.g., 'New York', '90210').
41
 
42
  Returns:
43
+ A string displaying the local date and time in the specified location.
44
  """
45
  try:
46
+ # Initialize geolocator and timezone finder
47
+ geolocator = Nominatim(user_agent="time-zone-fetcher")
48
+ timezone_finder = TimezoneFinder()
49
+
50
+ # Get location coordinates (latitude and longitude)
51
+ location_obj = geolocator.geocode(location)
52
+ if not location_obj:
53
+ return f"Error: Location '{location}' could not be found. Please provide a valid city or zip code."
54
+
55
+ latitude, longitude = location_obj.latitude, location_obj.longitude
56
+
57
+ # Get the timezone for the location
58
+ timezone_str = timezone_finder.timezone_at(lng=longitude, lat=latitude)
59
+ if timezone_str is None:
60
+ return f"Error: Could not determine the timezone for location '{location}'."
61
 
62
  # Get the current time in the specified timezone
63
+ tz = pytz.timezone(timezone_str)
64
  local_time = datetime.datetime.now(tz)
65
 
66
  # Format the output with day, date, and time
67
  formatted_time = local_time.strftime("%A, %Y-%m-%d %H:%M:%S %Z")
68
 
69
+ return f"The current local time in **{location}** ({timezone_str}) is: **{formatted_time}**"
70
 
71
  except Exception as e:
72
+ return f"Error fetching time for location '{location}': {str(e)}"
73
+
74
+
75
 
 
76
  @tool
77
+ def get_weather(location: str = None, zipcode: int = None) -> str:
78
+ """Fetches the current weather for a specified city or zip code without using an API key.
79
 
80
  Args:
81
  location: A string representing a valid city name (e.g., 'New York').
82
+ zipcode: An integer representing a valid zip code (e.g., 10001 for NYC).
83
 
84
  Returns:
85
+ A string describing the weather conditions, temperature, and humidity in one line.
86
  """
87
 
88
+ # Determine the correct location (city or zip code)
89
+ if location:
90
+ query = location
91
+ elif zipcode:
92
+ query = str(zipcode)
93
+ else:
94
+ return "Error: Please provide either a city name or zip code."
95
+
96
+ BASE_URL = f"https://wttr.in/{query}?format=%C|%t|%h"
97
 
98
  try:
99
  response = requests.get(BASE_URL)
 
101
 
102
  # Split the response into separate parts
103
  weather_data = response.text.strip().split("|")
104
+
105
+ if len(weather_data) != 3:
106
+ return "Error: Unexpected response format"
107
+
108
+ # Return the weather data in one line, comma separated
109
+ return f"The current Weather in {query}is: {weather_data[0]}, Temperature: {weather_data[1]}, Humidity: {weather_data[2]}"
110
 
111
  except requests.exceptions.RequestException as e:
112
  return f"Error fetching weather data: {str(e)}"