Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +104 -37
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,107 @@
|
|
| 1 |
-
import
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 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 numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Smartphone Price Range Classification", page_icon="📱", layout="wide")
|
| 7 |
+
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_artifacts():
|
| 10 |
+
model = joblib.load("src/knn_model.pkl")
|
| 11 |
+
scaler = joblib.load("src/scaler.pkl")
|
| 12 |
+
features = joblib.load("src/model_features.pkl")
|
| 13 |
+
return model, scaler, features
|
| 14 |
+
|
| 15 |
+
model, scaler, feature_names = load_artifacts()
|
| 16 |
+
|
| 17 |
+
PRICE_LABELS = {
|
| 18 |
+
0: "Low Cost",
|
| 19 |
+
1: "Medium Cost",
|
| 20 |
+
2: "High Cost",
|
| 21 |
+
3: "Very High Cost"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
st.title("📱 Smartphone Price Range Classification")
|
| 25 |
+
st.write(
|
| 26 |
+
"This app predicts the **price range** of a smartphone based on its technical specifications "
|
| 27 |
+
"using a **K-Nearest Neighbors (KNN)** model."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
with st.sidebar:
|
| 31 |
+
st.header("Input Features")
|
| 32 |
+
|
| 33 |
+
use_example = st.checkbox("Use example values", value=True)
|
| 34 |
+
|
| 35 |
+
# Sensible defaults (example-like)
|
| 36 |
+
default_vals = {
|
| 37 |
+
"battery_power": 1000,
|
| 38 |
+
"blue": 1,
|
| 39 |
+
"clock_speed": 1.5,
|
| 40 |
+
"dual_sim": 1,
|
| 41 |
+
"fc": 5,
|
| 42 |
+
"four_g": 1,
|
| 43 |
+
"int_memory": 32,
|
| 44 |
+
"m_dep": 0.5,
|
| 45 |
+
"mobile_wt": 150,
|
| 46 |
+
"n_cores": 4,
|
| 47 |
+
"pc": 12,
|
| 48 |
+
"px_height": 1000,
|
| 49 |
+
"px_width": 1800,
|
| 50 |
+
"ram": 3000,
|
| 51 |
+
"sc_h": 14,
|
| 52 |
+
"sc_w": 7,
|
| 53 |
+
"talk_time": 12,
|
| 54 |
+
"three_g": 1,
|
| 55 |
+
"touch_screen": 1,
|
| 56 |
+
"wifi": 1
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
inputs = {}
|
| 60 |
+
for f in feature_names:
|
| 61 |
+
# fallback if feature not in defaults
|
| 62 |
+
base = default_vals.get(f, 0)
|
| 63 |
+
|
| 64 |
+
if f in ["blue", "dual_sim", "four_g", "three_g", "touch_screen", "wifi"]:
|
| 65 |
+
inputs[f] = st.selectbox(f, [0, 1], index=int(base))
|
| 66 |
+
elif f in ["clock_speed", "m_dep"]:
|
| 67 |
+
inputs[f] = st.number_input(f, value=float(base), min_value=0.0, step=0.1)
|
| 68 |
+
else:
|
| 69 |
+
inputs[f] = st.number_input(f, value=float(base), min_value=0.0, step=1.0)
|
| 70 |
+
|
| 71 |
+
st.caption("Tip: Keep the same feature meaning as in the dataset.")
|
| 72 |
+
|
| 73 |
+
# Build input dataframe in correct order
|
| 74 |
+
X_input = pd.DataFrame([[inputs[f] for f in feature_names]], columns=feature_names)
|
| 75 |
+
|
| 76 |
+
col1, col2 = st.columns([1, 1])
|
| 77 |
+
|
| 78 |
+
with col1:
|
| 79 |
+
st.subheader("Your input")
|
| 80 |
+
st.dataframe(X_input, use_container_width=True)
|
| 81 |
+
|
| 82 |
+
with col2:
|
| 83 |
+
st.subheader("Prediction")
|
| 84 |
+
|
| 85 |
+
# Scale then predict
|
| 86 |
+
X_scaled = scaler.transform(X_input.values)
|
| 87 |
+
pred_class = int(model.predict(X_scaled)[0])
|
| 88 |
+
pred_label = PRICE_LABELS.get(pred_class, str(pred_class))
|
| 89 |
+
|
| 90 |
+
st.metric("Predicted Price Range", f"{pred_class} — {pred_label}")
|
| 91 |
+
|
| 92 |
+
# Probabilities (if available)
|
| 93 |
+
if hasattr(model, "predict_proba"):
|
| 94 |
+
proba = model.predict_proba(X_scaled)[0]
|
| 95 |
+
proba_df = pd.DataFrame(
|
| 96 |
+
{"Class": list(range(len(proba))),
|
| 97 |
+
"Label": [PRICE_LABELS.get(i, str(i)) for i in range(len(proba))],
|
| 98 |
+
"Probability": proba}
|
| 99 |
+
).sort_values("Probability", ascending=False)
|
| 100 |
+
|
| 101 |
+
st.write("Class probabilities:")
|
| 102 |
+
st.dataframe(proba_df, use_container_width=True)
|
| 103 |
|
| 104 |
+
st.divider()
|
| 105 |
+
st.write(
|
| 106 |
+
"Model files expected in the same folder: `knn_model.pkl`, `scaler.pkl`, `model_features.pkl`."
|
| 107 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|