import urllib.request import json import gradio as gr import os from dotenv import load_dotenv # ✅ important # 🔐 Load .env file load_dotenv() # Get API key API_KEY = os.getenv("WEATHER_API_KEY") def fetch_weather(location): if not API_KEY: return "❌ API key not found. Check your .env file." base_url = "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/" url = f"{base_url}{location}?unitGroup=metric&contentType=json&key={API_KEY}" try: response = urllib.request.urlopen(url) data = response.read() weather_data = json.loads(data.decode('utf-8')) output = f"📍 Weather Report for {location}\n\n" for day in weather_data['days'][:5]: output += ( f"📅 Date: {day['datetime']}\n" f"🌡️ Temp: {day['temp']}°C\n" f"☁️ Condition: {day['conditions']}\n" f"💧 Humidity: {day['humidity']}%\n" f"💨 Wind: {day['windspeed']} km/h\n\n" ) return output except Exception as e: return f"❌ Error: {str(e)}" interface = gr.Interface( fn=fetch_weather, inputs=gr.Textbox(placeholder="Enter city e.g. Islamabad"), outputs=gr.Textbox(lines=15), title="🌦️ Weather App", ) interface.launch()