test_space / src /streamlit_app.py
kurniawan
Add cache check before downloading models
a1bef06
raw
history blame
3.83 kB
import pandas as pd
import pickle
import numpy as np
import streamlit as st
import gdown
import os
# File IDs
model_id = "1HSQTjJ_hvBBmVJmYUmrkq5T7ubpfDwzF"
top_country_id = "1aLkaAqfrs3GcrMvZcuyQ0NjFhAhrdIlR"
model_url = f"https://drive.google.com/uc?id={model_id}"
top_country_url = f"https://drive.google.com/uc?id={top_country_id}"
@st.cache_resource
def load_model():
model_path = "best_rf_model.pkl"
if not os.path.exists(model_path):
gdown.download(model_url, model_path, quiet=False)
with open(model_path, "rb") as f:
return pickle.load(f)
@st.cache_resource
def load_top_country():
country_path = "top_country.pkl"
if not os.path.exists(country_path):
gdown.download(top_country_url, country_path, quiet=False)
with open(country_path, "rb") as f:
return pickle.load(f)
model = load_model()
top_country = load_top_country()
st.set_page_config(page_title="Hotel Booking Prediction", layout="wide")
st.markdown(
"""
<div style="
background-color: white;
padding: 50px;
border-radius: 20px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
max-width: 800px;
margin: auto;
text-align: center;
">
<h1 style="font-size:60px; font-weight:bold; color:black; margin-bottom:20px;">
Hotel Booking Prediction
</h1>
<p style="font-size:20px; color:gray; margin-bottom:30px;">
Welcome to Hotel Booking Prediction System
</p>
<p style="font-size:15px; color:black;">
Fill in the form below to predict hotel booking!
</p>
</div>
""",
unsafe_allow_html=True,
)
st.write("")
st.write("")
with st.form(key="hotel_bookings"):
col1, col2 = st.columns(2)
with col1:
name = st.selectbox("Hotel Type", ("city_hotel", "resort_hotel"), index=0)
lead = st.number_input(
"Lead Time",
min_value=0,
max_value=600,
value=0,
step=1,
help="jarak antar waktu booking dan check-in",
)
arrival_year = st.selectbox("Arrival Year", ("2015", "2016", "2017"), index=0)
arrival_month = st.selectbox(
"Arrival Months",
(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
),
index=0,
)
with col2:
arrival_week = st.number_input(
"Arrival Weeks",
min_value=1,
max_value=52,
value=1,
step=1,
help="minggu kedatangan",
)
arrival_day = st.number_input(
"Arrival Days",
min_value=1,
max_value=31,
value=1,
step=1,
help="tanggal kedatangan",
)
submitted = st.form_submit_button("Predict", use_container_width=True)
if submitted:
# Prepare data for prediction
data = {
"hotel": name,
"lead_time": lead,
"arrival_date_year": int(arrival_year),
"arrival_date_month": arrival_month,
"arrival_date_week_number": arrival_week,
"arrival_date_day_of_month": arrival_day,
}
df = pd.DataFrame([data])
try:
prediction = model.predict(df)
st.success("Prediction Complete!")
if prediction[0] == 1:
st.error("⚠️ This booking is likely to be CANCELLED")
else:
st.success("✅ This booking is likely to be CONFIRMED")
except Exception as e:
st.error(f"Error making prediction: {str(e)}")