Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from Rehan12345678/OpenWeatherMCPServer: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/spaces/Rehan12345678/OpenWeatherMCPServer/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/Rehan12345678/OpenWeatherMCPServer/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/Rehan12345678/OpenWeatherMCPServer/resolve/main/src/streamlit_app.py
1.11 kB
| 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 | |
| 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"] | |
| } | |
| def root(): | |
| return {"message": "OpenWeather MCP Server is running"} |