import gradio as gr import requests import os # 👉 Use environment variable for API key (important for Hugging Face) API_KEY = os.getenv("Weather") def get_weather(city): if not city: return "❌ Please enter a city name" if not API_KEY: return "❌ API key not set" url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric" try: response = requests.get(url) data = response.json() if str(data.get("cod")) == "200": temp = data["main"]["temp"] humidity = data["main"]["humidity"] desc = data["weather"][0]["description"] country = data["sys"]["country"] return f""" 🌍 City: {city}, {country} 🌡️ Temperature: {temp} °C 💧 Humidity: {humidity}% 🌤️ Condition: {desc} """ else: return f"❌ Error: {data.get('message', 'Unknown error')}" except Exception as e: return f"⚠️ Network Error: {str(e)}" # 👉 Gradio UI demo = gr.Interface( fn=get_weather, inputs=gr.Textbox(label="Enter City Name"), outputs=gr.Textbox(label="Weather Info"), title="🌦️ Weather App", description="Get real-time weather using OpenWeather API" ) # 👉 Launch (for Hugging Face) demo.launch()