EdBoy2202 commited on
Commit
b24890a
·
verified ·
1 Parent(s): 8c5b0dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -88
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("Capture a car image using your camera or upload an image to get its brand, model, overview, and expected price!")
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
- # Camera input for taking photo
121
- camera_image = st.camera_input("Take a picture of the car!")
122
 
123
- # Debug information
124
- st.write(f"Type of camera_image: {type(camera_image)}")
125
- st.write(f"Content of camera_image: {camera_image}")
126
 
127
- if camera_image is not None:
 
 
 
 
 
 
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
- # Classify the car image
134
- with st.spinner('Analyzing image...'):
135
- car_classifications = classify_image(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
- if car_classifications:
138
- st.write("Image classification successful.")
139
- st.subheader("Car Classification Results:")
140
- for classification in car_classifications:
141
- st.write(f"Model: {classification['label']}")
142
- st.write(f"Confidence: {classification['score']*100:.2f}%")
143
-
144
- # Use the top prediction for further processing
145
- top_prediction = car_classifications[0]['label']
146
- brand, model_name = top_prediction.split(' ', 1)
147
-
148
- st.write(f"Identified Car: {brand} {model_name}")
149
-
150
- # Find the closest match in the CSV
151
- df = load_datasets()
152
- match = find_closest_match(df, brand, model_name)
153
- if match is not None:
154
- st.write("Closest Match Found:")
155
- st.write(f"Make: {match['Make']}")
156
- st.write(f"Model: {match['Model']}")
157
- st.write(f"Year: {match['Year']}")
158
- st.write(f"Price: ${match['Price']}")
159
-
160
- # Get additional information using GPT-3.5-turbo
161
- overview = get_car_overview(match)
162
- st.write("Car Overview:")
163
- st.write(overview)
164
-
165
- # Interactive Price Prediction
166
- st.subheader("Price Prediction Over Time")
167
- selected_years = st.slider("Select range of years for price prediction",
168
- min_value=2000, max_value=2023, value=(2010, 2023))
169
-
170
- years = np.arange(selected_years[0], selected_years[1] + 1)
171
- predicted_prices = []
172
-
173
- for year in years:
174
- user_input = {
175
- 'Make': match['Make'],
176
- 'Model': match['Model'],
177
- 'Condition': match['Condition'],
178
- 'Fuel': match['Fuel'],
179
- 'Title_status': match['Title_status'],
180
- 'Transmission': match['Transmission'],
181
- 'Drive': match['Drive'],
182
- 'Size': match['Size'],
183
- 'Type': match['Type'],
184
- 'Paint_color': match['Paint_color'],
185
- 'Year': year
186
- }
187
-
188
- price = predict_price(model, label_encoders, user_input)
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.error("Could not classify the image. Please try again with a different image.")
204
- except Exception as e:
205
- st.error(f"Error processing image: {str(e)}")
206
  else:
207
- st.write("Please take a picture of the car to proceed.")
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.")