Lesterchia174 commited on
Commit
7f830f6
Β·
verified Β·
1 Parent(s): e701a60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -74
app.py CHANGED
@@ -1,12 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
- """app
3
-
4
- Automatically generated by Colab.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1-jiUnfRGcb_iRcTXISQT__JTrBD7QqFM
8
- """
9
-
10
  import gradio as gr
11
  import pandas as pd
12
  import numpy as np
@@ -19,8 +10,19 @@ from pathlib import Path
19
  import warnings
20
  warnings.filterwarnings('ignore')
21
 
 
 
 
 
 
 
 
 
 
 
 
22
  def create_dummy_model(model_type):
23
- """Create a realistic dummy model"""
24
  class RealisticDummyModel:
25
  def __init__(self, model_type):
26
  self.model_type = model_type
@@ -30,14 +32,20 @@ def create_dummy_model(model_type):
30
  'transaction_year', 'flat_type_encoded', 'town_encoded',
31
  'flat_model_encoded', 'dummy_feature'
32
  ]
 
 
 
33
 
34
  def predict(self, X):
35
  # Realistic prediction logic
36
- floor_area = X[0][0]
37
- storey_level = X[0][1]
38
- flat_age = X[0][2]
39
- town_encoded = X[0][6]
40
- flat_type_encoded = X[0][5]
 
 
 
41
 
42
  base_price = floor_area * (4800 + town_encoded * 200)
43
  storey_bonus = storey_level * 2500
@@ -50,59 +58,80 @@ def create_dummy_model(model_type):
50
  else:
51
  price = base_price + storey_bonus - age_discount - 25000
52
 
53
- return max(300000, price)
54
 
55
- return RealisticDummyModel(model_type)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- # Load models using Hugging Face Hub (handles Xet pointers)
58
  def load_models():
59
- """Load models using Hugging Face Hub library"""
60
  models = {}
61
 
 
62
  try:
63
- # Download XGBoost model (handles Xet pointer automatically)
64
  xgboost_path = hf_hub_download(
65
  repo_id="Lesterchia174/HDB_Price_Predictor",
66
  filename="best_model_xgboost.joblib",
67
  repo_type="space"
68
  )
69
- models['xgboost'] = joblib.load(xgboost_path)
70
- print("βœ… XGBoost model loaded successfully via Hugging Face Hub")
71
- print(f" File size: {os.path.getsize(xgboost_path)} bytes")
72
-
 
 
 
73
  except Exception as e:
74
- print(f"❌ Error loading XGBoost model: {e}")
75
- print("⚠️ This usually means xgboost package is not installed")
76
- models['xgboost'] = None
77
 
 
78
  try:
79
- # Download Linear Regression model
80
  linear_path = hf_hub_download(
81
  repo_id="Lesterchia174/HDB_Price_Predictor",
82
  filename="linear_regression.joblib",
83
  repo_type="space"
84
  )
85
- # Try to load without xgboost dependency
86
- try:
87
- models['linear_regression'] = joblib.load(linear_path)
88
- print("βœ… Linear Regression model loaded successfully via Hugging Face Hub")
89
- except Exception as e:
90
- if "xgboost" in str(e).lower():
91
- print("❌ Linear Regression model also requires xgboost")
92
- models['linear_regression'] = None
93
- else:
94
- raise e
95
-
96
  except Exception as e:
97
- print(f"❌ Error loading Linear Regression model: {e}")
98
- models['linear_regression'] = None
 
99
 
100
  return models
101
 
102
  def load_data():
103
  """Load data using Hugging Face Hub"""
104
  try:
