time-server / app.py
aifeifei799's picture
Upload 2 files
143db2a verified
Raw
History Blame Contribute Delete
1.47 kB
from datetime import datetime
import zoneinfo
import gradio as gr
import spaces
@spaces.GPU
def get_current_time(timezone: str = "Asia/Shanghai") -> str:
"""
Retrieve the current real-time system date, day of the week, and exact time for a specified timezone.
Args:
timezone: The IANA timezone name (e.g., 'Asia/Shanghai', 'UTC', 'America/New_York', 'Europe/London', 'Asia/Tokyo').
"""
try:
tz = zoneinfo.ZoneInfo(timezone)
now = datetime.now(tz)
return (
f"Current Time: {now.strftime('%Y-%m-%d %A %H:%M:%S')} "
f"(Timezone: {timezone}, UTC{now.strftime('%z')})"
)
except Exception as e:
return f"Failed to get current time. Please verify the timezone parameter. Error: {str(e)}"
# Create the Gradio interface
demo = gr.Interface(
fn=get_current_time,
api_name="get_current_time",
inputs=gr.Dropdown(
choices=[
"Asia/Shanghai",
"UTC",
"America/New_York",
"Europe/London",
"Asia/Tokyo",
],
value="Asia/Shanghai",
label="Select Timezone",
),
outputs=gr.Textbox(label="Current Time Result"),
title="⏰ Real-Time MCP Clock Server",
description="A dual-purpose app providing a Web UI and a native MCP (Model Context Protocol) tool endpoint.",
)
if __name__ == "__main__":
# Enable the native MCP server endpoint
demo.launch(mcp_server=True)