|
|
|
|
|
import gradio as gr |
|
import datetime |
|
import pytz |
|
from utils.tool_manager import tool |
|
|
|
def time_ui(): |
|
""" |
|
Builds the Gradio UI components for the Get Current Time tool. |
|
Returns a tuple: (ui_group, input_components, output_components, button_component) |
|
""" |
|
with gr.Group(visible=False) as ui_group: |
|
timezone_input = gr.Textbox(label="Timezone (e.g., UTC, Asia/Ho_Chi_Minh)", value="UTC", placeholder="Enter IANA timezone name") |
|
output_text = gr.Textbox(label="Current Time", interactive=False) |
|
run_button = gr.Button("Get Time") |
|
|
|
return ui_group, timezone_input, output_text, run_button |
|
|
|
@tool( |
|
name="Get Current Time", |
|
control_components=time_ui |
|
) |
|
def get_current_time(timezone: str = "UTC") -> str: |
|
""" |
|
Gets the current time for the given timezone string. |
|
|
|
This tool takes an IANA timezone name (like "UTC", "America/New_York", |
|
"Asia/Ho_Chi_Minh") and returns the current datetime in that zone. |
|
Defaults to "UTC" if no timezone is provided. |
|
|
|
Args: |
|
timezone (str): The IANA timezone string (e.g., "UTC"). |
|
|
|
Returns: |
|
str: The current time in the specified timezone, or an error message. |
|
""" |
|
try: |
|
|
|
tz = pytz.timezone(timezone) |
|
now_utc = datetime.datetime.utcnow() |
|
now_in_tz = pytz.utc.localize(now_utc).astimezone(tz) |
|
return now_in_tz.strftime('%Y-%m-%d %H:%M:%S %Z%z') |
|
except pytz.UnknownTimeZoneError: |
|
return f"Error: Unknown timezone '{timezone}'. Please provide a valid IANA timezone name." |
|
except Exception as e: |
|
return f"An unexpected error occurred: {e}" |