Update src/streamlit_app.py
Browse files- src/streamlit_app.py +49 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,50 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Tip Prediction", layout="centered")
|
| 7 |
+
# Load the trained model
|
| 8 |
+
model = joblib.load('taximodel.pkl')
|
| 9 |
+
|
| 10 |
+
#Streamlit UI configuration - Must be the first Streamlit command in your script
|
| 11 |
+
#st.set_page_config(page_title="Tip Prediction, layout = 'centered')
|
| 12 |
+
|
| 13 |
+
# Title of the app
|
| 14 |
+
st.title("Tip Predictor")
|
| 15 |
+
st.write("Enter the details of your taxi ride to predict the tip amount.")
|
| 16 |
+
|
| 17 |
+
# Streamlit UI to take inputs
|
| 18 |
+
with st.form("tip_form"):
|
| 19 |
+
total_bill = st.slider("Total Bill ($)", min_value=0.0, max_value=500.0, value=20.0)
|
| 20 |
+
sex = st.selectbox("Sex", ["Male", "Female"])
|
| 21 |
+
smoker = st.selectbox("Smoker", ["Yes", "No"])
|
| 22 |
+
day = st.selectbox("Day of the Week", ["Thur", "Fri", "Sat", "Sun"])
|
| 23 |
+
time = st.selectbox("Time of Day", ["Lunch", "Dinner"])
|
| 24 |
+
size = st.number_input("Party Size", min_value=1, value=2)
|
| 25 |
+
|
| 26 |
+
# Submit button
|
| 27 |
+
submitted = st.form_submit_button("Predict Tip")
|
| 28 |
+
|
| 29 |
+
#Prediction on form submission
|
| 30 |
+
if submitted:
|
| 31 |
+
input_df = pd.DataFrame([{
|
| 32 |
+
'total_bill': total_bill,
|
| 33 |
+
'sex': sex,
|
| 34 |
+
'smoker': smoker,
|
| 35 |
+
'day': day,
|
| 36 |
+
'time': time,
|
| 37 |
+
'size': size
|
| 38 |
+
}])
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
# predict the tip
|
| 42 |
+
prediction = model.predict(input_df)
|
| 43 |
+
|
| 44 |
+
#Ensure the output is a scaler value
|
| 45 |
+
predicted_tip = prediction[0] if isinstance(prediction,(list, np.ndarray)) else prediction
|
| 46 |
+
|
| 47 |
+
# Display the predicted tip
|
| 48 |
+
st.success(f"Predicted Tip Amount: **${predicted_tip:.2f}**")
|
| 49 |
+
except Exception as e:
|
| 50 |
+
st.error(f"Error: {str(e)}")
|