-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrice_prediction_app.py
148 lines (131 loc) · 5.41 KB
/
Price_prediction_app.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import streamlit as st
import joblib
import pandas as pd
from PIL import Image
import os
import base64
# Page configuration
st.set_page_config(layout="wide", page_title="Car Dheko - Price Prediction", page_icon="🚗")
# Load paths
logo_path = "E:/UDHAYA/Cardeko_project/Cardeko_logo.png"
dataset_path = "E:/UDHAYA/Cardeko_project/Cleaned_csv/Processed_dataset.csv"
model_path = "E:/UDHAYA/Cardeko_project/Reports/pipeline_model.pkl"
# Custom CSS for styling
st.markdown("""
<style>
.title {
font-size: 42px;
font-weight: bold;
color: #FFFFFF;
text-align: center;
}
.description {
text-align: center;
color: #FFFFFF;
font-size: 18px;
margin-bottom: 20px;
}
.stButton button {
background-color: #ff6600;
color: white;
font-size: 20px;
font-weight: bold;
padding: 10px 20px;
border-radius: 10px;
}
.sidebar-content {
font-size: 18px;
color: #ff6600;
}
</style>
""", unsafe_allow_html=True)
# Load logo
if os.path.exists(logo_path):
try:
# Open the logo and resize
with open(logo_path, "rb") as image_file:
logo_base64 = base64.b64encode(image_file.read()).decode("utf-8")
# Display the logo in the center with specific width
st.markdown(
f"""
<div style="display: flex; justify-content: center;">
<img src="data:image/png;base64,{logo_base64}" width="200"/>
</div>
""",
unsafe_allow_html=True
)
except Exception as e:
st.warning(f"Error loading logo: {e}")
else:
st.warning("Logo file not found.")
# App title and description
st.markdown('<p class="title">✨Car Dheko - Used Car Price Prediction✨</p>', unsafe_allow_html=True)
st.markdown('<p class="description">Get an estimated price for your car based on specifications and history.</p>', unsafe_allow_html=True)
# Load data and model functions
def load_data():
if os.path.exists(dataset_path):
return pd.read_csv(dataset_path)
else:
st.error("Dataset not found.")
return None
def load_model():
if os.path.exists(model_path):
return joblib.load(model_path)
else:
st.error("Model not found.")
return None
# Initialize data and model
df = load_data()
pipeline_model = load_model()
if df is not None and pipeline_model is not None:
st.sidebar.markdown("<p class='sidebar-content'>**Car Specifications**</p>", unsafe_allow_html=True)
# Sidebar inputs for user specifications
brand = st.sidebar.selectbox("Car Brand", options=df['Brand'].unique())
fuel_type = st.sidebar.selectbox("Fuel Type", ['Petrol', 'Diesel', 'Lpg', 'Cng', 'Electric'])
body_type = st.sidebar.selectbox("Body Type", ['Hatchback', 'SUV', 'Sedan', 'MUV', 'Coupe', 'Minivans', 'Convertibles', 'Hybrids', 'Wagon', 'Pickup Trucks'])
# Model dropdown based on filters
filtered_models = df[(df['Brand'] == brand) & (df['body type'] == body_type) & (df['Fuel type'] == fuel_type)]['model'].unique()
if filtered_models.size > 0:
car_model = st.sidebar.selectbox("Car Model", options=filtered_models)
else:
car_model = st.sidebar.selectbox("Car Model", options=["No models available"])
transmission = st.sidebar.selectbox("Transmission", ['Manual', 'Automatic'])
seats = st.sidebar.selectbox("Seats", sorted(df['Seats'].unique()))
insurance_type = st.sidebar.selectbox("Insurance Type", ['Third Party insurance', 'Comprehensive', 'Third Party', 'Zero Dep', '2', '1', 'Not Available'])
color = st.sidebar.selectbox("Color", df['Color'].unique())
city = st.sidebar.selectbox("City", options=df['City'].unique())
# Numeric inputs
model_year = st.sidebar.number_input("Manufacturing Year", min_value=1980, max_value=2025, step=1)
mileage = st.sidebar.number_input("Mileage (in km/l)", min_value=1.0, max_value=50.0,step=0.1)
owner_no = st.sidebar.number_input("Owner Number", min_value=1, max_value=5, step=1)
kms_driven = st.sidebar.number_input("Kilometers Driven", min_value=100, max_value=1000000, step=1000)
# Main area predict button
st.markdown("<br>", unsafe_allow_html=True)
predict_button = st.button("🚗 Predict Car Price")
# Predict
if predict_button:
if car_model != "No models available":
input_data = pd.DataFrame({
'Fuel type': [fuel_type],
'body type': [body_type],
'transmission': [transmission],
'ownerNo': [owner_no],
'Brand': [brand],
"model": [car_model],
'modelYear': [model_year],
'Insurance Type': [insurance_type],
'Kms Driven': [kms_driven],
'Mileage': [mileage],
'Seats': [seats],
'Color': [color],
'City': [city]
})
try:
prediction = pipeline_model.predict(input_data)
st.success(f"Estimated Price: ₹ {prediction[0]:,.2f}")
except Exception as e:
st.error("Error making prediction.")
else:
st.warning("Please select valid options for all fields.")
else:
st.error("Unable to load data or model.")