Spaces:
Runtime error
Runtime error
import gradio as gr | |
from backports.zoneinfo import ZoneInfo | |
from datetime import datetime, timedelta, timezone | |
import pickle | |
weathers = ['Broken clouds ', | |
'Clear ', | |
'Cloudy ', | |
'Cool ', | |
'Dense fog ', | |
'Drizzle Broken clouds ', | |
'Drizzle Dense fog ', | |
'Drizzle Fog ', | |
'Drizzle More clouds than sun ', | |
'Drizzle Mostly cloudy ', | |
'Drizzle Overcast ', | |
'Drizzle Partly sunny ', | |
'Duststorm ', | |
'Extremely hot ', | |
'Fog ', | |
'Hail Cloudy ', | |
'Hail Partly sunny ', | |
'Hail Passing clouds ', | |
'Haze ', | |
'Heavy rain More clouds than sun ', | |
'Heavy rain Mostly cloudy ', | |
'Heavy rain Overcast ', | |
'Heavy rain Partly sunny ', | |
'Hot ', | |
'Light rain Broken clouds ', | |
'Light rain Fog ', | |
'Light rain More clouds than sun ', | |
'Light rain Mostly cloudy ', | |
'Light rain Overcast ', | |
'Light rain Partly cloudy ', | |
'Light rain Partly sunny ', | |
'Light rain Passing clouds ', | |
'Light rain Scattered clouds ', | |
'Low level haze ', | |
'Mild ', | |
'More clouds than sun ', | |
'Mostly cloudy ', | |
'Overcast ', | |
'Partly cloudy ', | |
'Partly sunny ', | |
'Passing clouds ', | |
'Pleasantly warm ', | |
'Rain Broken clouds ', | |
'Rain Clear ', | |
'Rain Fog ', | |
'Rain More clouds than sun ', | |
'Rain Mostly cloudy ', | |
'Rain Overcast ', | |
'Rain Partly cloudy ', | |
'Rain Partly sunny ', | |
'Rain Passing clouds ', | |
'Rain Sandstorm ', | |
'Rain Scattered clouds ', | |
'Rain showers Partly sunny ', | |
'Refreshingly cool ', | |
'Sandstorm ', | |
'Scattered clouds ', | |
'Smoke ', | |
'Sprinkles Cloudy ', | |
'Sprinkles Duststorm ', | |
'Sprinkles Low level haze ', | |
'Sprinkles Overcast ', | |
'Strong thunderstorms Cloudy ', | |
'Strong thunderstorms More clouds than sun ', | |
'Strong thunderstorms Partly sunny ', | |
'Sunny ', | |
'Thundershowers Partly sunny ', | |
'Thundershowers Passing clouds ', | |
'Thundershowers Scattered clouds ', | |
'Thunderstorms Broken clouds ', | |
'Thunderstorms Cloudy ', | |
'Thunderstorms Fog ', | |
'Thunderstorms More clouds than sun ', | |
'Thunderstorms Mostly cloudy ', | |
'Thunderstorms Overcast ', | |
'Thunderstorms Partly cloudy ', | |
'Thunderstorms Partly sunny ', | |
'Thunderstorms Passing clouds ', | |
'Thunderstorms Sandstorm ', | |
'Thunderstorms Scattered clouds ', | |
'Warm '] | |
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July','August', 'September', 'October', 'November', 'December'] | |
cities = ['Assir', | |
'Baha', | |
'EP', | |
'Hail', | |
'Jawf', | |
'Jazan', | |
'Madina', | |
'Mecca', | |
'Najran', | |
'Northern boarder', | |
'Qassim', | |
'Riyadh', | |
'Tabuk'] | |
currentSecond= datetime.now().second | |
currentMinute = datetime.now().minute | |
currentHour = datetime.now().hour | |
currentDay = datetime.now().day | |
currentMonth = datetime.now().month | |
currentYear = datetime.now().year | |
dt = datetime(currentYear, currentMonth, currentDay, tzinfo=ZoneInfo("Asia/Riyadh")) | |
fulltime = str(dt) | |
ADD = 0 | |
try: | |
if "+" in fulltime: | |
ADD = int(fulltime.split("+")[1].split(":")[0]) | |
elif "-" in fulltime: | |
ADD = int(fulltime.split("-")[1].split(":")[0]) * -1 | |
except: | |
pass | |
def predict(city,month,year,day,hour,minute,weathertype,wind,hu,baro,vis): | |
loaded_model = pickle.load(open("WeatherPredictionKSA.pk", 'rb')) | |
details = [cities.index(city),year,months.index(month),day,hour,minute,weathers.index(weathertype),wind,hu,baro,vis] | |
temp = int(loaded_model.predict([details])) | |
if temp >= 50: | |
emoji = "🥵" | |
elif 40 <= temp <= 49: | |
emoji = "😳" | |
elif 25 <= temp <= 39: | |
emoji = "😌" | |
elif 10 <= temp <= 24: | |
emoji = "😊" | |
elif temp <= 9: | |
emoji = "🥶" | |
return gr.Markdown.update("# 🌡️ {0} C {1}🌡️".format(temp,emoji)) | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
gr.Markdown("# 🌧️☁️☀️KSA Weather prediction ☀️☁️🌧️\n**Day, Hour, and Minute** are set to **Asia/Riyadh** current time by default but can be changed") | |
with gr.Box(): | |
with gr.Row(): | |
city = gr.Dropdown(cities,label='City',value='Assir',interactive=True) | |
month = gr.Dropdown(months,label='Month',value=months[currentMonth-1],interactive=True) | |
with gr.Row(): | |
year = gr.Number(int(currentYear),label='Year',interactive=True) | |
day = gr.Number(int(currentDay),label='Day',interactive=True) | |
hour = gr.Number(int(currentHour)+ADD,label='Hour',interactive=True) | |
minute = gr.Number(int(currentMinute),label='Minute',interactive=True) | |
with gr.Row(): | |
weathertype = gr.Dropdown(weathers,label='Weather type',value='Cloudy ',interactive=True) | |
wind = gr.Number(label='Wind',interactive=True) | |
hu = gr.Number(label='Humidity',interactive=True) | |
baro = gr.Number(label='Barometer',interactive=True) | |
vis = gr.Number(label='Visibility',interactive=True) | |
with gr.Row(): | |
temp = gr.Markdown("") | |
btn = gr.Button(value="Predict") | |
btn.click(predict, inputs=[city,month,year,day,hour,minute,weathertype,wind,hu,baro,vis], outputs=temp) | |
demo.launch() |