File size: 1,619 Bytes
7d3bc7f
425c187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import streamlit as st
import pandas as pd
import numpy as np
import joblib

st.set_page_config(page_title="Tip Prediction", layout="centered")
# Load the trained model
model = joblib.load('taximodel.pkl')

#Streamlit UI configuration - Must be the first Streamlit command in your script
#st.set_page_config(page_title="Tip Prediction, layout = 'centered')

# Title of the app
st.title("Tip Predictor")
st.write("Enter the details of your taxi ride to predict the tip amount.")

# Streamlit UI to take inputs
with st.form("tip_form"):
    total_bill = st.slider("Total Bill ($)", min_value=0.0, max_value=500.0, value=20.0)
    sex = st.selectbox("Sex", ["Male", "Female"])
    smoker = st.selectbox("Smoker", ["Yes", "No"])
    day = st.selectbox("Day of the Week", ["Thur", "Fri", "Sat", "Sun"])
    time = st.selectbox("Time of Day", ["Lunch", "Dinner"])
    size = st.number_input("Party Size", min_value=1, value=2)

    # Submit button
    submitted = st.form_submit_button("Predict Tip")

#Prediction on form submission
if submitted:
    input_df = pd.DataFrame([{
        'total_bill': total_bill,
        'sex': sex,
        'smoker': smoker,
        'day': day,
        'time': time,
        'size': size
    }])

    try:
        # predict the tip
        prediction = model.predict(input_df)

        #Ensure the output is a scaler value
        predicted_tip = prediction[0] if isinstance(prediction,(list, np.ndarray)) else prediction

        # Display the predicted tip
        st.success(f"Predicted Tip Amount: **${predicted_tip:.2f}**")
    except Exception as e:
        st.error(f"Error: {str(e)}")