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()