GimhanC99's picture
Create app.py
d4e8c2f verified
Raw
History Blame Contribute Delete
2.35 kB
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()