Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,9 @@ import numpy as np
|
|
| 10 |
from sklearn.preprocessing import LabelEncoder
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Dataset loading function with caching
|
| 15 |
@st.cache_data
|
|
@@ -42,10 +45,28 @@ def classify_image(image):
|
|
| 42 |
return None
|
| 43 |
|
| 44 |
def find_closest_match(df, brand, model):
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def get_car_overview(car_data):
|
| 51 |
prompt = f"Provide an overview of the following car:\nYear: {car_data['Year']}\nMake: {car_data['Make']}\nModel: {car_data['Model']}\nTrim: {car_data['Trim']}\nPrice: ${car_data['Price']}\nCondition: {car_data['Condition']}\n"
|
|
@@ -124,7 +145,10 @@ if camera_image is not None:
|
|
| 124 |
match = find_closest_match(df, brand, model_name)
|
| 125 |
if match is not None:
|
| 126 |
st.write("Closest Match Found:")
|
| 127 |
-
st.write(match)
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
# Get additional information using GPT-3.5-turbo
|
| 130 |
overview = get_car_overview(match)
|
|
@@ -141,8 +165,8 @@ if camera_image is not None:
|
|
| 141 |
|
| 142 |
for year in years:
|
| 143 |
user_input = {
|
| 144 |
-
'Make':
|
| 145 |
-
'Model':
|
| 146 |
'Condition': match['Condition'],
|
| 147 |
'Fuel': match['Fuel'],
|
| 148 |
'Title_status': match['Title_status'],
|
|
@@ -160,7 +184,7 @@ if camera_image is not None:
|
|
| 160 |
# Plotting the results
|
| 161 |
plt.figure(figsize=(10, 5))
|
| 162 |
plt.plot(years, predicted_prices, marker='o')
|
| 163 |
-
plt.title(f"Predicted Price of {
|
| 164 |
plt.xlabel("Year")
|
| 165 |
plt.ylabel("Predicted Price ($)")
|
| 166 |
plt.grid()
|
|
|
|
| 10 |
from sklearn.preprocessing import LabelEncoder
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
from transformers import pipeline
|
| 13 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 14 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 15 |
+
import re
|
| 16 |
|
| 17 |
# Dataset loading function with caching
|
| 18 |
@st.cache_data
|
|
|
|
| 45 |
return None
|
| 46 |
|
| 47 |
def find_closest_match(df, brand, model):
|
| 48 |
+
# Combine brand and model names from the dataset
|
| 49 |
+
df['full_name'] = df['Make'] + ' ' + df['Model']
|
| 50 |
+
|
| 51 |
+
# Create a list of all car names
|
| 52 |
+
car_names = df['full_name'].tolist()
|
| 53 |
+
|
| 54 |
+
# Add the query car name
|
| 55 |
+
query_car = f"{brand} {model}"
|
| 56 |
+
car_names.append(query_car)
|
| 57 |
+
|
| 58 |
+
# Create TF-IDF vectorizer
|
| 59 |
+
vectorizer = TfidfVectorizer()
|
| 60 |
+
tfidf_matrix = vectorizer.fit_transform(car_names)
|
| 61 |
+
|
| 62 |
+
# Compute cosine similarity
|
| 63 |
+
cosine_similarities = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1]).flatten()
|
| 64 |
+
|
| 65 |
+
# Get the index of the most similar car
|
| 66 |
+
most_similar_index = cosine_similarities.argmax()
|
| 67 |
+
|
| 68 |
+
# Return the most similar car's data
|
| 69 |
+
return df.iloc[most_similar_index]
|
| 70 |
|
| 71 |
def get_car_overview(car_data):
|
| 72 |
prompt = f"Provide an overview of the following car:\nYear: {car_data['Year']}\nMake: {car_data['Make']}\nModel: {car_data['Model']}\nTrim: {car_data['Trim']}\nPrice: ${car_data['Price']}\nCondition: {car_data['Condition']}\n"
|
|
|
|
| 145 |
match = find_closest_match(df, brand, model_name)
|
| 146 |
if match is not None:
|
| 147 |
st.write("Closest Match Found:")
|
| 148 |
+
st.write(f"Make: {match['Make']}")
|
| 149 |
+
st.write(f"Model: {match['Model']}")
|
| 150 |
+
st.write(f"Year: {match['Year']}")
|
| 151 |
+
st.write(f"Price: ${match['Price']}")
|
| 152 |
|
| 153 |
# Get additional information using GPT-3.5-turbo
|
| 154 |
overview = get_car_overview(match)
|
|
|
|
| 165 |
|
| 166 |
for year in years:
|
| 167 |
user_input = {
|
| 168 |
+
'Make': match['Make'],
|
| 169 |
+
'Model': match['Model'],
|
| 170 |
'Condition': match['Condition'],
|
| 171 |
'Fuel': match['Fuel'],
|
| 172 |
'Title_status': match['Title_status'],
|
|
|
|
| 184 |
# Plotting the results
|
| 185 |
plt.figure(figsize=(10, 5))
|
| 186 |
plt.plot(years, predicted_prices, marker='o')
|
| 187 |
+
plt.title(f"Predicted Price of {match['Make']} {match['Model']} Over Time")
|
| 188 |
plt.xlabel("Year")
|
| 189 |
plt.ylabel("Predicted Price ($)")
|
| 190 |
plt.grid()
|