Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -109,7 +109,7 @@ def predict_price(model, encoders, user_input):
|
|
109 |
|
110 |
# Streamlit App
|
111 |
st.title("Auto Appraise")
|
112 |
-
st.write("
|
113 |
|
114 |
# Load model and encoders
|
115 |
model, label_encoders = load_model_and_encodings()
|
@@ -117,97 +117,94 @@ model, label_encoders = load_model_and_encodings()
|
|
117 |
# Initialize OpenAI API key
|
118 |
openai.api_key = st.secrets["GPT_TOKEN"]
|
119 |
|
120 |
-
#
|
121 |
-
|
122 |
|
123 |
-
#
|
124 |
-
st.
|
125 |
-
st.write(f"Content of camera_image: {camera_image}")
|
126 |
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
st.write("Image captured successfully.")
|
129 |
-
try:
|
130 |
-
image = load_image(camera_image)
|
131 |
-
st.image(image, caption='Captured Image.', use_container_width=True)
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
predicted_prices.append(price)
|
190 |
-
|
191 |
-
# Plotting the results
|
192 |
-
plt.figure(figsize=(10, 5))
|
193 |
-
plt.plot(years, predicted_prices, marker='o')
|
194 |
-
plt.title(f"Predicted Price of {match['Make']} {match['Model']} Over Time")
|
195 |
-
plt.xlabel("Year")
|
196 |
-
plt.ylabel("Predicted Price ($)")
|
197 |
-
plt.grid()
|
198 |
-
st.pyplot(plt)
|
199 |
-
|
200 |
-
else:
|
201 |
-
st.write("No match found in the database.")
|
202 |
else:
|
203 |
-
st.
|
204 |
-
|
205 |
-
st.error(
|
206 |
else:
|
207 |
-
st.write("Please take a picture
|
208 |
-
|
209 |
-
# # Alternative file uploader (for debugging)
|
210 |
-
# uploaded_file = st.file_uploader("Or choose a car image", type=["jpg", "jpeg", "png"])
|
211 |
-
# if uploaded_file is not None:
|
212 |
-
# image = Image.open(uploaded_file)
|
213 |
-
# st.image(image, caption='Uploaded Image.', use_container_width=True)
|
|
|
109 |
|
110 |
# Streamlit App
|
111 |
st.title("Auto Appraise")
|
112 |
+
st.write("Upload a car image or take a picture to get its brand, model, overview, and expected price!")
|
113 |
|
114 |
# Load model and encoders
|
115 |
model, label_encoders = load_model_and_encodings()
|
|
|
117 |
# Initialize OpenAI API key
|
118 |
openai.api_key = st.secrets["GPT_TOKEN"]
|
119 |
|
120 |
+
# File uploader for image
|
121 |
+
uploaded_file = st.file_uploader("Choose a car image", type=["jpg", "jpeg", "png"])
|
122 |
|
123 |
+
# Camera input as an alternative (optional)
|
124 |
+
camera_image = st.camera_input("Or take a picture of the car")
|
|
|
125 |
|
126 |
+
# Process the image (either uploaded or from camera)
|
127 |
+
image = None
|
128 |
+
if uploaded_file is not None:
|
129 |
+
image = Image.open(uploaded_file)
|
130 |
+
st.write("Image uploaded successfully.")
|
131 |
+
elif camera_image is not None:
|
132 |
+
image = Image.open(camera_image)
|
133 |
st.write("Image captured successfully.")
|
|
|
|
|
|
|
134 |
|
135 |
+
if image is not None:
|
136 |
+
st.image(image, caption='Processed Image', use_container_width=True)
|
137 |
+
|
138 |
+
# Classify the car image
|
139 |
+
with st.spinner('Analyzing image...'):
|
140 |
+
car_classifications = classify_image(image)
|
141 |
+
|
142 |
+
if car_classifications:
|
143 |
+
st.write("Image classification successful.")
|
144 |
+
st.subheader("Car Classification Results:")
|
145 |
+
for classification in car_classifications:
|
146 |
+
st.write(f"Model: {classification['label']}")
|
147 |
+
st.write(f"Confidence: {classification['score']*100:.2f}%")
|
148 |
+
|
149 |
+
# Use the top prediction for further processing
|
150 |
+
top_prediction = car_classifications[0]['label']
|
151 |
+
brand, model_name = top_prediction.split(' ', 1)
|
152 |
|
153 |
+
st.write(f"Identified Car: {brand} {model_name}")
|
154 |
+
|
155 |
+
# Find the closest match in the CSV
|
156 |
+
df = load_datasets()
|
157 |
+
match = find_closest_match(df, brand, model_name)
|
158 |
+
if match is not None:
|
159 |
+
st.write("Closest Match Found:")
|
160 |
+
st.write(f"Make: {match['Make']}")
|
161 |
+
st.write(f"Model: {match['Model']}")
|
162 |
+
st.write(f"Year: {match['Year']}")
|
163 |
+
st.write(f"Price: ${match['Price']}")
|
164 |
+
|
165 |
+
# Get additional information using GPT-3.5-turbo
|
166 |
+
overview = get_car_overview(match)
|
167 |
+
st.write("Car Overview:")
|
168 |
+
st.write(overview)
|
169 |
+
|
170 |
+
# Interactive Price Prediction
|
171 |
+
st.subheader("Price Prediction Over Time")
|
172 |
+
selected_years = st.slider("Select range of years for price prediction",
|
173 |
+
min_value=2000, max_value=2023, value=(2010, 2023))
|
174 |
+
|
175 |
+
years = np.arange(selected_years[0], selected_years[1] + 1)
|
176 |
+
predicted_prices = []
|
177 |
+
|
178 |
+
for year in years:
|
179 |
+
user_input = {
|
180 |
+
'Make': match['Make'],
|
181 |
+
'Model': match['Model'],
|
182 |
+
'Condition': match['Condition'],
|
183 |
+
'Fuel': match['Fuel'],
|
184 |
+
'Title_status': match['Title_status'],
|
185 |
+
'Transmission': match['Transmission'],
|
186 |
+
'Drive': match['Drive'],
|
187 |
+
'Size': match['Size'],
|
188 |
+
'Type': match['Type'],
|
189 |
+
'Paint_color': match['Paint_color'],
|
190 |
+
'Year': year
|
191 |
+
}
|
192 |
+
|
193 |
+
price = predict_price(model, label_encoders, user_input)
|
194 |
+
predicted_prices.append(price)
|
195 |
+
|
196 |
+
# Plotting the results
|
197 |
+
plt.figure(figsize=(10, 5))
|
198 |
+
plt.plot(years, predicted_prices, marker='o')
|
199 |
+
plt.title(f"Predicted Price of {match['Make']} {match['Model']} Over Time")
|
200 |
+
plt.xlabel("Year")
|
201 |
+
plt.ylabel("Predicted Price ($)")
|
202 |
+
plt.grid()
|
203 |
+
st.pyplot(plt)
|
204 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
else:
|
206 |
+
st.write("No match found in the database.")
|
207 |
+
else:
|
208 |
+
st.error("Could not classify the image. Please try again with a different image.")
|
209 |
else:
|
210 |
+
st.write("Please upload an image or take a picture to proceed.")
|
|
|
|
|
|
|
|
|
|
|
|