Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +162 -41
src/streamlit_app.py
CHANGED
|
@@ -5,7 +5,53 @@ from sklearn.preprocessing import StandardScaler
|
|
| 5 |
from sklearn.neighbors import KNeighborsRegressor
|
| 6 |
|
| 7 |
# ---------------------------
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# ---------------------------
|
| 10 |
|
| 11 |
def generate_dataset(n=400):
|
|
@@ -24,7 +70,6 @@ def generate_dataset(n=400):
|
|
| 24 |
tune = np.random.choice([0, 1, 2]) # none/mild/aggressive
|
| 25 |
altitude = np.random.uniform(0, 2000)
|
| 26 |
|
| 27 |
-
# Synthetic HP gain logic
|
| 28 |
hp_gain = (
|
| 29 |
intake * np.random.uniform(3, 10) +
|
| 30 |
exhaust * np.random.uniform(5, 20) +
|
|
@@ -35,51 +80,98 @@ def generate_dataset(n=400):
|
|
| 35 |
np.random.uniform(-3, 3)
|
| 36 |
)
|
| 37 |
|
| 38 |
-
data.append([
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
columns = [
|
|
|
|
|
|
|
|
|
|
| 41 |
return pd.DataFrame(data, columns=columns)
|
| 42 |
|
| 43 |
df = generate_dataset()
|
| 44 |
|
| 45 |
-
# ---------------------------
|
| 46 |
-
# Train model
|
| 47 |
-
# ---------------------------
|
| 48 |
X = df.drop("hp_gain", axis=1)
|
| 49 |
y = df["hp_gain"]
|
| 50 |
|
| 51 |
scaler = StandardScaler()
|
| 52 |
X_scaled = scaler.fit_transform(X)
|
| 53 |
|
| 54 |
-
model = KNeighborsRegressor(n_neighbors=5, weights=
|
| 55 |
model.fit(X_scaled, y)
|
| 56 |
|
| 57 |
# ---------------------------
|
| 58 |
-
#
|
| 59 |
# ---------------------------
|
| 60 |
|
| 61 |
-
st.
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
fuel = st.selectbox("Fuel Octane", ["87", "91", "93", "E85"])
|
| 74 |
-
tune = st.selectbox("ECU Tune Level", ["None", "Mild", "Aggressive"])
|
| 75 |
-
altitude = st.slider("Altitude (meters)", 0, 2000, 200)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
tune_map = {"None":0, "Mild":1, "Aggressive":2}
|
| 83 |
|
| 84 |
input_data = np.array([[
|
| 85 |
engine,
|
|
@@ -94,22 +186,51 @@ input_data = np.array([[
|
|
| 94 |
]])
|
| 95 |
|
| 96 |
input_scaled = scaler.transform(input_data)
|
| 97 |
-
pred = model.predict(input_scaled)[0]
|
| 98 |
-
|
| 99 |
new_hp = base_hp + pred
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
st.
|
| 105 |
-
st.metric("New Estimated Horsepower", f"{new_hp:.1f} HP")
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
)
|
| 113 |
-
)
|
| 114 |
|
| 115 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from sklearn.neighbors import KNeighborsRegressor
|
| 6 |
|
| 7 |
# ---------------------------
|
| 8 |
+
# Page config & basic styling
|
| 9 |
+
# ---------------------------
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Car Mod Performance Estimator",
|
| 12 |
+
page_icon="π",
|
| 13 |
+
layout="wide"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
st.markdown(
|
| 17 |
+
"""
|
| 18 |
+
<style>
|
| 19 |
+
.main {
|
| 20 |
+
background: radial-gradient(circle at top left, #111827 0, #020617 45%, #000000 100%);
|
| 21 |
+
color: #e5e7eb;
|
| 22 |
+
}
|
| 23 |
+
.stMetric {
|
| 24 |
+
background: #020617;
|
| 25 |
+
padding: 0.75rem 1rem;
|
| 26 |
+
border-radius: 0.75rem;
|
| 27 |
+
border: 1px solid #1f2937;
|
| 28 |
+
}
|
| 29 |
+
.mod-chip {
|
| 30 |
+
display: inline-block;
|
| 31 |
+
padding: 4px 10px;
|
| 32 |
+
margin: 2px 4px 2px 0;
|
| 33 |
+
border-radius: 999px;
|
| 34 |
+
font-size: 0.75rem;
|
| 35 |
+
background: rgba(59,130,246,0.12);
|
| 36 |
+
border: 1px solid rgba(59,130,246,0.4);
|
| 37 |
+
color: #bfdbfe;
|
| 38 |
+
}
|
| 39 |
+
.hp-badge {
|
| 40 |
+
font-size: 0.9rem;
|
| 41 |
+
padding: 4px 10px;
|
| 42 |
+
border-radius: 999px;
|
| 43 |
+
background: rgba(22,163,74,0.1);
|
| 44 |
+
border: 1px solid rgba(22,163,74,0.6);
|
| 45 |
+
color: #bbf7d0;
|
| 46 |
+
display: inline-block;
|
| 47 |
+
}
|
| 48 |
+
</style>
|
| 49 |
+
""",
|
| 50 |
+
unsafe_allow_html=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# ---------------------------
|
| 54 |
+
# Data & model
|
| 55 |
# ---------------------------
|
| 56 |
|
| 57 |
def generate_dataset(n=400):
|
|
|
|
| 70 |
tune = np.random.choice([0, 1, 2]) # none/mild/aggressive
|
| 71 |
altitude = np.random.uniform(0, 2000)
|
| 72 |
|
|
|
|
| 73 |
hp_gain = (
|
| 74 |
intake * np.random.uniform(3, 10) +
|
| 75 |
exhaust * np.random.uniform(5, 20) +
|
|
|
|
| 80 |
np.random.uniform(-3, 3)
|
| 81 |
)
|
| 82 |
|
| 83 |
+
data.append([
|
| 84 |
+
engine, cyl, base_hp, intake, exhaust,
|
| 85 |
+
induction, fuel, tune, altitude, hp_gain
|
| 86 |
+
])
|
| 87 |
|
| 88 |
+
columns = [
|
| 89 |
+
"engine", "cyl", "base_hp", "intake", "exhaust",
|
| 90 |
+
"induction", "fuel", "tune", "altitude", "hp_gain"
|
| 91 |
+
]
|
| 92 |
return pd.DataFrame(data, columns=columns)
|
| 93 |
|
| 94 |
df = generate_dataset()
|
| 95 |
|
|
|
|
|
|
|
|
|
|
| 96 |
X = df.drop("hp_gain", axis=1)
|
| 97 |
y = df["hp_gain"]
|
| 98 |
|
| 99 |
scaler = StandardScaler()
|
| 100 |
X_scaled = scaler.fit_transform(X)
|
| 101 |
|
| 102 |
+
model = KNeighborsRegressor(n_neighbors=5, weights="distance")
|
| 103 |
model.fit(X_scaled, y)
|
| 104 |
|
| 105 |
# ---------------------------
|
| 106 |
+
# Header
|
| 107 |
# ---------------------------
|
| 108 |
|
| 109 |
+
st.markdown(
|
| 110 |
+
"""
|
| 111 |
+
<div style="padding: 0.25rem 0 1.5rem 0;">
|
| 112 |
+
<h1 style="margin-bottom:0.2rem;">π Car Mod Performance Estimator</h1>
|
| 113 |
+
<p style="color:#9ca3af; margin:0;">
|
| 114 |
+
Play with intakes, exhausts, tunes and fuel to see how your build could respond in horsepower.
|
| 115 |
+
</p>
|
| 116 |
+
</div>
|
| 117 |
+
""",
|
| 118 |
+
unsafe_allow_html=True
|
| 119 |
+
)
|
| 120 |
|
| 121 |
+
# ---------------------------
|
| 122 |
+
# Layout
|
| 123 |
+
# ---------------------------
|
| 124 |
+
left, right = st.columns([1.4, 1])
|
| 125 |
+
|
| 126 |
+
with left:
|
| 127 |
+
st.subheader("π§ Build Your Setup")
|
| 128 |
+
|
| 129 |
+
spec_col1, spec_col2 = st.columns(2)
|
| 130 |
+
|
| 131 |
+
with spec_col1:
|
| 132 |
+
engine = st.selectbox("Engine Displacement (L)", [1.6, 2.0, 2.5, 3.0, 3.5, 5.0])
|
| 133 |
+
cyl = st.selectbox("Cylinders", [4, 6, 8])
|
| 134 |
+
altitude = st.slider("Altitude (meters)", 0, 2000, 200)
|
| 135 |
+
|
| 136 |
+
with spec_col2:
|
| 137 |
+
base_hp = st.number_input("Base Horsepower", min_value=80, max_value=700, value=200)
|
| 138 |
+
fuel = st.selectbox("Fuel Octane", ["87", "91", "93", "E85"])
|
| 139 |
+
tune = st.selectbox("ECU Tune Level", ["None", "Mild", "Aggressive"])
|
| 140 |
+
|
| 141 |
+
st.markdown("---")
|
| 142 |
+
st.markdown("#### π§© Bolt-ons")
|
| 143 |
+
|
| 144 |
+
bolt1, bolt2, bolt3 = st.columns(3)
|
| 145 |
+
with bolt1:
|
| 146 |
+
intake = st.selectbox("Intake", ["Stock", "Cold Air", "Performance"])
|
| 147 |
+
with bolt2:
|
| 148 |
+
exhaust = st.selectbox("Exhaust", ["Stock", "Cat-back", "Straight Pipe"])
|
| 149 |
+
with bolt3:
|
| 150 |
+
induction = st.selectbox("Forced Induction", ["None", "Turbo", "Supercharger"])
|
| 151 |
+
|
| 152 |
+
# Visual chips summary
|
| 153 |
+
st.markdown("#### π Mod Summary")
|
| 154 |
+
st.markdown(
|
| 155 |
+
f"""
|
| 156 |
+
<span class="mod-chip">Engine: {engine}L / {cyl} cyl</span>
|
| 157 |
+
<span class="mod-chip">Intake: {intake}</span>
|
| 158 |
+
<span class="mod-chip">Exhaust: {exhaust}</span>
|
| 159 |
+
<span class="mod-chip">Induction: {induction}</span>
|
| 160 |
+
<span class="mod-chip">Fuel: {fuel}</span>
|
| 161 |
+
<span class="mod-chip">Tune: {tune}</span>
|
| 162 |
+
""",
|
| 163 |
+
unsafe_allow_html=True
|
| 164 |
+
)
|
| 165 |
|
| 166 |
+
# ---------------------------
|
| 167 |
+
# Prediction
|
| 168 |
+
# ---------------------------
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
intake_map = {"Stock": 0, "Cold Air": 1, "Performance": 2}
|
| 171 |
+
exhaust_map = {"Stock": 0, "Cat-back": 1, "Straight Pipe": 2}
|
| 172 |
+
induction_map = {"None": 0, "Turbo": 1, "Supercharger": 2}
|
| 173 |
+
fuel_map = {"87": 0, "91": 1, "93": 2, "E85": 3}
|
| 174 |
+
tune_map = {"None": 0, "Mild": 1, "Aggressive": 2}
|
|
|
|
| 175 |
|
| 176 |
input_data = np.array([[
|
| 177 |
engine,
|
|
|
|
| 186 |
]])
|
| 187 |
|
| 188 |
input_scaled = scaler.transform(input_data)
|
| 189 |
+
pred = float(model.predict(input_scaled)[0])
|
|
|
|
| 190 |
new_hp = base_hp + pred
|
| 191 |
|
| 192 |
+
# ---------------------------
|
| 193 |
+
# Right panel: Dashboard
|
| 194 |
+
# ---------------------------
|
| 195 |
+
with right:
|
| 196 |
+
st.subheader("π Performance Dashboard")
|
| 197 |
+
|
| 198 |
+
m1, m2 = st.columns(2)
|
| 199 |
+
m1.metric("Estimated HP Gain", f"{pred:.1f} HP")
|
| 200 |
+
m2.metric("Estimated New Horsepower", f"{new_hp:.1f} HP")
|
| 201 |
+
|
| 202 |
+
st.markdown(
|
| 203 |
+
f"""
|
| 204 |
+
<p class="hp-badge">
|
| 205 |
+
Power to weight feeling: <strong>
|
| 206 |
+
{"Mild bump" if pred < 25 else "Serious pull" if pred < 60 else "Track monster"}
|
| 207 |
+
</strong>
|
| 208 |
+
</p>
|
| 209 |
+
""",
|
| 210 |
+
unsafe_allow_html=True
|
| 211 |
+
)
|
| 212 |
|
| 213 |
+
st.markdown(" ")
|
|
|
|
| 214 |
|
| 215 |
+
st.bar_chart(
|
| 216 |
+
pd.DataFrame(
|
| 217 |
+
{"Horsepower": [base_hp, new_hp]},
|
| 218 |
+
index=["Base", "Modified"]
|
| 219 |
+
)
|
| 220 |
)
|
|
|
|
| 221 |
|
| 222 |
+
st.caption(
|
| 223 |
+
"Move the sliders and dropdowns on the left to instantly see how your virtual build responds."
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
with st.expander("π Peek under the hood (data & model info)"):
|
| 227 |
+
st.write("Sample of the synthetic training dataset used for this estimator:")
|
| 228 |
+
st.dataframe(df.head(15), use_container_width=True)
|
| 229 |
+
st.markdown(
|
| 230 |
+
"- Model: K-Nearest Neighbors Regressor (distance weighted)\n"
|
| 231 |
+
"- Inputs are standardized using `StandardScaler`\n"
|
| 232 |
+
"- Data is synthetic and for fun/educational purposes only π"
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
st.success("Tweak your setup and watch the graph react in real time!π")
|
| 236 |
+
|