File size: 15,251 Bytes
0337d3f 31fd85f f406703 31fd85f bc872ec 31fd85f 4e6d72a 31fd85f 610674c bc872ec 48c5c63 31fd85f 610674c 31fd85f 91f8b58 4e6d72a 21b2af3 bc872ec 31fd85f 4e6d72a bc872ec 31fd85f bc872ec 31fd85f bc872ec 610674c bc872ec 44073b3 610674c bc872ec 610674c 4e6d72a 610674c bc872ec 610674c 91f8b58 d4b3c99 8735586 d4b3c99 8735586 91f8b58 8735586 d4b3c99 91f8b58 d4b3c99 8735586 d4b3c99 8735586 91f8b58 8735586 d4b3c99 8735586 91f8b58 d4b3c99 8735586 d4b3c99 8735586 91f8b58 8735586 d4b3c99 2aea707 8735586 0e7a640 2aea707 338949d bc872ec 31fd85f bc872ec 31fd85f bc872ec 31fd85f 4e6d72a 31fd85f 4e6d72a 31fd85f be625c5 bc872ec 31fd85f be625c5 bc872ec 31fd85f 4e6d72a bc872ec be625c5 bc872ec 31fd85f 4e6d72a 44073b3 be625c5 bc872ec 4e6d72a bc872ec 4e6d72a bc872ec 4e6d72a bc872ec 4e6d72a 31fd85f be625c5 bc872ec 31fd85f c365127 d8de06e bc872ec 86bcd45 cb0345f 31fd85f 4e6d72a 8735586 4aae3e4 4e6d72a 31fd85f 4e6d72a cd2dbd7 bc872ec be625c5 bc872ec 31fd85f 4e6d72a 31fd85f 4e6d72a cd2dbd7 31fd85f bc872ec 4e6d72a 31fd85f 4e6d72a 10fed76 8735586 2aea707 4e567b5 268963b 4e567b5 8735586 2c9ea62 8735586 2c9ea62 10fed76 31fd85f bc872ec 8735586 31fd85f be625c5 31fd85f 4e6d72a bc872ec 4e6d72a bc872ec 4e6d72a bc872ec 4e6d72a 31fd85f 4e6d72a 31fd85f 4e6d72a 31fd85f 8735586 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
from flask import Flask, render_template, request, redirect, url_for, send_file
import os
import shutil
import pandas as pd
from werkzeug.utils import secure_filename
from joblib import load, dump
import numpy as np
from sklearn.preprocessing import LabelEncoder
from time import time
from huggingface_hub import hf_hub_download
import pickle
import uuid
from pathlib import Path
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from xgboost import XGBRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_error
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PowerTransformer, StandardScaler
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split, cross_val_score, RandomizedSearchCV
import lightgbm as lgb
from catboost import CatBoostRegressor
from sklearn.ensemble import StackingRegressor
app = Flask(__name__)
# Set the secret key for session management
app.secret_key = os.urandom(24)
# Configurations
UPLOAD_FOLDER = "uploads/"
DATA_FOLDER = "data/"
MODEL_FOLDER = "models/"
os.makedirs(MODEL_FOLDER, exist_ok=True)
# Define the model directory and label encoder directory
MODEL_DIR = r'./Model'
LABEL_ENCODER_DIR = r'./Label_encoders' # Renamed for clarity
# Global file names for outputs; these will be updated per prediction.
# Note: we now include a unique id to avoid overwriting.
PRED_OUTPUT_FILE = None
CLASS_OUTPUT_FILE = None
ALLOWED_EXTENSIONS = {'csv', 'xlsx'}
# Create directories if they do not exist.
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
app.config['DATA_FOLDER'] = DATA_FOLDER
os.makedirs(app.config['DATA_FOLDER'], exist_ok=True)
os.makedirs("data", exist_ok=True)
app.config['MODEL_FOLDER'] = MODEL_FOLDER
os.makedirs(app.config['MODEL_FOLDER'], exist_ok=True)
# ------------------------------
# Load Models and Label Encoders
# ------------------------------
# Prediction analysis models loaded from Hugging Face.
src_path = hf_hub_download(
repo_id="WebashalarForML/Diamond_model_",
filename="models_list/mkble/DecisionTree_best_pipeline_mkble_0_to_0.99_al.pkl",
cache_dir=MODEL_FOLDER
)
dst_path = os.path.join(MODEL_FOLDER, "DecisionTree_best_pipeline_mkble_0_to_0.99_al.pkl")
shutil.copy(src_path, dst_path)
makable_model_0 = load(dst_path)
src_path = hf_hub_download(
repo_id="WebashalarForML/Diamond_model_",
filename="models_list/mkble/DecisionTree_best_pipeline_mkble_1_to_1.49.pkl",
cache_dir=MODEL_FOLDER
)
dst_path = os.path.join(MODEL_FOLDER, "DecisionTree_best_pipeline_mkble_1_to_1.49.pkl")
shutil.copy(src_path, dst_path)
makable_model_1 = load(dst_path)
# Prediction analysis models loaded from Hugging Face.
src_path = hf_hub_download(
repo_id="WebashalarForML/Diamond_model_",
filename="models_list/mkble/DecisionTree_best_pipeline_mkble_1.50_to_1.99.pkl",
cache_dir=MODEL_FOLDER
)
dst_path = os.path.join(MODEL_FOLDER, "DecisionTree_best_pipeline_mkble_1.50_to_1.99.pkl")
shutil.copy(src_path, dst_path)
makable_model_2 = load(dst_path)
#classsification model on the task
src_path = hf_hub_download(
repo_id="WebashalarForML/Diamond_model_",
filename="models_list/classification/3_pipeline.pkl",
cache_dir=MODEL_FOLDER
)
dst_path = os.path.join(MODEL_FOLDER, "3_pipeline.pkl")
shutil.copy(src_path, dst_path)
mkble_amt_class_model = load(dst_path)
#print("makable_model type:", type(makable_model))
#print("grade_model type:", type(grade_model))
#print("bygrade_model type:", type(bygrade_model))
#print("gia_model type:", type(gia_model))
print("================================")
print("mkble_amt_class_model type:", type(mkble_amt_class_model))
# List of label encoder names.
encoder_list = [
'Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo',
'EngNts', 'EngMikly', 'EngLab','EngBlk', 'EngWht', 'EngOpen','EngPav',
'Change_cts_value', 'Change_shape_value', 'Change_quality_value', 'Change_color_value',
'Change_cut_value', 'Change_Blk_Eng_to_Mkbl_value', 'Change_Wht_Eng_to_Mkbl_value',
'Change_Open_Eng_to_Mkbl_value', 'Change_Pav_Eng_to_Mkbl_value', 'Change_Blk_Eng_to_Grd_value',
'Change_Wht_Eng_to_Grd_value', 'Change_Open_Eng_to_Grd_value', 'Change_Pav_Eng_to_Grd_value',
'Change_Blk_Eng_to_ByGrd_value', 'Change_Wht_Eng_to_ByGrd_value', 'Change_Open_Eng_to_ByGrd_value',
'Change_Pav_Eng_to_ByGrd_value', 'Change_Blk_Eng_to_Gia_value', 'Change_Wht_Eng_to_Gia_value',
'Change_Open_Eng_to_Gia_value', 'Change_Pav_Eng_to_Gia_value'
]
# Load label encoders using pathlib for cleaner path management.
loaded_label_encoder = {}
enc_path = Path(LABEL_ENCODER_DIR)
for val in encoder_list:
encoder_file = enc_path / f"label_encoder_{val}.joblib"
loaded_label_encoder[val] = load(encoder_file)
# ------------------------------
# Utility: Allowed File Check
# ------------------------------
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# ------------------------------
# Routes
# ------------------------------
@app.route('/')
def index():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
if 'file' not in request.files:
print('No file part', 'error')
return redirect(url_for('index'))
file = request.files['file']
if file.filename == '':
print('No selected file', 'error')
return redirect(url_for('index'))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
# Convert file to DataFrame
try:
if filename.endswith('.csv'):
df = pd.read_csv(filepath)
else:
df = pd.read_excel(filepath)
except Exception as e:
print(f'Error reading file: {e}', 'error')
return redirect(url_for('index'))
# Process the DataFrame and generate predictions and classification analysis.
df_pred, dx_class = process_dataframe(df)
if df_pred.empty:
print("Processed prediction DataFrame is empty. Check the input file and processing logic.", "error")
return redirect(url_for('index'))
# Save output files with a timestamp and unique id.
current_date = pd.Timestamp.now().strftime("%Y-%m-%d")
unique_id = uuid.uuid4().hex[:8]
global PRED_OUTPUT_FILE, CLASS_OUTPUT_FILE
PRED_OUTPUT_FILE = f'data/prediction_output_{current_date}_{unique_id}.csv'
CLASS_OUTPUT_FILE = f'data/classification_output_{current_date}_{unique_id}.csv'
df_pred.to_csv(PRED_OUTPUT_FILE, index=False)
dx_class.to_csv(CLASS_OUTPUT_FILE, index=False)
# Redirect to report view; default to prediction report, page 1.
return redirect(url_for('report_view', report_type='pred', page=1))
else:
print('Invalid file type. Only CSV and Excel files are allowed.', 'error')
return redirect(url_for('index'))
def process_dataframe(df):
try:
#df = df[df["MkblAmt"].notna()]
# Define the columns needed for two parts.
required_columns = ['Tag', 'EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngBlk', 'EngWht', 'EngOpen',
'EngPav', 'EngAmt']
required_columns_2 = ['Tag', 'EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngAmt']
# Create two DataFrames: one for prediction and one for classification.
df_pred = df[required_columns].copy()
#df_pred = df_pred[(df_pred[['EngCts']] > 0.99).all(axis=1) & (df_pred[['EngCts']] < 1.50).all(axis=1)]
df_pred[['EngBlk', 'EngWht', 'EngOpen', 'EngPav']]=df_pred[['EngBlk', 'EngWht', 'EngOpen', 'EngPav']].fillna("NA")
df_class = df[required_columns_2].fillna("NA").copy()
# Transform categorical columns for prediction DataFrame using the label encoders.
for col in ['Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly','EngBlk', 'EngWht', 'EngOpen', 'EngPav']:
try:
df_pred[col] = loaded_label_encoder[col].transform(df_pred[col])
except ValueError as e:
print(f'Invalid value in column {col}: {e}', 'error')
return pd.DataFrame(), pd.DataFrame()
# Update the classification DataFrame with the transformed prediction columns.
for col in ['Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly']:
df_class[col] = df_pred[col]
# Transform the extra columns in the classification DataFrame.
#for col in ['EngBlk', 'EngWht', 'EngOpen', 'EngPav']:
# try:
# df_class[col] = loaded_label_encoder[col].transform(df_class[col])
# except ValueError as e:
# print(f'Invalid value in column {col}: {e}', 'error')
# return pd.DataFrame(), pd.DataFrame()
# Convert both DataFrames to float.
df_pred = df_pred.astype(float)
df_class = df_class.astype(float)
# -------------------------
# Prediction Report Section
# -------------------------
try:
# for model 0 to 0.99
df_pred_0 = df_pred[(df_pred[['EngCts']] > 0.00).all(axis=1) & (df_pred[['EngCts']] < 0.99).all(axis=1)]
df_pred_0['change_in_amt_mkble'] = pd.DataFrame(mkble_amt_class_model.predict(df_pred_0), columns=["pred_change_in_eng_to_mkble"])
print(df_pred_0.columns)
df_pred_0 = df_pred_0[['Tag', 'EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngBlk', 'EngWht', 'EngOpen',
'EngPav', 'EngAmt',
'change_in_amt_mkble'
]]
df_pred_0['Makable_Predicted'] = pd.DataFrame(np.expm1(makable_model_0.predict(df_pred_0)), columns=["Predicted"])
print(df_pred_0.columns)
# for model 1 to 1.49
df_pred_1 = df_pred[(df_pred[['EngCts']] > 0.99).all(axis=1) & (df_pred[['EngCts']] < 1.50).all(axis=1)]
df_pred_1['change_in_amt_mkble'] = pd.DataFrame(mkble_amt_class_model.predict(df_pred_1), columns=["pred_change_in_eng_to_mkble"])
print(df_pred_1.columns)
df_pred_1 = df_pred_1[['Tag', 'EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngBlk', 'EngWht', 'EngOpen',
'EngPav', 'EngAmt',
'change_in_amt_mkble'
]]
df_pred_1['Makable_Predicted'] = pd.DataFrame(np.expm1(makable_model_1.predict(df_pred_1)), columns=["Predicted"])
print(df_pred_1.columns)
# for model 1.50 to 1.99
df_pred_2 = df_pred[(df_pred[['EngCts']] > 1.49).all(axis=1) & (df_pred[['EngCts']] < 2.00).all(axis=1)]
df_pred_2['change_in_amt_mkble'] = pd.DataFrame(mkble_amt_class_model.predict(df_pred_2), columns=["pred_change_in_eng_to_mkble"])
print(df_pred_2.columns)
df_pred_2 = df_pred_2[['Tag', 'EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngBlk', 'EngWht', 'EngOpen',
'EngPav', 'EngAmt',
'change_in_amt_mkble'
]]
df_pred_2['Makable_Predicted'] = pd.DataFrame(np.expm1(makable_model_2.predict(df_pred_2)), columns=["Predicted"])
print(df_pred_2.columns)
df_pred_main = pd.concat([df_pred_0, df_pred_1, df_pred_2])
df_pred_main['Makable_Diff'] = df_pred_main['EngAmt'] - df_pred_main['Makable_Predicted']
for col in ['Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly','EngBlk', 'EngWht', 'EngOpen', 'EngPav']:
try:
df_pred_main[col] = loaded_label_encoder[col].inverse_transform(df_pred_main[col].astype(int))
except ValueError as e:
print(f'inverse transform fails value in column {col}: {e}', 'error')
except ValueError as e:
print(f'pred model error----->: {e}', 'error')
# Final return with full data for pagination.
return df_pred_main, df_pred_main
except Exception as e:
print(f'Error processing file: {e}', 'error')
return pd.DataFrame(), pd.DataFrame()
# ------------------------------
# Report View Route with Pagination & Toggle
# ------------------------------
@app.route('/report')
def report_view():
report_type = request.args.get('report_type', 'pred')
try:
page = int(request.args.get('page', 1))
except ValueError:
page = 1
per_page = 15 # records per page
# Read the appropriate CSV file.
if report_type == 'pred':
df = pd.read_csv(PRED_OUTPUT_FILE)
else:
df = pd.read_csv(CLASS_OUTPUT_FILE)
start_idx = (page - 1) * per_page
end_idx = start_idx + per_page
total_records = len(df)
df_page = df.iloc[start_idx:end_idx]
table_html = df_page.to_html(classes="data-table", index=False)
has_prev = page > 1
has_next = end_idx < total_records
return render_template('output.html',
table_html=table_html,
report_type=report_type,
page=page,
has_prev=has_prev,
has_next=has_next)
# ------------------------------
# Download Routes
# ------------------------------
@app.route('/download_pred', methods=['GET'])
def download_pred():
return send_file(PRED_OUTPUT_FILE, as_attachment=True)
@app.route('/download_class', methods=['GET'])
def download_class():
return send_file(CLASS_OUTPUT_FILE, as_attachment=True)
if __name__ == "__main__":
app.run(debug=True) |