jaidesign commited on
Commit
0e7819b
·
verified ·
1 Parent(s): b0cb86d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -28
app.py CHANGED
@@ -1,12 +1,12 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
- from timezonefinder import TimezoneFinder
3
- from tools.final_answer import FinalAnswerTool
4
  import datetime
5
  import requests
6
  import re
7
  import pytz
8
  import yaml
9
 
 
 
10
  from Gradio_UI import GradioUI
11
 
12
 
@@ -30,46 +30,40 @@ def get_stock_price(stock_symbol: str) -> str:
30
 
31
  return current_price
32
 
33
-
34
  @tool
35
- def get_current_time_in_location(location: str) -> str:
36
- """A tool that fetches the current local time for a specified location (city or zip code).
37
 
38
  Args:
39
- location: A string representing a city or zip code (e.g., 'New York', '90210').
40
 
41
  Returns:
42
- A string displaying the local date and time in the specified location.
43
  """
44
  try:
45
- # Initialize geolocator and timezone finder
46
- geolocator = Nominatim(user_agent="time-zone-fetcher")
47
- timezone_finder = TimezoneFinder()
48
-
49
- # Get location coordinates (latitude and longitude)
50
- location_obj = geolocator.geocode(location)
51
- if not location_obj:
52
- return f"Error: Location '{location}' could not be found. Please provide a valid city or zip code."
53
-
54
- latitude, longitude = location_obj.latitude, location_obj.longitude
55
-
56
- # Get the timezone for the location
57
- timezone_str = timezone_finder.timezone_at(lng=longitude, lat=latitude)
58
- if timezone_str is None:
59
- return f"Error: Could not determine the timezone for location '{location}'."
60
 
61
  # Get the current time in the specified timezone
62
- tz = pytz.timezone(timezone_str)
63
  local_time = datetime.datetime.now(tz)
64
 
65
- # Format the output with day, date, and time
66
- formatted_time = local_time.strftime("%A, %Y-%m-%d %H:%M:%S %Z")
 
67
 
68
- return f"The current local time in **{location}** ({timezone_str}) is: **{formatted_time}**"
 
69
 
70
- except Exception as e:
71
- return f"Error fetching time for location '{location}': {str(e)}"
 
72
 
 
 
 
 
73
 
74
 
75
  @tool
 
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
 
8
+ from tools.final_answer import FinalAnswerTool
9
+
10
  from Gradio_UI import GradioUI
11
 
12
 
 
30
 
31
  return current_price
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 time and timezone abbreviation (like 'EST' or 'GMT-5') in the specified location.
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
+ # Get the timezone abbreviation (e.g., 'EST', 'PDT') and UTC offset (e.g., 'GMT-5')
53
+ tz_abbr = local_time.strftime('%Z') # Timezone abbreviation (e.g., 'EST')
54
+ utc_offset = local_time.strftime('%z') # UTC offset (e.g., '+0000')
55
 
56
+ # Format the UTC offset for display (e.g., 'GMT-5')
57
+ formatted_offset = f"GMT{int(utc_offset[:3]):+d}{int(utc_offset[0:3] + utc_offset[3:5]):02d}"
58
 
59
+ # Format the output with the location name, time, and timezone abbreviation and UTC offset
60
+ formatted_time = local_time.strftime("%Y-%m-%d %H:%M:%S")
61
+ location_name = timezone.split('/')[-1].replace('_', ' ')
62
 
63
+ return f"{location_name}: {formatted_time} {tz_abbr} {formatted_offset}"
64
+
65
+ except Exception as e:
66
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
67
 
68
 
69
  @tool