SunnyShaurya commited on
Commit
0a6ad30
·
verified ·
1 Parent(s): fcb2f57

Add app.py for Streamlit app deployment

Browse files
Files changed (1) hide show
  1. app.py +126 -1
app.py CHANGED
@@ -1 +1,126 @@
1
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import joblib
4
+ from huggingface_hub import hf_hub_download
5
+
6
+ # ---------------------------------------
7
+ # CONFIG
8
+ # ---------------------------------------
9
+ MODEL_REPO_ID = "SunnyShaurya/tourism-package-model1"
10
+
11
+ FILES = {
12
+ "model": "overall_best_model.joblib",
13
+ "scaler": "scaler.joblib",
14
+ "cat_cols": "categorical_cols.joblib",
15
+ "num_cols": "numerical_cols.joblib",
16
+ "processed_cols": "processed_columns.joblib",
17
+ }
18
+
19
+ # ---------------------------------------
20
+ # LOAD ARTIFACTS
21
+ # ---------------------------------------
22
+ @st.cache_resource
23
+ def load_artifacts():
24
+ model = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["model"]))
25
+ scaler = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["scaler"]))
26
+ categorical_cols = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["cat_cols"]))
27
+ numerical_cols = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["num_cols"]))
28
+ processed_columns = joblib.load(hf_hub_download(MODEL_REPO_ID, FILES["processed_cols"]))
29
+ return model, scaler, categorical_cols, numerical_cols, processed_columns
30
+
31
+
32
+ model, scaler, categorical_cols, numerical_cols, processed_columns = load_artifacts()
33
+
34
+ # ---------------------------------------
35
+ # STREAMLIT UI
36
+ # ---------------------------------------
37
+ st.set_page_config(page_title="Tourism Package Prediction", layout="centered")
38
+ st.title("✈️ Tourism Package Prediction")
39
+ st.write("Predict whether a customer will purchase a tourism package.")
40
+
41
+ with st.form("prediction_form"):
42
+ col1, col2 = st.columns(2)
43
+
44
+ with col1:
45
+ Age = st.number_input("Age", 18, 100, 30)
46
+ TypeofContact = st.selectbox("Type of Contact", ["Self Enquiry", "Company Invited"])
47
+ CityTier = st.selectbox("City Tier", [1, 2, 3])
48
+ DurationOfPitch = st.number_input("Duration of Pitch", 0, 120, 10)
49
+ Occupation = st.selectbox(
50
+ "Occupation",
51
+ ["Salaried", "Small Business", "Large Business", "Free Lancer", "Government"]
52
+ )
53
+ Gender = st.selectbox("Gender", ["Male", "Female", "Fe Male"])
54
+ NumberOfPersonVisiting = st.number_input("Persons Visiting", 1, 10, 2)
55
+ NumberOfFollowups = st.number_input("Followups", 0, 10, 3)
56
+ ProductPitched = st.selectbox(
57
+ "Product Pitched", ["Basic", "Standard", "Deluxe", "Super Deluxe", "King"]
58
+ )
59
+
60
+ with col2:
61
+ PreferredPropertyStar = st.number_input("Property Star", 1, 5, 3)
62
+ MaritalStatus = st.selectbox(
63
+ "Marital Status", ["Married", "Single", "Unmarried", "Divorced"]
64
+ )
65
+ NumberOfTrips = st.number_input("Trips Last Year", 0, 50, 2)
66
+ Passport = st.selectbox("Passport", [0, 1])
67
+ PitchSatisfactionScore = st.number_input("Pitch Satisfaction", 1, 5, 3)
68
+ OwnCar = st.selectbox("Own Car", [0, 1])
69
+ NumberOfChildrenVisiting = st.number_input("Children Visiting", 0, 10, 1)
70
+ Designation = st.selectbox(
71
+ "Designation", ["Executive", "Manager", "Senior Manager", "AVP", "VP"]
72
+ )
73
+ MonthlyIncome = st.number_input("Monthly Income", 0, 500000, 25000)
74
+
75
+ submitted = st.form_submit_button("Predict")
76
+
77
+ # ---------------------------------------
78
+ # PREDICTION
79
+ # ---------------------------------------
80
+ if submitted:
81
+ df = pd.DataFrame([{
82
+ "Age": Age,
83
+ "TypeofContact": TypeofContact,
84
+ "CityTier": CityTier,
85
+ "DurationOfPitch": DurationOfPitch,
86
+ "Occupation": Occupation,
87
+ "Gender": "Female" if Gender == "Fe Male" else Gender,
88
+ "NumberOfPersonVisiting": NumberOfPersonVisiting,
89
+ "NumberOfFollowups": NumberOfFollowups,
90
+ "ProductPitched": ProductPitched,
91
+ "PreferredPropertyStar": PreferredPropertyStar,
92
+ "MaritalStatus": MaritalStatus,
93
+ "NumberOfTrips": NumberOfTrips,
94
+ "Passport": Passport,
95
+ "PitchSatisfactionScore": PitchSatisfactionScore,
96
+ "OwnCar": OwnCar,
97
+ "NumberOfChildrenVisiting": NumberOfChildrenVisiting,
98
+ "Designation": Designation,
99
+ "MonthlyIncome": MonthlyIncome
100
+ }])
101
+
102
+ # 🔑 IMPORTANT: NO drop_first
103
+ df_encoded = pd.get_dummies(df, columns=categorical_cols)
104
+
105
+ # Align with training features
106
+ df_encoded = df_encoded.reindex(columns=processed_columns, fill_value=0)
107
+
108
+ # Scale numerical columns
109
+ df_encoded[numerical_cols] = scaler.transform(df_encoded[numerical_cols])
110
+
111
+ # Debug info
112
+ st.caption(f"Active features: {(df_encoded != 0).sum().sum()}")
113
+
114
+ # Predict
115
+ pred = model.predict(df_encoded)[0]
116
+ prob = model.predict_proba(df_encoded)[0][pred]
117
+
118
+ st.subheader("Prediction Result")
119
+ if pred == 1:
120
+ st.success("✅ Customer is **LIKELY** to purchase the tourism package")
121
+ else:
122
+ st.warning("❌ Customer is **UNLIKELY** to purchase the tourism package")
123
+
124
+ st.metric("Confidence", f"{prob * 100:.2f}%")
125
+
126
+ st.caption("⚠️ ML-based prediction for decision support only.")