abhishek-kumar commited on
Commit
eb3b9bc
·
verified ·
1 Parent(s): 30ba263

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +201 -0
app.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Streamlit App for Tourism Package Prediction
4
+ """
5
+
6
+ import streamlit as st
7
+ import pandas as pd
8
+ import numpy as np
9
+ import joblib
10
+ from huggingface_hub import hf_hub_download
11
+
12
+ # Page configuration
13
+ st.set_page_config(
14
+ page_title="Tourism Package Prediction",
15
+ page_icon="🏖️",
16
+ layout="wide",
17
+ initial_sidebar_state="expanded"
18
+ )
19
+
20
+ @st.cache_resource
21
+ def load_model():
22
+ """Load the trained model from HuggingFace Hub"""
23
+ try:
24
+ model_path = hf_hub_download(
25
+ repo_id="abhishek-kumar/tourism-package-prediction-model",
26
+ filename="best_model.joblib"
27
+ )
28
+ model = joblib.load(model_path)
29
+ return model
30
+ except Exception as e:
31
+ st.error(f"Error loading model: {e}")
32
+ return None
33
+
34
+ def prepare_input_data(age, gender, marital_status, city_tier, type_of_contact,
35
+ occupation, designation, monthly_income, num_person_visiting,
36
+ num_children_visiting, preferred_property_star, num_trips,
37
+ passport, own_car, duration_of_pitch, product_pitched,
38
+ num_followups, pitch_satisfaction_score):
39
+ """Prepare input data for model prediction"""
40
+
41
+ # Create mapping dictionaries
42
+ gender_map = {"Male": 1, "Female": 0}
43
+ marital_map = {"Single": 2, "Married": 1, "Divorced": 0, "Unmarried": 3}
44
+ contact_map = {"Self Enquiry": 1, "Company Invited": 0}
45
+ occupation_map = {"Salaried": 2, "Small Business": 1, "Free Lancer": 0}
46
+ designation_map = {"Executive": 0, "Manager": 1, "Senior Manager": 2, "AVP": 3, "VP": 4}
47
+ product_map = {"Basic": 0, "Standard": 1, "Deluxe": 2, "Super Deluxe": 3}
48
+ passport_map = {"Yes": 1, "No": 0}
49
+ car_map = {"Yes": 1, "No": 0}
50
+
51
+ # Feature engineering (matching training data encoding)
52
+ if monthly_income <= 15000:
53
+ income_category = 0 # Low
54
+ elif monthly_income <= 25000:
55
+ income_category = 1 # Medium
56
+ elif monthly_income <= 35000:
57
+ income_category = 2 # High
58
+ else:
59
+ income_category = 3 # Very High
60
+
61
+ if age <= 25:
62
+ age_group = 0 # Young
63
+ elif age <= 35:
64
+ age_group = 1 # Adult
65
+ elif age <= 45:
66
+ age_group = 2 # Middle-aged
67
+ elif age <= 55:
68
+ age_group = 3 # Senior
69
+ else:
70
+ age_group = 4 # Elderly
71
+
72
+ # Create input array
73
+ input_array = np.array([[
74
+ age, contact_map[type_of_contact], city_tier, duration_of_pitch,
75
+ occupation_map[occupation], gender_map[gender], num_person_visiting,
76
+ num_followups, product_map[product_pitched], preferred_property_star,
77
+ marital_map[marital_status], num_trips, passport_map[passport],
78
+ pitch_satisfaction_score, car_map[own_car], num_children_visiting,
79
+ designation_map[designation], monthly_income, income_category, age_group
80
+ ]])
81
+
82
+ return input_array
83
+
84
+ def main():
85
+ """Main Streamlit app"""
86
+
87
+ st.title("Tourism Package Prediction")
88
+ st.markdown("### Predict Customer Purchase Likelihood for Wellness Tourism Package")
89
+ st.markdown("---")
90
+
91
+ # Load model
92
+ model = load_model()
93
+ if model is None:
94
+ st.error("Failed to load the prediction model.")
95
+ return
96
+
97
+ # Sidebar inputs
98
+ st.sidebar.header("Customer Information")
99
+
100
+ # Demographics
101
+ st.sidebar.subheader("Demographics")
102
+ age = st.sidebar.slider("Age", 18, 80, 35)
103
+ gender = st.sidebar.selectbox("Gender", ["Male", "Female"])
104
+ marital_status = st.sidebar.selectbox("Marital Status", ["Single", "Married", "Divorced", "Unmarried"])
105
+
106
+ # Location & Contact
107
+ st.sidebar.subheader("Location & Contact")
108
+ city_tier = st.sidebar.selectbox("City Tier", [1, 2, 3])
109
+ type_of_contact = st.sidebar.selectbox("Type of Contact", ["Self Enquiry", "Company Invited"])
110
+
111
+ # Professional Info
112
+ st.sidebar.subheader("Professional Info")
113
+ occupation = st.sidebar.selectbox("Occupation", ["Salaried", "Small Business", "Free Lancer"])
114
+ designation = st.sidebar.selectbox("Designation", ["Executive", "Manager", "Senior Manager", "AVP", "VP"])
115
+ monthly_income = st.sidebar.number_input("Monthly Income", 10000, 50000, 20000)
116
+
117
+ # Travel Preferences
118
+ st.sidebar.subheader("Travel Preferences")
119
+ num_person_visiting = st.sidebar.slider("Number of Persons Visiting", 1, 5, 2)
120
+ num_children_visiting = st.sidebar.slider("Number of Children Visiting", 0, 3, 0)
121
+ preferred_property_star = st.sidebar.slider("Preferred Property Star Rating", 1.0, 5.0, 3.0, 0.5)
122
+ num_trips = st.sidebar.slider("Number of Trips per Year", 0.0, 10.0, 2.0, 0.5)
123
+
124
+ # Additional Info
125
+ st.sidebar.subheader("Additional Info")
126
+ passport = st.sidebar.selectbox("Has Passport", ["Yes", "No"])
127
+ own_car = st.sidebar.selectbox("Owns Car", ["Yes", "No"])
128
+
129
+ # Sales Interaction
130
+ st.sidebar.subheader("Sales Interaction")
131
+ duration_of_pitch = st.sidebar.slider("Duration of Pitch (minutes)", 5, 60, 15)
132
+ product_pitched = st.sidebar.selectbox("Product Pitched", ["Basic", "Standard", "Deluxe", "Super Deluxe"])
133
+ num_followups = st.sidebar.slider("Number of Followups", 0.0, 6.0, 3.0, 0.5)
134
+ pitch_satisfaction_score = st.sidebar.slider("Pitch Satisfaction Score", 1, 5, 3)
135
+
136
+ # Main content
137
+ col1, col2 = st.columns([2, 1])
138
+
139
+ with col1:
140
+ st.subheader("Customer Profile Summary")
141
+ profile_data = {
142
+ "Age": age,
143
+ "Gender": gender,
144
+ "Marital Status": marital_status,
145
+ "City Tier": city_tier,
146
+ "Occupation": occupation,
147
+ "Monthly Income": f"₹{monthly_income:,}",
148
+ "Number of Persons": num_person_visiting,
149
+ "Preferred Star Rating": preferred_property_star,
150
+ "Annual Trips": num_trips,
151
+ "Has Passport": passport,
152
+ "Owns Car": own_car
153
+ }
154
+
155
+ for key, value in profile_data.items():
156
+ st.write(f"**{key}:** {value}")
157
+
158
+ with col2:
159
+ st.subheader("Prediction")
160
+
161
+ if st.button("Predict Purchase Likelihood", type="primary"):
162
+ input_data = prepare_input_data(
163
+ age, gender, marital_status, city_tier, type_of_contact,
164
+ occupation, designation, monthly_income, num_person_visiting,
165
+ num_children_visiting, preferred_property_star, num_trips,
166
+ passport, own_car, duration_of_pitch, product_pitched,
167
+ num_followups, pitch_satisfaction_score
168
+ )
169
+
170
+ try:
171
+ prediction = model.predict(input_data)[0]
172
+ prediction_proba = model.predict_proba(input_data)[0]
173
+
174
+ if prediction == 1:
175
+ st.success("High likelihood of purchase!")
176
+ st.write(f"**Confidence:** {prediction_proba[1]:.2%}")
177
+ st.balloons()
178
+ else:
179
+ st.warning("Low likelihood of purchase")
180
+ st.write(f"**Confidence:** {prediction_proba[0]:.2%}")
181
+
182
+ # Probability breakdown
183
+ st.subheader("Probability Breakdown")
184
+ prob_df = pd.DataFrame({
185
+ 'Outcome': ['Will Not Purchase', 'Will Purchase'],
186
+ 'Probability': [prediction_proba[0], prediction_proba[1]]
187
+ })
188
+ st.bar_chart(prob_df.set_index('Outcome'))
189
+
190
+ except Exception as e:
191
+ st.error(f"Prediction error: {e}")
192
+
193
+ st.markdown("---")
194
+ st.markdown("### About This Model")
195
+ st.info("""
196
+ This ML model predicts customer purchase likelihood for the Wellness Tourism Package
197
+ based on demographics, travel preferences, and sales interaction data.
198
+ """)
199
+
200
+ if __name__ == "__main__":
201
+ main()