Spaces:
Sleeping
Sleeping
File size: 1,352 Bytes
6af6e98 9ee5950 6af6e98 9ee5950 74032fb 6af6e98 9ee5950 6af6e98 74032fb 6af6e98 | 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 42 43 44 45 46 47 48 | 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() |