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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
def get_weather(city_name):
|
| 7 |
+
"""Fetch weather data for a given city using the provided API key"""
|
| 8 |
+
if not city_name.strip():
|
| 9 |
+
city_name = "Dubai" # Default city
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
# Use the provided API key and URL structure
|
| 13 |
+
api_key = "openweather"
|
| 14 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid=79007029562c2ab46617d801d8018750&units=metric"
|
| 15 |
+
|
| 16 |
+
response = requests.get(url)
|
| 17 |
+
data = response.json()
|
| 18 |
+
|
| 19 |
+
if data["cod"] == 200:
|
| 20 |
+
# Extract weather information
|
| 21 |
+
city = data["name"]
|
| 22 |
+
country = data["sys"]["country"]
|
| 23 |
+
temperature = int(data["main"]["temp"])
|
| 24 |
+
feels_like = int(data["main"]["feels_like"])
|
| 25 |
+
humidity = data["main"]["humidity"]
|
| 26 |
+
pressure = data["main"]["pressure"]
|
| 27 |
+
description = data["weather"][0]["description"].title()
|
| 28 |
+
wind_speed = data["wind"]["speed"]
|
| 29 |
+
visibility = data.get("visibility", 10000) // 1000 # Convert to km
|
| 30 |
+
|
| 31 |
+
# Mock some additional data for the UI
|
| 32 |
+
rain_chance = f"{humidity//3}%" # Mock rain chance based on humidity
|
| 33 |
+
|
| 34 |
+
return {
|
| 35 |
+
"city": city,
|
| 36 |
+
"country": country,
|
| 37 |
+
"temperature": temperature,
|
| 38 |
+
"feels_like": feels_like,
|
| 39 |
+
"description": description,
|
| 40 |
+
"humidity": humidity,
|
| 41 |
+
"pressure": pressure,
|
| 42 |
+
"wind_speed": wind_speed,
|
| 43 |
+
"visibility": visibility,
|
| 44 |
+
"rain_chance": rain_chance
|
| 45 |
+
}
|
| 46 |
+
else:
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
def format_weather_display(weather_data):
|
| 53 |
+
"""Format weather data for display"""
|
| 54 |
+
if not weather_data:
|
| 55 |
+
return "Sorry, I couldn't find weather information for that city."
|
| 56 |
+
|
| 57 |
+
# Create the main weather display
|
| 58 |
+
main_display = f"""
|
| 59 |
+
<div style="text-align: center; margin-bottom: 30px;">
|
| 60 |
+
<h1 style="font-size: 48px; margin: 0; color: #333; font-weight: 300;">{weather_data['city']}</h1>
|
| 61 |
+
<div style="display: flex; align-items: center; justify-content: center; margin: 20px 0;">
|
| 62 |
+
<span style="font-size: 72px; font-weight: 200; color: #333;">{weather_data['temperature']}°C</span>
|
| 63 |
+
</div>
|
| 64 |
+
<p style="color: #666; font-size: 18px; margin: 0;">{weather_data['description']}</p>
|
| 65 |
+
</div>
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
# Create weather details grid
|
| 69 |
+
details_grid = f"""
|
| 70 |
+
<div>
|
| 71 |
+
<h3 style="color: #666; font-size: 14px; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 20px;">WEATHER DETAILS</h3>
|
| 72 |
+
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px;">
|
| 73 |
+
<div style="text-align: center; padding: 15px; background: rgba(255,255,255,0.5); border-radius: 12px;">
|
| 74 |
+
<div style="color: #666; font-size: 12px; margin-bottom: 5px;">Chance of rain</div>
|
| 75 |
+
<div style="font-size: 18px; font-weight: 500; color: #333;">{weather_data['rain_chance']}</div>
|
| 76 |
+
<div style="font-size: 16px; margin-top: 5px;">💧</div>
|
| 77 |
+
</div>
|
| 78 |
+
<div style="text-align: center; padding: 15px; background: rgba(255,255,255,0.5); border-radius: 12px;">
|
| 79 |
+
<div style="color: #666; font-size: 12px; margin-bottom: 5px;">Pressure</div>
|
| 80 |
+
<div style="font-size: 18px; font-weight: 500; color: #333;">{weather_data['pressure']} mb</div>
|
| 81 |
+
<div style="font-size: 16px; margin-top: 5px;">📊</div>
|
| 82 |
+
</div>
|
| 83 |
+
<div style="text-align: center; padding: 15px; background: rgba(255,255,255,0.5); border-radius: 12px;">
|
| 84 |
+
<div style="color: #666; font-size: 12px; margin-bottom: 5px;">Wind</div>
|
| 85 |
+
<div style="font-size: 18px; font-weight: 500; color: #333;">{weather_data['wind_speed']} km/h</div>
|
| 86 |
+
<div style="font-size: 16px; margin-top: 5px;">💨</div>
|
| 87 |
+
</div>
|
| 88 |
+
<div style="text-align: center; padding: 15px; background: rgba(255,255,255,0.5); border-radius: 12px;">
|
| 89 |
+
<div style="color: #666; font-size: 12px; margin-bottom: 5px;">Feels like</div>
|
| 90 |
+
<div style="font-size: 18px; font-weight: 500; color: #333;">{weather_data['feels_like']}°C</div>
|
| 91 |
+
<div style="font-size: 16px; margin-top: 5px;">🌡️</div>
|
| 92 |
+
</div>
|
| 93 |
+
<div style="text-align: center; padding: 15px; background: rgba(255,255,255,0.5); border-radius: 12px;">
|
| 94 |
+
<div style="color: #666; font-size: 12px; margin-bottom: 5px;">Visibility</div>
|
| 95 |
+
<div style="font-size: 18px; font-weight: 500; color: #333;">{weather_data['visibility']} km</div>
|
| 96 |
+
<div style="font-size: 16px; margin-top: 5px;">👁️</div>
|
| 97 |
+
</div>
|
| 98 |
+
<div style="text-align: center; padding: 15px; background: rgba(255,255,255,0.5); border-radius: 12px;">
|
| 99 |
+
<div style="color: #666; font-size: 12px; margin-bottom: 5px;">Humidity</div>
|
| 100 |
+
<div style="font-size: 18px; font-weight: 500; color: #333;">{weather_data['humidity']}%</div>
|
| 101 |
+
<div style="font-size: 16px; margin-top: 5px;">💧</div>
|
| 102 |
+
</div>
|
| 103 |
+
</div>
|
| 104 |
+
</div>
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
return main_display + details_grid
|
| 108 |
+
|
| 109 |
+
def search_weather(city_name):
|
| 110 |
+
"""Search for weather and format the display"""
|
| 111 |
+
weather_data = get_weather(city_name)
|
| 112 |
+
return format_weather_display(weather_data)
|
| 113 |
+
|
| 114 |
+
# Custom CSS for the purple gradient background and card styling
|
| 115 |
+
custom_css = """
|
| 116 |
+
body, .gradio-container {
|
| 117 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 118 |
+
min-height: 100vh;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
.main-container {
|
| 122 |
+
background: white !important;
|
| 123 |
+
border-radius: 24px !important;
|
| 124 |
+
padding: 40px !important;
|
| 125 |
+
margin: 40px auto !important;
|
| 126 |
+
max-width: 900px !important;
|
| 127 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.1) !important;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
.search-container {
|
| 131 |
+
display: flex !important;
|
| 132 |
+
align-items: center !important;
|
| 133 |
+
justify-content: space-between !important;
|
| 134 |
+
margin-bottom: 30px !important;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
.app-title {
|
| 138 |
+
font-size: 20px !important;
|
| 139 |
+
font-weight: 600 !important;
|
| 140 |
+
color: #333 !important;
|
| 141 |
+
display: flex !important;
|
| 142 |
+
align-items: center !important;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.search-input {
|
| 146 |
+
flex: 1 !important;
|
| 147 |
+
margin: 0 20px !important;
|
| 148 |
+
border-radius: 12px !important;
|
| 149 |
+
border: 1px solid #e0e0e0 !important;
|
| 150 |
+
padding: 12px 16px !important;
|
| 151 |
+
font-size: 16px !important;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
#weather_display {
|
| 155 |
+
border: none !important;
|
| 156 |
+
background: transparent !important;
|
| 157 |
+
}
|
| 158 |
+
"""
|
| 159 |
+
|
| 160 |
+
# Create the Gradio interface
|
| 161 |
+
with gr.Blocks(css=custom_css, title="Shafaq's Weather App") as app:
|
| 162 |
+
with gr.Column(elem_classes=["main-container"]):
|
| 163 |
+
# Header
|
| 164 |
+
with gr.Row(elem_classes=["search-container"]):
|
| 165 |
+
gr.HTML('<div class="app-title">🌤️ Shafaq\'s Weather App</div>')
|
| 166 |
+
city_input = gr.Textbox(
|
| 167 |
+
placeholder="Search for cities",
|
| 168 |
+
show_label=False,
|
| 169 |
+
elem_classes=["search-input"],
|
| 170 |
+
value="Dubai"
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# Weather display
|
| 174 |
+
weather_display = gr.HTML(
|
| 175 |
+
value="",
|
| 176 |
+
elem_id="weather_display"
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# Load Dubai weather on startup
|
| 180 |
+
app.load(
|
| 181 |
+
fn=search_weather,
|
| 182 |
+
inputs=gr.Textbox(value="Dubai", visible=False),
|
| 183 |
+
outputs=weather_display
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
# Search functionality
|
| 187 |
+
city_input.submit(
|
| 188 |
+
fn=search_weather,
|
| 189 |
+
inputs=city_input,
|
| 190 |
+
outputs=weather_display
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
# Launch the app
|
| 194 |
+
if __name__ == "__main__":
|
| 195 |
+
print("🌤️ Starting Shafaq's Weather App...")
|
| 196 |
+
print("🏙️ Default city: Dubai")
|
| 197 |
+
app.launch()
|