Pushp123 commited on
Commit
93db1b3
·
verified ·
1 Parent(s): 5683f83

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #1. Importing lib
2
+
3
+ import gradio as gr
4
+ import numpy as np
5
+ import pandas as pd
6
+ from sklearn.model_selection import train_test_split
7
+ from sklearn.ensemble import RandomForestRegressor
8
+ from sklearn.metrics import accuracy_score,r2_score
9
+
10
+ #2.Data Preprocesing
11
+
12
+ df=pd.read_csv("car data.csv")
13
+
14
+ df.head()
15
+ df.tail()
16
+ df.info()
17
+ df.describe()
18
+ df.isnull().sum()
19
+
20
+ df["Fuel_Type"].unique()
21
+
22
+ df["Seller_Type"].unique()
23
+
24
+ df["Transmission"].unique()
25
+
26
+ df.replace({"Fuel_Type":{"Diesel":0,"Petrol":1,"CNG":2}},inplace=True)
27
+
28
+ df.replace({"Seller_Type":{"Dealer":0,"Individual":1}},inplace=True)
29
+
30
+ df.replace({"Transmission":{"Manual":0,"Automatic":1}},inplace=True)
31
+
32
+ # Spliting Data into x and y(independent/dependent)
33
+ x= df.drop(["Car_Name","Selling_Price"],axis=1)
34
+ y = df["Selling_Price"]
35
+
36
+ #3. Modeling Part
37
+
38
+ x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)
39
+ model=RandomForestRegressor()
40
+
41
+ model.fit(x_train,y_train)
42
+ model.fit(x_test,y_test)
43
+
44
+ x_predict=model.predict(x_train)
45
+ x_accuracy=r2_score(x_predict,y_train)
46
+
47
+ y_predict=model.predict(x_test)
48
+ y_accuracy=r2_score(y_predict,y_test)
49
+
50
+ #4. UI For Model(Help of Gradio)
51
+
52
+ # Function to make predictions
53
+ def predict_car_price(year,Present_Price, km_driven, fuel_type, seller_type, transmission,owner):
54
+ input_data = np.array([[year,Present_Price, km_driven, fuel_type, seller_type, transmission,owner]])
55
+ prediction = model.predict(input_data)
56
+ return f"Predicted Selling Price: ₹{prediction[0]:,.2f}"
57
+
58
+ # Create the Gradio interface
59
+ iface = gr.Interface(
60
+ fn=predict_car_price, # Function that makes predictions
61
+ inputs=[
62
+ gr.Slider(minimum=2003, maximum=2018, step=1, label="Car Year (Year of Manufacture)"),
63
+ gr.Slider(minimum=0, maximum=93, step=1, label="Present Pcice "),
64
+ gr.Slider(minimum=0, maximum=500000, step=1000, label="Kilometers Driven (km)"),
65
+ gr.Dropdown([0, 1, 2], label="Fuel Type (0 = Diesel, 1 = Petrol, 2 = CNG)"),
66
+ gr.Dropdown([0, 1], label="Seller Type (0 = Dealer, 1 = Individual)"),
67
+ gr.Dropdown([0, 1], label="Transmission (0 = Manual, 1 = Automatic)"),
68
+ gr.Dropdown([0, 1, 2, 3], label="Number of Owners (0 = First, 1 = Second, 2 = Third, 3 = Fourth)")
69
+ ], # Input fields for the model's features
70
+ outputs="text" # Output the predicted selling price as text
71
+ )
72
+
73
+ # Launch the Gradio UI
74
+ iface.launch()