MatCod commited on
Commit
a699baf
·
verified ·
1 Parent(s): 9e81c24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -80
app.py CHANGED
@@ -64,92 +64,91 @@ class EnergyMLPredictor:
64
  data = json.loads(json_input)
65
 
66
  # Handle both single object and array formats
67
- if isinstance(data, list):
68
- # For week/month data, only use first item for threshold detection
69
- data = data[0]
70
-
71
- # Parse input data
72
- date_obj = datetime.strptime(data['data'], '%Y-%m-%d')
73
-
74
- # Color mapping
75
- color_mapping = {0: 'incolor', 1: 'verde', 2: 'cinza', 3: 'bronze'}
76
- cor_str = color_mapping.get(data['cor'], 'incolor')
77
-
78
- # Create input features (with autoclave)
79
- input_data = {
80
- 'boosting': data['pot_boost'],
81
- 'espessura': data['espessura'],
82
- 'extracao_forno': data['extracao_forno'],
83
- 'porcentagem_caco': data['porcentagem_caco'],
84
- 'cor': cor_str,
85
- 'prod_e': data['Prod_E'],
86
- 'prod_l': data['Prod_L'],
87
- 'autoclave': data.get('autoclave', 1),
88
- 'week_day': date_obj.weekday(),
89
- 'month': date_obj.month,
90
- 'quarter': (date_obj.month - 1) // 3 + 1,
91
- 'is_weekend': int(date_obj.weekday() >= 5),
92
- 'week_of_year': date_obj.isocalendar()[1]
93
- }
94
-
95
- # Convert to DataFrame
96
- input_df = pd.DataFrame([input_data])
97
-
98
- # Preprocess
99
- X_processed = self.threshold_preprocessor.transform(input_df)
100
-
101
- # Make predictions with error handling
102
- try:
103
- prob_83_raw = self.threshold_model_83.predict_proba(X_processed)
104
- prob_83 = prob_83_raw[0][1] if len(prob_83_raw[0]) > 1 else prob_83_raw[0][0]
105
- # Ensure probability is between 0 and 1
106
- prob_83 = max(0.0, min(1.0, float(prob_83)))
107
- except Exception as e:
108
- print(f"Error with threshold_83 prediction: {e}")
109
- prob_83 = 0.0
110
-
111
- pred_83 = int(prob_83 > 0.5)
112
 
113
- try:
114
- prob_90_raw = self.threshold_model_90.predict_proba(X_processed)
115
- prob_90 = prob_90_raw[0][1] if len(prob_90_raw[0]) > 1 else prob_90_raw[0][0]
116
- # Ensure probability is between 0 and 1
117
- prob_90 = max(0.0, min(1.0, float(prob_90)))
118
- except Exception as e:
119
- print(f"Error with threshold_90 prediction: {e}")
120
- prob_90 = 0.0
121
 
