CoolFace
Apppublic

mrjohn1134/Tips_Model_Predictor1

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
Tips_model.py48 linesDownload Raw Back to root
1import streamlit as st
2import pandas as pd
3import joblib
4import numpy as np
5import os
6
7st.set_page_config(page_title="Tip Predictor", layout="centered")
8st.title("Taxi Tip Predictor")
9st.write("Enter the details below to predict the expected tip amount.")
10
11# For debugging, print current working directory
12st.write("Current working directory:", os.getcwd())
13
14# ✅ Correct path usage
15model_path = os.path.join("Deployment", "tipsmodel.pkl")
16
17try:
18    model = joblib.load(model_path)
19except FileNotFoundError:
20    st.error(f"Model file '{model_path}' not found. Please verify the filename and path.")
21    st.stop()
22
23with st.form("tip_form"):
24    total_bill = st.slider("Total Bill ($)", 0.0, 500.0, 20.0)
25    sex = st.selectbox("Sex", ["Male", "Female"])
26    smoker = st.selectbox("Smoker", ["Yes", "No"])
27    day = st.selectbox("Day", ["Thur", "Fri", "Sat", "Sun"])
28    time = st.selectbox("Time", ["Lunch", "Dinner"])
29    size = st.number_input("Party Size", min_value=1, value=2)
30
31    submitted = st.form_submit_button("Predict Tip")
32
33if submitted:
34    input_df = pd.DataFrame([{
35        'total_bill': total_bill,
36        'sex': sex,
37        'smoker': smoker,
38        'day': day,
39        'time': time,
40        'size': size
41    }])
42
43    try:
44        prediction = model.predict(input_df)
45        predicted_tip = prediction[0] if isinstance(prediction, (list, np.ndarray)) else prediction
46        st.success(f"Predicted Tip: ${predicted_tip:.2f}")
47    except Exception as e:
48        st.error(f"Error during prediction: {e}")