Spaces:
Sleeping
Sleeping
import gradio as gr | |
import random | |
from datetime import datetime | |
def get_weather(city: str, unit: str = "celsius") -> dict: | |
"""Get weather information for a city. | |
Args: | |
city: Name of the city | |
unit: Temperature unit (celsius or fahrenheit) | |
Returns: | |
Dictionary with weather information | |
""" | |
conditions = ["Sunny", "Cloudy", "Rainy", "Partly Cloudy", "Stormy"] | |
condition = random.choice(conditions) | |
if unit.lower() == "fahrenheit": | |
temp = random.randint(32, 90) | |
unit_symbol = "°F" | |
else: | |
temp = random.randint(0, 32) | |
unit_symbol = "°C" | |
return { | |
"city": city.title(), | |
"temperature": temp, | |
"unit": unit_symbol, | |
"condition": condition, | |
"humidity": random.randint(30, 90), | |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), | |
"message": f"Weather in {city.title()}: {temp}{unit_symbol}, {condition}" | |
} | |
demo = gr.Interface( | |
fn=get_weather, | |
inputs=[ | |
gr.Textbox(label="City", placeholder="Enter city name"), | |
gr.Dropdown(choices=["celsius", "fahrenheit"], value="celsius", label="Unit") | |
], | |
outputs=gr.JSON(label="Weather Data"), | |
title="Weather Service MCP Server", | |
description="Get weather information for any city" | |
) | |
if __name__ == "__main__": | |
demo.launch(mcp_server=True) |