Spaces:
Runtime error

File size: 2,938 Bytes
4aed0d5
1dedbbb
 
 
 
 
 
4aed0d5
 
4ee8a62
1dedbbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
import streamlit as st

df=pd.read_csv('src/cars.csv')
x=df.drop('Price', axis=1)
y=df[['Price']]

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.20, random_state=42)

preprocessor=ColumnTransformer(
    transformers=[
        ('num',StandardScaler(),['Mileage','Cylinder','Liter','Doors']),
        ('cat',OneHotEncoder(),['Make','Model','Trim','Type'])
    ]
)

model=LinearRegression()

pipeline=Pipeline(steps=[('preprocessor',preprocessor),('regressor',model)])

pipeline.fit(x_train,y_train)

pred=pipeline.predict(x_test)

r2=r2_score(y_test,pred)

rmse= mean_squared_error(y_test,pred)**0.5


def price_pred(make,model,trim,mileage,type_,cylinder,liter,doors,cruise,sound,leather):
    input_data=pd.DataFrame({
        'Make':[make],
        'Model':[model],
        'Trim':[trim],
        'Mileage':[mileage],
        'Type':[type_],
        'Cylinder':[cylinder],
        'Liter':[liter],
        'Doors':[doors],
        'Cruise':[cruise],
        'Sound':[sound],
        'Leather':[leather],
    })
    
    prediction=pipeline.predict(input_data)[0]
    return prediction

st.title('MLOps Car Price Prediction App :red_car:')
st.write('Enter Car Details to Predict the Price')

make=st.selectbox('Make',df['Make'].unique())
carmodel=st.selectbox('Model',df[df['Make']==make]['Model'].unique())
trim=st.selectbox('Trim',df[df['Model']==carmodel]['Trim'].unique())
mileage=st.number_input('Mileage',min_value=200,max_value=160000, step=100)
car_type=st.selectbox('Type',df['Type'].unique())
cylinder=st.selectbox('Cylinder',df['Cylinder'].unique())
liter=st.number_input('Liter',min_value=1,max_value=6, step=1)
doors=st.selectbox('Doors',df['Doors'].unique())
cruise=st.radio('Cruise',[0,1])
sound=st.radio('Sound',[0,1])
leather=st.radio('Leather',[0,1])

if st.button('Predict'):
    predicted_price=price_pred(make,carmodel,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather)
    price=float(predicted_price)
    st.success(f'The Predicted price of the car is: ${price:.2f}')
    st.success(f'The R2 Score is : {r2:.2f}')
    st.success(f'The RMSE Score is : {rmse:.2f}')
    new = pd.DataFrame([{
        'Price':price, 'Make': make, 'Model': carmodel, 'Trim': trim, 'Mileage': mileage,
        'Type': car_type, 'Cylinder': cylinder, 'Liter': liter, 'Doors': doors,
        'Cruise': cruise, 'Sound': sound, 'Leather': leather
        }])
    st.write(new)
    st.write(df.shape)
    updated = pd.concat([df, new], ignore_index=True)
    st.write(updated.shape)
    updated.to_excel('cars.xlsx', index=False)

#CI/CD COntinues integration , Continues Development