Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,8 +8,6 @@ from io import BytesIO
|
|
8 |
import matplotlib.pyplot as plt
|
9 |
import numpy as np
|
10 |
from sklearn.preprocessing import LabelEncoder
|
11 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
12 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
13 |
from huggingface_hub import hf_hub_download
|
14 |
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
15 |
import torch
|
@@ -58,32 +56,8 @@ def classify_image(image):
|
|
58 |
st.error(f"Classification error: {e}")
|
59 |
return None
|
60 |
|
61 |
-
def
|
62 |
-
|
63 |
-
df['full_name'] = df['Make'] + ' ' + df['Model']
|
64 |
-
|
65 |
-
# Create a list of all car names
|
66 |
-
car_names = df['full_name'].tolist()
|
67 |
-
|
68 |
-
# Add the query car name
|
69 |
-
query_car = f"{brand} {model}"
|
70 |
-
car_names.append(query_car)
|
71 |
-
|
72 |
-
# Create TF-IDF vectorizer
|
73 |
-
vectorizer = TfidfVectorizer()
|
74 |
-
tfidf_matrix = vectorizer.fit_transform(car_names)
|
75 |
-
|
76 |
-
# Compute cosine similarity
|
77 |
-
cosine_similarities = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1]).flatten()
|
78 |
-
|
79 |
-
# Get the index of the most similar car
|
80 |
-
most_similar_index = cosine_similarities.argmax()
|
81 |
-
|
82 |
-
# Return the most similar car's data
|
83 |
-
return df.iloc[most_similar_index]
|
84 |
-
|
85 |
-
def get_car_overview(car_data):
|
86 |
-
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"
|
87 |
response = openai.ChatCompletion.create(
|
88 |
model="gpt-3.5-turbo",
|
89 |
messages=[{"role": "user", "content": prompt}]
|
@@ -100,30 +74,28 @@ def load_model_and_encodings():
|
|
100 |
st.error(f"Error loading model: {str(e)}")
|
101 |
raise e
|
102 |
|
103 |
-
def predict_price(model,
|
104 |
-
#
|
105 |
-
input_data =
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
# Calculate age
|
111 |
current_year = datetime.now().year
|
112 |
-
input_data['
|
113 |
-
input_data['
|
114 |
-
|
115 |
-
# If odometer is missing, estimate it based on age and average yearly mileage
|
116 |
-
if 'Odometer' not in input_data or pd.isna(input_data['Odometer']):
|
117 |
-
avg_yearly_mileage = 12000 # Adjust this value as needed
|
118 |
-
input_data['Odometer'] = input_data['Age'] * avg_yearly_mileage
|
119 |
-
|
120 |
-
# Ensure all required columns are present
|
121 |
-
required_columns = ['Make', 'Model', 'Year', 'Condition', 'Fuel', 'Odometer', 'Title_status', 'Transmission', 'Drive', 'Size', 'Type', 'Paint_color', 'Age', 'Age_squared']
|
122 |
-
|
123 |
-
for col in required_columns:
|
124 |
-
if col not in input_data or pd.isna(input_data[col]):
|
125 |
-
# If a required column is missing, fill it with the most common value from the dataset
|
126 |
-
input_data[col] = df[col].mode().iloc[0]
|
127 |
|
128 |
# Prepare the input for the model
|
129 |
input_df = pd.DataFrame([input_data])
|
@@ -181,44 +153,33 @@ if image is not None:
|
|
181 |
|
182 |
st.write(f"Identified Car: {brand} {model_name}")
|
183 |
|
184 |
-
#
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
# Plotting the results
|
212 |
-
plt.figure(figsize=(10, 5))
|
213 |
-
plt.plot(years, predicted_prices, marker='o')
|
214 |
-
plt.title(f"Predicted Price of {match['Make']} {match['Model']} Over Time")
|
215 |
-
plt.xlabel("Year")
|
216 |
-
plt.ylabel("Predicted Price ($)")
|
217 |
-
plt.grid()
|
218 |
-
st.pyplot(plt)
|
219 |
-
|
220 |
-
else:
|
221 |
-
st.write("No match found in the database.")
|
222 |
else:
|
223 |
st.error("Could not classify the image. Please try again with a different image.")
|
224 |
else:
|
|
|
8 |
import matplotlib.pyplot as plt
|
9 |
import numpy as np
|
10 |
from sklearn.preprocessing import LabelEncoder
|
|
|
|
|
11 |
from huggingface_hub import hf_hub_download
|
12 |
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
13 |
import torch
|
|
|
56 |
st.error(f"Classification error: {e}")
|
57 |
return None
|
58 |
|
59 |
+
def get_car_overview(brand, model, year):
|
60 |
+
prompt = f"Provide an overview of the following car:\nYear: {year}\nMake: {brand}\nModel: {model}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
response = openai.ChatCompletion.create(
|
62 |
model="gpt-3.5-turbo",
|
63 |
messages=[{"role": "user", "content": prompt}]
|
|
|
74 |
st.error(f"Error loading model: {str(e)}")
|
75 |
raise e
|
76 |
|
77 |
+
def predict_price(model, brand, model_name, year):
|
78 |
+
# Create a dictionary with default values
|
79 |
+
input_data = {
|
80 |
+
'year': year,
|
81 |
+
'make': brand,
|
82 |
+
'model': model_name,
|
83 |
+
'trim': 'Base', # Default trim
|
84 |
+
'condition': 'Used', # Default condition
|
85 |
+
'fuel': 'Gasoline', # Default fuel type
|
86 |
+
'odometer': year * 12000, # Estimate based on year and average annual mileage
|
87 |
+
'title_status': 'Clean', # Default title status
|
88 |
+
'transmission': 'Automatic', # Default transmission
|
89 |
+
'drive': 'Fwd', # Default drive
|
90 |
+
'size': 'Mid-Size', # Default size
|
91 |
+
'type': 'Sedan', # Default type
|
92 |
+
'paint_color': 'White' # Default color
|
93 |
+
}
|
94 |
|
95 |
# Calculate age
|
96 |
current_year = datetime.now().year
|
97 |
+
input_data['age'] = current_year - year
|
98 |
+
input_data['age_squared'] = input_data['age'] ** 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
# Prepare the input for the model
|
101 |
input_df = pd.DataFrame([input_data])
|
|
|
153 |
|
154 |
st.write(f"Identified Car: {brand} {model_name}")
|
155 |
|
156 |
+
# Get additional information using GPT-3.5-turbo
|
157 |
+
current_year = datetime.now().year
|
158 |
+
overview = get_car_overview(brand, model_name, current_year)
|
159 |
+
st.write("Car Overview:")
|
160 |
+
st.write(overview)
|
161 |
+
|
162 |
+
# Interactive Price Prediction
|
163 |
+
st.subheader("Price Prediction Over Time")
|
164 |
+
selected_years = st.slider("Select range of years for price prediction",
|
165 |
+
min_value=2000, max_value=2023, value=(2010, 2023))
|
166 |
+
|
167 |
+
years = np.arange(selected_years[0], selected_years[1] + 1)
|
168 |
+
predicted_prices = []
|
169 |
+
|
170 |
+
for year in years:
|
171 |
+
price = predict_price(model, brand, model_name, year)
|
172 |
+
predicted_prices.append(price)
|
173 |
+
|
174 |
+
# Plotting the results
|
175 |
+
plt.figure(figsize=(10, 5))
|
176 |
+
plt.plot(years, predicted_prices, marker='o')
|
177 |
+
plt.title(f"Predicted Price of {brand} {model_name} Over Time")
|
178 |
+
plt.xlabel("Year")
|
179 |
+
plt.ylabel("Predicted Price ($)")
|
180 |
+
plt.grid()
|
181 |
+
st.pyplot(plt)
|
182 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
else:
|
184 |
st.error("Could not classify the image. Please try again with a different image.")
|
185 |
else:
|