SagarAtHf commited on
Commit
33a4490
·
verified ·
1 Parent(s): 5bc4d50

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +71 -61
app.py CHANGED
@@ -1,84 +1,94 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import joblib
4
- import os
5
  from huggingface_hub import hf_hub_download
6
 
7
- # 1. Configuration - Matching your train.py setup
8
- REPO_ID = "SagarAtHf/tourismpackagepredict-model"
9
  FILENAME = "productionmodel.joblib"
10
 
11
- @st.cache_resource # This ensures the model only downloads once, not on every click
12
  def load_model():
13
- try:
14
- # Pulling the model from the Model Hub
15
- model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
16
- return joblib.load(model_path)
17
- except Exception as e:
18
- st.error(f"Error loading model from Hub: {e}")
19
- return None
20
 
21
- # Load the model
22
  model = load_model()
23
 
24
- # 2. UI Header
25
- st.title("🌴 Wellness Tourism Package Predictor")
26
- st.markdown("Enter customer details below to predict the likelihood of a package purchase.")
27
 
28
- # 3. User Input Form
29
  with st.form("prediction_form"):
30
- col1, col2 = st.columns(2)
 
31
 
32
- with col1:
33
- age = st.number_input("Age", min_value=18, max_value=100, value=30)
 
34
  city_tier = st.selectbox("City Tier", [1, 2, 3])
 
35
  occupation = st.selectbox("Occupation", ["Salaried", "Small Business", "Large Business", "Free Lancer"])
 
 
36
  gender = st.selectbox("Gender", ["Male", "Female"])
37
- duration = st.number_input("Duration of Pitch", value=15)
38
-
39
- with col2:
 
 
 
40
  marital_status = st.selectbox("Marital Status", ["Married", "Unmarried", "Divorced"])
41
- designation = st.selectbox("Designation", ["Manager", "Executive", "Senior Manager", "AVP", "VP"])
42
- product_pitched = st.selectbox("Product Pitched", ["Deluxe", "Basic", "Standard", "Super Deluxe", "King"])
43
- monthly_income = st.number_input("Monthly Income", value=20000)
44
  passport = st.selectbox("Has Passport?", [0, 1], format_func=lambda x: "Yes" if x==1 else "No")
 
 
45
 
46
- # Additional features required by the model (using averages/defaults)
47
- submit = st.form_submit_button("Predict Probability")
 
 
48
 
49
- # 4. Prediction Logic
50
- if submit and model:
51
- # Prepare input dataframe with exact column names from training
52
- input_data = pd.DataFrame({
53
- 'Age': [age],
54
- 'CityTier': [city_tier],
55
- 'DurationOfPitch': [duration],
56
- 'Occupation': [occupation],
57
- 'Gender': [gender],
58
- 'NumberOfPersonVisiting': [2], # Defaulting common values
59
- 'NumberOfFollowups': [3],
60
- 'ProductPitched': [product_pitched],
61
- 'PreferredPropertyStar': [3],
62
- 'MaritalStatus': [marital_status],
63
- 'NumberOfTrips': [1],
64
- 'Passport': [passport],
65
- 'PitchSatisfactionScore': [3],
66
- 'OwnCar': [1],
67
- 'NumberOfChildrenVisiting': [0],
68
- 'Designation': [designation],
69
- 'MonthlyIncome': [monthly_income],
70
- 'TypeofContact': ["Self Enquiry"]
71
- })
72
 
73
- # Get probability from the model
74
- # Note: We use the threshold 0.45 you defined in your local tests
75
- prob = model.predict_proba(input_data)[0][1]
76
- prediction = 1 if prob >= 0.45 else 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- # 5. Display Results
79
- st.divider()
80
- if prediction == 1:
81
- st.success(f"🎯 High Potential Customer! (Probability: {prob:.2%})")
82
- st.balloons()
83
- else:
84
- st.warning(f"⏳ Low Likelihood of purchase. (Probability: {prob:.2%})")
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import joblib
 
4
  from huggingface_hub import hf_hub_download
5
 
6
+ # 1. Load Model from Hub
7
+ REPO_ID = "SagarAtHf/wellness-tourism-model-hub"
8
  FILENAME = "productionmodel.joblib"
9
 
10
+ @st.cache_resource
11
  def load_model():
