deva8217/tourism-wellness-app
0
1 2import streamlit as st3from pathlib import Path4Path.home().joinpath('.streamlit').mkdir(parents=True, exist_ok=True)5import pandas as pd6import json7import joblib8from huggingface_hub import snapshot_download9from pathlib import Path10import os11 12st.set_page_config(page_title="Wellness Package Purchase Prediction", layout="centered")13 14HF_USERNAME = os.environ.get("HF_USERNAME", "")15HF_MODEL_REPO = os.environ.get("HF_MODEL_REPO", "")16MODEL_ID = f"{HF_USERNAME}/{HF_MODEL_REPO}" if HF_USERNAME and HF_MODEL_REPO else None17 18@st.cache_resource19def load_model_from_hub():20 assert MODEL_ID is not None, "Set HF_USERNAME and HF_MODEL_REPO in environment"21 local_dir = snapshot_download(repo_id=MODEL_ID, repo_type="model", local_dir="hf_model")22 model = joblib.load(Path(local_dir)/"best_model.joblib")23 with open(Path(local_dir)/"threshold.json") as f:24 threshold = json.load(f).get("threshold", 0.5)25 return model, threshold26 27st.title("๐ง Wellness Tourism โ Purchase Predictor")28st.write("This app loads the **best model** from the Hugging Face Model Hub and predicts the purchase probability.")29 30with st.form("input_form"):31 col1, col2 = st.columns(2)32 with col1:33 Age = st.number_input("Age", min_value=0, max_value=120, value=35)34 CityTier = st.text_input("CityTier", value="1")35 Occupation = st.text_input("Occupation", value="Salaried")36 Gender = st.text_input("Gender", value="Male")37 NumberOfPersonVisiting = st.number_input("NumberOfPersonVisiting", min_value=0, max_value=20, value=2)38 PreferredPropertyStar = st.number_input("PreferredPropertyStar", min_value=1, max_value=5, value=3)39 MaritalStatus = st.text_input("MaritalStatus", value="Married")40 NumberOfTrips = st.number_input("NumberOfTrips", min_value=0, max_value=50, value=2)41 with col2:42 Passport = st.number_input("Passport (0/1)", min_value=0, max_value=1, value=1)43 OwnCar = st.number_input("OwnCar (0/1)", min_value=0, max_value=1, value=1)44 NumberOfChildrenVisiting = st.number_input("NumberOfChildrenVisiting", min_value=0, max_value=10, value=0)45 Designation = st.text_input("Designation", value="Executive")46 MonthlyIncome = st.number_input("MonthlyIncome", min_value=0, value=50000, step=1000)47 PitchSatisfactionScore = st.number_input("PitchSatisfactionScore", min_value=0, max_value=10, value=7)48 ProductPitched = st.text_input("ProductPitched", value="Basic")49 NumberOfFollowups = st.number_input("NumberOfFollowups", min_value=0, max_value=20, value=2)50 DurationOfPitch = st.number_input("DurationOfPitch", min_value=0, max_value=300, value=30)51 TypeofContact = st.text_input("TypeofContact", value="Company Invited")52 53 submitted = st.form_submit_button("Predict")54 55if submitted:56 model, threshold = load_model_from_hub()57 data = {58 "Age": Age, "TypeofContact": TypeofContact, "CityTier": CityTier, "Occupation": Occupation,59 "Gender": Gender, "NumberOfPersonVisiting": NumberOfPersonVisiting, "PreferredPropertyStar": PreferredPropertyStar,60 "MaritalStatus": MaritalStatus, "NumberOfTrips": NumberOfTrips, "Passport": Passport, "OwnCar": OwnCar,61 "NumberOfChildrenVisiting": NumberOfChildrenVisiting, "Designation": Designation, "MonthlyIncome": MonthlyIncome,62 "PitchSatisfactionScore": PitchSatisfactionScore, "ProductPitched": ProductPitched, "NumberOfFollowups": NumberOfFollowups,63 "DurationOfPitch": DurationOfPitch64 }65 X = pd.DataFrame([data])66 proba = float(model.predict_proba(X)[0, 1])67 label = int(proba >= threshold)68 69 st.subheader("Prediction")70 st.json({"purchase_probability": proba, "will_purchase": label, "threshold_used": threshold})71 