|
this is how my app looks [import gradio as gr |
|
import joblib |
|
import numpy as np |
|
import pandas as pd |
|
|
|
encoder0 = joblib.load('Fuel_Type.joblib') |
|
encoder1 = joblib.load('Seller_Type.joblib') |
|
encoder2 = joblib.load('Transmission.joblib') |
|
|
|
|
|
xg = joblib.load('xgb.joblib') |
|
|
|
|
|
scaler = joblib.load('scaler.joblib') |
|
def Pred_func(Kms_Driven,Present_Price, Fuel_Type, Seller_Type, Transmission, Age): |
|
|
|
Fuel_Type = encoder0.transform([Fuel_Type])[0] |
|
Seller_Type = encoder1.transform([Seller_Type])[0] |
|
Transmission = encoder2.transform([Transmission])[0] |
|
|
|
x_new = np.array([Kms_Driven,Present_Price, Fuel_Type, Seller_Type, Transmission, Age]) |
|
x_new = x_new.reshape(1,-1) |
|
|
|
x_new = scaler.transform(x_new) |
|
|
|
y_pred = xg.predict(x_new) |
|
|
|
y_pred = round(y_pred[0],2) |
|
return str(y_pred) + 'k$' |
|
|
|
def Pred_func_csv(file): |
|
|
|
df = pd.read_csv(file) |
|
predictions = [] |
|
|
|
for row in df.iloc[:, :].values: |
|
|
|
new_row = np.array([row[0], row[1], encoder0.transform([row[2]])[0], encoder1.transform([row[3]])[0], encoder2.transform([row[4]])[0], row[5]]) |
|
new_row = new_row.reshape(1,-1) |
|
|
|
new_row = scaler.transform(new_row) |
|
|
|
y_pred = xg.predict(new_row) |
|
|
|
y_pred = round(y_pred[0],2) |
|
|
|
predictions.append(y_pred) |
|
|
|
df['Selling_Price'] = predictions |
|
df.to_csv('predictions.csv', index = False) |
|
return 'predictions.csv' |
|
|
|
|
|
demo = gr.Blocks(theme='abidlabs/seafoam') |
|
|
|
|
|
inputs = [gr.Number(label='Kms Driven'), |
|
gr.Number(label='Present Price'), |
|
gr.Radio(choices=['Petrol', 'Diesel', 'CNG'], label='Fuel Type'), |
|
gr.Radio(choices=['Dealer', 'Individual'], label='Seller Type'), |
|
gr.Radio(choices=['Manual', 'Automatic'], label='Transmission'), |
|
gr.Number(label='Age')] |
|
|
|
outputs = gr.Textbox(label='Selling Price') |
|
|
|
|
|
interface1 = gr.Interface(fn = Pred_func, |
|
inputs = inputs, |
|
outputs = outputs, |
|
title="Predict the selling price of a car with a single input", |
|
description = """This machine learning model allows us to predict the selling price of a car |
|
from the kms driven, present price, fuel type, seller type, transmission and age of the car. |
|
""") |
|
|
|
interface2 = gr.Interface(fn = Pred_func_csv, |
|
inputs = gr.File(label='Upload a csv file'), |
|
outputs = gr.File(label='Download a csv file'), |
|
title="Predict the selling price of a car with a multiple inputs", |
|
description = """This machine learning model allows us to predict the selling price of a car |
|
from the kms driven, present price, fuel type, seller type, transmission and age of the car. |
|
""") |
|
|
|
|
|
with demo: |
|
gr.TabbedInterface([interface1, interface2], ['Simple Prediction', 'Prédiction multiple']) |
|
|
|
|
|
demo.launch() |
|
] |