Spaces:
Sleeping
Sleeping
File size: 1,114 Bytes
5ed1c89 8ac4069 5ed1c89 8ac4069 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import os
import requests
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
# Read API key from Hugging Face Secrets
OPENWEATHER_API_KEY = os.getenv("OWM_API_KEY")
BASE_URL = "https://api.openweathermap.org/data/2.5/weather"
class WeatherRequest(BaseModel):
city: str
@app.post("/weather")
def get_weather(request: WeatherRequest):
"""Fetch live weather data for a given city."""
params = {
"q": request.city,
"appid": OPENWEATHER_API_KEY,
"units": "metric"
}
response = requests.get(BASE_URL, params=params)
if response.status_code != 200:
return {"error": response.json()}
data = response.json()
return {
"city": data["name"],
"country": data["sys"]["country"],
"temperature": data["main"]["temp"],
"feels_like": data["main"]["feels_like"],
"humidity": data["main"]["humidity"],
"weather": data["weather"][0]["description"],
"wind_speed": data["wind"]["speed"]
}
@app.get("/")
def root():
return {"message": "OpenWeather MCP Server is running"} |