Spaces:
Sleeping
Sleeping
Upload tool
Browse files- app.py +6 -0
- requirements.txt +2 -0
- tool.py +72 -0
app.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import launch_gradio_demo
|
| 2 |
+
from tool import GetCurrentDateAndTime
|
| 3 |
+
|
| 4 |
+
tool = GetCurrentDateAndTime()
|
| 5 |
+
|
| 6 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pytz
|
| 2 |
+
smolagents
|
tool.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
import pytz
|
| 4 |
+
import datetime
|
| 5 |
+
|
| 6 |
+
class GetCurrentDateAndTime(Tool):
|
| 7 |
+
name = "get_current_date_and_time"
|
| 8 |
+
description = "This is a tool that gets the current date, day of the week, and time for the specified timezone. Ask user for the timezone or location if not provided."
|
| 9 |
+
inputs = {'timezone': {'type': 'string', 'description': "Timezone (e.g., 'UTC', 'US/Pacific', 'Europe/London')", 'required': True}}
|
| 10 |
+
output_type = "string"
|
| 11 |
+
|
| 12 |
+
def _validate_timezone(self, timezone: str) -> Any:
|
| 13 |
+
"""
|
| 14 |
+
Validate and return a timezone object.
|
| 15 |
+
Returns None if timezone is invalid.
|
| 16 |
+
"""
|
| 17 |
+
# Import pytz locally
|
| 18 |
+
import pytz
|
| 19 |
+
|
| 20 |
+
if not isinstance(timezone, str):
|
| 21 |
+
raise ValueError("Timezone must be a string")
|
| 22 |
+
|
| 23 |
+
if not timezone.strip():
|
| 24 |
+
raise ValueError("Timezone cannot be empty")
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
return pytz.timezone(timezone)
|
| 28 |
+
except pytz.exceptions.UnknownTimeZoneError:
|
| 29 |
+
suggested_timezones = [
|
| 30 |
+
tz for tz in pytz.all_timezones
|
| 31 |
+
if timezone.lower() in tz.lower()
|
| 32 |
+
][:3]
|
| 33 |
+
|
| 34 |
+
error_msg = f"Invalid timezone: '{timezone}'"
|
| 35 |
+
if suggested_timezones:
|
| 36 |
+
error_msg += f". Did you mean one of these? {', '.join(suggested_timezones)}"
|
| 37 |
+
else:
|
| 38 |
+
error_msg += ". Please use a valid timezone identifier from the IANA Time Zone Database"
|
| 39 |
+
|
| 40 |
+
raise ValueError(error_msg)
|
| 41 |
+
|
| 42 |
+
def forward(self, timezone: str) -> str:
|
| 43 |
+
# Import datetime and pytz locally
|
| 44 |
+
from datetime import datetime
|
| 45 |
+
import pytz
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
# Validate and get timezone object
|
| 49 |
+
tz = self._validate_timezone(timezone)
|
| 50 |
+
|
| 51 |
+
# Get current datetime in the specified timezone
|
| 52 |
+
current_datetime = datetime.now(tz)
|
| 53 |
+
|
| 54 |
+
formatted_date = current_datetime.strftime("%Y-%m-%d")
|
| 55 |
+
formatted_time = current_datetime.strftime("%H:%M:%S")
|
| 56 |
+
day_of_week = current_datetime.strftime("%A")
|
| 57 |
+
|
| 58 |
+
return f"In {timezone}, it is {day_of_week}, {formatted_date} at {formatted_time}"
|
| 59 |
+
|
| 60 |
+
except ValueError as ve:
|
| 61 |
+
# Re-raise ValueError with the detailed message
|
| 62 |
+
raise ValueError(str(ve))
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
# Handle any unexpected errors
|
| 66 |
+
raise ValueError(
|
| 67 |
+
f"An unexpected error occurred while getting date and time: {str(e)}. "
|
| 68 |
+
"Please try again or contact support if the issue persists."
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
def __init__(self, *args, **kwargs):
|
| 72 |
+
self.is_initialized = False
|