Divija89/Tips-predictor-model1
1
1import streamlit as st
2import pandas as pd
3import joblib
4import numpy as np
5
6st.set_page_config(page_title=" Taxi Tip Predictor", layout="centered")
7
8
9st.title("Tip prediction ")
10st.write("Enter the details below to predict the expected tip amount.")
11
12# Load the trained model (should be a pipeline)
13model = joblib.load("tips.pkl")
14
15# Streamlit UI to take inputs
16with st.form("tip_form"):
17 total_bill = st.slider("Total Bill ($)", min_value=0.0, max_value=500.00,value=20.0)
18 sex = st.selectbox("Sex", ["Male", "Female"])
19 smoker = st.selectbox("Smoker", ["Yes", "No"])
20 day = st.selectbox("Day", ["Thur", "Fri", "Sat", "Sun"])
21 time = st.selectbox("Time", ["Lunch", "Dinner"])
22 size = st.number_input("Party Size", min_value=1, value=2)
23
24 # Submit button
25 submitted = st.form_submit_button("Predict Tip")
26
27# Prediction on form submission
28if submitted:
29 input_df = pd.DataFrame([{
30 'total_bill': total_bill,
31 'sex': sex,
32 'smoker': smoker,
33 'day': day,
34 'time': time,
35 'size': size
36 }])
37
38 # Print input data
39 #st.write("Input Data:")
40 #st.dataframe(input_df)
41
42 # Check the model type again just before prediction
43 #st.write(f"Model type before prediction: {type(model)}") # Should show <class 'sklearn.pipeline.Pipeline'>
44
45 try:
46 # Predict the tip
47 prediction = model.predict(input_df)
48
49 # Ensure the output is a scalar value
50 predicted_tip = prediction[0] if isinstance(prediction, (list, np.ndarray)) else prediction
51
52 # Display the predicted tip
53 st.success(f"Predicted Tip: *${predicted_tip:.2f}*")
54 except Exception as e:
55
56 st.error(f" Error: {str(e)}")