WebashalarForML commited on
Commit
6500d36
·
verified ·
1 Parent(s): 12ee3da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -76
app.py CHANGED
@@ -76,102 +76,102 @@ os.makedirs("data", exist_ok=True)
76
  app.config['MODEL_FOLDER'] = MODEL_FOLDER
77
  os.makedirs(app.config['MODEL_FOLDER'], exist_ok=True)
78
 
79
- # ------------------------------
80
- # Load Models and Label Encoders
81
- # ------------------------------
82
-
83
  # Prediction analysis models loaded from Hugging Face.
84
 
85
 
86
  #classsification model on the task
 
 
 
 
87
  src_path = hf_hub_download(
88
  repo_id="WebashalarForML/Diamond_model_",
89
- filename="CLASS_DUMMY/LR_best_2_clas_shp_change.pkl",
90
  cache_dir=MODEL_FOLDER
91
  )
92
- dst_path = os.path.join(MODEL_FOLDER, "LR_best_2_clas_shp_change.pkl")
93
  shutil.copy(src_path, dst_path)
94
- shape_change = load(dst_path)
95
-
96
 
 
97
  src_path = hf_hub_download(
98
  repo_id="WebashalarForML/Diamond_model_",
99
- filename="CLASS_DUMMY/LR_best_2_class_blk(M)_change.pkl",
100
  cache_dir=MODEL_FOLDER
101
  )
102
- dst_path = os.path.join(MODEL_FOLDER, "LR_best_2_class_blk(M)_change.pkl")
103
  shutil.copy(src_path, dst_path)
104
- blk_change = load(dst_path)
105
- '''
106
- # Prediction analysis models loaded from Hugging Face.
107
  src_path = hf_hub_download(
108
  repo_id="WebashalarForML/Diamond_model_",
109
- filename="CLASS_DUMMY/LR_best_2_class_wht(M)_change.pkl",
110
  cache_dir=MODEL_FOLDER
111
  )
112
- dst_path = os.path.join(MODEL_FOLDER, "LR_best_2_class_wht(M)_change.pkl")
113
  shutil.copy(src_path, dst_path)
114
- wht_change = load(dst_path)
115
-
116
 
 
117
  src_path = hf_hub_download(
118
  repo_id="WebashalarForML/Diamond_model_",
119
- filename="CLASS_DUMMY/LR_best_2_class_pav(M)_change.pkl",
120
  cache_dir=MODEL_FOLDER
121
  )
122
- dst_path = os.path.join(MODEL_FOLDER, "LR_best_2_class_pav(M)_change.pkl")
123
  shutil.copy(src_path, dst_path)
124
- pav_change = load(dst_path)
 
 
 
 
125
 
 
126
  src_path = hf_hub_download(
127
  repo_id="WebashalarForML/Diamond_model_",
128
- filename="CLASS_DUMMY/LR_best_2_class_open(M)_change.pkl",
129
  cache_dir=MODEL_FOLDER
130
  )
131
- dst_path = os.path.join(MODEL_FOLDER, "LR_best_2_class_open(M)_change.pkl")
132
  shutil.copy(src_path, dst_path)
133
- open_change = load(dst_path)
134
 
135
- # other param class change
136
  src_path = hf_hub_download(
137
  repo_id="WebashalarForML/Diamond_model_",
138
- filename="CLASS_DUMMY/LR_best_2_class_col_change.pkl",
139
  cache_dir=MODEL_FOLDER
140
  )
141
- dst_path = os.path.join(MODEL_FOLDER, "LR_best_2_class_col_change.pkl")
142
  shutil.copy(src_path, dst_path)
143
  col_change = load(dst_path)
144
 
 
145
  src_path = hf_hub_download(
146
  repo_id="WebashalarForML/Diamond_model_",
147
- filename="CLASS_DUMMY/LR_best_2_class_qua_change.pkl",
148
  cache_dir=MODEL_FOLDER
149
  )
150
- dst_path = os.path.join(MODEL_FOLDER, "LR_best_2_class_qua_change.pkl")
151
  shutil.copy(src_path, dst_path)
152
  qua_change = load(dst_path)
153
 
 
154
  src_path = hf_hub_download(
155
  repo_id="WebashalarForML/Diamond_model_",
156
- filename="CLASS_DUMMY/LR_best_2_class_cut_change.pkl",
157
  cache_dir=MODEL_FOLDER
158
  )
159
- dst_path = os.path.join(MODEL_FOLDER, "LR_best_2_class_cut_change.pkl")
160
  shutil.copy(src_path, dst_path)
161
  cut_change = load(dst_path)
162
 
163
- '''
164
-
165
- #print("makable_model type:", type(makable_model))
166
- #print("grade_model type:", type(grade_model))
167
- #print("bygrade_model type:", type(bygrade_model))
168
- #print("gia_model type:", type(gia_model))
169
  print("================================")
