Pushp123's picture
Create app.py
93db1b3 verified
#1. Importing lib
import gradio as gr
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import accuracy_score,r2_score
#2.Data Preprocesing
df=pd.read_csv("car data.csv")
df.head()
df.tail()
df.info()
df.describe()
df.isnull().sum()
df["Fuel_Type"].unique()
df["Seller_Type"].unique()
df["Transmission"].unique()
df.replace({"Fuel_Type":{"Diesel":0,"Petrol":1,"CNG":2}},inplace=True)
df.replace({"Seller_Type":{"Dealer":0,"Individual":1}},inplace=True)
df.replace({"Transmission":{"Manual":0,"Automatic":1}},inplace=True)
# Spliting Data into x and y(independent/dependent)
x= df.drop(["Car_Name","Selling_Price"],axis=1)
y = df["Selling_Price"]
#3. Modeling Part
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)
model=RandomForestRegressor()
model.fit(x_train,y_train)
model.fit(x_test,y_test)
x_predict=model.predict(x_train)
x_accuracy=r2_score(x_predict,y_train)
y_predict=model.predict(x_test)
y_accuracy=r2_score(y_predict,y_test)
#4. UI For Model(Help of Gradio)
# Function to make predictions
def predict_car_price(year,Present_Price, km_driven, fuel_type, seller_type, transmission,owner):
input_data = np.array([[year,Present_Price, km_driven, fuel_type, seller_type, transmission,owner]])
prediction = model.predict(input_data)
return f"Predicted Selling Price: ₹{prediction[0]:,.2f}"
# Create the Gradio interface
iface = gr.Interface(
fn=predict_car_price, # Function that makes predictions
inputs=[
gr.Slider(minimum=2003, maximum=2018, step=1, label="Car Year (Year of Manufacture)"),
gr.Slider(minimum=0, maximum=93, step=1, label="Present Pcice "),
gr.Slider(minimum=0, maximum=500000, step=1000, label="Kilometers Driven (km)"),
gr.Dropdown([0, 1, 2], label="Fuel Type (0 = Diesel, 1 = Petrol, 2 = CNG)"),
gr.Dropdown([0, 1], label="Seller Type (0 = Dealer, 1 = Individual)"),
gr.Dropdown([0, 1], label="Transmission (0 = Manual, 1 = Automatic)"),
gr.Dropdown([0, 1, 2, 3], label="Number of Owners (0 = First, 1 = Second, 2 = Third, 3 = Fourth)")
], # Input fields for the model's features
outputs="text" # Output the predicted selling price as text
)
# Launch the Gradio UI
iface.launch()