import streamlit as st import numpy as np import pandas as pd import joblib from sklearn.preprocessing import StandardScaler, OneHotEncoder from sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline # Load the trained model (assuming it's saved as a .pkl file) model = joblib.load('RandomForestRegressor_model.pkl') # Example: sample transformers for preprocessing num_features = ['year', 'km_driven'] # Example numerical features cat_features = ['seller_type', 'transmission_type', 'fuel_type', 'model'] # Example categorical features # 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') # Sidebar form for user input st.sidebar.header('Enter Car Details') # Input fields year = st.sidebar.number_input('Year of Manufacture', min_value=1990, max_value=2023, value=2015) km_driven = st.sidebar.number_input('Kilometers Driven', min_value=0, max_value=300000, value=50000) # Dropdown fields seller_type = st.sidebar.selectbox('Seller Type', ['Dealer', 'Individual']) transmission_type = st.sidebar.selectbox('Transmission Type', ['Manual', 'Automatic']) fuel_type = st.sidebar.selectbox('Fuel Type', ['Petrol', 'Diesel', 'CNG', 'LPG']) model = st.sidebar.text_input('Car Model (encoded)') # Button to trigger the prediction if st.sidebar.button('Predict Price'): # Create input dataframe input_data = pd.DataFrame({ 'year': [year], 'km_driven': [km_driven], 'seller_type': [seller_type], 'transmission_type': [transmission_type], 'fuel_type': [fuel_type], 'model': [model] }) # Preprocess the input input_data_transformed = preprocessor.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}')