Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
35 |
-
"""A tool that fetches the current local time
|
36 |
|
37 |
Args:
|
38 |
-
|
39 |
|
40 |
Returns:
|
41 |
-
A string displaying the local date and time in the specified
|
42 |
"""
|
43 |
try:
|
44 |
-
#
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# Get the current time in the specified timezone
|
49 |
-
tz = pytz.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 **{
|
56 |
|
57 |
except Exception as e:
|
58 |
-
return f"Error fetching time for
|
|
|
|
|
59 |
|
60 |
-
|
61 |
@tool
|
62 |
-
def get_weather(location: str =
|
63 |
-
"""Fetches the current weather for a specified
|
64 |
|
65 |
Args:
|
66 |
location: A string representing a valid city name (e.g., 'New York').
|
67 |
-
|
68 |
|
69 |
Returns:
|
70 |
-
A string describing the weather conditions.
|
71 |
"""
|
72 |
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
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)}"
|