codechrl/test_space
0
1import pandas as pd2import pickle3import numpy as np4import streamlit as st5import gdown6import os7 8# File IDs9model_id = "1HSQTjJ_hvBBmVJmYUmrkq5T7ubpfDwzF"10top_country_id = "1aLkaAqfrs3GcrMvZcuyQ0NjFhAhrdIlR"11 12model_url = f"https://drive.google.com/uc?id={model_id}"13top_country_url = f"https://drive.google.com/uc?id={top_country_id}"14 15 16@st.cache_resource17def load_model():18 model_path = "best_rf_model.pkl"19 if not os.path.exists(model_path):20 gdown.download(model_url, model_path, quiet=False)21 with open(model_path, "rb") as f:22 return pickle.load(f)23 24 25@st.cache_resource26def load_top_country():27 country_path = "top_country.pkl"28 if not os.path.exists(country_path):29 gdown.download(top_country_url, country_path, quiet=False)30 with open(country_path, "rb") as f:31 return pickle.load(f)32 33 34model = load_model()35top_country = load_top_country()36 37st.set_page_config(page_title="Hotel Booking Prediction", layout="wide")38 39st.markdown(40 """41<div style="42 background-color: white;43 padding: 50px;44 border-radius: 20px;45 box-shadow: 0 4px 20px rgba(0,0,0,0.1);46 max-width: 800px;47 margin: auto;48 text-align: center;49">50 <h1 style="font-size:60px; font-weight:bold; color:black; margin-bottom:20px;">51 Hotel Booking Prediction52 </h1>53 <p style="font-size:20px; color:gray; margin-bottom:30px;">54 Welcome to Hotel Booking Prediction System55 </p>56 <p style="font-size:15px; color:black;">57 Fill in the form below to predict hotel booking!58 </p>59</div>60""",61 unsafe_allow_html=True,62)63 64st.write("")65st.write("")66 67with st.form(key="hotel_bookings"):68 col1, col2 = st.columns(2)69 70 with col1:71 name = st.selectbox("Hotel Type", ("city_hotel", "resort_hotel"), index=0)72 lead = st.number_input(73 "Lead Time",74 min_value=0,75 max_value=600,76 value=0,77 step=1,78 help="jarak antar waktu booking dan check-in",79 )80 arrival_year = st.selectbox("Arrival Year", ("2015", "2016", "2017"), index=0)81 arrival_month = st.selectbox(82 "Arrival Months",83 (84 "January",85 "February",86 "March",87 "April",88 "May",89 "June",90 "July",91 "August",92 "September",93 "October",94 "November",95 "December",96 ),97 index=0,98 )99 100 with col2:101 arrival_week = st.number_input(102 "Arrival Weeks",103 min_value=1,104 max_value=52,105 value=1,106 step=1,107 help="minggu kedatangan",108 )109 arrival_day = st.number_input(110 "Arrival Days",111 min_value=1,112 max_value=31,113 value=1,114 step=1,115 help="tanggal kedatangan",116 )117 118 submitted = st.form_submit_button("Predict", use_container_width=True)119 120 if submitted:121 # Prepare data for prediction122 data = {123 "hotel": name,124 "lead_time": lead,125 "arrival_date_year": int(arrival_year),126 "arrival_date_month": arrival_month,127 "arrival_date_week_number": arrival_week,128 "arrival_date_day_of_month": arrival_day,129 }130 131 df = pd.DataFrame([data])132 133 try:134 prediction = model.predict(df)135 136 st.success("Prediction Complete!")137 138 if prediction[0] == 1:139 st.error("⚠️ This booking is likely to be CANCELLED")140 else:141 st.success("✅ This booking is likely to be CONFIRMED")142 143 except Exception as e:144 st.error(f"Error making prediction: {str(e)}")145 