File size: 2,134 Bytes
63cfaa2
 
9a69467
e331fd0
 
a79bf3e
63cfaa2
a79bf3e
9a69467
 
63cfaa2
a79bf3e
63cfaa2
 
 
 
 
a79bf3e
2b02aba
 
63cfaa2
 
 
 
 
 
 
 
2b02aba
 
a79bf3e
2b02aba
 
 
63cfaa2
 
a79bf3e
63cfaa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b02aba
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
import gradio as gr
import pandas as pd
import xgboost as xgb
import os

os.environ["MPLCONFIGDIR"] = "/tmp"  # Fix for fontconfig error

# Load XGBoost booster from JSON
booster = xgb.Booster()
booster.load_model("xgb_model.json")

# Define options
makes = ['FORD', 'TOYOTA', 'HONDA', 'BMW', 'AUDI']
transmissions = ['Automatic', 'Manual', 'CVT', 'Auto-Manual', 'Direct Drive']
fuels = ['Regular Gasoline', 'Premium Gasoline', 'Diesel', 'Electricity', 'E85', 'CNG']
classes = ['SUV', 'Compact Cars', 'Mid-size Cars', 'Large Cars', 'Pickup Trucks', 'Station Wagons']

# Preprocess input into DMatrix
def preprocess_input(year, cylinders, displ, make, trany, fuel, vclass):
    df = pd.DataFrame([{
        "year": year,
        "cylinders": cylinders,
        "displ": displ,
        "make": make,
        "trany": trany,
        "fuelType": fuel,
        "VClass": vclass
    }])
    return xgb.DMatrix(df)

# Predict MPG
def predict_mpg(year, cylinders, displ, make, trany, fuel, vclass):
    input_dmatrix = preprocess_input(year, cylinders, displ, make, trany, fuel, vclass)
    prediction = booster.predict(input_dmatrix)[0]
    return f"๐Ÿš— Predicted Combined MPG: {prediction:.2f} MPG"

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## ๐Ÿš˜ Vehicle MPG Predictor")
    gr.Markdown("Enter vehicle details to predict Combined MPG")
    with gr.Row():
        with gr.Column():
            year = gr.Slider(2000, 2025, value=2020, step=1, label="Year")
            cylinders = gr.Slider(2, 16, value=4, step=1, label="Cylinders")
            displ = gr.Slider(1.0, 8.0, value=2.0, step=0.1, label="Engine Displacement (L)")
        with gr.Column():
            make = gr.Dropdown(makes, label="Make")
            trany = gr.Dropdown(transmissions, label="Transmission")
            fuel = gr.Dropdown(fuels, label="Fuel Type")
            vclass = gr.Dropdown(classes, label="Vehicle Class")
    result = gr.Textbox(label="Prediction Result")
    predict_btn = gr.Button("๐Ÿš€ Predict MPG")
    predict_btn.click(fn=predict_mpg, inputs=[year, cylinders, displ, make, trany, fuel, vclass], outputs=result)

demo.launch()