ma4389 commited on
Commit
7a92a07
·
verified ·
1 Parent(s): fa7f74c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -66
app.py CHANGED
@@ -1,66 +1,59 @@
1
- import gradio as gr
2
- import pandas as pd
3
- import pickle
4
-
5
- # Load trained model and preprocessor
6
- with open("rfr.pkl", "rb") as f:
7
- model = pickle.load(f)
8
-
9
- with open("prepocessor.pkl", "rb") as f:
10
- preprocessor = pickle.load(f)
11
-
12
- # Final features (everything except sellingprice)
13
- input_features = [
14
- "year", "make", "model", "trim", "body", "transmission", "vin",
15
- "state", "condition", "odometer", "color", "interior", "seller",
16
- "mmr", "Year", "Month", "Day", "Quarter"
17
- ]
18
-
19
- def predict_price(year, make, model_name, trim, body, transmission, vin,
20
- state, condition, odometer, color, interior, seller, mmr,
21
- Year, Month, Day, Quarter):
22
-
23
- # Create DataFrame
24
- df = pd.DataFrame([[
25
- year, make, model_name, trim, body, transmission, vin,
26
- state, condition, odometer, color, interior, seller, mmr,
27
- Year, Month, Day, Quarter
28
- ]], columns=input_features)
29
-
30
- # Preprocess
31
- X_processed = preprocessor.transform(df)
32
-
33
- # Predict
34
- prediction = model.predict(X_processed)[0]
35
- return f"💰 Estimated Selling Price: ${prediction:,.2f}"
36
-
37
- # Gradio UI
38
- demo = gr.Interface(
39
- fn=predict_price,
40
- inputs=[
41
- gr.Number(label="year"),
42
- gr.Textbox(label="make"),
43
- gr.Textbox(label="model"),
44
- gr.Textbox(label="trim"),
45
- gr.Textbox(label="body"),
46
- gr.Dropdown(choices=["automatic", "manual"], label="transmission"),
47
- gr.Textbox(label="vin"),
48
- gr.Textbox(label="state"),
49
- gr.Number(label="condition"),
50
- gr.Number(label="odometer"),
51
- gr.Textbox(label="color"),
52
- gr.Textbox(label="interior"),
53
- gr.Textbox(label="seller"),
54
- gr.Number(label="mmr"),
55
- gr.Number(label="Year"),
56
- gr.Number(label="Month"),
57
- gr.Number(label="Day"),
58
- gr.Number(label="Quarter"),
59
- ],
60
- outputs=gr.Textbox(label="Prediction"),
61
- title="🚗 Car Price Prediction",
62
- description="Enter car details to predict the estimated selling price."
63
- )
64
-
65
- if __name__ == "__main__":
66
- demo.launch()
 
1
+ import gradio as gr
2
+ import pickle as pkl
3
+ import pandas as pd
4
+
5
+ # === Load Preprocessor and Model ===
6
+ preprocessor = pkl.load(open("prepocessr.pkl", "rb"))
7
+ model = pkl.load(open("rfrc.pkl", "rb"))
8
+
9
+ # === Define Prediction Function ===
10
+ def predict(make, model_car, year, trim, body, transmission, state, condition, odometer, color, interior, mmr):
11
+ # Put inputs into a DataFrame with the same column names as training
12
+ input_df = pd.DataFrame([{
13
+ "make": make,
14
+ "model": model_car,
15
+ "year": year,
16
+ "trim": trim,
17
+ "body": body,
18
+ "transmission": transmission,
19
+ "state": state,
20
+ "condition": condition,
21
+ "odometer": odometer,
22
+ "color": color,
23
+ "interior": interior,
24
+ "mmr": mmr
25
+ }])
26
+
27
+ # Apply preprocessing
28
+ X_proc = preprocessor.transform(input_df)
29
+
30
+ # Predict with model
31
+ prediction = model.predict(X_proc)[0]
32
+
33
+ return f"Predicted Selling Price: ${prediction:,.2f}"
34
+
35
+ # === Gradio Interface ===
36
+ iface = gr.Interface(
37
+ fn=predict,
38
+ inputs=[
39
+ gr.Textbox(label="Make"),
40
+ gr.Textbox(label="Model"),
41
+ gr.Number(label="Year"),
42
+ gr.Textbox(label="Trim"),
43
+ gr.Textbox(label="Body"),
44
+ gr.Textbox(label="Transmission"),
45
+ gr.Textbox(label="State"),
46
+ gr.Number(label="Condition"),
47
+ gr.Number(label="Odometer"),
48
+ gr.Textbox(label="Color"),
49
+ gr.Textbox(label="Interior"),
50
+ gr.Number(label="MMR"),
51
+ ],
52
+ outputs=gr.Textbox(label="Prediction"),
53
+ title="Car Price Prediction App",
54
+ description="Enter car details to predict the selling price."
55
+ )
56
+
57
+ # Launch app
58
+ if __name__ == "__main__":
59
+ iface.launch()