Muhaimin60 commited on
Commit
bac638e
·
verified ·
1 Parent(s): 3b151f8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # Define the API URL and key
5
+ WEATHER_API_KEY = "your_openweathermap_api_key_here" # Replace with your actual API key
6
+ BASE_URL = "http://api.openweathermap.org/data/2.5/weather"
7
+
8
+ # Function to get weather data from OpenWeatherMap
9
+ def get_weather(city_name):
10
+ try:
11
+ # Make a request to the OpenWeather API
12
+ response = requests.get(f"{BASE_URL}?q={city_name}&appid={WEATHER_API_KEY}&units=metric")
13
+ data = response.json()
14
+
15
+ # Check if the request was successful
16
+ if data["cod"] != 200:
17
+ return f"Error: {data.get('message', 'Unknown error')}"
18
+
19
+ # Extract relevant weather details
20
+ city = data["name"]
21
+ country = data["sys"]["country"]
22
+ temperature = data["main"]["temp"]
23
+ description = data["weather"][0]["description"]
24
+ humidity = data["main"]["humidity"]
25
+ wind_speed = data["wind"]["speed"]
26
+
27
+ # Prepare the weather information to display
28
+ weather_info = f"Weather in {city}, {country}:\n"
29
+ weather_info += f"Temperature: {temperature}°C\n"
30
+ weather_info += f"Description: {description}\n"
31
+ weather_info += f"Humidity: {humidity}%\n"
32
+ weather_info += f"Wind Speed: {wind_speed} m/s"
33
+
34
+ return weather_info
35
+ except Exception as e:
36
+ return f"Error: {str(e)}"
37
+
38
+ # Create a Gradio interface
39
+ def main():
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown("# Lahore Weather App")
42
+ gr.Markdown("### Enter a city to get the current weather.")
43
+
44
+ city_input = gr.Textbox(label="City Name", value="Lahore", interactive=True)
45
+ weather_output = gr.Textbox(label="Weather Info", interactive=False)
46
+
47
+ city_input.submit(get_weather, inputs=city_input, outputs=weather_output)
48
+
49
+ demo.launch()
50
+
51
+ if __name__ == "__main__":
52
+ main()