File size: 1,987 Bytes
afca03c
 
82a0540
5d17183
 
 
 
 
 
afca03c
 
 
 
 
 
 
 
 
db806bf
afca03c
 
 
 
 
 
 
db806bf
afca03c
 
 
 
 
 
 
 
 
 
db806bf
 
 
 
 
 
 
afca03c
db806bf
afca03c
db806bf
afca03c
 
 
 
 
db806bf
afca03c
 
 
 
 
 
db806bf
 
 
afca03c
 
db806bf
afca03c
db806bf
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import requests
import gradio as gr

from dotenv import load_dotenv
import os

load_dotenv()  # Load variables from .env
API_KEY = os.getenv("API_KEY")  # Access the API_KEY

# Function to get weather data
def get_weather(city_name):
    base_url = "http://api.openweathermap.org/data/2.5/weather"
    params = {
        "q": city_name,
        "appid": API_KEY,
        "units": "metric"  # Fetch data in Celsius
    }
    response = requests.get(base_url, params=params)

    if response.status_code == 200:
        data = response.json()
        city = data.get('name', 'Unknown')
        temp = data['main'].get('temp', 'N/A')
        feels_like = data['main'].get('feels_like', 'N/A')
        humidity = data['main'].get('humidity', 'N/A')
        weather_desc = data['weather'][0].get('description', 'N/A')

        result = (
            f"Weather in {city}:\n"
            f"Temperature: {temp}°C\n"
            f"Feels Like: {feels_like}°C\n"
            f"Humidity: {humidity}%\n"
            f"Condition: {weather_desc.capitalize()}"
        )
    else:
        result = "Could not retrieve weather data. Please check the city name and try again."

    return result

# List of cities for autocomplete (Add more cities as needed)
city_suggestions = [
    "Dubai", "London", "New York", "Tokyo", "Sydney", "Paris",
    "Berlin", "Moscow", "Mumbai", "Beijing", "Rome", "Los Angeles"
]

# Gradio interface
def gradio_interface(city_name):
    return get_weather(city_name)

# Set up the Gradio app
autocomplete_box = gr.Textbox(
    label="Enter City Name",
    placeholder="Start typing a city name...",
    #autocomplete="on",  # Enables autocomplete
    lines=1
)

interface = gr.Interface(
    fn=gradio_interface,
    inputs=autocomplete_box,
    outputs="text",
    title="Weather Checker",
    description="Enter a city name to get the current weather. Autocomplete suggestions are available."
)

# Launch the Gradio app
if __name__ == "__main__":
    interface.launch()