| import streamlit as st |
| import pandas as pd |
| import numpy as np |
| import joblib |
|
|
| st.set_page_config(page_title="Tip Prediction", layout="centered") |
| |
| model = joblib.load('taximodel.pkl') |
|
|
| |
| |
|
|
| |
| st.title("Tip Predictor") |
| st.write("Enter the details of your taxi ride to predict the tip amount.") |
|
|
| |
| 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) |
|
|
| |
| submitted = st.form_submit_button("Predict Tip") |
|
|
| |
| if submitted: |
| input_df = pd.DataFrame([{ |
| 'total_bill': total_bill, |
| 'sex': sex, |
| 'smoker': smoker, |
| 'day': day, |
| 'time': time, |
| 'size': size |
| }]) |
|
|
| try: |
| |
| prediction = model.predict(input_df) |
|
|
| |
| predicted_tip = prediction[0] if isinstance(prediction,(list, np.ndarray)) else prediction |
|
|
| |
| st.success(f"Predicted Tip Amount: **${predicted_tip:.2f}**") |
| except Exception as e: |
| st.error(f"Error: {str(e)}") |