Spaces:
Sleeping
Sleeping
| 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() |