Car-price-pred / app.py
Gokulnath2003's picture
Update app.py
06b9cbb verified
raw
history blame
2.87 kB
import streamlit as st
import pandas as pd
import numpy as np
import joblib
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
# Load the trained Random Forest model
@st.cache
def load_model():
# Replace with path to your trained Random Forest model if necessary
return joblib.load('random_forest_model.pkl')
model = load_model()
# Sample Data
url = "https://raw.githubusercontent.com/manishkr1754/CarDekho_Used_Car_Price_Prediction/main/notebooks/data/cardekho_dataset.csv"
df = pd.read_csv(url)
# Extract features for preprocessing
num_features = ['vehicle_age', 'km_driven', 'mileage', 'engine', 'max_power', 'seats']
cat_features = ['brand', 'model', 'seller_type', 'fuel_type', 'transmission_type']
# Preprocessing pipeline
numeric_transformer = StandardScaler()
onehot_transformer = OneHotEncoder()
preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, num_features),
('cat', onehot_transformer, cat_features)
])
# Streamlit app
st.title('Used Car Price Prediction')
# Main form for user input
st.header('Enter Car Details')
# Input fields
brand = st.selectbox('Brand', df['brand'].unique())
model = st.text_input('Model', '')
vehicle_age = st.number_input('Vehicle Age (in years)', min_value=0, max_value=50, value=5)
km_driven = st.number_input('Kilometers Driven', min_value=0, max_value=300000, value=50000)
mileage = st.number_input('Mileage (kmpl)', min_value=0.0, max_value=50.0, value=15.0)
engine = st.number_input('Engine (cc)', min_value=500, max_value=5000, value=1500)
max_power = st.number_input('Max Power (bhp)', min_value=0, max_value=500, value=100)
seats = st.number_input('Seats', min_value=2, max_value=8, value=5)
seller_type = st.selectbox('Seller Type', df['seller_type'].unique())
fuel_type = st.selectbox('Fuel Type', df['fuel_type'].unique())
transmission_type = st.selectbox('Transmission Type', df['transmission_type'].unique())
# Button to trigger the prediction
if st.button('Predict Price'):
# Create input dataframe
input_data = pd.DataFrame({
'brand': [brand],
'model': [model],
'vehicle_age': [vehicle_age],
'km_driven': [km_driven],
'mileage': [mileage],
'engine': [engine],
'max_power': [max_power],
'seats': [seats],
'seller_type': [seller_type],
'fuel_type': [fuel_type],
'transmission_type': [transmission_type]
})
# Preprocess the input
input_data_transformed = preprocessor.fit_transform(input_data)
# Predict the price
predicted_price = model.predict(input_data_transformed)
# Display the result
st.write(f'The predicted selling price for the car is: ₹ {predicted_price[0]:,.2f}')