Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +99 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,101 @@
|
|
| 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 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load Model
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_artifacts():
|
| 9 |
+
try:
|
| 10 |
+
return joblib.load('src/backpack_model.joblib')
|
| 11 |
+
except FileNotFoundError:
|
| 12 |
+
st.error("Model file 'backpack_model.joblib' not found. Please upload it to the Files section.")
|
| 13 |
+
return None
|
| 14 |
+
|
| 15 |
+
artifacts = load_artifacts()
|
| 16 |
+
|
| 17 |
+
if artifacts:
|
| 18 |
+
model = artifacts['model']
|
| 19 |
+
encoders = artifacts['encoders']
|
| 20 |
+
size_mapping = artifacts['size_mapping']
|
| 21 |
+
medians = artifacts.get('medians', {})
|
| 22 |
+
|
| 23 |
+
# Page Configuration
|
| 24 |
+
st.set_page_config(
|
| 25 |
+
page_title="Backpack Price Predictor",
|
| 26 |
+
page_icon="π",
|
| 27 |
+
layout="centered")
|
| 28 |
+
|
| 29 |
+
st.title("π Backpack Price Prediction AI")
|
| 30 |
+
st.markdown("""
|
| 31 |
+
This AI tool predicts the price of a backpack based on its features.
|
| 32 |
+
\n**Adjust the values below and click 'Predict Price'.**
|
| 33 |
+
""")
|
| 34 |
+
|
| 35 |
+
# Input
|
| 36 |
+
if artifacts:
|
| 37 |
+
with st.form("prediction_form"):
|
| 38 |
+
st.subheader("1. Backpack Features")
|
| 39 |
+
|
| 40 |
+
col1, col2 = st.columns(2)
|
| 41 |
+
|
| 42 |
+
with col1:
|
| 43 |
+
brand = st.selectbox("Brand", options=encoders['Brand'].classes_)
|
| 44 |
+
material = st.selectbox("Material", options=encoders['Material'].classes_)
|
| 45 |
+
style = st.selectbox("Style", options=encoders['Style'].classes_)
|
| 46 |
+
color = st.selectbox("Color", options=encoders['Color'].classes_)
|
| 47 |
+
|
| 48 |
+
with col2:
|
| 49 |
+
size = st.selectbox("Size", options=['Small', 'Medium', 'Large', 'Unknown'])
|
| 50 |
+
compartments = st.number_input("Number of Compartments", min_value=0, max_value=20, value=5, step=1)
|
| 51 |
+
weight_capacity = st.number_input("Weight Capacity (kg)", min_value=0.0, max_value=100.0, value=10.0, step=0.5)
|
| 52 |
+
|
| 53 |
+
st.subheader("2. Extra Features")
|
| 54 |
+
col3, col4 = st.columns(2)
|
| 55 |
+
with col3:
|
| 56 |
+
laptop = st.selectbox("Laptop Compartment?", options=['Yes', 'No'])
|
| 57 |
+
with col4:
|
| 58 |
+
waterproof = st.selectbox("Waterproof?", options=['Yes', 'No'])
|
| 59 |
+
|
| 60 |
+
submit_btn = st.form_submit_button("Predict Price π", type="primary")
|
| 61 |
+
|
| 62 |
+
# Prediction
|
| 63 |
+
if submit_btn:
|
| 64 |
+
input_data = {
|
| 65 |
+
'Brand': brand,
|
| 66 |
+
'Material': material,
|
| 67 |
+
'Style': style,
|
| 68 |
+
'Color': color,
|
| 69 |
+
'Laptop Compartment': laptop,
|
| 70 |
+
'Waterproof': waterproof,
|
| 71 |
+
'Size': size,
|
| 72 |
+
'Compartments': compartments,
|
| 73 |
+
'Weight Capacity (kg)': weight_capacity}
|
| 74 |
+
|
| 75 |
+
df_input = pd.DataFrame([input_data])
|
| 76 |
+
|
| 77 |
+
try:
|
| 78 |
+
|
| 79 |
+
df_input['Size_Encoded'] = df_input['Size'].map(size_mapping).fillna(-1)
|
| 80 |
+
|
| 81 |
+
encode_cols = ['Brand', 'Material', 'Style', 'Color', 'Laptop Compartment', 'Waterproof']
|
| 82 |
+
|
| 83 |
+
for col in encode_cols:
|
| 84 |
+
df_input[f'{col}_Encoded'] = encoders[col].transform([input_data[col]])
|
| 85 |
+
|
| 86 |
+
model_features = model.get_booster().feature_names
|
| 87 |
+
|
| 88 |
+
X_pred = df_input[model_features]
|
| 89 |
+
|
| 90 |
+
prediction = model.predict(X_pred)[0]
|
| 91 |
+
|
| 92 |
+
st.success(f"π° Estimated Price: **${prediction:.2f}**")
|
| 93 |
+
|
| 94 |
+
st.progress(min(int(prediction), 300) / 300)
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
st.error(f"An error occurred during prediction: {e}")
|
| 98 |
+
st.warning("Technical Detail: Ensure the input categories match the training data.")
|
| 99 |
|
| 100 |
+
else:
|
| 101 |
+
st.warning("Please upload the 'backpack_model.joblib' file to the app files.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|