File size: 4,523 Bytes
0a0b0ba
 
 
 
 
 
8ebaef0
0a0b0ba
 
 
 
8ebaef0
 
 
 
0a0b0ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34e436b
0a0b0ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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)