Spaces:
Sleeping
Sleeping
Rename app2.py to app.py
Browse files- app2.py → app.py +283 -282
app2.py → app.py
RENAMED
|
@@ -1,283 +1,284 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, redirect, url_for, flash, send_file
|
| 2 |
-
import os
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from werkzeug.utils import secure_filename
|
| 5 |
-
from joblib import load
|
| 6 |
-
import numpy as np
|
| 7 |
-
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
|
| 8 |
-
from sklearn.model_selection import train_test_split
|
| 9 |
-
from sklearn.preprocessing import StandardScaler
|
| 10 |
-
from sklearn.decomposition import PCA
|
| 11 |
-
from sklearn.pipeline import Pipeline
|
| 12 |
-
from sklearn.tree import DecisionTreeRegressor
|
| 13 |
-
from sklearn.ensemble import RandomForestRegressor
|
| 14 |
-
from sklearn.linear_model import LinearRegression
|
| 15 |
-
from xgboost import XGBRegressor
|
| 16 |
-
from sklearn.neighbors import KNeighborsRegressor
|
| 17 |
-
from sklearn.model_selection import cross_val_score
|
| 18 |
-
from sklearn.metrics import mean_squared_error
|
| 19 |
-
from sklearn import metrics
|
| 20 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 21 |
-
from time import time
|
| 22 |
-
|
| 23 |
-
app = Flask(__name__)
|
| 24 |
-
|
| 25 |
-
# Set the secret key for session management
|
| 26 |
-
app.secret_key = os.urandom(24)
|
| 27 |
-
|
| 28 |
-
# Configurations
|
| 29 |
-
UPLOAD_FOLDER = "uploads/"
|
| 30 |
-
DATA_FOLDER = "data/"
|
| 31 |
-
|
| 32 |
-
# Define the model directory (ensuring correct path formatting)
|
| 33 |
-
MODEL_DIR = r'.\Model'
|
| 34 |
-
LABEL_ENOCDER_DIR = r'.\Label_encoders'
|
| 35 |
-
|
| 36 |
-
# Define the output file path
|
| 37 |
-
PRED_OUTPUT_FILE = "data/pred_output.csv"
|
| 38 |
-
CLASS_OUTPUT_FILE = "data/class_output.csv"
|
| 39 |
-
|
| 40 |
-
ALLOWED_EXTENSIONS = {'csv', 'xlsx'}
|
| 41 |
-
|
| 42 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 43 |
-
|
| 44 |
-
# Ensure the upload folder exists
|
| 45 |
-
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# print(
|
| 64 |
-
# print(
|
| 65 |
-
# print(
|
| 66 |
-
# print(
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
loaded_label_encoder = {}
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
encoder_path =
|
| 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 |
-
df = df
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
df["
|
| 140 |
-
df["
|
| 141 |
-
df["
|
| 142 |
-
df["
|
| 143 |
-
df["
|
| 144 |
-
df["
|
| 145 |
-
df["
|
| 146 |
-
df["
|
| 147 |
-
df["
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
# print("
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
#
|
| 165 |
-
#
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
df['
|
| 170 |
-
df['
|
| 171 |
-
df['
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
df['
|
| 177 |
-
df['
|
| 178 |
-
df['
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
#print(
|
| 186 |
-
print(
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
#
|
| 190 |
-
#
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
dx['
|
| 194 |
-
dx['
|
| 195 |
-
dx['
|
| 196 |
-
dx['
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
dx['
|
| 201 |
-
dx['
|
| 202 |
-
dx['
|
| 203 |
-
dx['
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
PRED_OUTPUT_FILE
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
CLASS_OUTPUT_FILE
|
| 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 |
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, flash, send_file
|
| 2 |
+
import os
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from werkzeug.utils import secure_filename
|
| 5 |
+
from joblib import load
|
| 6 |
+
import numpy as np
|
| 7 |
+
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
|
| 8 |
+
from sklearn.model_selection import train_test_split
|
| 9 |
+
from sklearn.preprocessing import StandardScaler
|
| 10 |
+
from sklearn.decomposition import PCA
|
| 11 |
+
from sklearn.pipeline import Pipeline
|
| 12 |
+
from sklearn.tree import DecisionTreeRegressor
|
| 13 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 14 |
+
from sklearn.linear_model import LinearRegression
|
| 15 |
+
from xgboost import XGBRegressor
|
| 16 |
+
from sklearn.neighbors import KNeighborsRegressor
|
| 17 |
+
from sklearn.model_selection import cross_val_score
|
| 18 |
+
from sklearn.metrics import mean_squared_error
|
| 19 |
+
from sklearn import metrics
|
| 20 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 21 |
+
from time import time
|
| 22 |
+
|
| 23 |
+
app = Flask(__name__)
|
| 24 |
+
|
| 25 |
+
# Set the secret key for session management
|
| 26 |
+
app.secret_key = os.urandom(24)
|
| 27 |
+
|
| 28 |
+
# Configurations
|
| 29 |
+
UPLOAD_FOLDER = "uploads/"
|
| 30 |
+
DATA_FOLDER = "data/"
|
| 31 |
+
|
| 32 |
+
# Define the model directory (ensuring correct path formatting)
|
| 33 |
+
MODEL_DIR = r'.\Model'
|
| 34 |
+
LABEL_ENOCDER_DIR = r'.\Label_encoders'
|
| 35 |
+
|
| 36 |
+
# Define the output file path
|
| 37 |
+
PRED_OUTPUT_FILE = "data/pred_output.csv"
|
| 38 |
+
CLASS_OUTPUT_FILE = "data/class_output.csv"
|
| 39 |
+
|
| 40 |
+
ALLOWED_EXTENSIONS = {'csv', 'xlsx'}
|
| 41 |
+
|
| 42 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 43 |
+
|
| 44 |
+
# Ensure the upload folder exists
|
| 45 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 46 |
+
os.makedirs(app.config['DATA_FOLDER'], exist_ok=True)
|
| 47 |
+
|
| 48 |
+
# Load models using os.path.join for better cross-platform compatibility
|
| 49 |
+
|
| 50 |
+
# linear_regression_model
|
| 51 |
+
gia_model = load(os.path.join(MODEL_DIR, 'linear_regression_model_gia_price.joblib'))
|
| 52 |
+
grade_model = load(os.path.join(MODEL_DIR, 'linear_regression_model_grade_price.joblib'))
|
| 53 |
+
bygrade_model = load(os.path.join(MODEL_DIR, 'linear_regression_model_bygrade_price.joblib'))
|
| 54 |
+
makable_model = load(os.path.join(MODEL_DIR, 'linear_regression_model_makable_price.joblib'))
|
| 55 |
+
|
| 56 |
+
# classifier_model
|
| 57 |
+
col_model = load(os.path.join(MODEL_DIR, 'classification_LogisticRegression_col.joblib'))
|
| 58 |
+
cts_model = load(os.path.join(MODEL_DIR, 'classification_LogisticRegression_cts.joblib'))
|
| 59 |
+
cut_model = load(os.path.join(MODEL_DIR, 'classification_LogisticRegression_cut.joblib'))
|
| 60 |
+
qua_model = load(os.path.join(MODEL_DIR, 'classification_LogisticRegression_qua.joblib'))
|
| 61 |
+
shp_model = load(os.path.join(MODEL_DIR, 'classification_LogisticRegression_shp.joblib'))
|
| 62 |
+
|
| 63 |
+
# print("===================================models==================================")
|
| 64 |
+
# print(gia_model)
|
| 65 |
+
# print(grade_model)
|
| 66 |
+
# print(bygrade_model)
|
| 67 |
+
# print(makable_model)
|
| 68 |
+
|
| 69 |
+
# Load label encoders
|
| 70 |
+
encoder_list = ['Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngLab',
|
| 71 |
+
'Change_cts_value', 'Change_shape_value', 'Change_quality_value', 'Change_color_value', 'Change_cut_value']
|
| 72 |
+
#loaded_label_encoder = {val: load(f"./Label_encoders/label_encoder_{val}.joblib") for val in encoder_list}
|
| 73 |
+
loaded_label_encoder = {}
|
| 74 |
+
for val in encoder_list:
|
| 75 |
+
#encoder_path = f"H:/DEV PATEL/2025/AI_In_Diamond_Industry/Label_encoders/label_encoder_{val}.joblib"
|
| 76 |
+
encoder_path = os.path.join(LABEL_ENOCDER_DIR, f"label_encoder_{val}.joblib")
|
| 77 |
+
loaded_label_encoder[val] = load(encoder_path)
|
| 78 |
+
|
| 79 |
+
# print(loaded_label_encoder)
|
| 80 |
+
|
| 81 |
+
# Ensure upload folder exists
|
| 82 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 83 |
+
|
| 84 |
+
def allowed_file(filename):
|
| 85 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 86 |
+
|
| 87 |
+
@app.route('/')
|
| 88 |
+
def index():
|
| 89 |
+
return render_template('index.html')
|
| 90 |
+
|
| 91 |
+
@app.route('/predict', methods=['POST'])
|
| 92 |
+
def predict():
|
| 93 |
+
if 'file' not in request.files:
|
| 94 |
+
flash('No file part', 'error')
|
| 95 |
+
return redirect(request.url)
|
| 96 |
+
|
| 97 |
+
file = request.files['file']
|
| 98 |
+
if file.filename == '':
|
| 99 |
+
flash('No selected file', 'error')
|
| 100 |
+
return redirect(request.url)
|
| 101 |
+
|
| 102 |
+
if file and allowed_file(file.filename):
|
| 103 |
+
filename = secure_filename(file.filename)
|
| 104 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 105 |
+
file.save(filepath)
|
| 106 |
+
|
| 107 |
+
# Convert to DataFrame
|
| 108 |
+
if filename.endswith('.csv'):
|
| 109 |
+
df = pd.read_csv(filepath)
|
| 110 |
+
else:
|
| 111 |
+
df = pd.read_excel(filepath)
|
| 112 |
+
|
| 113 |
+
# Preprocess DataFrame
|
| 114 |
+
print("===================================process_dataframe=0==================================")
|
| 115 |
+
df,dx = process_dataframe(df)
|
| 116 |
+
print("===================================process_dataframe=5==================================")
|
| 117 |
+
return render_template('output.html', df=df.to_html(), dx=dx.to_html())
|
| 118 |
+
else:
|
| 119 |
+
flash('Invalid file type. Only CSV and Excel files are allowed.', 'error')
|
| 120 |
+
print('Invalid file type. Only CSV and Excel files are allowed.')
|
| 121 |
+
return redirect(request.url)
|
| 122 |
+
|
| 123 |
+
def process_dataframe(df):
|
| 124 |
+
try:
|
| 125 |
+
print("===================================process_dataframe=1==================================")
|
| 126 |
+
# 'EngLab' is not in the required columns
|
| 127 |
+
required_columns = ['Tag', 'EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol',
|
| 128 |
+
'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngAmt']
|
| 129 |
+
|
| 130 |
+
# for prediction
|
| 131 |
+
df = df[required_columns]
|
| 132 |
+
df = df.copy()
|
| 133 |
+
# for classification
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
# df[col] = df[col].map(lambda x: loaded_label_encoder[col].transform([x])[0] if x in loaded_label_encoder[col].classes_ else np.nan)
|
| 137 |
+
|
| 138 |
+
# Transform categorical features using loaded label encoders
|
| 139 |
+
df["Tag"] = loaded_label_encoder['Tag'].transform(df["Tag"])
|
| 140 |
+
df["EngShp"] = loaded_label_encoder['EngShp'].transform(df["EngShp"])
|
| 141 |
+
df["EngQua"] = loaded_label_encoder['EngQua'].transform(df["EngQua"])
|
| 142 |
+
df["EngCol"] = loaded_label_encoder['EngCol'].transform(df["EngCol"])
|
| 143 |
+
df["EngCut"] = loaded_label_encoder['EngCut'].transform(df["EngCut"])
|
| 144 |
+
df["EngPol"] = loaded_label_encoder['EngPol'].transform(df["EngPol"])
|
| 145 |
+
df["EngSym"] = loaded_label_encoder['EngSym'].transform(df["EngSym"])
|
| 146 |
+
df["EngFlo"] = loaded_label_encoder['EngFlo'].transform(df["EngFlo"])
|
| 147 |
+
df["EngNts"] = loaded_label_encoder['EngNts'].transform(df["EngNts"])
|
| 148 |
+
df["EngMikly"] = loaded_label_encoder['EngMikly'].transform(df["EngMikly"])
|
| 149 |
+
#EngLab = loaded_label_encoder['EngLab'].transform(df[EngLab])
|
| 150 |
+
|
| 151 |
+
df=df.astype(float)
|
| 152 |
+
print(df.head())
|
| 153 |
+
|
| 154 |
+
dx = df.copy()
|
| 155 |
+
|
| 156 |
+
print(df.columns)
|
| 157 |
+
x= df.copy()
|
| 158 |
+
|
| 159 |
+
# print("Model expects", gia_model.n_features_in_, "features.")
|
| 160 |
+
# print("X_features shape:", x.shape)
|
| 161 |
+
|
| 162 |
+
print("===================================process_dataframe=2==================================")
|
| 163 |
+
|
| 164 |
+
# ================================================================================================
|
| 165 |
+
# Prediction report
|
| 166 |
+
# ================================================================================================
|
| 167 |
+
|
| 168 |
+
# Predict prices
|
| 169 |
+
df['GIA_Predicted'] = gia_model.predict(x)
|
| 170 |
+
df['Grade_Predicted'] = grade_model.predict(x)
|
| 171 |
+
df['ByGrade_Predicted'] = bygrade_model.predict(x)
|
| 172 |
+
df['Makable_Predicted'] = makable_model.predict(x)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
# Compute differences
|
| 176 |
+
df['GIA_Diff'] = df['EngAmt'] - df['GIA_Predicted']
|
| 177 |
+
df['Grade_Diff'] = df['EngAmt'] - df['Grade_Predicted']
|
| 178 |
+
df['ByGrade_Diff'] = df['EngAmt'] - df['ByGrade_Predicted']
|
| 179 |
+
df['Makable_Diff'] = df['EngAmt'] - df['Makable_Predicted']
|
| 180 |
+
|
| 181 |
+
print(df.head())
|
| 182 |
+
|
| 183 |
+
predictions = df.to_dict(orient='records')
|
| 184 |
+
analysis = df.describe().to_html()
|
| 185 |
+
#print(analysis)
|
| 186 |
+
#print(predictions)
|
| 187 |
+
print("===================================process_dataframe=3==================================")
|
| 188 |
+
|
| 189 |
+
# ================================================================================================
|
| 190 |
+
# Classification report
|
| 191 |
+
# ================================================================================================
|
| 192 |
+
|
| 193 |
+
dx['col_change'] = col_model.predict(x)
|
| 194 |
+
dx['cts_change'] = cts_model.predict(x)
|
| 195 |
+
dx['cut_change'] = cut_model.predict(x)
|
| 196 |
+
dx['qua_change'] = qua_model.predict(x)
|
| 197 |
+
dx['shp_change'] = shp_model.predict(x)
|
| 198 |
+
|
| 199 |
+
# Inverse transform the predictions
|
| 200 |
+
dx['col_change'] = loaded_label_encoder['Change_color_value'].inverse_transform(dx['col_change'])
|
| 201 |
+
dx['cts_change'] = loaded_label_encoder['Change_cts_value'].inverse_transform(dx['cts_change'])
|
| 202 |
+
dx['cut_change'] = loaded_label_encoder['Change_cut_value'].inverse_transform(dx['cut_change'])
|
| 203 |
+
dx['qua_change'] = loaded_label_encoder['Change_quality_value'].inverse_transform(dx['qua_change'])
|
| 204 |
+
dx['shp_change'] = loaded_label_encoder['Change_shape_value'].inverse_transform(dx['shp_change'])
|
| 205 |
+
|
| 206 |
+
print(dx.head())
|
| 207 |
+
|
| 208 |
+
print("===================================process_dataframe=4==================================")
|
| 209 |
+
|
| 210 |
+
# Save output file with date and time
|
| 211 |
+
time = str(pd.Timestamp.now().strftime("%Y-%m-%d"))
|
| 212 |
+
|
| 213 |
+
#saving the output file
|
| 214 |
+
global PRED_OUTPUT_FILE
|
| 215 |
+
PRED_OUTPUT_FILE = f'data/prediction_output_{time}.csv'
|
| 216 |
+
df.to_csv(PRED_OUTPUT_FILE, index=False)
|
| 217 |
+
|
| 218 |
+
#saving the output file
|
| 219 |
+
global CLASS_OUTPUT_FILE
|
| 220 |
+
CLASS_OUTPUT_FILE = f'data/classification_output_{time}.csv'
|
| 221 |
+
dx.to_csv(CLASS_OUTPUT_FILE, index=False)
|
| 222 |
+
|
| 223 |
+
print("===================================Output file saved as output.csv===================================")
|
| 224 |
+
|
| 225 |
+
return df.head(), dx.head()
|
| 226 |
+
except Exception as e:
|
| 227 |
+
print(f'Error processing file: {e}')
|
| 228 |
+
flash(f'Error processing file: {e}', 'error')
|
| 229 |
+
return pd.DataFrame(), pd.DataFrame()
|
| 230 |
+
|
| 231 |
+
def classification_report(df):
|
| 232 |
+
try:
|
| 233 |
+
classifcation_data = df[["EngGraphCts","EngCts","EngShp","EngQua","EngCol","EngCut","EngPol","EngSym","EngFlo","EngNts","EngMikly","EngLab","EngAmt",
|
| 234 |
+
"MkblCts","MkblShp","MkblQua","MkblCol","MkblCut","MkblPol","MkblSym","MkblFlo","MkblNts","MkblMikly","MkblLab","MkblAmt"]]
|
| 235 |
+
|
| 236 |
+
# Make predictions
|
| 237 |
+
classifcation_data["Cts_diff_eng_mkbl"] = round(classifcation_data["EngCts"] - classifcation_data["MkblCts"],2)
|
| 238 |
+
|
| 239 |
+
# Create a new column 'Change_Label' based on the values in 'Cts_diff_eng_mkbl'
|
| 240 |
+
classifcation_data['Change_cts_value'] = classifcation_data['Cts_diff_eng_mkbl'].apply(
|
| 241 |
+
lambda x: str(x)+' negative change' if x < 0 else (str(x)+' positive change' if x > 0 else 'no change')
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
# Create a new column 'Shape_Change' based on the values in 'EngShp' and 'MkblShp'
|
| 245 |
+
classifcation_data['Change_shape_value'] = classifcation_data.apply(
|
| 246 |
+
lambda row: str(row['EngShp'])+' to '+str(row['MkblShp'])+' shape change' if row['EngShp'] != row['MkblShp'] else 'shape not change', axis=1
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# Create a new column 'quality_Change' based on the values in 'EngQua' and 'MkblQua'
|
| 250 |
+
classifcation_data['Change_quality_value'] = classifcation_data.apply(
|
| 251 |
+
lambda row: str(row['EngQua'])+' to '+str(row['MkblQua'])+' quality change' if row['EngQua'] != row['MkblQua'] else 'quality not change', axis=1
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
# Create a new column 'color_Change' based on the values in 'EngCol' and 'MkblCol'
|
| 255 |
+
classifcation_data['Change_color_value'] = classifcation_data.apply(
|
| 256 |
+
lambda row: str(row['EngCol'])+' to '+str(row['MkblCol'])+' color change' if row['EngCol'] != row['MkblCol'] else 'color not change', axis=1
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
# Create a new column 'cut_Change' based on the values in 'EngCut' and 'MkblCut'
|
| 260 |
+
classifcation_data['Change_cut_value'] = classifcation_data.apply(
|
| 261 |
+
lambda row: str(row['EngCut'])+' to '+str(row['MkblCut'])+' cut change' if row['EngCut'] != row['MkblCut'] else 'cut not change', axis=1
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
# Generate classification report
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
return classifcation_data
|
| 268 |
+
except Exception as e:
|
| 269 |
+
flash(f'Error generating classification report: {e}', 'error')
|
| 270 |
+
print(f'Error generating classification report: {e}')
|
| 271 |
+
return None
|
| 272 |
+
|
| 273 |
+
@app.route('/download_pred', methods=['GET'])
|
| 274 |
+
def download_pred():
|
| 275 |
+
"""Serve the output.csv file for download."""
|
| 276 |
+
return send_file(PRED_OUTPUT_FILE, as_attachment=True)
|
| 277 |
+
|
| 278 |
+
@app.route('/download_class', methods=['GET'])
|
| 279 |
+
def download_class():
|
| 280 |
+
"""Serve the output.csv file for download."""
|
| 281 |
+
return send_file(CLASS_OUTPUT_FILE, as_attachment=True)
|
| 282 |
+
|
| 283 |
+
if __name__ == "__main__":
|
| 284 |
app.run(debug=True)
|