Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -100,24 +100,31 @@ def load_model_and_encodings():
|
|
| 100 |
original_data = load_datasets()
|
| 101 |
|
| 102 |
label_encoders = {}
|
| 103 |
-
categorical_features = ['
|
| 104 |
-
'Transmission', 'Drive', 'Size', 'Type', 'Paint_color']
|
| 105 |
|
| 106 |
for feature in categorical_features:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
label_encoders[feature.lower()] = le
|
| 112 |
|
| 113 |
-
return model, label_encoders
|
| 114 |
except Exception as e:
|
| 115 |
st.error(f"Error loading model: {str(e)}")
|
| 116 |
raise e
|
| 117 |
|
| 118 |
-
def predict_price(model, encoders, user_input):
|
| 119 |
-
encoded_features = {
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
input_data = pd.DataFrame([encoded_features])
|
| 122 |
predicted_price = model.predict(input_data)
|
| 123 |
return predicted_price[0]
|
|
@@ -127,7 +134,7 @@ st.title("Auto Appraise")
|
|
| 127 |
st.write("Upload a car image or take a picture to get its brand, model, overview, and expected price!")
|
| 128 |
|
| 129 |
# Load model and encoders
|
| 130 |
-
model, label_encoders = load_model_and_encodings()
|
| 131 |
|
| 132 |
# Initialize OpenAI API key
|
| 133 |
openai.api_key = st.secrets["GPT_TOKEN"]
|
|
@@ -192,21 +199,21 @@ if image is not None:
|
|
| 192 |
|
| 193 |
for year in years:
|
| 194 |
user_input = {
|
| 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))
|
|
|
|
| 100 |
original_data = load_datasets()
|
| 101 |
|
| 102 |
label_encoders = {}
|
| 103 |
+
categorical_features = original_data.select_dtypes(include=['object']).columns.tolist()
|
|
|
|
| 104 |
|
| 105 |
for feature in categorical_features:
|
| 106 |
+
le = LabelEncoder()
|
| 107 |
+
unique_values = original_data[feature].fillna('unknown').str.strip().unique()
|
| 108 |
+
le.fit(unique_values)
|
| 109 |
+
label_encoders[feature.lower()] = le
|
|
|
|
| 110 |
|
| 111 |
+
return model, label_encoders, categorical_features
|
| 112 |
except Exception as e:
|
| 113 |
st.error(f"Error loading model: {str(e)}")
|
| 114 |
raise e
|
| 115 |
|
| 116 |
+
def predict_price(model, encoders, categorical_features, user_input):
|
| 117 |
+
encoded_features = {}
|
| 118 |
+
for feature, value in user_input.items():
|
| 119 |
+
if feature.lower() in encoders:
|
| 120 |
+
encoded_features[feature] = encoders[feature.lower()].transform([value])[0]
|
| 121 |
+
elif feature in categorical_features:
|
| 122 |
+
# If it's a categorical feature but not in encoders, set to 0 (unknown)
|
| 123 |
+
encoded_features[feature] = 0
|
| 124 |
+
else:
|
| 125 |
+
# For numerical features, use the value as is
|
| 126 |
+
encoded_features[feature] = value
|
| 127 |
+
|
| 128 |
input_data = pd.DataFrame([encoded_features])
|
| 129 |
predicted_price = model.predict(input_data)
|
| 130 |
return predicted_price[0]
|
|
|
|
| 134 |
st.write("Upload a car image or take a picture to get its brand, model, overview, and expected price!")
|
| 135 |
|
| 136 |
# Load model and encoders
|
| 137 |
+
model, label_encoders, categorical_features = load_model_and_encodings()
|
| 138 |
|
| 139 |
# Initialize OpenAI API key
|
| 140 |
openai.api_key = st.secrets["GPT_TOKEN"]
|
|
|
|
| 199 |
|
| 200 |
for year in years:
|
| 201 |
user_input = {
|
| 202 |
+
'make': match['Make'],
|
| 203 |
+
'model': match['Model'],
|
| 204 |
+
'condition': match.get('Condition', 'unknown'),
|
| 205 |
+
'fuel': match.get('Fuel', 'unknown'),
|
| 206 |
+
'title_status': match.get('Title_status', 'unknown'),
|
| 207 |
+
'transmission': match.get('Transmission', 'unknown'),
|
| 208 |
+
'drive': match.get('Drive', 'unknown'),
|
| 209 |
+
'size': match.get('Size', 'unknown'),
|
| 210 |
+
'type': match.get('Type', 'unknown'),
|
| 211 |
+
'paint_color': match.get('Paint_color', 'unknown'),
|
| 212 |
+
'year': year
|
| 213 |
}
|
| 214 |
|
| 215 |
+
price = predict_price(model, label_encoders, categorical_features, user_input)
|
| 216 |
+
predicted_prices.append(price)
|
| 217 |
|
| 218 |
# Plotting the results
|
| 219 |
plt.figure(figsize=(10, 5))
|