105
- # Download data file
106
  data_path = hf_hub_download(
107
  repo_id="Lesterchia174/HDB_Price_Predictor",
108
  filename="base_hdb_resale_prices_2015Jan-2025Jun_processed.csv",
@@ -111,11 +140,8 @@ def load_data():
111
  df = pd.read_csv(data_path)
112
  print("βœ… Data loaded successfully via Hugging Face Hub")
113
  return df
114
-
115
  except Exception as e:
116
  print(f"❌ Error loading data: {e}")
117
- # Fallback to creating sample data
118
- print("⚠️ Creating sample data for demonstration")
119
  return create_sample_data()
120
 
121
  def create_sample_data():
@@ -151,20 +177,6 @@ def create_sample_data():
151
 
152
  return pd.DataFrame(data)
153
 
154
- # Preload models and data
155
- print("Loading models and data using Hugging Face Hub...")
156
- models = load_models()
157
- data = load_data()
158
-
159
- # If models failed to load, create dummy ones
160
- if models.get('xgboost') is None:
161
- print("⚠️ Creating dummy XGBoost model for demonstration")
162
- models['xgboost'] = create_dummy_model("xgboost")
163
-
164
- if models.get('linear_regression') is None:
165
- print("⚠️ Creating dummy Linear Regression model for demonstration")
166
- models['linear_regression'] = create_dummy_model("linear_regression")
167
-
168
  def preprocess_input(user_input, model_type='xgboost'):
169
  """Preprocess user input for prediction with correct feature mapping"""
170
  # Flat type mapping
@@ -236,7 +248,7 @@ def create_market_insights_chart(data, user_input, predicted_price_xgb, predicte
236
  return None
237
 
238
  def predict_hdb_price(town, flat_type, flat_model, floor_area_sqm, storey_level, flat_age, model_choice):
239
- """Main prediction function for Gradio"""
240
  user_input = {
241
  'town': town,
242
  'flat_type': flat_type,
@@ -246,15 +258,21 @@ def predict_hdb_price(town, flat_type, flat_model, floor_area_sqm, storey_level,
246
  'flat_age': flat_age
247
  }
248
 
249
- if models['xgboost'] is None or models['linear_regression'] is None:
250
- return "Error: Models not loaded", None, "Models failed to load. Please check the model files."
251
-
252
  try:
253
  processed_input = preprocess_input(user_input)
254
 
255
- # Get predictions from both models
256
- predicted_price_xgb = max(0, models['xgboost'].predict(processed_input)[0])
257
- predicted_price_lr = max(0, models['linear_regression'].predict(processed_input)[0])
 
 
 
 
 
 
 
 
 
258
 
259
  # Use selected model's prediction
260
  if model_choice == "XGBoost":
@@ -302,7 +320,14 @@ def predict_hdb_price(town, flat_type, flat_model, floor_area_sqm, storey_level,
302
  return f"${final_price:,.0f}", chart, insights
303
 
304
  except Exception as e:
305
- return f"Error: {str(e)}", None, f"Prediction failed. Error: {str(e)}"
 
 
 
 
 
 
 
306
 
307
  # Define Gradio interface
308
  towns_list = [
@@ -346,13 +371,6 @@ with gr.Blocks(title="🏠 HDB Price Predictor", theme=gr.themes.Soft()) as demo
346
  outputs=[predicted_price, chart_output, insights]
347
  )
348
 
349
- # For debugging
350
- if models['xgboost'] is not None:
351
- print(f"XGBoost model expects {models['xgboost'].n_features_in_} features")
352
-
353
- if models['linear_regression'] is not None:
354
- print(f"Linear Regression model expects {models['linear_regression'].n_features_in_} features")
355
-
356
  # To run in Colab
357
  if __name__ == "__main__":
358
  demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import numpy as np
 
10
  import warnings
11
  warnings.filterwarnings('ignore')
12
 
13
+ # Try to import xgboost, but fallback to scikit-learn
14
+ try:
15
+ import xgboost as xgb
16
+ XGB_AVAILABLE = True
17
+ print("βœ… XGBoost is available")
18
+ except ImportError:
19
+ XGB_AVAILABLE = False
20
+ print("⚠️ XGBoost not available, using scikit-learn models")
21
+ from sklearn.ensemble import RandomForestRegressor
22
+ from sklearn.linear_model import LinearRegression
23
+
24
  def create_dummy_model(model_type):
25
+ """Create a realistic dummy model that has all required methods"""
26
  class RealisticDummyModel:
27
  def __init__(self, model_type):
28
  self.model_type = model_type
 
32
  'transaction_year', 'flat_type_encoded', 'town_encoded',
33
  'flat_model_encoded', 'dummy_feature'
34
  ]
35
+ # Add methods that might be called by joblib or other code
36
+ self.get_params = lambda deep=True: {}
37
+ self.set_params = lambda **params: self
38
 
39
  def predict(self, X):
40
  # Realistic prediction logic
41
+ if isinstance(X, np.ndarray) and len(X.shape) == 2:
42
+ X = X[0] # Take first row if it's a 2D array
43
+
44
+ floor_area = X[0]
45
+ storey_level = X[1]
46
+ flat_age = X[2]
47
+ town_encoded = X[6]
48
+ flat_type_encoded = X[5]
49
 
50
  base_price = floor_area * (4800 + town_encoded * 200)
51
  storey_bonus = storey_level * 2500
 
58
  else:
59
  price = base_price + storey_bonus - age_discount - 25000
60
 
61
+ return np.array([max(300000, price)])
62
 
63
+ return RealisticDummyModel(model_type)()
64
+
65
+ def safe_joblib_load(filepath):
66
+ """Safely load joblib file with error handling"""
67
+ try:
68
+ model = joblib.load(filepath)
69
+ print(f"βœ… Successfully loaded model from {filepath}")
70
+
71
+ # Check if model has required methods
72
+ if not hasattr(model, 'predict'):
73
+ print("❌ Loaded object doesn't have predict method")
74
+ return None
75
+
76
+ # Add missing methods if needed
77
+ if not hasattr(model, 'get_params'):
78
+ model.get_params = lambda deep=True: {}
79
+ if not hasattr(model, 'set_params'):
80
+ model.set_params = lambda **params: model
81
+
82
+ return model
83
+
84
+ except Exception as e:
85
+ print(f"❌ Error loading model from {filepath}: {e}")
86
+ return None
87
 
 
88
  def load_models():
89
+ """Load models with robust error handling"""
90
  models = {}
91
 
92
+ # Try to load XGBoost model
93
  try:
 
94
  xgboost_path = hf_hub_download(
95
  repo_id="Lesterchia174/HDB_Price_Predictor",
96
  filename="best_model_xgboost.joblib",
97
  repo_type="space"
98
  )
99
+ models['xgboost'] = safe_joblib_load(xgboost_path)
100
+ if models['xgboost'] is None:
101
+ print("⚠️ Creating dummy model for XGBoost")
102
+ models['xgboost'] = create_dummy_model("xgboost")
103
+ else:
104
+ print("βœ… XGBoost model loaded and validated")
105
+
106
  except Exception as e:
107
+ print(f"❌ Error downloading XGBoost model: {e}")
108
+ print("⚠️ Creating dummy model for XGBoost")
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
 
132
  def load_data():
133
  """Load data using Hugging Face Hub"""
134
  try:
 
135
  data_path = hf_hub_download(
136
  repo_id="Lesterchia174/HDB_Price_Predictor",
137
  filename="base_hdb_resale_prices_2015Jan-2025Jun_processed.csv",
 
140
  df = pd.read_csv(data_path)
141
  print("βœ… Data loaded successfully via Hugging Face Hub")
142
  return df
 
143
  except Exception as e:
144
  print(f"❌ Error loading data: {e}")
 
 
145
  return create_sample_data()
146
 
147
  def create_sample_data():
 
177
 
178
  return pd.DataFrame(data)
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  def preprocess_input(user_input, model_type='xgboost'):
181
  """Preprocess user input for prediction with correct feature mapping"""
182
  # Flat type mapping
 
248
  return None
249
 
250
  def predict_hdb_price(town, flat_type, flat_model, floor_area_sqm, storey_level, flat_age, model_choice):
251
+ """Main prediction function for Gradio with robust error handling"""
252
  user_input = {
253
  'town': town,
254
  'flat_type': flat_type,
 
258
  'flat_age': flat_age
259
  }
260
 
 
 
 
261
  try:
262
  processed_input = preprocess_input(user_input)
263
 
264
+ # Get predictions from both models with error handling
265
+ try:
266
+ predicted_price_xgb = max(0, float(models['xgboost'].predict(processed_input)[0]))
267
+ except Exception as e:
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":
 
320
  return f"${final_price:,.0f}", chart, insights
321
 
322
  except Exception as e:
323
+ error_msg = f"Prediction failed. Error: {str(e)}"
324
+ print(error_msg)
325
+ return "Error: Prediction failed", None, error_msg
326
+
327
+ # Preload models and data
328
+ print("Loading models and data...")
329
+ models = load_models()
330
+ data = load_data()
331
 
332
  # Define Gradio interface
333
  towns_list = [
 
371
  outputs=[predicted_price, chart_output, insights]
372
  )
373
 
 
 
 
 
 
 
 
374
  # To run in Colab
375
  if __name__ == "__main__":
376
  demo.launch(share=True)