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