Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -109,23 +109,23 @@ def load_models():
|
|
| 109 |
models['xgboost'] = create_dummy_model("xgboost")
|
| 110 |
|
| 111 |
# Try to load Linear Regression model
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
return models
|
| 131 |
|
|
@@ -237,11 +237,11 @@ def create_market_insights_chart(data, user_input, predicted_price_xgb, predicte
|
|
| 237 |
line=dict(width=2, color='darkred')),
|
| 238 |
name='XGBoost Prediction'))
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
|
| 246 |
fig.update_layout(template="plotly_white", height=400, showlegend=True)
|
| 247 |
return fig
|
|
@@ -268,11 +268,11 @@ def predict_hdb_price(town, flat_type, flat_model, floor_area_sqm, storey_level,
|
|
| 268 |
print(f"❌ XGBoost prediction error: {e}")
|
| 269 |
predicted_price_xgb = 400000 # Fallback value
|
| 270 |
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
|
| 277 |
# Use selected model's prediction
|
| 278 |
if model_choice == "XGBoost":
|
|
@@ -299,8 +299,8 @@ def predict_hdb_price(town, flat_type, flat_model, floor_area_sqm, storey_level,
|
|
| 299 |
|
| 300 |
**Model Predictions:**
|
| 301 |
- XGBoost: ${predicted_price_xgb:,.0f}
|
| 302 |
-
|
| 303 |
-
|
| 304 |
|
| 305 |
**Selected Model: {model_choice}**
|
| 306 |
|
|
|
|
| 109 |
models['xgboost'] = create_dummy_model("xgboost")
|
| 110 |
|
| 111 |
# Try to load Linear Regression model
|
| 112 |
+
try:
|
| 113 |
+
linear_path = hf_hub_download(
|
| 114 |
+
repo_id="Lesterchia174/HDB_Price_Predictor",
|
| 115 |
+
filename="linear_regression.joblib",
|
| 116 |
+
repo_type="space"
|
| 117 |
+
)
|
| 118 |
+
models['linear_regression'] = safe_joblib_load(linear_path)
|
| 119 |
+
if models['linear_regression'] is None:
|
| 120 |
+
print("⚠️ Creating dummy model for Linear Regression")
|
| 121 |
+
models['linear_regression'] = create_dummy_model("linear_regression")
|
| 122 |
+
else:
|
| 123 |
+
print("✅ Linear Regression model loaded and validated")
|
| 124 |
|
| 125 |
+
except Exception as e:
|
| 126 |
+
print(f"❌ Error downloading Linear Regression model: {e}")
|
| 127 |
+
print("⚠️ Creating dummy model for Linear Regression")
|
| 128 |
+
models['linear_regression'] = create_dummy_model("linear_regression")
|
| 129 |
|
| 130 |
return models
|
| 131 |
|
|
|
|
| 237 |
line=dict(width=2, color='darkred')),
|
| 238 |
name='XGBoost Prediction'))
|
| 239 |
|
| 240 |
+
fig.add_trace(go.Scatter(x=[user_input['floor_area_sqm']], y=[predicted_price_lr],
|
| 241 |
+
mode='markers',
|
| 242 |
+
marker=dict(symbol='diamond', size=20, color='blue',
|
| 243 |
+
line=dict(width=2, color='darkblue')),
|
| 244 |
+
name='Linear Regression Prediction'))
|
| 245 |
|
| 246 |
fig.update_layout(template="plotly_white", height=400, showlegend=True)
|
| 247 |
return fig
|
|
|
|
| 268 |
print(f"❌ XGBoost prediction error: {e}")
|
| 269 |
predicted_price_xgb = 400000 # Fallback value
|
| 270 |
|
| 271 |
+
try:
|
| 272 |
+
predicted_price_lr = max(0, float(models['linear_regression'].predict(processed_input)[0]))
|
| 273 |
+
except Exception as e:
|
| 274 |
+
print(f"❌ Linear Regression prediction error: {e}")
|
| 275 |
+
predicted_price_lr = 380000 # Fallback value
|
| 276 |
|
| 277 |
# Use selected model's prediction
|
| 278 |
if model_choice == "XGBoost":
|
|
|
|
| 299 |
|
| 300 |
**Model Predictions:**
|
| 301 |
- XGBoost: ${predicted_price_xgb:,.0f}
|
| 302 |
+
- Linear Regression: ${predicted_price_lr:,.0f}
|
| 303 |
+
- Difference: ${abs(predicted_price_xgb - predicted_price_lr):,.0f}
|
| 304 |
|
| 305 |
**Selected Model: {model_choice}**
|
| 306 |
|