Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import joblib | |
| import pandas as pd | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| from prediction import predict | |
| import os | |
| # ================================ | |
| # Load model & preprocessor | |
| # ================================ | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| model = joblib.load(os.path.join(BASE_DIR, 'rf_model.pkl')) | |
| prep = joblib.load(os.path.join(BASE_DIR, 'preprocessor.pkl')) | |
| # ================================ | |
| # Sidebar Navigation | |
| # ================================ | |
| st.sidebar.title("Navigation") | |
| page = st.sidebar.selectbox("Choose Page", ["Prediction", "EDA"]) | |
| st.sidebar.markdown("### Created by Fernando Brian") | |
| st.sidebar.markdown("### Deployed on Hugging Face") | |
| # ================================ | |
| # π PAGE 1: PREDICTION | |
| # ================================ | |
| if page == "Prediction": | |
| st.title("Hotel Booking Cancellation Prediction") | |
| st.write("Masukkan data booking untuk memprediksi kemungkinan pembatalan.") | |
| # ================================ | |
| # Input User | |
| # ================================ | |
| lead_time = st.slider("Lead Time", 0, 365, 50) | |
| hotel = st.selectbox("Hotel Type", ["City Hotel", "Resort Hotel"]) | |
| deposit_type = st.selectbox("Deposit Type", ["No Deposit", "Non Refund", "Refundable"]) | |
| market_segment = st.selectbox("Market Segment", ["Online TA", "Offline TA/TO", "Direct", "Corporate"]) | |
| country = st.text_input("Country (contoh: PRT, GBR)", "PRT") | |
| # ================================ | |
| # Prediction Button | |
| # ================================ | |
| if st.button("Predict"): | |
| data = { | |
| 'hotel': hotel, | |
| 'lead_time': lead_time, | |
| 'arrival_date_year': 2017, | |
| 'arrival_date_month': 'July', | |
| 'arrival_date_week_number': 27, | |
| 'arrival_date_day_of_month': 1, | |
| 'stays_in_weekend_nights': 1, | |
| 'stays_in_week_nights': 2, | |
| 'adults': 2, | |
| 'children': 0, | |
| 'babies': 0, | |
| 'meal': 'BB', | |
| 'country': country, | |
| 'market_segment': market_segment, | |
| 'distribution_channel': 'TA/TO', | |
| 'is_repeated_guest': 0, | |
| 'previous_cancellations': 0, | |
| 'previous_bookings_not_canceled': 0, | |
| 'reserved_room_type': 'A', | |
| 'assigned_room_type': 'A', | |
| 'booking_changes': 0, | |
| 'deposit_type': deposit_type, | |
| 'days_in_waiting_list': 0, | |
| 'customer_type': 'Transient', | |
| 'adr': 100.0, | |
| 'required_car_parking_spaces': 0, | |
| 'total_of_special_requests': 1, | |
| 'agent': 0, | |
| 'company': 0 | |
| } | |
| # Predict | |
| result = predict(data, model, prep) | |
| # Output | |
| if result == 1: | |
| st.error("β Booking kemungkinan akan dibatalkan") | |
| else: | |
| st.success("β Booking kemungkinan tidak dibatalkan") | |
| # ================================ | |
| # π PAGE 2: EDA | |
| # ================================ | |
| elif page == "EDA": | |
| st.title("Exploratory Data Analysis") | |
| # Load dataset | |
| df = pd.read_csv(os.path.join(BASE_DIR, "hotel_bookings.csv")) | |
| # ================================ | |
| # Dataset Preview | |
| # ================================ | |
| st.subheader("Dataset Preview") | |
| st.dataframe(df.head()) | |
| # ================================ | |
| # Plot 1 - Cancellation Distribution | |
| # ================================ | |
| st.subheader("Distribusi Pembatalan") | |
| fig, ax = plt.subplots() | |
| sns.countplot(x='is_canceled', data=df, ax=ax) | |
| ax.set_title("Cancellation Distribution") | |
| st.pyplot(fig) | |
| # ================================ | |
| # Plot 2 - Lead Time vs Cancellation | |
| # ================================ | |
| st.subheader("Lead Time vs Cancellation") | |
| fig, ax = plt.subplots() | |
| sns.boxplot(x='is_canceled', y='lead_time', data=df, ax=ax) | |
| ax.set_title("Lead Time vs Cancellation") | |
| st.pyplot(fig) | |
| # ================================ | |
| # Plot 3 - Market Segment Distribution | |
| # ================================ | |
| st.subheader("Market Segment Distribution") | |
| fig, ax = plt.subplots() | |
| sns.countplot( | |
| y='market_segment', | |
| data=df, | |
| order=df['market_segment'].value_counts().index, | |
| ax=ax | |
| ) | |
| ax.set_title("Market Segment Distribution") | |
| st.pyplot(fig) |