Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import pickle | |
import base64 | |
def get_base64_of_image(image_path): | |
with open(image_path, "rb") as img_file: | |
data = img_file.read() | |
return base64.b64encode(data).decode() | |
def set_background_local(image_path): | |
base64_img = get_base64_of_image(image_path) | |
st.markdown(f""" | |
<style> | |
.stApp {{ | |
background-image: url("data:image/jpg;base64,{base64_img}"); | |
background-size: cover; | |
background-repeat: no-repeat; | |
background-attachment: fixed; | |
background-position: center; | |
}} | |
.overlay {{ | |
position: fixed; | |
top: 0; | |
left: 0; | |
width: 100vw; | |
height: 100vh; | |
background-color: rgba(0, 0, 0, 0.2); | |
z-index: 0; | |
}} | |
</style> | |
<div class="overlay"></div> | |
""", unsafe_allow_html=True) | |
def enable_glassmorphism(): | |
st.markdown(""" | |
<style> | |
section.main > div {{ | |
backdrop-filter: blur(100px); | |
background-color: rgba(255, 0, 0, 0.08); | |
border-radius: 16px; | |
padding: 1.5rem; | |
}} | |
</style> | |
""", unsafe_allow_html=True) | |
st.set_page_config(page_title="Rain Prediction App", page_icon="🌧️", layout="centered") | |
# Inject CSS for raining effect | |
set_background_local("filip-zrnzevic-_EMkxLdko9k-unsplash.jpg") | |
enable_glassmorphism() | |
# Load model | |
with open("rain.pkl", "rb") as file: | |
model = pickle.load(file) | |
# Title | |
st.markdown("<h1 style='text-align: center; color: #ffffff;'>🌧️ Rain Prediction App</h1>", unsafe_allow_html=True) | |
st.markdown("<p style='text-align: center;'>Enter today’s weather details below to predict if it will rain tomorrow.</p>", unsafe_allow_html=True) | |
# Sidebar icon | |
st.sidebar.image("https://cdn-icons-png.flaticon.com/512/1163/1163624.png", width=100) | |
st.sidebar.markdown("### ⛅ About") | |
st.sidebar.info("This app uses a trained machine learning model to predict the likelihood of rainfall tomorrow based on various weather parameters.") | |
# Input Form | |
with st.form("rain_form"): | |
st.markdown("### 🌡️ Weather Inputs") | |
col1, col2 = st.columns(2) | |
with col1: | |
min_temp = st.number_input("Min Temperature (°C)", value=0.0) | |
rainfall = st.number_input("Rainfall (mm)", value=0.0) | |
sunshine = st.number_input("Sunshine (hrs)", value=0.0) | |
wind_speed_9am = st.number_input("Wind Speed at 9 AM (km/h)", value=0.0) | |
humidity_9am = st.slider("Humidity at 9 AM (%)", 0, 100, 50) | |
pressure_9am = st.number_input("Pressure at 9 AM (hPa)", value=1010.0) | |
cloud_9am = st.slider("Cloud at 9 AM (0-9)", 0, 9, 4) | |
temp_9am = st.number_input("Temperature at 9 AM (°C)", value=15.0) | |
with col2: | |
max_temp = st.number_input("Max Temperature (°C)", value=0.0) | |
evaporation = st.number_input("Evaporation (mm)", value=0.0) | |
wind_gust_speed = st.number_input("Wind Gust Speed (km/h)", value=0.0) | |
wind_speed_3pm = st.number_input("Wind Speed at 3 PM (km/h)", value=0.0) | |
humidity_3pm = st.slider("Humidity at 3 PM (%)", 0, 100, 50) | |
pressure_3pm = st.number_input("Pressure at 3 PM (hPa)", value=1010.0) | |
cloud_3pm = st.slider("Cloud at 3 PM (0-9)", 0, 9, 4) | |
temp_3pm = st.number_input("Temperature at 3 PM (°C)", value=15.0) | |
rain_today = st.selectbox("🌧️ Did it rain today?", ["No", "Yes"]) | |
submitted = st.form_submit_button("🔍 Predict") | |
# Prediction | |
if submitted: | |
input_data = pd.DataFrame([[ | |
min_temp, max_temp, rainfall, evaporation, sunshine, | |
wind_gust_speed, wind_speed_9am, wind_speed_3pm, | |
humidity_9am, humidity_3pm, pressure_9am, pressure_3pm, | |
cloud_9am, cloud_3pm, temp_9am, temp_3pm, rain_today | |
]], columns=[ | |
'MinTemp', 'MaxTemp', 'Rainfall', 'Evaporation', 'Sunshine', | |
'WindGustSpeed', 'WindSpeed9am', 'WindSpeed3pm', | |
'Humidity9am', 'Humidity3pm', 'Pressure9am', 'Pressure3pm', | |
'Cloud9am', 'Cloud3pm', 'Temp9am', 'Temp3pm', 'RainToday' | |
]) | |
prediction = model.predict(input_data)[0] | |
proba = model.predict_proba(input_data)[0][1] | |
if prediction == 'Yes': | |
if proba < 0.5: | |
st.success("☔ It **will rain tomorrow.**") | |
else: | |
st.success("☔ It **will rain tomorrow.**") | |
else: | |
st.info("☀️ It **won't rain tomorrow.**") | |
st.markdown("### 🌦️ Prediction Result") | |
st.markdown(f"### 🔎 Confidence: `{proba:.2%}`") | |
st.progress(proba) | |