122
- pred_90 = int(prob_90 > 0.5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  # Format response
125
- next_date = (date_obj + pd.Timedelta(days=1)).strftime('%Y-%m-%d')
126
-
127
  result = {
128
  "predictions": {
129
- "prediction_1": [
130
- {
131
- "datetime": data['data'],
132
- "probabilidade_de_estouro": float(prob_83),
133
- "estouro_previsto": pred_83
134
- },
135
- {
136
- "datetime": next_date,
137
- "probabilidade_de_estouro": float(prob_83 * 0.98),
138
- "estouro_previsto": int(prob_83 * 0.98 > 0.5)
139
- }
140
- ],
141
- "prediction_2": [
142
- {
143
- "datetime": data['data'],
144
- "probabilidade_de_estouro": float(prob_90),
145
- "estouro_previsto": pred_90
146
- },
147
- {
148
- "datetime": next_date,
149
- "probabilidade_de_estouro": float(prob_90 * 0.99),
150
- "estouro_previsto": int(prob_90 * 0.99 > 0.5)
151
- }
152
- ]
153
  }
154
  }
155
 
 
64
  data = json.loads(json_input)
65
 
66
  # Handle both single object and array formats
67
+ if not isinstance(data, list):
68
+ data = [data]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ # Process all items
71
+ results_83 = []
72
+ results_90 = []
 
 
 
 
 
73
 
74
+ for item in data:
75
+ # Parse input data
76
+ date_obj = datetime.strptime(item['data'], '%Y-%m-%d')
77
+
78
+ # Color mapping
79
+ color_mapping = {0: 'incolor', 1: 'verde', 2: 'cinza', 3: 'bronze'}
80
+ if isinstance(item['cor'], str):
81
+ cor_str = item['cor'].lower()
82
+ else:
83
+ cor_str = color_mapping.get(item['cor'], 'incolor')
84
+
85
+ # Handle different field name formats for threshold
86
+ boosting_val = item.get('pot_boost', item.get('ext_boosting', 3.0))
87
+
88
+ # Create input features (with autoclave)
89
+ input_data = {
90
+ 'boosting': boosting_val,
91
+ 'espessura': item['espessura'],
92
+ 'extracao_forno': item['extracao_forno'],
93
+ 'porcentagem_caco': item['porcentagem_caco'],
94
+ 'cor': cor_str,
95
+ 'prod_e': item.get('Prod_E', item.get('prod_e', 1)),
96
+ 'prod_l': item.get('Prod_L', item.get('prod_l', 1)),
97
+ 'autoclave': item.get('autoclave', 1),
98
+ 'week_day': date_obj.weekday(),
99
+ 'month': date_obj.month,
100
+ 'quarter': (date_obj.month - 1) // 3 + 1,
101
+ 'is_weekend': int(date_obj.weekday() >= 5),
102
+ 'week_of_year': date_obj.isocalendar()[1]
103
+ }
104
+
105
+ # Convert to DataFrame
106
+ input_df = pd.DataFrame([input_data])
107
+
108
+ # Preprocess
109
+ X_processed = self.threshold_preprocessor.transform(input_df)
110
+
111
+ # Make predictions with error handling
112
+ try:
113
+ prob_83_raw = self.threshold_model_83.predict_proba(X_processed)
114
+ prob_83 = prob_83_raw[0][1] if len(prob_83_raw[0]) > 1 else prob_83_raw[0][0]
115
+ # Ensure probability is between 0 and 1
116
+ prob_83 = max(0.0, min(1.0, float(prob_83)))
117
+ except Exception as e:
118
+ print(f"Error with threshold_83 prediction: {e}")
119
+ prob_83 = 0.0
120
+
121
+ pred_83 = int(prob_83 > 0.5)
122
+
123
+ try:
124
+ prob_90_raw = self.threshold_model_90.predict_proba(X_processed)
125
+ prob_90 = prob_90_raw[0][1] if len(prob_90_raw[0]) > 1 else prob_90_raw[0][0]
126
+ # Ensure probability is between 0 and 1
127
+ prob_90 = max(0.0, min(1.0, float(prob_90)))
128
+ except Exception as e:
129
+ print(f"Error with threshold_90 prediction: {e}")
130
+ prob_90 = 0.0
131
+
132
+ pred_90 = int(prob_90 > 0.5)
133
+
134
+ # Add to results
135
+ results_83.append({
136
+ "datetime": item['data'],
137
+ "probabilidade_de_estouro": round(prob_83, 4),
138
+ "estouro_previsto": pred_83
139
+ })
140
+
141
+ results_90.append({
142
+ "datetime": item['data'],
143
+ "probabilidade_de_estouro": round(prob_90, 4),
144
+ "estouro_previsto": pred_90
145
+ })
146
 
147
  # Format response
 
 
148
  result = {
149
  "predictions": {
150
+ "prediction_1": results_83,
151
+ "prediction_2": results_90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  }
153
  }
154