12
+ model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
13
+ return joblib.load(model_path)
 
 
 
 
 
14
 
 
15
  model = load_model()
16
 
17
+ st.set_page_config(page_title="Tourism Predictor", layout="wide")
18
+ st.title("🌴 Full Feature Wellness Tourism Predictor")
19
+ st.write("Please fill in all 19 parameters to get an accurate prediction.")
20
 
21
+ # 2. Complete Form with All 19 Features
22
  with st.form("prediction_form"):
23
+ # Using 4 columns to fit all 19 features neatly
24
+ c1, c2, c3, c4 = st.columns(4)
25
 
26
+ with c1:
27
+ age = st.number_input("Age", 18, 100, 30)
28
+ type_of_contact = st.selectbox("Type of Contact", ["Self Enquiry", "Company Invited"])
29
  city_tier = st.selectbox("City Tier", [1, 2, 3])
30
+ duration_pitch = st.number_input("Duration of Pitch (mins)", 0, 120, 15)
31
  occupation = st.selectbox("Occupation", ["Salaried", "Small Business", "Large Business", "Free Lancer"])
32
+
33
+ with c2:
34
  gender = st.selectbox("Gender", ["Male", "Female"])
35
+ num_person = st.number_input("Number of Persons Visiting", 1, 10, 2)
36
+ num_followups = st.number_input("Number of Follow-ups", 1, 10, 3)
37
+ product_pitched = st.selectbox("Product Pitched", ["Basic", "Deluxe", "Standard", "Super Deluxe", "King"])
38
+ prop_stars = st.slider("Preferred Property Star", 3, 5, 3)
39
+
40
+ with c3:
41
  marital_status = st.selectbox("Marital Status", ["Married", "Unmarried", "Divorced"])
42
+ num_trips = st.number_input("Number of Trips", 1, 20, 1)
 
 
43
  passport = st.selectbox("Has Passport?", [0, 1], format_func=lambda x: "Yes" if x==1 else "No")
44
+ pitch_satisfaction = st.slider("Pitch Satisfaction Score", 1, 5, 3)
45
+ own_car = st.selectbox("Owns a Car?", [0, 1], format_func=lambda x: "Yes" if x==1 else "No")
46
 
47
+ with c4:
48
+ num_children = st.number_input("Number of Children", 0, 5, 0)
49
+ designation = st.selectbox("Designation", ["Executive", "Manager", "Senior Manager", "AVP", "VP"])
50
+ monthly_income = st.number_input("Monthly Income", value=25000)
51
 
52
+ submit = st.form_submit_button("Generate Prediction")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ # 3. Prediction Logic
55
+ if submit:
56
+ # IMPORTANT: Dictionary keys must match the EXACT column names used during training
57
+ data = {
58
+ "Age": age,
59
+ "TypeofContact": type_of_contact,
60
+ "CityTier": city_tier,
61
+ "DurationOfPitch": duration_pitch,
62
+ "Occupation": occupation,
63
+ "Gender": gender,
64
+ "NumberOfPersonVisiting": num_person,
65
+ "NumberOfFollowups": num_followups,
66
+ "ProductPitched": product_pitched,
67
+ "PreferredPropertyStar": prop_stars,
68
+ "MaritalStatus": marital_status,
69
+ "NumberOfTrips": num_trips,
70
+ "Passport": passport,
71
+ "PitchSatisfactionScore": pitch_satisfaction,
72
+ "OwnCar": own_car,
73
+ "NumberOfChildrenVisiting": num_children,
74
+ "Designation": designation,
75
+ "MonthlyIncome": monthly_income
76
+ }
77
+
78
+ input_df = pd.DataFrame([data])
79
 
80
+ # Get the probability
81
+ try:
82
+ # Note: Pipeline applies ColumnTransformer automatically
83
+ prob = model.predict_proba(input_df)[0][1]
84
+
85
+ st.divider()
86
+ if prob >= 0.45:
87
+ st.success(f"### Result: 🎯 High Potential (Prob: {prob:.2%})")
88
+ st.balloons()
89
+ else:
90
+ st.warning(f"### Result: ⏳ Low Likelihood (Prob: {prob:.2%})")
91
+
92
+ except Exception as e:
93
+ st.error(f"Prediction Error: {e}")
94
+ st.info("Ensure the column names in app.py match your training data exactly.")