MatCod commited on
Commit
47d96da
·
verified ·
1 Parent(s): 94f60eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -94,11 +94,27 @@ class EnergyMLPredictor:
94
  # Preprocess
95
  X_processed = self.threshold_preprocessor.transform(input_df)
96
 
97
- # Make predictions
98
- prob_83 = self.threshold_model_83.predict_proba(X_processed)[0][1] if len(self.threshold_model_83.classes_) > 1 else 0.0
 
 
 
 
 
 
 
 
99
  pred_83 = int(prob_83 > 0.5)
100
 
101
- prob_90 = self.threshold_model_90.predict_proba(X_processed)[0][1] if len(self.threshold_model_90.classes_) > 1 else 0.0
 
 
 
 
 
 
 
 
102
  pred_90 = int(prob_90 > 0.5)
103
 
104
  # Format response
@@ -212,23 +228,21 @@ class EnergyMLPredictor:
212
  boosting_val = float(item['boosting'].replace(',', '.'))
213
  extracao_val = float(item['extracao_forno'].replace(',', '.'))
214
 
215
- # Create features
216
  input_data = {
217
  'boosting': boosting_val,
218
  'espessura': item['espessura'],
219
  'extracao_forno': extracao_val,
220
  'porcentagem_caco': item['porcentagem_caco'],
221
  'cor': item['cor'].lower(),
222
- 'prod_e': 1,
223
- 'prod_l': 1,
224
- 'autoclave': 1,
225
  'week_day': date_obj.weekday(),
226
  'month': date_obj.month,
227
  'quarter': (date_obj.month - 1) // 3 + 1,
228
- 'is_weekend': int(date_obj.weekday() >= 5),
229
  'week_of_year': date_obj.isocalendar()[1],
230
- 'day_of_month': date_obj.day,
231
- 'day_of_year': date_obj.timetuple().tm_yday
 
 
232
  }
233
 
234
  # Encode categorical features
 
94
  # Preprocess
95
  X_processed = self.threshold_preprocessor.transform(input_df)
96
 
97
+ # Make predictions with error handling
98
+ try:
99
+ prob_83_raw = self.threshold_model_83.predict_proba(X_processed)
100
+ prob_83 = prob_83_raw[0][1] if len(prob_83_raw[0]) > 1 else prob_83_raw[0][0]
101
+ # Ensure probability is between 0 and 1
102
+ prob_83 = max(0.0, min(1.0, float(prob_83)))
103
+ except Exception as e:
104
+ print(f"Error with threshold_83 prediction: {e}")
105
+ prob_83 = 0.0
106
+
107
  pred_83 = int(prob_83 > 0.5)
108
 
109
+ try:
110
+ prob_90_raw = self.threshold_model_90.predict_proba(X_processed)
111
+ prob_90 = prob_90_raw[0][1] if len(prob_90_raw[0]) > 1 else prob_90_raw[0][0]
112
+ # Ensure probability is between 0 and 1
113
+ prob_90 = max(0.0, min(1.0, float(prob_90)))
114
+ except Exception as e:
115
+ print(f"Error with threshold_90 prediction: {e}")
116
+ prob_90 = 0.0
117
+
118
  pred_90 = int(prob_90 > 0.5)
119
 
120
  # Format response
 
228
  boosting_val = float(item['boosting'].replace(',', '.'))
229
  extracao_val = float(item['extracao_forno'].replace(',', '.'))
230
 
231
+ # Create features (match training: numerical + categorical + boolean, NO day_of_month/day_of_year)
232
  input_data = {
233
  'boosting': boosting_val,
234
  'espessura': item['espessura'],
235
  'extracao_forno': extracao_val,
236
  'porcentagem_caco': item['porcentagem_caco'],
237
  'cor': item['cor'].lower(),
 
 
 
238
  'week_day': date_obj.weekday(),
239
  'month': date_obj.month,
240
  'quarter': (date_obj.month - 1) // 3 + 1,
 
241
  'week_of_year': date_obj.isocalendar()[1],
242
+ 'prod_e': 1,
243
+ 'prod_l': 1,
244
+ 'is_weekend': int(date_obj.weekday() >= 5),
245
+ 'autoclave': 1
246
  }
247
 
248
  # Encode categorical features