#!/usr/bin/env python3 """ Streamlit App for Tourism Package Prediction """ %%writefile tourism_project/deployment/app.py import streamlit as st import pandas as pd import joblib from huggingface_hub import hf_hub_download import os # Page configuration st.set_page_config( page_title="Tourism Package Prediction", page_icon="✈️", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for better styling st.markdown(""" """, unsafe_allow_html=True) # Cache the model loading to avoid reloading on every interaction @st.cache_resource def load_model_and_preprocessor(): """Load model and preprocessor from HuggingFace""" try: with st.spinner("Loading model and preprocessor from HuggingFace..."): model_path = hf_hub_download( repo_id="dararaje/Tourism_Package_Prediction", filename="tourism_model.pkl", repo_type="model" ) preprocessor_path = hf_hub_download( repo_id="dararaje/Tourism_Package_Prediction", filename="tourism_preprocessor.pkl", repo_type="model" ) model = joblib.load(model_path) preprocessor = joblib.load(preprocessor_path) return model, preprocessor except Exception as e: st.error(f"Error loading model: {str(e)}") st.error("Make sure you have run upload_model.py to upload the model files to HuggingFace") st.stop() # Load model and preprocessor model, preprocessor = load_model_and_preprocessor() # Main title st.markdown('
👤 Customer Demographics
', unsafe_allow_html=True) age = st.number_input("Age", min_value=18, max_value=100, value=30, step=1) gender = st.selectbox("Gender", options=["Male", "Female"]) marital_status = st.selectbox( "Marital Status", options=["Single", "Married", "Divorced", "Unmarried"] ) city_tier = st.selectbox( "City Tier", options=[1, 2, 3], format_func=lambda x: f"Tier {x} {'(Metro)' if x==1 else '(Tier-2)' if x==2 else '(Tier-3)'}" ) occupation = st.selectbox( "Occupation", options=["Salaried", "Small Business", "Large Business", "Free Lancer"] ) designation = st.selectbox( "Designation", options=["Executive", "Manager", "Senior Manager", "AVP", "VP"] ) monthly_income = st.number_input( "Monthly Income (₹)", min_value=0, max_value=1000000, value=20000, step=1000 ) with col2: st.markdown('🧳 Travel Preferences
', unsafe_allow_html=True) num_person_visiting = st.number_input( "Number of Persons Visiting", min_value=1, max_value=10, value=2, step=1 ) num_children_visiting = st.number_input( "Number of Children Visiting (below 5 years)", min_value=0, max_value=5, value=0, step=1 ) preferred_property_star = st.selectbox( "Preferred Property Star Rating", options=[3.0, 4.0, 5.0], format_func=lambda x: f"{int(x)} Star" ) num_trips = st.number_input( "Number of Trips per Year", min_value=0, max_value=20, value=2, step=1 ) passport = st.selectbox( "Has Passport?", options=[1, 0], format_func=lambda x: "Yes" if x == 1 else "No" ) own_car = st.selectbox( "Owns Car?", options=[1, 0], format_func=lambda x: "Yes" if x == 1 else "No" ) with col3: st.markdown('💼 Sales Interaction Details
', unsafe_allow_html=True) type_of_contact = st.selectbox( "Type of Contact", options=["Company Invited", "Self Inquiry"] ) product_pitched = st.selectbox( "Product Pitched", options=["Basic", "Standard", "Deluxe", "Super Deluxe", "King"] ) pitch_satisfaction_score = st.slider( "Pitch Satisfaction Score", min_value=1, max_value=5, value=3, step=1 ) num_followups = st.number_input( "Number of Follow-ups", min_value=0, max_value=10, value=3, step=1 ) duration_of_pitch = st.number_input( "Duration of Pitch (minutes)", min_value=0, max_value=120, value=15, step=1 ) # Center the predict button st.markdown("---") col_button1, col_button2, col_button3 = st.columns([1, 1, 1]) with col_button2: predict_button = st.button("🔮 Predict Purchase Likelihood", type="primary", use_container_width=True) # Prediction logic if predict_button: try: # Create input dataframe input_data = pd.DataFrame({ 'Age': [age], 'TypeofContact': [type_of_contact], 'CityTier': [city_tier], 'Occupation': [occupation], 'Gender': [gender], 'NumberOfPersonVisiting': [num_person_visiting], 'PreferredPropertyStar': [preferred_property_star], 'MaritalStatus': [marital_status], 'NumberOfTrips': [num_trips], 'Passport': [passport], 'OwnCar': [own_car], 'NumberOfChildrenVisiting': [num_children_visiting], 'Designation': [designation], 'MonthlyIncome': [monthly_income], 'PitchSatisfactionScore': [pitch_satisfaction_score], 'ProductPitched': [product_pitched], 'NumberOfFollowups': [num_followups], 'DurationOfPitch': [duration_of_pitch] }) # Preprocess and predict processed_data = preprocessor.transform(input_data) prediction = model.predict(processed_data) probability = model.predict_proba(processed_data) # Display results st.markdown("---") if prediction[0] == 1: st.markdown('Built with ❤️ using Streamlit and HuggingFace | Tourism Package Prediction MLOps Project