Spaces:
Sleeping
Sleeping
Create the Base version
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install required packages (uncomment if running in Colab)
|
2 |
+
# !pip install gradio
|
3 |
+
# !pip install folium
|
4 |
+
|
5 |
+
import requests
|
6 |
+
import gradio as gr
|
7 |
+
import folium
|
8 |
+
|
9 |
+
# OpenWeatherMap API key
|
10 |
+
API_KEY = "ff3fe364119d79a928965cae2f3cd040" # Replace with your OpenWeatherMap API key
|
11 |
+
|
12 |
+
# Function to get weather data
|
13 |
+
def get_weather(city_name):
|
14 |
+
base_url = "http://api.openweathermap.org/data/2.5/weather"
|
15 |
+
params = {
|
16 |
+
"q": city_name,
|
17 |
+
"appid": API_KEY,
|
18 |
+
"units": "metric" # Fetch data in Celsius
|
19 |
+
}
|
20 |
+
response = requests.get(base_url, params=params)
|
21 |
+
|
22 |
+
if response.status_code == 200:
|
23 |
+
data = response.json()
|
24 |
+
city = data.get('name', 'Unknown')
|
25 |
+
temp = data['main'].get('temp', 'N/A')
|
26 |
+
feels_like = data['main'].get('feels_like', 'N/A')
|
27 |
+
humidity = data['main'].get('humidity', 'N/A')
|
28 |
+
weather_desc = data['weather'][0].get('description', 'N/A')
|
29 |
+
lat = data['coord']['lat']
|
30 |
+
lon = data['coord']['lon']
|
31 |
+
|
32 |
+
result = (
|
33 |
+
f"Weather in {city}:\n"
|
34 |
+
f"Temperature: {temp}°C\n"
|
35 |
+
f"Feels Like: {feels_like}°C\n"
|
36 |
+
f"Humidity: {humidity}%\n"
|
37 |
+
f"Condition: {weather_desc.capitalize()}"
|
38 |
+
)
|
39 |
+
return result, lat, lon
|
40 |
+
else:
|
41 |
+
result = "Could not retrieve weather data. Please check the city name and try again."
|
42 |
+
return result, None, None
|
43 |
+
|
44 |
+
# Function to generate the map
|
45 |
+
def generate_map(lat, lon, weather_info):
|
46 |
+
weather_map = folium.Map(location=[lat, lon], zoom_start=10)
|
47 |
+
folium.Marker(
|
48 |
+
location=[lat, lon],
|
49 |
+
popup=folium.Popup(weather_info, max_width=300),
|
50 |
+
tooltip="Click for weather details"
|
51 |
+
).add_to(weather_map)
|
52 |
+
|
53 |
+
map_file = "weather_map.html"
|
54 |
+
weather_map.save(map_file)
|
55 |
+
with open(map_file, "r") as f:
|
56 |
+
map_html = f.read()
|
57 |
+
return map_html
|
58 |
+
|
59 |
+
# Gradio interface function
|
60 |
+
def gradio_interface(city_name):
|
61 |
+
weather_info, lat, lon = get_weather(city_name)
|
62 |
+
if lat is not None and lon is not None:
|
63 |
+
map_html = generate_map(lat, lon, weather_info)
|
64 |
+
return weather_info, map_html
|
65 |
+
else:
|
66 |
+
return weather_info, "<p>Map could not be generated.</p>"
|
67 |
+
|
68 |
+
# Set up the Gradio app
|
69 |
+
autocomplete_box = gr.Textbox(
|
70 |
+
label="Enter City Name",
|
71 |
+
placeholder="Start typing a city name...",
|
72 |
+
lines=1
|
73 |
+
)
|
74 |
+
|
75 |
+
interface = gr.Interface(
|
76 |
+
fn=gradio_interface,
|
77 |
+
inputs=autocomplete_box,
|
78 |
+
outputs=[
|
79 |
+
"text", # Original weather details output
|
80 |
+
gr.HTML(label="Weather Map") # Map display in the same Gradio context
|
81 |
+
],
|
82 |
+
title="Weather Checker with Map",
|
83 |
+
description="Enter a city name to get the current weather and view it on a map."
|
84 |
+
)
|
85 |
+
|
86 |
+
# Launch the Gradio app with shareable link
|
87 |
+
if __name__ == "__main__":
|
88 |
+
interface.launch(share=True)
|