CoolFace
Apppublic

asadsheikh/cpp-project

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py67 linesDownload Raw Back to root
1import streamlit as st 
2import pickle
3import numpy as np
4import pandas as pd 
5
6# Streamlit app
7st.title("Car Price Predictor for Selling")
8
9# model 
10df = pickle.load(open('df.pkl', 'rb'))
11pipe = pickle.load(open('pipe.pkl', 'rb'))
12
13def create_car_mapping(car_df):
14    return car_df.groupby("company")["name"].apply(list).to_dict()
15
16
17# Load the pickled DataFrame
18@st.cache_data
19def load_data(pickle_file):
20    with open(pickle_file, "rb") as file:
21        return pickle.load(file)
22
23# Path to your pickle file
24pickle_file = "df.pkl"  # Update with the actual path of your file
25
26# Load the dataset
27car_df = load_data(pickle_file)
28
29# Ensure the DataFrame has the necessary columns
30if "company" in car_df.columns and "name" in car_df.columns:
31    # Create car mapping dynamically
32    car_mapping = create_car_mapping(car_df)
33
34    # Select car company
35    company = st.selectbox("Select a Car Company", options=list(car_mapping.keys()))
36
37    # Get models for the selected company
38    car_models = car_mapping.get(company, [])
39
40    # Select car model
41    name = st.selectbox("Select a Car Model", options=car_models)
42
43    # Display selected company and model
44    # st.write(f"You selected: {selected_company} - {selected_model}")
45else:
46    st.error("The dataset does not have the required columns: 'Car_Company' and 'Car_Model'")
47
48
49year = st.selectbox('Model Year', df['year'].unique())
50kms_driven = st.number_input("Kms Driven", min_value=0, step=1, value=0, format="%d")
51
52fuel_type = st.selectbox('Fuel Type', df['fuel_type'].unique())
53
54input_data = pd.DataFrame({'company': [company], 'name': [name], 'year': [year], 'kms_driven': [kms_driven], 'fuel_type': [fuel_type]})
55
56
57if st.button('Price Predict'):
58
59    prediction = pipe.predict(input_data)
60    # Replace negative price with 0
61    prediction = max(0, prediction[0])
62    st.title(f"Predicted Price: {int(prediction)} INR")
63
64    # Debug
65    # print(type(input_data))
66    # print(input_data.head())  # Check input structure
67