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)}")