Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, redirect, url_for,
|
| 2 |
import os
|
| 3 |
import pandas as pd
|
| 4 |
from werkzeug.utils import secure_filename
|
|
@@ -142,12 +142,12 @@ def index():
|
|
| 142 |
@app.route('/predict', methods=['POST'])
|
| 143 |
def predict():
|
| 144 |
if 'file' not in request.files:
|
| 145 |
-
|
| 146 |
return redirect(url_for('index'))
|
| 147 |
|
| 148 |
file = request.files['file']
|
| 149 |
if file.filename == '':
|
| 150 |
-
|
| 151 |
return redirect(url_for('index'))
|
| 152 |
|
| 153 |
if file and allowed_file(file.filename):
|
|
@@ -162,13 +162,13 @@ def predict():
|
|
| 162 |
else:
|
| 163 |
df = pd.read_excel(filepath)
|
| 164 |
except Exception as e:
|
| 165 |
-
|
| 166 |
return redirect(url_for('index'))
|
| 167 |
|
| 168 |
# Process the DataFrame and generate predictions and classification analysis.
|
| 169 |
df_pred, dx_class = process_dataframe(df)
|
| 170 |
if df_pred.empty:
|
| 171 |
-
|
| 172 |
return redirect(url_for('index'))
|
| 173 |
|
| 174 |
# Save output files with a timestamp and unique id.
|
|
@@ -183,7 +183,7 @@ def predict():
|
|
| 183 |
# Redirect to report view; default to prediction report, page 1.
|
| 184 |
return redirect(url_for('report_view', report_type='pred', page=1))
|
| 185 |
else:
|
| 186 |
-
|
| 187 |
return redirect(url_for('index'))
|
| 188 |
|
| 189 |
def process_dataframe(df):
|
|
@@ -202,7 +202,7 @@ def process_dataframe(df):
|
|
| 202 |
try:
|
| 203 |
df_pred[col] = loaded_label_encoder[col].transform(df_pred[col])
|
| 204 |
except ValueError as e:
|
| 205 |
-
|
| 206 |
return pd.DataFrame(), pd.DataFrame()
|
| 207 |
|
| 208 |
# Update the classification DataFrame with the transformed prediction columns.
|
|
@@ -214,7 +214,7 @@ def process_dataframe(df):
|
|
| 214 |
try:
|
| 215 |
df_class[col] = loaded_label_encoder[col].transform(df_class[col])
|
| 216 |
except ValueError as e:
|
| 217 |
-
|
| 218 |
return pd.DataFrame(), pd.DataFrame()
|
| 219 |
|
| 220 |
# Convert both DataFrames to float.
|
|
@@ -287,7 +287,7 @@ def process_dataframe(df):
|
|
| 287 |
# Final return with full data for pagination.
|
| 288 |
return df_pred, dx.head(len(df_pred))
|
| 289 |
except Exception as e:
|
| 290 |
-
|
| 291 |
return pd.DataFrame(), pd.DataFrame()
|
| 292 |
|
| 293 |
# ------------------------------
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, print, send_file
|
| 2 |
import os
|
| 3 |
import pandas as pd
|
| 4 |
from werkzeug.utils import secure_filename
|
|
|
|
| 142 |
@app.route('/predict', methods=['POST'])
|
| 143 |
def predict():
|
| 144 |
if 'file' not in request.files:
|
| 145 |
+
print('No file part', 'error')
|
| 146 |
return redirect(url_for('index'))
|
| 147 |
|
| 148 |
file = request.files['file']
|
| 149 |
if file.filename == '':
|
| 150 |
+
print('No selected file', 'error')
|
| 151 |
return redirect(url_for('index'))
|
| 152 |
|
| 153 |
if file and allowed_file(file.filename):
|
|
|
|
| 162 |
else:
|
| 163 |
df = pd.read_excel(filepath)
|
| 164 |
except Exception as e:
|
| 165 |
+
print(f'Error reading file: {e}', 'error')
|
| 166 |
return redirect(url_for('index'))
|
| 167 |
|
| 168 |
# Process the DataFrame and generate predictions and classification analysis.
|
| 169 |
df_pred, dx_class = process_dataframe(df)
|
| 170 |
if df_pred.empty:
|
| 171 |
+
print("Processed prediction DataFrame is empty. Check the input file and processing logic.", "error")
|
| 172 |
return redirect(url_for('index'))
|
| 173 |
|
| 174 |
# Save output files with a timestamp and unique id.
|
|
|
|
| 183 |
# Redirect to report view; default to prediction report, page 1.
|
| 184 |
return redirect(url_for('report_view', report_type='pred', page=1))
|
| 185 |
else:
|
| 186 |
+
print('Invalid file type. Only CSV and Excel files are allowed.', 'error')
|
| 187 |
return redirect(url_for('index'))
|
| 188 |
|
| 189 |
def process_dataframe(df):
|
|
|
|
| 202 |
try:
|
| 203 |
df_pred[col] = loaded_label_encoder[col].transform(df_pred[col])
|
| 204 |
except ValueError as e:
|
| 205 |
+
print(f'Invalid value in column {col}: {e}', 'error')
|
| 206 |
return pd.DataFrame(), pd.DataFrame()
|
| 207 |
|
| 208 |
# Update the classification DataFrame with the transformed prediction columns.
|
|
|
|
| 214 |
try:
|
| 215 |
df_class[col] = loaded_label_encoder[col].transform(df_class[col])
|
| 216 |
except ValueError as e:
|
| 217 |
+
print(f'Invalid value in column {col}: {e}', 'error')
|
| 218 |
return pd.DataFrame(), pd.DataFrame()
|
| 219 |
|
| 220 |
# Convert both DataFrames to float.
|
|
|
|
| 287 |
# Final return with full data for pagination.
|
| 288 |
return df_pred, dx.head(len(df_pred))
|
| 289 |
except Exception as e:
|
| 290 |
+
print(f'Error processing file: {e}', 'error')
|
| 291 |
return pd.DataFrame(), pd.DataFrame()
|
| 292 |
|
| 293 |
# ------------------------------
|