Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import joblib | |
| import pandas as pd | |
| import numpy as np | |
| pipeline = joblib.load("model.joblib") | |
| NUM = ["lead_time","stays_in_weekend_nights","stays_in_week_nights","adults","children","babies", | |
| "previous_cancellations","previous_bookings_not_canceled","booking_changes", | |
| "days_in_waiting_list","adr","required_car_parking_spaces","total_of_special_requests", | |
| "total_nights","total_guests","revenue_est","risky_repeat"] | |
| CAT = ["hotel","meal","market_segment","distribution_channel", | |
| "reserved_room_type","deposit_type","customer_type","lead_cat","arrival_season"] | |
| DEFAULTS = { | |
| "stays_in_weekend_nights":2,"stays_in_week_nights":3,"adults":2,"children":0,"babies":0, | |
| "previous_bookings_not_canceled":0,"booking_changes":0,"days_in_waiting_list":0, | |
| "required_car_parking_spaces":0,"meal":"BB","distribution_channel":"TA/TO", | |
| "reserved_room_type":"A","customer_type":"Transient","arrival_season":"summer", | |
| "total_guests":2, | |
| } | |
| def predict(hotel, lead_time, deposit_type, market_segment, | |
| total_of_special_requests, previous_cancellations, adr, total_nights): | |
| row = DEFAULTS.copy() | |
| row.update({ | |
| "hotel": hotel, "lead_time": float(lead_time), | |
| "deposit_type": deposit_type, "market_segment": market_segment, | |
| "total_of_special_requests": float(total_of_special_requests), | |
| "previous_cancellations": float(previous_cancellations), | |
| "adr": float(adr), "total_nights": float(total_nights), | |
| "revenue_est": float(adr) * max(float(total_nights), 1), | |
| "risky_repeat": int(float(previous_cancellations) > 0), | |
| "lead_cat": pd.cut([float(lead_time)], bins=[-1,7,30,90,180,9999], | |
| labels=["last_minute","short","medium","long","very_long"])[0], | |
| }) | |
| X = pd.DataFrame([row])[NUM + CAT] | |
| prob = float(pipeline.predict_proba(X)[0, 1]) | |
| label = "Low risk" if prob < 0.30 else ("Medium risk" if prob < 0.60 else "High risk") | |
| return {label: prob} | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=[ | |
| gr.Dropdown(["City Hotel","Resort Hotel"], label="Hotel type", value="City Hotel"), | |
| gr.Slider(0, 500, value=30, step=1, label="Lead time (days)"), | |
| gr.Dropdown(["No Deposit","Non Refund","Refundable"], label="Deposit type", value="No Deposit"), | |
| gr.Dropdown(["Direct","Online TA","Offline TA/TO","Corporate","Groups"], | |
| label="Market segment", value="Online TA"), | |
| gr.Slider(0, 5, value=1, step=1, label="Special requests"), | |
| gr.Slider(0, 20, value=0, step=1, label="Previous cancellations"), | |
| gr.Slider(0, 500, value=100, step=5, label="ADR (€)"), | |
| gr.Slider(1, 30, value=3, step=1, label="Total nights"), | |
| ], | |
| outputs=gr.Label(label="Cancellation risk"), | |
| title="Hotel Booking Cancellation Risk", | |
| description="Predict cancellation probability from booking parameters. Educational demo only.", | |
| examples=[ | |
| ["City Hotel",200,"Non Refund","Online TA",0,3,95,2], | |
| ["Resort Hotel",7,"No Deposit","Direct",3,0,150,7], | |
| ], | |
| flagging_mode="never", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |