Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import datetime
|
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
import numpy as np
|
| 7 |
-
import mysql.connector
|
| 8 |
import gradio as gr
|
| 9 |
from sklearn.ensemble import RandomForestRegressor
|
| 10 |
from sklearn.preprocessing import StandardScaler
|
|
@@ -15,7 +14,7 @@ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
| 15 |
from reportlab.lib import colors
|
| 16 |
|
| 17 |
# ---------------------------------------------------------------------------
|
| 18 |
-
# 1. DATABASE CONFIGURATION (Safe MySQL / FreeDatabase Connector)
|
| 19 |
# ---------------------------------------------------------------------------
|
| 20 |
DB_HOST = os.getenv("MYSQL_HOST", "sql6.freedatabase.com")
|
| 21 |
DB_USER = os.getenv("MYSQL_USER", "your_username")
|
|
@@ -25,6 +24,7 @@ DB_PORT = int(os.getenv("MYSQL_PORT", 3306))
|
|
| 25 |
|
| 26 |
def get_db_connection():
|
| 27 |
try:
|
|
|
|
| 28 |
conn = mysql.connector.connect(
|
| 29 |
host=DB_HOST,
|
| 30 |
user=DB_USER,
|
|
@@ -96,13 +96,12 @@ def build_or_load_artifacts():
|
|
| 96 |
except Exception:
|
| 97 |
pass
|
| 98 |
|
| 99 |
-
# If artifacts are missing, build model from dataset or realistic synthesis
|
| 100 |
csv_file = "Steel_industry_data.csv"
|
| 101 |
if os.path.exists(csv_file):
|
| 102 |
df_train = pd.read_csv(csv_file)
|
| 103 |
df_train = clean_cols(df_train)
|
| 104 |
else:
|
| 105 |
-
#
|
| 106 |
np.random.seed(42)
|
| 107 |
n = 5000
|
| 108 |
lag_kvarh = np.random.exponential(13.0, n)
|
|
@@ -211,7 +210,7 @@ def preprocess_and_predict(df_raw):
|
|
| 211 |
return df
|
| 212 |
|
| 213 |
# ---------------------------------------------------------------------------
|
| 214 |
-
# 4. PDF GENERATION ENGINE
|
| 215 |
# ---------------------------------------------------------------------------
|
| 216 |
def generate_pdf_report(df_results, summary_stats):
|
| 217 |
pdf_buffer = io.BytesIO()
|
|
@@ -244,6 +243,7 @@ def generate_pdf_report(df_results, summary_stats):
|
|
| 244 |
summary_data = [
|
| 245 |
["Metric Description", "Value"],
|
| 246 |
["Total Processed Records", f"{summary_stats['total_records']}"],
|
|
|
|
| 247 |
["Mean Electricity Demand (kWh)", f"{summary_stats['mean_kwh']:.2f} kWh"],
|
| 248 |
["Max Electricity Demand (kWh)", f"{summary_stats['max_kwh']:.2f} kWh"],
|
| 249 |
["Total Estimated CO2 Footprint", f"{summary_stats['total_co2']:.4f} Metric Tonnes"],
|
|
@@ -264,13 +264,24 @@ def generate_pdf_report(df_results, summary_stats):
|
|
| 264 |
elements.append(Spacer(1, 15))
|
| 265 |
|
| 266 |
elements.append(Paragraph("Sample Predictions Preview (First 10 Rows)", styles['Heading2']))
|
| 267 |
-
preview_df = df_results[['Predicted_Usage_kWh', 'Estimated_CO2_Tonnes']].head(10).reset_index()
|
| 268 |
|
| 269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
for _, row in preview_df.iterrows():
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
-
pt = Table(preview_data, colWidths=[
|
| 274 |
pt.setStyle(TableStyle([
|
| 275 |
('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#0284c7')),
|
| 276 |
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
|
@@ -291,7 +302,7 @@ def generate_pdf_report(df_results, summary_stats):
|
|
| 291 |
return pdf_path
|
| 292 |
|
| 293 |
# ---------------------------------------------------------------------------
|
| 294 |
-
# 5. GRADIO HANDLERS
|
| 295 |
# ---------------------------------------------------------------------------
|
| 296 |
def handle_single_prediction(lag_kvarh, lead_kvarh, lag_pf, lead_pf, nsm, hour, month, day_of_week, load_type):
|
| 297 |
data = {
|
|
@@ -311,7 +322,13 @@ def handle_single_prediction(lag_kvarh, lead_kvarh, lag_pf, lead_pf, nsm, hour,
|
|
| 311 |
kwh = float(res['Predicted_Usage_kWh'].iloc[0])
|
| 312 |
co2 = float(res['Estimated_CO2_Tonnes'].iloc[0])
|
| 313 |
|
| 314 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
conn = get_db_connection()
|
| 316 |
if conn:
|
| 317 |
try:
|
|
@@ -326,7 +343,7 @@ def handle_single_prediction(lag_kvarh, lead_kvarh, lag_pf, lead_pf, nsm, hour,
|
|
| 326 |
except Exception:
|
| 327 |
pass
|
| 328 |
|
| 329 |
-
return f"{kwh:.2f} kWh", f"{co2:.6f} Metric Tonnes"
|
| 330 |
|
| 331 |
def handle_csv_upload(file):
|
| 332 |
if file is None:
|
|
@@ -335,9 +352,20 @@ def handle_csv_upload(file):
|
|
| 335 |
df_raw = pd.read_csv(file.name)
|
| 336 |
df_results = preprocess_and_predict(df_raw)
|
| 337 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
total_records = len(df_results)
|
| 339 |
|
| 340 |
-
# Use ground-truth Usage_kWh if present in uploaded CSV, else use predictions
|
| 341 |
cleaned_raw_cols = [c.strip().replace(' ', '_').replace('(', '').replace(')', '').replace('.', '_') for c in df_raw.columns]
|
| 342 |
if 'Usage_kWh' in cleaned_raw_cols:
|
| 343 |
target_col = df_results['Usage_kWh'] if 'Usage_kWh' in df_results.columns else df_results['Predicted_Usage_kWh']
|
|
@@ -348,7 +376,7 @@ def handle_csv_upload(file):
|
|
| 348 |
max_kwh = float(target_col.max())
|
| 349 |
total_co2 = float((target_col * 0.00042).sum())
|
| 350 |
|
| 351 |
-
# Mitigation simulation (10%
|
| 352 |
mitigated_kwh = target_col.copy()
|
| 353 |
mitigated_kwh[mitigated_kwh > mean_kwh] *= 0.90
|
| 354 |
mitigated_co2 = float(total_co2 - (mitigated_kwh * 0.00042).sum())
|
|
@@ -358,7 +386,8 @@ def handle_csv_upload(file):
|
|
| 358 |
'mean_kwh': mean_kwh,
|
| 359 |
'max_kwh': max_kwh,
|
| 360 |
'total_co2': total_co2,
|
| 361 |
-
'mitigated_co2': mitigated_co2
|
|
|
|
| 362 |
}
|
| 363 |
|
| 364 |
pdf_path = generate_pdf_report(df_results, summary_stats)
|
|
@@ -366,6 +395,7 @@ def handle_csv_upload(file):
|
|
| 366 |
summary_text = f"""
|
| 367 |
### 📊 Analysis Summary
|
| 368 |
* **Total Processed Records:** {total_records:,}
|
|
|
|
| 369 |
* **Average Demand:** {mean_kwh:.2f} kWh
|
| 370 |
* **Peak Demand Recorded:** {max_kwh:.2f} kWh
|
| 371 |
* **Total Estimated Carbon Footprint:** {total_co2:.4f} Metric Tonnes
|
|
@@ -375,7 +405,7 @@ def handle_csv_upload(file):
|
|
| 375 |
return df_results, summary_text, pdf_path
|
| 376 |
|
| 377 |
# ---------------------------------------------------------------------------
|
| 378 |
-
# 6. SLEEK & GLOSSY GRADIO UI
|
| 379 |
# ---------------------------------------------------------------------------
|
| 380 |
custom_css = """
|
| 381 |
body, .gradio-container {
|
|
@@ -457,7 +487,7 @@ with gr.Blocks(css=custom_css, title="Steel Industry Energy Predictor") as demo:
|
|
| 457 |
|
| 458 |
with gr.Row():
|
| 459 |
with gr.Column(scale=2):
|
| 460 |
-
output_dataframe = gr.Dataframe(label="Predictions
|
| 461 |
with gr.Column(scale=1):
|
| 462 |
summary_output = gr.Markdown(value="*Upload a CSV to view analysis summary.*")
|
| 463 |
pdf_download = gr.File(label="📄 Download PDF Audit Report")
|
|
@@ -465,22 +495,22 @@ with gr.Blocks(css=custom_css, title="Steel Industry Energy Predictor") as demo:
|
|
| 465 |
# TAB 2: SINGLE PREDICTION RESULT
|
| 466 |
with gr.TabItem("🎯 Single Record Result"):
|
| 467 |
with gr.Group(elem_classes=["glass-panel"]):
|
| 468 |
-
gr.Markdown("### Single Inspection
|
| 469 |
out_kwh = gr.Textbox(label="Predicted Energy Consumption (Usage_kWh)", interactive=False)
|
| 470 |
out_co2 = gr.Textbox(label="Estimated Carbon Emissions (Tonnes CO2)", interactive=False)
|
|
|
|
| 471 |
|
| 472 |
-
# Inputs list for single predictions
|
| 473 |
single_inputs = [lag_kvarh, lead_kvarh, lag_pf, lead_pf, nsm, hour, month, day_of_week, load_type]
|
| 474 |
-
single_outputs = [out_kwh, out_co2]
|
| 475 |
|
| 476 |
-
#
|
| 477 |
predict_btn.click(
|
| 478 |
fn=handle_single_prediction,
|
| 479 |
inputs=single_inputs,
|
| 480 |
outputs=single_outputs
|
| 481 |
)
|
| 482 |
|
| 483 |
-
# Real-time change
|
| 484 |
for inp in single_inputs:
|
| 485 |
inp.change(
|
| 486 |
fn=handle_single_prediction,
|
|
@@ -488,14 +518,14 @@ with gr.Blocks(css=custom_css, title="Steel Industry Energy Predictor") as demo:
|
|
| 488 |
outputs=single_outputs
|
| 489 |
)
|
| 490 |
|
| 491 |
-
#
|
| 492 |
demo.load(
|
| 493 |
fn=handle_single_prediction,
|
| 494 |
inputs=single_inputs,
|
| 495 |
outputs=single_outputs
|
| 496 |
)
|
| 497 |
|
| 498 |
-
# Batch CSV
|
| 499 |
process_csv_btn.click(
|
| 500 |
fn=handle_csv_upload,
|
| 501 |
inputs=[file_input],
|
|
|
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
from sklearn.ensemble import RandomForestRegressor
|
| 9 |
from sklearn.preprocessing import StandardScaler
|
|
|
|
| 14 |
from reportlab.lib import colors
|
| 15 |
|
| 16 |
# ---------------------------------------------------------------------------
|
| 17 |
+
# 1. OPTIONAL DATABASE CONFIGURATION (Safe MySQL / FreeDatabase Connector)
|
| 18 |
# ---------------------------------------------------------------------------
|
| 19 |
DB_HOST = os.getenv("MYSQL_HOST", "sql6.freedatabase.com")
|
| 20 |
DB_USER = os.getenv("MYSQL_USER", "your_username")
|
|
|
|
| 24 |
|
| 25 |
def get_db_connection():
|
| 26 |
try:
|
| 27 |
+
import mysql.connector
|
| 28 |
conn = mysql.connector.connect(
|
| 29 |
host=DB_HOST,
|
| 30 |
user=DB_USER,
|
|
|
|
| 96 |
except Exception:
|
| 97 |
pass
|
| 98 |
|
|
|
|
| 99 |
csv_file = "Steel_industry_data.csv"
|
| 100 |
if os.path.exists(csv_file):
|
| 101 |
df_train = pd.read_csv(csv_file)
|
| 102 |
df_train = clean_cols(df_train)
|
| 103 |
else:
|
| 104 |
+
# Generate realistic steel plant dataset matching UCI bounds
|
| 105 |
np.random.seed(42)
|
| 106 |
n = 5000
|
| 107 |
lag_kvarh = np.random.exponential(13.0, n)
|
|
|
|
| 210 |
return df
|
| 211 |
|
| 212 |
# ---------------------------------------------------------------------------
|
| 213 |
+
# 4. PDF REPORT GENERATION ENGINE
|
| 214 |
# ---------------------------------------------------------------------------
|
| 215 |
def generate_pdf_report(df_results, summary_stats):
|
| 216 |
pdf_buffer = io.BytesIO()
|
|
|
|
| 243 |
summary_data = [
|
| 244 |
["Metric Description", "Value"],
|
| 245 |
["Total Processed Records", f"{summary_stats['total_records']}"],
|
| 246 |
+
["Low Power Factor (<85%) Anomaly Flagged", f"{summary_stats['anomaly_count']} records"],
|
| 247 |
["Mean Electricity Demand (kWh)", f"{summary_stats['mean_kwh']:.2f} kWh"],
|
| 248 |
["Max Electricity Demand (kWh)", f"{summary_stats['max_kwh']:.2f} kWh"],
|
| 249 |
["Total Estimated CO2 Footprint", f"{summary_stats['total_co2']:.4f} Metric Tonnes"],
|
|
|
|
| 264 |
elements.append(Spacer(1, 15))
|
| 265 |
|
| 266 |
elements.append(Paragraph("Sample Predictions Preview (First 10 Rows)", styles['Heading2']))
|
|
|
|
| 267 |
|
| 268 |
+
cols_to_show = ['Predicted_Usage_kWh', 'Estimated_CO2_Tonnes']
|
| 269 |
+
if 'PF_Status' in df_results.columns:
|
| 270 |
+
cols_to_show.append('PF_Status')
|
| 271 |
+
|
| 272 |
+
preview_df = df_results[cols_to_show].head(10).reset_index()
|
| 273 |
+
|
| 274 |
+
preview_data = [["Row ID", "Predicted Usage (kWh)", "Estimated CO2 (Tonnes)", "PF Status"]]
|
| 275 |
for _, row in preview_df.iterrows():
|
| 276 |
+
pf_st = row.get('PF_Status', 'N/A')
|
| 277 |
+
preview_data.append([
|
| 278 |
+
str(int(row['index'] + 1)),
|
| 279 |
+
f"{row['Predicted_Usage_kWh']:.2f}",
|
| 280 |
+
f"{row['Estimated_CO2_Tonnes']:.6f}",
|
| 281 |
+
str(pf_st)
|
| 282 |
+
])
|
| 283 |
|
| 284 |
+
pt = Table(preview_data, colWidths=[50, 150, 150, 150])
|
| 285 |
pt.setStyle(TableStyle([
|
| 286 |
('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#0284c7')),
|
| 287 |
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
|
|
|
| 302 |
return pdf_path
|
| 303 |
|
| 304 |
# ---------------------------------------------------------------------------
|
| 305 |
+
# 5. GRADIO UI HANDLERS
|
| 306 |
# ---------------------------------------------------------------------------
|
| 307 |
def handle_single_prediction(lag_kvarh, lead_kvarh, lag_pf, lead_pf, nsm, hour, month, day_of_week, load_type):
|
| 308 |
data = {
|
|
|
|
| 322 |
kwh = float(res['Predicted_Usage_kWh'].iloc[0])
|
| 323 |
co2 = float(res['Estimated_CO2_Tonnes'].iloc[0])
|
| 324 |
|
| 325 |
+
# Low Power Factor (< 85%) Anomaly Diagnostic Output
|
| 326 |
+
if lag_pf < 85.0:
|
| 327 |
+
pf_status = f"⚠️ CRITICAL: Low Power Factor ({lag_pf:.1f}% < 85%). Risk of transformer heat loss & utility surcharge penalties!"
|
| 328 |
+
else:
|
| 329 |
+
pf_status = f"✅ OPTIMAL: Power Factor ({lag_pf:.1f}% ≥ 85%). Operating within safe efficiency limits."
|
| 330 |
+
|
| 331 |
+
# Optional DB record insertion
|
| 332 |
conn = get_db_connection()
|
| 333 |
if conn:
|
| 334 |
try:
|
|
|
|
| 343 |
except Exception:
|
| 344 |
pass
|
| 345 |
|
| 346 |
+
return f"{kwh:.2f} kWh", f"{co2:.6f} Metric Tonnes", pf_status
|
| 347 |
|
| 348 |
def handle_csv_upload(file):
|
| 349 |
if file is None:
|
|
|
|
| 352 |
df_raw = pd.read_csv(file.name)
|
| 353 |
df_results = preprocess_and_predict(df_raw)
|
| 354 |
|
| 355 |
+
# Tag Anomaly Status Column on Batch CSV Dataframe
|
| 356 |
+
if 'Lagging_Current_Power_Factor' in df_results.columns:
|
| 357 |
+
df_results['PF_Status'] = np.where(
|
| 358 |
+
df_results['Lagging_Current_Power_Factor'] < 85.0,
|
| 359 |
+
'⚠️ CRITICAL (<85%)',
|
| 360 |
+
'✅ NORMAL'
|
| 361 |
+
)
|
| 362 |
+
anomaly_count = int((df_results['Lagging_Current_Power_Factor'] < 85.0).sum())
|
| 363 |
+
else:
|
| 364 |
+
df_results['PF_Status'] = 'N/A'
|
| 365 |
+
anomaly_count = 0
|
| 366 |
+
|
| 367 |
total_records = len(df_results)
|
| 368 |
|
|
|
|
| 369 |
cleaned_raw_cols = [c.strip().replace(' ', '_').replace('(', '').replace(')', '').replace('.', '_') for c in df_raw.columns]
|
| 370 |
if 'Usage_kWh' in cleaned_raw_cols:
|
| 371 |
target_col = df_results['Usage_kWh'] if 'Usage_kWh' in df_results.columns else df_results['Predicted_Usage_kWh']
|
|
|
|
| 376 |
max_kwh = float(target_col.max())
|
| 377 |
total_co2 = float((target_col * 0.00042).sum())
|
| 378 |
|
| 379 |
+
# Mitigation simulation (10% drop on peak intervals > mean)
|
| 380 |
mitigated_kwh = target_col.copy()
|
| 381 |
mitigated_kwh[mitigated_kwh > mean_kwh] *= 0.90
|
| 382 |
mitigated_co2 = float(total_co2 - (mitigated_kwh * 0.00042).sum())
|
|
|
|
| 386 |
'mean_kwh': mean_kwh,
|
| 387 |
'max_kwh': max_kwh,
|
| 388 |
'total_co2': total_co2,
|
| 389 |
+
'mitigated_co2': mitigated_co2,
|
| 390 |
+
'anomaly_count': anomaly_count
|
| 391 |
}
|
| 392 |
|
| 393 |
pdf_path = generate_pdf_report(df_results, summary_stats)
|
|
|
|
| 395 |
summary_text = f"""
|
| 396 |
### 📊 Analysis Summary
|
| 397 |
* **Total Processed Records:** {total_records:,}
|
| 398 |
+
* **Low PF Anomaly Alerts (<85%):** {anomaly_count:,} records
|
| 399 |
* **Average Demand:** {mean_kwh:.2f} kWh
|
| 400 |
* **Peak Demand Recorded:** {max_kwh:.2f} kWh
|
| 401 |
* **Total Estimated Carbon Footprint:** {total_co2:.4f} Metric Tonnes
|
|
|
|
| 405 |
return df_results, summary_text, pdf_path
|
| 406 |
|
| 407 |
# ---------------------------------------------------------------------------
|
| 408 |
+
# 6. SLEEK & GLOSSY GRADIO UI LAYOUT
|
| 409 |
# ---------------------------------------------------------------------------
|
| 410 |
custom_css = """
|
| 411 |
body, .gradio-container {
|
|
|
|
| 487 |
|
| 488 |
with gr.Row():
|
| 489 |
with gr.Column(scale=2):
|
| 490 |
+
output_dataframe = gr.Dataframe(label="Predictions & Anomaly Status Preview", interactive=False)
|
| 491 |
with gr.Column(scale=1):
|
| 492 |
summary_output = gr.Markdown(value="*Upload a CSV to view analysis summary.*")
|
| 493 |
pdf_download = gr.File(label="📄 Download PDF Audit Report")
|
|
|
|
| 495 |
# TAB 2: SINGLE PREDICTION RESULT
|
| 496 |
with gr.TabItem("🎯 Single Record Result"):
|
| 497 |
with gr.Group(elem_classes=["glass-panel"]):
|
| 498 |
+
gr.Markdown("### Single Inspection Diagnostic Result")
|
| 499 |
out_kwh = gr.Textbox(label="Predicted Energy Consumption (Usage_kWh)", interactive=False)
|
| 500 |
out_co2 = gr.Textbox(label="Estimated Carbon Emissions (Tonnes CO2)", interactive=False)
|
| 501 |
+
out_pf_status = gr.Textbox(label="⚡ Power Factor Diagnostic & Surcharge Alert", interactive=False)
|
| 502 |
|
|
|
|
| 503 |
single_inputs = [lag_kvarh, lead_kvarh, lag_pf, lead_pf, nsm, hour, month, day_of_week, load_type]
|
| 504 |
+
single_outputs = [out_kwh, out_co2, out_pf_status]
|
| 505 |
|
| 506 |
+
# Button event
|
| 507 |
predict_btn.click(
|
| 508 |
fn=handle_single_prediction,
|
| 509 |
inputs=single_inputs,
|
| 510 |
outputs=single_outputs
|
| 511 |
)
|
| 512 |
|
| 513 |
+
# Real-time change event listeners
|
| 514 |
for inp in single_inputs:
|
| 515 |
inp.change(
|
| 516 |
fn=handle_single_prediction,
|
|
|
|
| 518 |
outputs=single_outputs
|
| 519 |
)
|
| 520 |
|
| 521 |
+
# Initial load trigger (prevents blank textboxes on startup)
|
| 522 |
demo.load(
|
| 523 |
fn=handle_single_prediction,
|
| 524 |
inputs=single_inputs,
|
| 525 |
outputs=single_outputs
|
| 526 |
)
|
| 527 |
|
| 528 |
+
# Batch CSV process button
|
| 529 |
process_csv_btn.click(
|
| 530 |
fn=handle_csv_upload,
|
| 531 |
inputs=[file_input],
|