import os import io import datetime import joblib import pandas as pd import numpy as np import gradio as gr from sklearn.ensemble import RandomForestRegressor from sklearn.preprocessing import StandardScaler from reportlab.lib.pagesizes import letter from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib import colors # --------------------------------------------------------------------------- # 1. OPTIONAL DATABASE CONFIGURATION (Safe MySQL / FreeDatabase Connector) # --------------------------------------------------------------------------- DB_HOST = os.getenv("MYSQL_HOST", "sql6.freedatabase.com") DB_USER = os.getenv("MYSQL_USER", "your_username") DB_PASS = os.getenv("MYSQL_PASSWORD", "your_password") DB_NAME = os.getenv("MYSQL_DB", "your_dbname") DB_PORT = int(os.getenv("MYSQL_PORT", 3306)) def get_db_connection(): try: import mysql.connector conn = mysql.connector.connect( host=DB_HOST, user=DB_USER, password=DB_PASS, database=DB_NAME, port=DB_PORT, connect_timeout=3 ) return conn except Exception: return None def init_db(): conn = get_db_connection() if conn: try: cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS energy_predictions ( id INT AUTO_INCREMENT PRIMARY KEY, prediction_type VARCHAR(20), avg_usage_kwh FLOAT, total_co2_tonnes FLOAT, timestamp DATETIME ) """) conn.commit() cursor.close() conn.close() except Exception as e: print(f"[DB Warning] {e}") init_db() # --------------------------------------------------------------------------- # 2. MODEL AUTO-TRAINING & ARTIFACT LOADING PIPELINE # --------------------------------------------------------------------------- MODEL_PATH = 'steel_model.joblib' SCALER_PATH = 'scaler.joblib' COLUMNS_PATH = 'model_columns.joblib' feature_columns = [ 'Lagging_Current_Reactive_Power_kVarh', 'Leading_Current_Reactive_Power_kVarh', 'Lagging_Current_Power_Factor', 'Leading_Current_Power_Factor', 'NSM', 'Hour', 'Month', 'Reactive_Power_Ratio', 'Power_Factor_Ratio', 'WeekStatus_Weekend', 'Day_of_week_Monday', 'Day_of_week_Saturday', 'Day_of_week_Sunday', 'Day_of_week_Thursday', 'Day_of_week_Tuesday', 'Day_of_week_Wednesday', 'Load_Type_Maximum_Load', 'Load_Type_Medium_Load' ] def clean_cols(df_input): df = df_input.copy() df.columns = ( df.columns.str.strip() .str.replace(' ', '_') .str.replace('(', '', regex=False) .str.replace(')', '', regex=False) .str.replace('.', '_', regex=False) ) return df def build_or_load_artifacts(): if os.path.exists(MODEL_PATH) and os.path.exists(SCALER_PATH) and os.path.exists(COLUMNS_PATH): try: m = joblib.load(MODEL_PATH) s = joblib.load(SCALER_PATH) c = joblib.load(COLUMNS_PATH) return m, s, c except Exception: pass csv_file = "Steel_industry_data.csv" if os.path.exists(csv_file): df_train = pd.read_csv(csv_file) df_train = clean_cols(df_train) else: # Generate realistic steel plant dataset matching UCI bounds np.random.seed(42) n = 5000 lag_kvarh = np.random.exponential(13.0, n) lead_kvarh = np.random.exponential(3.8, n) lag_pf = np.random.uniform(50, 100, n) lead_pf = np.random.uniform(50, 100, n) nsm = np.random.uniform(0, 86400, n) hour = (nsm // 3600).astype(int) month = np.random.randint(1, 13, n) dow = np.random.choice(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], n) load = np.random.choice(['Light_Load', 'Medium_Load', 'Maximum_Load'], n) usage_kwh = ( 2.5 + 1.2 * lag_kvarh + 0.12 * lead_kvarh + 0.15 * lag_pf + np.where(load == 'Maximum_Load', 28.0, np.where(load == 'Medium_Load', 14.0, 0.0)) + np.random.normal(0, 1.2, n) ) usage_kwh = np.maximum(usage_kwh, 0.0) df_train = pd.DataFrame({ 'Lagging_Current_Reactive_Power_kVarh': lag_kvarh, 'Leading_Current_Reactive_Power_kVarh': lead_kvarh, 'Lagging_Current_Power_Factor': lag_pf, 'Leading_Current_Power_Factor': lead_pf, 'NSM': nsm, 'Hour': hour, 'Month': month, 'Day_of_week': dow, 'Load_Type': load, 'Usage_kWh': usage_kwh }) eps = 1e-5 df_train['Reactive_Power_Ratio'] = df_train['Lagging_Current_Reactive_Power_kVarh'] / (df_train['Leading_Current_Reactive_Power_kVarh'] + eps) df_train['Power_Factor_Ratio'] = df_train['Lagging_Current_Power_Factor'] / (df_train['Leading_Current_Power_Factor'] + eps) df_train['WeekStatus'] = np.where(df_train['Day_of_week'].isin(['Saturday', 'Sunday']), 'Weekend', 'Weekday') df_enc = pd.get_dummies(df_train, drop_first=True) X_proc = pd.DataFrame(0, index=df_train.index, columns=feature_columns) for col in feature_columns: if col in df_enc.columns: X_proc[col] = df_enc[col] scaler = StandardScaler() X_scaled = scaler.fit_transform(X_proc) model = RandomForestRegressor(n_estimators=50, max_depth=12, random_state=42) model.fit(X_scaled, df_train['Usage_kWh']) joblib.dump(model, MODEL_PATH) joblib.dump(scaler, SCALER_PATH) joblib.dump(feature_columns, COLUMNS_PATH) return model, scaler, feature_columns model, scaler, feature_columns = build_or_load_artifacts() # --------------------------------------------------------------------------- # 3. FEATURE ENGINEERING & PREDICTION PIPELINE # --------------------------------------------------------------------------- def preprocess_and_predict(df_raw): df = clean_cols(df_raw) if 'date' in df.columns: df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y %H:%M', errors='coerce') df['Hour'] = df['date'].dt.hour.fillna(12).astype(int) df['Month'] = df['date'].dt.month.fillna(1).astype(int) else: if 'Hour' not in df.columns: df['Hour'] = 12 if 'Month' not in df.columns: df['Month'] = 1 eps = 1e-5 df['Reactive_Power_Ratio'] = ( df['Lagging_Current_Reactive_Power_kVarh'] / (df['Leading_Current_Reactive_Power_kVarh'] + eps) ) df['Power_Factor_Ratio'] = ( df['Lagging_Current_Power_Factor'] / (df['Leading_Current_Power_Factor'] + eps) ) if 'Day_of_week' in df.columns: df['WeekStatus'] = np.where(df['Day_of_week'].isin(['Saturday', 'Sunday']), 'Weekend', 'Weekday') else: df['WeekStatus'] = 'Weekday' df['Day_of_week'] = 'Monday' df_encoded = pd.get_dummies(df, drop_first=True) X_processed = pd.DataFrame(0, index=df.index, columns=feature_columns) for col in feature_columns: if col in df_encoded.columns: X_processed[col] = df_encoded[col] X_scaled = scaler.transform(X_processed) predictions = model.predict(X_scaled) df['Predicted_Usage_kWh'] = np.round(predictions, 4) df['Estimated_CO2_Tonnes'] = np.round(predictions * 0.00042, 6) return df # --------------------------------------------------------------------------- # 4. PDF REPORT GENERATION ENGINE # --------------------------------------------------------------------------- def generate_pdf_report(df_results, summary_stats): pdf_buffer = io.BytesIO() doc = SimpleDocTemplate(pdf_buffer, pagesize=letter, rightMargin=36, leftMargin=36, topMargin=36, bottomMargin=36) styles = getSampleStyleSheet() title_style = ParagraphStyle( 'DocTitle', parent=styles['Heading1'], fontName='Helvetica-Bold', fontSize=18, textColor=colors.HexColor('#0f172a'), spaceAfter=12 ) subtitle_style = ParagraphStyle( 'DocSubTitle', parent=styles['Normal'], fontName='Helvetica', fontSize=10, textColor=colors.HexColor('#64748b'), spaceAfter=18 ) elements = [ Paragraph("Steel Plant Energy & CO2 Audit Report", title_style), Paragraph(f"Generated on: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} | Hugging Face Spaces Engine", subtitle_style) ] summary_data = [ ["Metric Description", "Value"], ["Total Processed Records", f"{summary_stats['total_records']}"], ["Low Power Factor (<85%) Anomaly Flagged", f"{summary_stats['anomaly_count']} records"], ["Mean Electricity Demand (kWh)", f"{summary_stats['mean_kwh']:.2f} kWh"], ["Max Electricity Demand (kWh)", f"{summary_stats['max_kwh']:.2f} kWh"], ["Total Estimated CO2 Footprint", f"{summary_stats['total_co2']:.4f} Metric Tonnes"], ["Mitigated Potential CO2 Saved (10% Peak Drop)", f"{summary_stats['mitigated_co2']:.4f} Metric Tonnes"] ] st = Table(summary_data, colWidths=[280, 220]) st.setStyle(TableStyle([ ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#1e293b')), ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke), ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'), ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#cbd5e1')), ('TOPPADDING', (0, 0), (-1, -1), 6), ('BOTTOMPADDING', (0, 0), (-1, -1), 6), ])) elements.append(st) elements.append(Spacer(1, 15)) elements.append(Paragraph("Sample Predictions Preview (First 10 Rows)", styles['Heading2'])) cols_to_show = ['Predicted_Usage_kWh', 'Estimated_CO2_Tonnes'] if 'PF_Status' in df_results.columns: cols_to_show.append('PF_Status') preview_df = df_results[cols_to_show].head(10).reset_index() preview_data = [["Row ID", "Predicted Usage (kWh)", "Estimated CO2 (Tonnes)", "PF Status"]] for _, row in preview_df.iterrows(): pf_st = row.get('PF_Status', 'N/A') preview_data.append([ str(int(row['index'] + 1)), f"{row['Predicted_Usage_kWh']:.2f}", f"{row['Estimated_CO2_Tonnes']:.6f}", str(pf_st) ]) pt = Table(preview_data, colWidths=[50, 150, 150, 150]) pt.setStyle(TableStyle([ ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#0284c7')), ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke), ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'), ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#e2e8f0')), ('TOPPADDING', (0, 0), (-1, -1), 5), ('BOTTOMPADDING', (0, 0), (-1, -1), 5), ])) elements.append(pt) doc.build(elements) pdf_buffer.seek(0) pdf_path = "/tmp/energy_prediction_report.pdf" with open(pdf_path, "wb") as f: f.write(pdf_buffer.read()) return pdf_path # --------------------------------------------------------------------------- # 5. GRADIO UI HANDLERS # --------------------------------------------------------------------------- def handle_single_prediction(lag_kvarh, lead_kvarh, lag_pf, lead_pf, nsm, hour, month, day_of_week, load_type): data = { 'Lagging_Current_Reactive_Power_kVarh': [lag_kvarh], 'Leading_Current_Reactive_Power_kVarh': [lead_kvarh], 'Lagging_Current_Power_Factor': [lag_pf], 'Leading_Current_Power_Factor': [lead_pf], 'NSM': [nsm], 'Hour': [hour], 'Month': [month], 'Day_of_week': [day_of_week], 'Load_Type': [load_type] } df_single = pd.DataFrame(data) res = preprocess_and_predict(df_single) kwh = float(res['Predicted_Usage_kWh'].iloc[0]) co2 = float(res['Estimated_CO2_Tonnes'].iloc[0]) # Low Power Factor (< 85%) Anomaly Diagnostic Output if lag_pf < 85.0: pf_status = f"⚠️ CRITICAL: Low Power Factor ({lag_pf:.1f}% < 85%). Risk of transformer heat loss & utility surcharge penalties!" else: pf_status = f"✅ OPTIMAL: Power Factor ({lag_pf:.1f}% ≥ 85%). Operating within safe efficiency limits." # Optional DB record insertion conn = get_db_connection() if conn: try: cursor = conn.cursor() cursor.execute( "INSERT INTO energy_predictions (prediction_type, avg_usage_kwh, total_co2_tonnes, timestamp) VALUES (%s, %s, %s, %s)", ('SINGLE', kwh, co2, datetime.datetime.now()) ) conn.commit() cursor.close() conn.close() except Exception: pass return f"{kwh:.2f} kWh", f"{co2:.6f} Metric Tonnes", pf_status def handle_csv_upload(file): if file is None: return None, "Please upload a valid CSV file.", None df_raw = pd.read_csv(file.name) df_results = preprocess_and_predict(df_raw) # Tag Anomaly Status Column on Batch CSV Dataframe if 'Lagging_Current_Power_Factor' in df_results.columns: df_results['PF_Status'] = np.where( df_results['Lagging_Current_Power_Factor'] < 85.0, '⚠️ CRITICAL (<85%)', '✅ NORMAL' ) anomaly_count = int((df_results['Lagging_Current_Power_Factor'] < 85.0).sum()) else: df_results['PF_Status'] = 'N/A' anomaly_count = 0 total_records = len(df_results) cleaned_raw_cols = [c.strip().replace(' ', '_').replace('(', '').replace(')', '').replace('.', '_') for c in df_raw.columns] if 'Usage_kWh' in cleaned_raw_cols: target_col = df_results['Usage_kWh'] if 'Usage_kWh' in df_results.columns else df_results['Predicted_Usage_kWh'] else: target_col = df_results['Predicted_Usage_kWh'] mean_kwh = float(target_col.mean()) max_kwh = float(target_col.max()) total_co2 = float((target_col * 0.00042).sum()) # Mitigation simulation (10% drop on peak intervals > mean) mitigated_kwh = target_col.copy() mitigated_kwh[mitigated_kwh > mean_kwh] *= 0.90 mitigated_co2 = float(total_co2 - (mitigated_kwh * 0.00042).sum()) summary_stats = { 'total_records': total_records, 'mean_kwh': mean_kwh, 'max_kwh': max_kwh, 'total_co2': total_co2, 'mitigated_co2': mitigated_co2, 'anomaly_count': anomaly_count } pdf_path = generate_pdf_report(df_results, summary_stats) summary_text = f""" ### 📊 Analysis Summary * **Total Processed Records:** {total_records:,} * **Low PF Anomaly Alerts (<85%):** {anomaly_count:,} records * **Average Demand:** {mean_kwh:.2f} kWh * **Peak Demand Recorded:** {max_kwh:.2f} kWh * **Total Estimated Carbon Footprint:** {total_co2:.4f} Metric Tonnes * **Potential CO2 Saved (via Peak Factor Mitigation):** {mitigated_co2:.4f} Metric Tonnes """ return df_results, summary_text, pdf_path # --------------------------------------------------------------------------- # 6. SLEEK & GLOSSY GRADIO UI LAYOUT (REFACTORED FOR CLEAR SEPARATION) # --------------------------------------------------------------------------- custom_css = """ body, .gradio-container { background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%) !important; font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important; color: #f8fafc !important; } .glass-panel { background: rgba(30, 41, 59, 0.7) !important; backdrop-filter: blur(12px) !important; border: 1px solid rgba(255, 255, 255, 0.1) !important; border-radius: 16px !important; padding: 20px !important; margin-bottom: 20px !important; } .btn-primary { background: linear-gradient(90deg, #0284c7 0%, #0369a1 100%) !important; border: none !important; color: #ffffff !important; font-weight: 600 !important; border-radius: 10px !important; box-shadow: 0 4px 14px 0 rgba(2, 132, 199, 0.39) !important; } """ with gr.Blocks(css=custom_css, title="Steel Industry Energy Predictor") as demo: gr.Markdown( """ # ⚡ Steel Industry Power Consumption & Carbon Footprint Portal ### *Sleek AI-Powered Energy Demand & Environmental Mitigation Analytics* """ ) with gr.Tabs(): # TAB 1: CSV BATCH ANALYSIS & PDF EXPORT with gr.TabItem("📂 Batch CSV Upload & PDF Audit"): with gr.Group(elem_classes=["glass-panel"]): gr.Markdown("### Upload Steel Industry Data CSV for Dataset Analysis") gr.Markdown("Upload `Steel_industry_data.csv` to process thousands of historical plant records and export an audit PDF.") file_input = gr.File(label="Upload CSV File (`Steel_industry_data.csv`)", file_types=[".csv"]) process_csv_btn = gr.Button("⚡ Analyze CSV Data & Generate PDF Report", elem_classes=["btn-primary"]) with gr.Row(): with gr.Column(scale=2): output_dataframe = gr.Dataframe(label="Predictions & Anomaly Status Preview", interactive=False) with gr.Column(scale=1): summary_output = gr.Markdown(value="*Upload a CSV above to view dataset summary.*") pdf_download = gr.File(label="📄 Download PDF Audit Report") # TAB 2: SINGLE RECORD SIMULATOR & DIAGNOSTICS with gr.TabItem("🎯 Single Record Simulator & Diagnostic"): gr.Markdown("### Single Inspection Simulator") gr.Markdown("Adjust sliders and parameters below to simulate power usage for a single specific timestamp/operational condition.") with gr.Row(): # LEFT COLUMN: INPUT CONTROLS with gr.Column(scale=1, elem_classes=["glass-panel"]): gr.Markdown("#### ⚙️ Input Parameters") lag_kvarh = gr.Number(label="Lagging Reactive Power (kVarh)", value=12.5) lead_kvarh = gr.Number(label="Leading Reactive Power (kVarh)", value=2.1) lag_pf = gr.Slider(minimum=0, maximum=100, label="Lagging Power Factor (%)", value=85.0) lead_pf = gr.Slider(minimum=0, maximum=100, label="Leading Power Factor (%)", value=95.0) nsm = gr.Number(label="Number of Seconds from Midnight (NSM)", value=36000) hour = gr.Slider(minimum=0, maximum=23, step=1, label="Hour of Day", value=10) month = gr.Slider(minimum=1, maximum=12, step=1, label="Month of Year", value=6) day_of_week = gr.Dropdown( choices=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], value='Monday', label="Day of Week" ) load_type = gr.Dropdown( choices=['Light_Load', 'Medium_Load', 'Maximum_Load'], value='Medium_Load', label="Load Type Category" ) predict_btn = gr.Button("🚀 Predict Single Record", elem_classes=["btn-primary"]) # RIGHT COLUMN: PREDICTION OUTPUTS with gr.Column(scale=1, elem_classes=["glass-panel"]): gr.Markdown("#### 📊 Diagnostic Results") out_kwh = gr.Textbox(label="Predicted Energy Consumption (Usage_kWh)", interactive=False) out_co2 = gr.Textbox(label="Estimated Carbon Emissions (Tonnes CO2)", interactive=False) out_pf_status = gr.Textbox(label="⚡ Power Factor Diagnostic & Surcharge Alert", interactive=False) single_inputs = [lag_kvarh, lead_kvarh, lag_pf, lead_pf, nsm, hour, month, day_of_week, load_type] single_outputs = [out_kwh, out_co2, out_pf_status] # Single Prediction Event Listeners predict_btn.click( fn=handle_single_prediction, inputs=single_inputs, outputs=single_outputs ) for inp in single_inputs: inp.change( fn=handle_single_prediction, inputs=single_inputs, outputs=single_outputs ) demo.load( fn=handle_single_prediction, inputs=single_inputs, outputs=single_outputs ) # Batch CSV Event Listener process_csv_btn.click( fn=handle_csv_upload, inputs=[file_input], outputs=[output_dataframe, summary_output, pdf_download] ) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860)