Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
36 |
-
"""A tool that fetches the current local time
|
37 |
|
38 |
Args:
|
39 |
-
|
40 |
|
41 |
Returns:
|
42 |
-
A string displaying the local
|
43 |
"""
|
44 |
try:
|
45 |
-
#
|
46 |
-
|
47 |
-
|
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(
|
63 |
local_time = datetime.datetime.now(tz)
|
64 |
|
65 |
-
#
|
66 |
-
|
|
|
67 |
|
68 |
-
|
|
|
69 |
|
70 |
-
|
71 |
-
|
|
|
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
|