SandeepMM's picture
Upload folder using huggingface_hub
eebb89e verified
import streamlit as st
import pandas as pd
from huggingface_hub import hf_hub_download
import joblib
# Download and load the model
#model_path = hf_hub_download(repo_id="SandeepMM/GL-MLOps-VisitWithUs", filename="best_visitwithus_model_v1.joblib")
try:
model_repo_id = "SandeepMM/GL-MLOps-VisitWithUs"
model_filename = "best_visitwithus_model_v1.joblib"
# Use hf_hub_download for reliable model artifact fetching
model_path = hf_hub_download(repo_id=model_repo_id, filename=model_filename)
model = joblib.load(model_path)
st.sidebar.success("Model loaded successfully!")
except Exception as e:
st.sidebar.error(f"Error loading model: {e}")
model = None
# Streamlit UI for Machine Failure Prediction
st.title("Visit With Us! Tourism App")
st.write("""
This application predicts the likelihood of a customer buying a tourism package.
Please enter the customer data below to get a prediction.
""")
# --- User Input Fields (Using snake_case for variables) ---
st.header("Customer Profile")
age = st.number_input("Age", min_value=18, max_value=120, value=30, step=1)
gender = st.selectbox("Gender", ['Female','Male'], index=0)
marital_status = st.selectbox("Marital Status", ['Unmarried','Married','Divorced'], index=0)
occupation = st.selectbox("Occupation", ['Large Business','Salaried','Small Business'], index=1)
designation = st.selectbox("Designation", ['Executive','Manager','Senior Manager','AVP','VP'], index=0)
monthly_income = st.number_input("Monthly Income", min_value=1000, max_value=100000, value=25000, step=1000)
number_of_person_visiting = st.number_input("Number Of Person Visiting", min_value=1, max_value=5, value=2, step=1)
number_of_children_visiting = st.number_input("Number Of Children Visiting", min_value=0, max_value=5, value=2, step=1)
city_tier = st.number_input("City Tier", min_value=1, max_value=3, value=1, step=1)
passport = st.number_input("Passport (0=No, 1=Yes)", min_value=0, max_value=1, value=0, step=1)
own_car = st.number_input("Own a Car (0=No, 1=Yes)", min_value=0, max_value=1, value=0, step=1)
preferred_property_star = st.number_input("Preferred Property Star (1 to 5)", min_value=1, max_value=5, value=3, step=1)
st.header("Trip Details")
number_of_trips = st.number_input("Number Of Trips Taken Previously", min_value=1, max_value=22, value=5, step=1)
type_of_contact = st.selectbox("Type of Contact", ['Company Invited','Self Enquiry'], index=0)
product_pitched = st.selectbox("Product Pitched", ['Basic','Deluxe','Standard','Super Deluxe','King'], index=1)
duration_of_pitch = st.number_input("Duration Of Pitch (minutes)", min_value=5, max_value=127, value=15, step=1)
pitch_satisfaction_score = st.number_input("Pitch Satisfaction Score (1 to 5)", min_value=1, max_value=5, value=3, step=1)
number_of_followups = st.number_input("Number Of Followups", min_value=1, max_value=6, value=2, step=1)
# Assemble input into DataFrame (column names must match training data features)
input_data = pd.DataFrame([{
'Age': age,
'Gender': gender,
'MaritalStatus': marital_status,
'Occupation': occupation,
'Designation': designation,
'MonthlyIncome': monthly_income,
'NumberOfPersonVisiting': number_of_person_visiting,
'NumberOfChildrenVisiting': number_of_children_visiting,
'CityTier': city_tier,
'Passport': passport,
'OwnCar': own_car,
'PreferredPropertyStar': preferred_property_star,
'NumberOfTrips': number_of_trips,
'TypeofContact': type_of_contact,
'ProductPitched': product_pitched,
'DurationOfPitch': duration_of_pitch,
'PitchSatisfactionScore': pitch_satisfaction_score,
'NumberOfFollowups': number_of_followups,
}])
# Set a consistent classification threshold
CLASSIFICATION_THRESHOLD = 0.4951
if st.button("Predict Package Purchase"):
if model is not None:
# Get the probability of the positive class (ProdTaken=1)
prediction_proba = model.predict_proba(input_data)[:, 1][0]
# Apply the optimized classification threshold
prediction = 1 if prediction_proba >= CLASSIFICATION_THRESHOLD else 0
result = "Customer Purchase Potential! (Likely to buy)" if prediction == 1 else "No Sale (Unlikely to buy)"
st.subheader("Prediction Result:")
if prediction == 1:
st.success(f"The model predicts: **{result}**")
else:
st.warning(f"The model predicts: **{result}**")
st.info(f"Probability of Purchase: **{prediction_proba:.4f}**")
else:
st.error("Cannot predict: Model failed to load.")