Spaces:
Sleeping
Sleeping
File size: 3,642 Bytes
63dce96 f0bade0 63dce96 ecae6dc 63dce96 63b55ed 63dce96 63b55ed 63dce96 63b55ed 63dce96 ea53bcc 63dce96 bd96a0e 63dce96 ea53bcc 63dce96 8c5b0dd 63dce96 7ff0ab2 63dce96 bd96a0e 63dce96 b24890a bd96a0e b24890a ef8a626 b24890a 63dce96 d91e2c5 63dce96 b24890a 63dce96 |
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 |
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.") |