NeuralGearheads commited on
Commit
29d7e5e
ยท
verified ยท
1 Parent(s): 6791fb4

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +113 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,115 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
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 numpy as np
4
+ from sklearn.preprocessing import StandardScaler
5
+ from sklearn.neighbors import KNeighborsRegressor
6
+
7
+ # ---------------------------
8
+ # Load / generate dataset
9
+ # ---------------------------
10
+
11
+ def generate_dataset(n=400):
12
+ np.random.seed(42)
13
+ data = []
14
+
15
+ for _ in range(n):
16
+ engine = np.random.choice([1.6, 2.0, 2.5, 3.0, 3.5, 5.0])
17
+ cyl = np.random.choice([4, 6, 8])
18
+ base_hp = int(engine * cyl * np.random.uniform(18, 22))
19
+
20
+ intake = np.random.choice([0, 1, 2]) # stock/CAI/perf
21
+ exhaust = np.random.choice([0, 1, 2]) # stock/catback/straight
22
+ induction = np.random.choice([0, 1, 2]) # none/turbo/super
23
+ fuel = np.random.choice([0, 1, 2, 3]) # 87/91/93/E85
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) +
31
+ induction * np.random.uniform(25, 100) +
32
+ tune * np.random.uniform(10, 35) +
33
+ fuel * np.random.uniform(2, 8) -
34
+ altitude * 0.01 +
35
+ np.random.uniform(-3, 3)
36
+ )
37
+
38
+ data.append([engine, cyl, base_hp, intake, exhaust, induction, fuel, tune, altitude, hp_gain])
39
+
40
+ columns = ["engine", "cyl", "base_hp", "intake", "exhaust", "induction", "fuel", "tune", "altitude", "hp_gain"]
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='distance')
55
+ model.fit(X_scaled, y)
56
+
57
+ # ---------------------------
58
+ # Streamlit UI
59
+ # ---------------------------
60
+
61
+ st.title("๐Ÿš— Car Modification Performance Estimator")
62
+ st.subheader("Predict horsepower gain from your car modifications")
63
+
64
+ st.divider()
65
+
66
+ engine = st.selectbox("Engine Displacement (L)", [1.6, 2.0, 2.5, 3.0, 3.5, 5.0])
67
+ cyl = st.selectbox("Cylinders", [4, 6, 8])
68
+ base_hp = st.number_input("Base Horsepower", min_value=80, max_value=700, value=200)
69
+
70
+ intake = st.selectbox("Intake Type", ["Stock", "Cold Air", "Performance"])
71
+ exhaust = st.selectbox("Exhaust Type", ["Stock", "Cat-back", "Straight Pipe"])
72
+ induction = st.selectbox("Forced Induction", ["None", "Turbo", "Supercharger"])
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
+ # Map categorical to numeric
78
+ intake_map = {"Stock":0, "Cold Air":1, "Performance":2}
79
+ exhaust_map = {"Stock":0, "Cat-back":1, "Straight Pipe":2}
80
+ induction_map = {"None":0, "Turbo":1, "Supercharger":2}
81
+ fuel_map = {"87":0, "91":1, "93":2, "E85":3}
82
+ tune_map = {"None":0, "Mild":1, "Aggressive":2}
83
+
84
+ input_data = np.array([[
85
+ engine,
86
+ cyl,
87
+ base_hp,
88
+ intake_map[intake],
89
+ exhaust_map[exhaust],
90
+ induction_map[induction],
91
+ fuel_map[fuel],
92
+ tune_map[tune],
93
+ altitude
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
+ st.divider()
102
+ st.subheader("๐Ÿ“ˆ Prediction Results")
103
+
104
+ st.metric("Estimated HP Gain", f"{pred:.1f} HP")
105
+ st.metric("New Estimated Horsepower", f"{new_hp:.1f} HP")
106
+
107
+ # Bar chart
108
+ st.bar_chart(
109
+ pd.DataFrame(
110
+ {"Horsepower": [base_hp, new_hp]},
111
+ index=["Base", "Modified"]
112
+ )
113
+ )
114
 
115
+ st.success("Prediction complete! Adjust mods to see how HP changes.")