File size: 2,348 Bytes
d4e8c2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
import joblib
import numpy as np

# Load the model
model = joblib.load("train_model.pkl")

# Define input handler
def predict_price(make_year, mileage_kmpl, engine_cc, owner_count, accidents_reported,
                  fuel_type, brand, transmission, color, insurance_valid):

    # One-hot encoding
    fuel_dict = {'Diesel': [1, 0, 0], 'Electric': [0, 1, 0], 'Petrol': [0, 0, 1]}
    brand_dict = {
        'BMW': [1,0,0,0,0,0,0,0,0,0],
        'Chevrolet': [0,1,0,0,0,0,0,0,0,0],
        'Ford': [0,0,1,0,0,0,0,0,0,0],
        'Honda': [0,0,0,1,0,0,0,0,0,0],
        'Hyundai': [0,0,0,0,1,0,0,0,0,0],
        'Kia': [0,0,0,0,0,1,0,0,0,0],
        'Nissan': [0,0,0,0,0,0,1,0,0,0],
        'Tesla': [0,0,0,0,0,0,0,1,0,0],
        'Toyota': [0,0,0,0,0,0,0,0,1,0],
        'Volkswagen': [0,0,0,0,0,0,0,0,0,1]
    }
    trans_dict = {'Automatic': [1, 0], 'Manual': [0, 1]}
    color_dict = {
        'Black':[1,0,0,0,0,0], 'Blue':[0,1,0,0,0,0], 'Gray':[0,0,1,0,0,0],
        'Red':[0,0,0,1,0,0], 'Silver':[0,0,0,0,1,0], 'White':[0,0,0,0,0,1]
    }
    insurance_dict = {'No': [1, 0], 'Yes': [0, 1]}

    # Combine all features
    features = [
        make_year, mileage_kmpl, engine_cc, owner_count, accidents_reported
    ] + fuel_dict[fuel_type] + brand_dict[brand] + trans_dict[transmission] + color_dict[color] + insurance_dict[insurance_valid]

    prediction = model.predict([features])[0]
    return round(prediction, 2)

# Gradio UI
gr.Interface(
    fn=predict_price,
    inputs=[
        gr.Number(label="Make Year"),
        gr.Number(label="Mileage (km/l)"),
        gr.Number(label="Engine Capacity (cc)"),
        gr.Slider(1, 5, step=1, label="Owner Count"),
        gr.Slider(0, 10, step=1, label="Accidents Reported"),
        gr.Radio(choices=["Diesel", "Electric", "Petrol"], label="Fuel Type"),
        gr.Dropdown(choices=[
            'BMW', 'Chevrolet', 'Ford', 'Honda', 'Hyundai', 'Kia', 'Nissan', 'Tesla', 'Toyota', 'Volkswagen'
        ], label="Brand"),
        gr.Radio(choices=["Automatic", "Manual"], label="Transmission"),
        gr.Dropdown(choices=["Black", "Blue", "Gray", "Red", "Silver", "White"], label="Color"),
        gr.Radio(choices=["Yes", "No"], label="Insurance Valid")
    ],
    outputs=gr.Number(label="Predicted Price ($)"),
    title="🚗 Used Car Price Prediction"
).launch()