Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,45 +21,44 @@ def my_custom_tool(
|
|
| 21 |
"""
|
| 22 |
return "What magic will you build ?"
|
| 23 |
|
| 24 |
-
|
| 25 |
-
import datetime
|
| 26 |
-
import pytz
|
| 27 |
-
from smolagents import tool
|
| 28 |
-
|
| 29 |
@tool
|
| 30 |
def time_difference_tool(timezone1: str, timezone2: str) -> str:
|
| 31 |
"""Calculates the time difference between two given timezones.
|
| 32 |
|
| 33 |
-
This tool returns the current time in both timezones and their time difference.
|
| 34 |
|
| 35 |
Args:
|
| 36 |
-
timezone1 (str): The first timezone (e.g., 'America/New_York').
|
| 37 |
-
timezone2 (str): The second timezone (e.g., 'Europe/Istanbul').
|
| 38 |
|
| 39 |
Returns:
|
| 40 |
-
str: A message displaying the current time in both timezones and their time difference.
|
| 41 |
"""
|
| 42 |
try:
|
|
|
|
| 43 |
tz1 = pytz.timezone(timezone1)
|
| 44 |
tz2 = pytz.timezone(timezone2)
|
| 45 |
|
|
|
|
| 46 |
now = datetime.datetime.utcnow()
|
| 47 |
-
|
| 48 |
time1 = datetime.datetime.now(tz1).strftime("%Y-%m-%d %H:%M:%S")
|
| 49 |
time2 = datetime.datetime.now(tz2).strftime("%Y-%m-%d %H:%M:%S")
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
return (
|
| 54 |
f"Current time in {timezone1}: {time1}\n"
|
| 55 |
f"Current time in {timezone2}: {time2}\n"
|
| 56 |
-
f"Time difference: {
|
| 57 |
)
|
| 58 |
except Exception as e:
|
| 59 |
return f"Error: Invalid timezone input. Please provide a valid timezone. ({str(e)})"
|
| 60 |
|
| 61 |
|
| 62 |
|
|
|
|
| 63 |
@tool
|
| 64 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 65 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 21 |
"""
|
| 22 |
return "What magic will you build ?"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
@tool
|
| 25 |
def time_difference_tool(timezone1: str, timezone2: str) -> str:
|
| 26 |
"""Calculates the time difference between two given timezones.
|
| 27 |
|
| 28 |
+
This tool returns the current time in both timezones and their time difference in hours.
|
| 29 |
|
| 30 |
Args:
|
| 31 |
+
timezone1 (str): The name of the first timezone (e.g., 'America/New_York').
|
| 32 |
+
timezone2 (str): The name of the second timezone (e.g., 'Europe/Istanbul').
|
| 33 |
|
| 34 |
Returns:
|
| 35 |
+
str: A message displaying the current time in both timezones and their time difference in hours.
|
| 36 |
"""
|
| 37 |
try:
|
| 38 |
+
# Load timezone objects
|
| 39 |
tz1 = pytz.timezone(timezone1)
|
| 40 |
tz2 = pytz.timezone(timezone2)
|
| 41 |
|
| 42 |
+
# Get current times in both timezones
|
| 43 |
now = datetime.datetime.utcnow()
|
|
|
|
| 44 |
time1 = datetime.datetime.now(tz1).strftime("%Y-%m-%d %H:%M:%S")
|
| 45 |
time2 = datetime.datetime.now(tz2).strftime("%Y-%m-%d %H:%M:%S")
|
| 46 |
|
| 47 |
+
# Calculate time difference in hours
|
| 48 |
+
diff_seconds = (datetime.datetime.now(tz1) - datetime.datetime.now(tz2)).total_seconds()
|
| 49 |
+
diff_hours = abs(diff_seconds / 3600)
|
| 50 |
|
| 51 |
return (
|
| 52 |
f"Current time in {timezone1}: {time1}\n"
|
| 53 |
f"Current time in {timezone2}: {time2}\n"
|
| 54 |
+
f"Time difference: {diff_hours:.1f} hours"
|
| 55 |
)
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error: Invalid timezone input. Please provide a valid timezone. ({str(e)})"
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
+
|
| 62 |
@tool
|
| 63 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 64 |
"""A tool that fetches the current local time in a specified timezone.
|