Auto_Appraise / app.py
EdBoy2202's picture
Update app.py
f0bade0 verified
raw
history blame
3.64 kB
import pandas as pd
import numpy as np
import streamlit as st
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.preprocessing import LabelEncoder
# Load the CTP_Model1.csv file
def load_car_data():
try:
df = pd.read_csv('CTP_Model1.csv') # Replace with the path to your actual CSV file
return df
except Exception as e:
st.error(f"Error loading CSV file: {e}")
return None
# Preprocess car data and encode categorical features
def preprocess_car_data(df):
label_encoders = {}
# Encode categorical columns (make, model, trim, fuel, title_status, etc.)
for col in ['make', 'model', 'trim', 'fuel', 'title_status', 'transmission', 'drive', 'size', 'type', 'paint_color']:
le = LabelEncoder()
df[col] = le.fit_transform(df[col])
label_encoders[col] = le
return df, label_encoders
# Calculate similarity between the classified car and entries in the CSV
def find_closest_car(df, label_encoders, make, model, year):
# Encode the user-provided make and model
make_encoded = label_encoders['make'].transform([make])[0]
model_encoded = label_encoders['model'].transform([model])[0]
# Create a feature vector for the classified car (make, model, year)
classified_car_vector = np.array([make_encoded, model_encoded, year]).reshape(1, -1)
# Prepare the data for similarity calculation
feature_columns = ['make', 'model', 'year']
df_feature_vectors = df[feature_columns].values
# Compute cosine similarity between the classified car and all entries in the CSV
similarity_scores = cosine_similarity(classified_car_vector, df_feature_vectors)
# Get the index of the closest match
closest_match_idx = similarity_scores.argmax()
# Return the closest match details
return df.iloc[closest_match_idx]
# Streamlit App Updates
# Load and preprocess the car data once (globally for the session)
car_data = load_car_data()
if car_data is not None:
processed_car_data, label_encoders = preprocess_car_data(car_data)
# Your existing code for image upload and classification ...
# After classification, find the closest car match
if st.session_state.image is not None:
# Classify the car image (already done earlier)
with st.spinner('Analyzing image...'):
car_classifications = classify_image(st.session_state.image)
if car_classifications:
st.write("Image classification successful.")
top_prediction = car_classifications[0]['label']
make_name, model_name = top_prediction.split(' ', 1)
# Get the year (you may want to adjust this based on available data)
current_year = datetime.now().year
# Find the closest match in the CSV based on the classification
closest_car = find_closest_car(processed_car_data, label_encoders, make_name, model_name, current_year)
st.write(f"Closest match in database:")
st.write(f"Year: {closest_car['year']}")
st.write(f"Make: {label_encoders['make'].inverse_transform([closest_car['make']])[0]}")
st.write(f"Model: {label_encoders['model'].inverse_transform([closest_car['model']])[0]}")
st.write(f"Price: ${closest_car['price']}")
st.write(f"Condition: {closest_car['condition']}")
st.write(f"Fuel: {closest_car['fuel']}")
st.write(f"Transmission: {closest_car['transmission']}")
st.write(f"Drive: {closest_car['drive']}")
st.write(f"Type: {closest_car['type']}")
else:
st.error("Could not classify the image. Please try again with a different image.")