170
- #print("mkble_amt_class_model type:", type(mkble_amt_class_model))
171
 
172
  # List of label encoder names.
173
  encoder_list = [
174
- 'Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo',
175
  'EngNts', 'EngMikly','EngBlk', 'EngWht', 'EngOpen','EngPav',
176
  'Change_cts_value', 'Change_shape_value', 'Change_quality_value', 'Change_color_value',
177
  'Change_cut_value', 'Change_Blk_Eng_to_Mkbl_value', 'Change_Wht_Eng_to_Mkbl_value',
@@ -189,15 +189,15 @@ for val in encoder_list:
189
  encoder_file = enc_path / f"label_encoder_{val}.joblib"
190
  loaded_label_encoder[val] = load(encoder_file)
191
 
192
- # ------------------------------
193
  # Utility: Allowed File Check
194
- # ------------------------------
195
  def allowed_file(filename):
196
  return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
197
 
198
- # ------------------------------
199
  # Routes
200
- # ------------------------------
201
  @app.route('/')
202
  def index():
203
  return render_template('index.html')
@@ -249,17 +249,15 @@ def predict():
249
  print('Invalid file type. Only CSV and Excel files are allowed.', 'error')
250
  return redirect(url_for('index'))
251
 
252
-
253
-
254
  def process_dataframe(df):
255
  try:
256
  #df = df[df["MkblAmt"].notna()]
257
 
258
  # Define the columns needed for two parts.
259
- required_columns = ['Tag', 'EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
260
  'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngBlk', 'EngWht', 'EngOpen',
261
  'EngPav', 'EngAmt']
262
- required_columns_2 = ['Tag', 'EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
263
  'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngAmt']
264
 
265
  # Create two DataFrames: one for prediction and one for classification.
@@ -269,7 +267,7 @@ def process_dataframe(df):
269
  df_class = df[required_columns_2].fillna("NA").copy()
270
 
271
  # Transform categorical columns for prediction DataFrame using the label encoders.
272
- for col in ['Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly','EngBlk', 'EngWht', 'EngOpen', 'EngPav']:
273
  try:
274
  encoder = loaded_label_encoder[col]
275
  df_pred[col] = df_pred[col].map(lambda x: encoder.transform([x])[0] if x in encoder.classes_ else -1)
@@ -279,38 +277,24 @@ def process_dataframe(df):
279
  return pd.DataFrame(), pd.DataFrame()
280
 
281
  # Update the classification DataFrame with the transformed prediction columns.
282
- for col in ['Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly']:
283
  df_class[col] = df_pred[col]
284
 
285
- # Transform the extra columns in the classification DataFrame.
286
- #for col in ['EngBlk', 'EngWht', 'EngOpen', 'EngPav']:
287
- # try:
288
- # df_class[col] = loaded_label_encoder[col].transform(df_class[col])
289
- # except ValueError as e:
290
- # print(f'Invalid value in column {col}: {e}', 'error')
291
- # return pd.DataFrame(), pd.DataFrame()
292
-
293
- # Convert both DataFrames to float.
294
  df_pred = df_pred.astype(float)
295
  df_class = df_class.astype(float)
296
 
297
- # -------------------------
298
  # Prediction Report Section
299
- # -------------------------
300
  try:
301
 
302
  # for model BLK CODE
303
  df_pred_0 = df_pred.copy()
304
  df_pred_0['Change_Blk_Eng_to_Mkbl_value'] = pd.DataFrame(blk_change.predict(df_pred), columns=["Change_Blk_Eng_to_Mkbl_value"])
305
  print(df_pred_0.columns)
306
-
307
- # for model SHP CODE (need change)
308
- df_pred_0['Change_shape_value'] = pd.DataFrame(shape_change.predict(df_pred), columns=["Change_shape_value"])
309
- print(df_pred_0.columns)
310
 
311
- '''
312
  # for model WHT CODE
313
- df_pred_0['Change_Wht_Eng_to_Mkbl_value'] = pd.DataFrame(shape_change.predict(df_pred), columns=["Change_Wht_Eng_to_Mkbl_value"])
314
  print(df_pred_0.columns)
315
 
316
  # for model PAV CODE (need change)
@@ -321,30 +305,34 @@ def process_dataframe(df):
321
  df_pred_0['Change_Open_Eng_to_Mkbl_value'] = pd.DataFrame(mkble_amt_class_model.predict(df_pred), columns=["Change_Open_Eng_to_Mkbl_value"])
322
  print(df_pred_0.columns)
323
 
 
 
 
 
324
  # for model COL CODE (need change)
325
- df_pred_0['Change_color_value'] = pd.DataFrame(mkble_amt_class_model.predict(df_pred), columns=["Change_color_value"])
326
  print(df_pred_0.columns)
327
 
328
  # for model CUT CODE (need change)
329
- df_pred_0['Change_cut_value'] = pd.DataFrame(mkble_amt_class_model.predict(df_pred), columns=["Change_cut_value"])
330
  print(df_pred_0.columns)
331
 
332
  # for model QUA CODE (need change)
333
- df_pred_0['Change_quality_value'] = pd.DataFrame(mkble_amt_class_model.predict(df_pred), columns=["Change_quality_value"])
334
  print(df_pred_0.columns)
335
- '''
336
  # Concatenate the DataFrames row-wise
337
  #df_pred_main = pd.concat([df_pred_0, df_pred_1, df_pred_0], ignore_index=True)
338
  df_pred_main = df_pred_0.copy()
339
 
340
- for col in ['Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo',
341
  'EngNts', 'EngMikly','EngBlk', 'EngWht', 'EngOpen','EngPav',
342
  'Change_shape_value',
343
- #'Change_cts_value','Change_quality_value', 'Change_color_value', 'Change_cut_value',
344
  'Change_Blk_Eng_to_Mkbl_value',
345
- #'Change_Wht_Eng_to_Mkbl_value',
346
- #'Change_Open_Eng_to_Mkbl_value',
347
- #'Change_Pav_Eng_to_Mkbl_value',
348
  #'Change_Blk_Eng_to_Grd_value','Change_Wht_Eng_to_Grd_value', 'Change_Open_Eng_to_Grd_value', 'Change_Pav_Eng_to_Grd_value',
349
  #'Change_Blk_Eng_to_ByGrd_value', 'Change_Wht_Eng_to_ByGrd_value', 'Change_Open_Eng_to_ByGrd_value', 'Change_Pav_Eng_to_ByGrd_value',
350
  #'Change_Blk_Eng_to_Gia_value', 'Change_Wht_Eng_to_Gia_value', 'Change_Open_Eng_to_Gia_value', 'Change_Pav_Eng_to_Gia_value'
@@ -384,9 +372,9 @@ def process_dataframe(df):
384
  print(f'Error processing file: {e}', 'error')
385
  return pd.DataFrame(), pd.DataFrame()
386
 
387
- # ------------------------------
388
  # Report View Route with Pagination & Toggle
389
- # ------------------------------
390
 
391
  @app.route("/report")
392
  def report_view():
 
76
  app.config['MODEL_FOLDER'] = MODEL_FOLDER
77
  os.makedirs(app.config['MODEL_FOLDER'], exist_ok=True)
78
 
 
 
 
 
79
  # Prediction analysis models loaded from Hugging Face.
80
 
81
 
82
  #classsification model on the task
83
+ # ----------------------------------------------
84
+ # Code classification models for real data.
85
+ # ----------------------------------------------
86
+ #black code change
87
  src_path = hf_hub_download(
88
  repo_id="WebashalarForML/Diamond_model_",
89
+ filename="CLASS_DUMMY/DT_best__2_class_blk(M)_change.pkl",
90
  cache_dir=MODEL_FOLDER
91
  )
92
+ dst_path = os.path.join(MODEL_FOLDER, "DT_best__2_class_blk(M)_change.pkl")
93
  shutil.copy(src_path, dst_path)
94
+ blk_change = load(dst_path)
 
95
 
96
+ # white code change
97
  src_path = hf_hub_download(
98
  repo_id="WebashalarForML/Diamond_model_",
99
+ filename="CLASS_DUMMY/DT_best__2_class_wht(M)_change.pkl",
100
  cache_dir=MODEL_FOLDER
101
  )
102
+ dst_path = os.path.join(MODEL_FOLDER, "DT_best__2_class_wht(M)_change.pkl")
103
  shutil.copy(src_path, dst_path)
104
+ wht_change = load(dst_path)
105
+
106
+ # pav code change
107
  src_path = hf_hub_download(
108
  repo_id="WebashalarForML/Diamond_model_",
109
+ filename="CLASS_DUMMY/DT_best__2_class_pav(M)_change.pkl",
110
  cache_dir=MODEL_FOLDER
111
  )
112
+ dst_path = os.path.join(MODEL_FOLDER, "DT_best__2_class_pav(M)_change.pkl")
113
  shutil.copy(src_path, dst_path)
114
+ pav_change = load(dst_path)
 
115
 
116
+ #open code change
117
  src_path = hf_hub_download(
118
  repo_id="WebashalarForML/Diamond_model_",
119
+ filename="CLASS_DUMMY/DT_best__2_class_open(M)_change.pkl",
120
  cache_dir=MODEL_FOLDER
121
  )
122
+ dst_path = os.path.join(MODEL_FOLDER, "DT_best__2_class_open(M)_change.pkl")
123
  shutil.copy(src_path, dst_path)
124
+ open_change = load(dst_path)
125
+
126
+ # ----------------------------------------------
127
+ # parameter classification models for real data.
128
+ # ----------------------------------------------
129
 
130
+ #shape change
131
  src_path = hf_hub_download(
132
  repo_id="WebashalarForML/Diamond_model_",
133
+ filename="CLASS_DUMMY/DT_best__2_class_shp_change.pkl",
134
  cache_dir=MODEL_FOLDER
135
  )
136
+ dst_path = os.path.join(MODEL_FOLDER, "DT_best__2_class_shp_change.pkl")
137
  shutil.copy(src_path, dst_path)
138
+ shape_change = load(dst_path)
139
 
140
+ # color change
141
  src_path = hf_hub_download(
142
  repo_id="WebashalarForML/Diamond_model_",
143
+ filename="CLASS_DUMMY/DT_best__2_class_col_change.pkl",
144
  cache_dir=MODEL_FOLDER
145
  )
146
+ dst_path = os.path.join(MODEL_FOLDER, "DT_best__2_class_col_change.pkl")
147
  shutil.copy(src_path, dst_path)
148
  col_change = load(dst_path)
149
 
150
+ # quality change
151
  src_path = hf_hub_download(
152
  repo_id="WebashalarForML/Diamond_model_",
153
+ filename="CLASS_DUMMY/LR_best__2_class_qua_change.pkl",
154
  cache_dir=MODEL_FOLDER
155
  )
156
+ dst_path = os.path.join(MODEL_FOLDER, "LR_best__2_class_qua_change.pkl")
157
  shutil.copy(src_path, dst_path)
158
  qua_change = load(dst_path)
159
 
160
+ # cut change
161
  src_path = hf_hub_download(
162
  repo_id="WebashalarForML/Diamond_model_",
163
+ filename="CLASS_DUMMY/LR_best__2_class_cut_change.pkl",
164
  cache_dir=MODEL_FOLDER
165
  )
166
+ dst_path = os.path.join(MODEL_FOLDER, "LR_best__2_class_cut_change.pkl")
167
  shutil.copy(src_path, dst_path)
168
  cut_change = load(dst_path)
169
 
 
 
 
 
 
 
170
  print("================================")
 
171
 
172
  # List of label encoder names.
173
  encoder_list = [
174
+ 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo',
175
  'EngNts', 'EngMikly','EngBlk', 'EngWht', 'EngOpen','EngPav',
176
  'Change_cts_value', 'Change_shape_value', 'Change_quality_value', 'Change_color_value',
177
  'Change_cut_value', 'Change_Blk_Eng_to_Mkbl_value', 'Change_Wht_Eng_to_Mkbl_value',
 
189
  encoder_file = enc_path / f"label_encoder_{val}.joblib"
190
  loaded_label_encoder[val] = load(encoder_file)
191
 
192
+ # -----------------------------------------
193
  # Utility: Allowed File Check
194
+ # -----------------------------------------
195
  def allowed_file(filename):
196
  return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
197
 
198
+ # -----------------------------------------
199
  # Routes
200
+ # -----------------------------------------
201
  @app.route('/')
202
  def index():
203
  return render_template('index.html')
 
249
  print('Invalid file type. Only CSV and Excel files are allowed.', 'error')
250
  return redirect(url_for('index'))
251
 
 
 
252
  def process_dataframe(df):
253
  try:
254
  #df = df[df["MkblAmt"].notna()]
255
 
256
  # Define the columns needed for two parts.
257
+ required_columns = ['EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
258
  'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngBlk', 'EngWht', 'EngOpen',
259
  'EngPav', 'EngAmt']
260
+ required_columns_2 = ['EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
261
  'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngAmt']
262
 
263
  # Create two DataFrames: one for prediction and one for classification.
 
267
  df_class = df[required_columns_2].fillna("NA").copy()
268
 
269
  # Transform categorical columns for prediction DataFrame using the label encoders.
270
+ for col in ['EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly','EngBlk', 'EngWht', 'EngOpen', 'EngPav']:
271
  try:
272
  encoder = loaded_label_encoder[col]
273
  df_pred[col] = df_pred[col].map(lambda x: encoder.transform([x])[0] if x in encoder.classes_ else -1)
 
277
  return pd.DataFrame(), pd.DataFrame()
278
 
279
  # Update the classification DataFrame with the transformed prediction columns.
280
+ for col in ['EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly']:
281
  df_class[col] = df_pred[col]
282
 
 
 
 
 
 
 
 
 
 
283
  df_pred = df_pred.astype(float)
284
  df_class = df_class.astype(float)
285
 
286
+ # ------------------------------------
287
  # Prediction Report Section
288
+ # ------------------------------------
289
  try:
290
 
291
  # for model BLK CODE
292
  df_pred_0 = df_pred.copy()
293
  df_pred_0['Change_Blk_Eng_to_Mkbl_value'] = pd.DataFrame(blk_change.predict(df_pred), columns=["Change_Blk_Eng_to_Mkbl_value"])
294
  print(df_pred_0.columns)
 
 
 
 
295
 
 
296
  # for model WHT CODE
297
+ df_pred_0['Change_Wht_Eng_to_Mkbl_value'] = pd.DataFrame(wht_change.predict(df_pred), columns=["Change_Wht_Eng_to_Mkbl_value"])
298
  print(df_pred_0.columns)
299
 
300
  # for model PAV CODE (need change)
 
305
  df_pred_0['Change_Open_Eng_to_Mkbl_value'] = pd.DataFrame(mkble_amt_class_model.predict(df_pred), columns=["Change_Open_Eng_to_Mkbl_value"])
306
  print(df_pred_0.columns)
307
 
308
+ # for model SHP CODE (need change)
309
+ df_pred_0['Change_shape_value'] = pd.DataFrame(shape_change.predict(df_class), columns=["Change_shape_value"])
310
+ print(df_pred_0.columns)
311
+
312
  # for model COL CODE (need change)
313
+ df_pred_0['Change_color_value'] = pd.DataFrame(mkble_amt_class_model.predict(df_class), columns=["Change_color_value"])
314
  print(df_pred_0.columns)
315
 
316
  # for model CUT CODE (need change)
317
+ df_pred_0['Change_cut_value'] = pd.DataFrame(mkble_amt_class_model.predict(df_class), columns=["Change_cut_value"])
318
  print(df_pred_0.columns)
319
 
320
  # for model QUA CODE (need change)
321
+ df_pred_0['Change_quality_value'] = pd.DataFrame(mkble_amt_class_model.predict(df_class), columns=["Change_quality_value"])
322
  print(df_pred_0.columns)
323
+
324
  # Concatenate the DataFrames row-wise
325
  #df_pred_main = pd.concat([df_pred_0, df_pred_1, df_pred_0], ignore_index=True)
326
  df_pred_main = df_pred_0.copy()
327
 
328
+ for col in ['EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo',
329
  'EngNts', 'EngMikly','EngBlk', 'EngWht', 'EngOpen','EngPav',
330
  'Change_shape_value',
331
+ 'Change_cts_value','Change_quality_value', 'Change_color_value', 'Change_cut_value',
332
  'Change_Blk_Eng_to_Mkbl_value',
333
+ 'Change_Wht_Eng_to_Mkbl_value',
334
+ 'Change_Open_Eng_to_Mkbl_value',
335
+ 'Change_Pav_Eng_to_Mkbl_value',
336
  #'Change_Blk_Eng_to_Grd_value','Change_Wht_Eng_to_Grd_value', 'Change_Open_Eng_to_Grd_value', 'Change_Pav_Eng_to_Grd_value',
337
  #'Change_Blk_Eng_to_ByGrd_value', 'Change_Wht_Eng_to_ByGrd_value', 'Change_Open_Eng_to_ByGrd_value', 'Change_Pav_Eng_to_ByGrd_value',
338
  #'Change_Blk_Eng_to_Gia_value', 'Change_Wht_Eng_to_Gia_value', 'Change_Open_Eng_to_Gia_value', 'Change_Pav_Eng_to_Gia_value'
 
372
  print(f'Error processing file: {e}', 'error')
373
  return pd.DataFrame(), pd.DataFrame()
374
 
375
+ # ----------------------------------------------------
376
  # Report View Route with Pagination & Toggle
377
+ # ----------------------------------------------------
378
 
379
  @app.route("/report")
380
  def report_view():