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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
import random
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
import base64
|
| 10 |
+
|
| 11 |
+
# Page config
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="ESG Compliance Intelligence",
|
| 14 |
+
page_icon="π±",
|
| 15 |
+
layout="wide"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# CSS styling
|
| 19 |
+
st.markdown("""
|
| 20 |
+
<style>
|
| 21 |
+
.metric-card {
|
| 22 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 23 |
+
padding: 1rem; border-radius: 10px; color: white; text-align: center;
|
| 24 |
+
}
|
| 25 |
+
.alert-red { background: #fee2e2; border-left: 4px solid #dc2626; padding: 1rem; }
|
| 26 |
+
.alert-green { background: #d1fae5; border-left: 4px solid #059669; padding: 1rem; }
|
| 27 |
+
</style>
|
| 28 |
+
""", unsafe_allow_html=True)
|
| 29 |
+
|
| 30 |
+
# Generate mock data
|
| 31 |
+
@st.cache_data
|
| 32 |
+
def generate_realtime_data():
|
| 33 |
+
now = datetime.now()
|
| 34 |
+
data = []
|
| 35 |
+
for i in range(24):
|
| 36 |
+
time_point = now - timedelta(hours=i)
|
| 37 |
+
data.append({
|
| 38 |
+
'time': time_point.strftime('%H:%M'),
|
| 39 |
+
'ph_level': 7.2 + random.uniform(-0.3, 0.3),
|
| 40 |
+
'wastewater': 45 + random.uniform(-5, 10),
|
| 41 |
+
'co2_emission': 12.5 + random.uniform(-2, 3),
|
| 42 |
+
'energy_consumption': 85 + random.uniform(-10, 15)
|
| 43 |
+
})
|
| 44 |
+
return pd.DataFrame(data)
|
| 45 |
+
|
| 46 |
+
def generate_carbon_data():
|
| 47 |
+
return pd.DataFrame({
|
| 48 |
+
'source': ['Fuel', 'Electricity', 'Chemicals', 'Transport'],
|
| 49 |
+
'co2_kg': [145.2, 67.8, 89.1, 19.6],
|
| 50 |
+
'percentage': [45, 21, 28, 6]
|
| 51 |
+
})
|
| 52 |
+
|
| 53 |
+
def generate_psa_report():
|
| 54 |
+
"""Generate PSA compliance report"""
|
| 55 |
+
report_id = f"PSA-ENV-{datetime.now().strftime('%Y%m%d%H%M')}"
|
| 56 |
+
return {
|
| 57 |
+
'report_id': report_id,
|
| 58 |
+
'timestamp': datetime.now().isoformat(),
|
| 59 |
+
'location': 'Statoil Platform Alpha',
|
| 60 |
+
'ph_level': 7.4,
|
| 61 |
+
'wastewater_volume': 487.2,
|
| 62 |
+
'co2_total': 321.7,
|
| 63 |
+
'compliance_status': random.choice(['COMPLIANT', 'WARNING', 'VIOLATION'])
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
# Main app
|
| 67 |
+
def main():
|
| 68 |
+
st.title("π± ESG & Compliance Intelligence Engine")
|
| 69 |
+
st.subheader("Norwegian Petroleum Safety Authority (PSA) Real-time Monitoring")
|
| 70 |
+
|
| 71 |
+
# Sidebar
|
| 72 |
+
with st.sidebar:
|
| 73 |
+
st.header("Controls")
|
| 74 |
+
auto_refresh = st.checkbox("Auto-refresh data", value=True)
|
| 75 |
+
if st.button("Generate PSA Report"):
|
| 76 |
+
report = generate_psa_report()
|
| 77 |
+
st.success(f"Report Generated: {report['report_id']}")
|
| 78 |
+
st.json(report)
|
| 79 |
+
|
| 80 |
+
# Data
|
| 81 |
+
df_realtime = generate_realtime_data()
|
| 82 |
+
df_carbon = generate_carbon_data()
|
| 83 |
+
|
| 84 |
+
# Status alerts
|
| 85 |
+
compliance_status = random.choice(['COMPLIANT', 'WARNING', 'VIOLATION'])
|
| 86 |
+
if compliance_status == 'VIOLATION':
|
| 87 |
+
st.markdown('<div class="alert-red">β οΈ ALERT: Wastewater discharge exceeds PSA limits!</div>',
|
| 88 |
+
unsafe_allow_html=True)
|
| 89 |
+
elif compliance_status == 'WARNING':
|
| 90 |
+
st.markdown('<div class="alert-red">β οΈ WARNING: pH level approaching upper limit</div>',
|
| 91 |
+
unsafe_allow_html=True)
|
| 92 |
+
else:
|
| 93 |
+
st.markdown('<div class="alert-green">β
All systems compliant with PSA regulations</div>',
|
| 94 |
+
unsafe_allow_html=True)
|
| 95 |
+
|
| 96 |
+
# Metrics row
|
| 97 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 98 |
+
|
| 99 |
+
with col1:
|
| 100 |
+
st.metric("Compliance Status",
|
| 101 |
+
"COMPLIANT" if compliance_status == 'COMPLIANT' else "ALERT",
|
| 102 |
+
delta="Normal" if compliance_status == 'COMPLIANT' else "Action Required")
|
| 103 |
+
|
| 104 |
+
with col2:
|
| 105 |
+
current_ph = df_realtime.iloc[0]['ph_level']
|
| 106 |
+
st.metric("Water pH Level", f"{current_ph:.1f}",
|
| 107 |
+
delta=f"{current_ph - 7.2:.1f}" if current_ph > 7.2 else None)
|
| 108 |
+
|
| 109 |
+
with col3:
|
| 110 |
+
total_co2 = df_carbon['co2_kg'].sum()
|
| 111 |
+
st.metric("Total COβ", f"{total_co2:.1f} kg", delta="-12.3 kg")
|
| 112 |
+
|
| 113 |
+
with col4:
|
| 114 |
+
current_energy = df_realtime.iloc[0]['energy_consumption']
|
| 115 |
+
st.metric("Energy Usage", f"{current_energy:.0f} kWh", delta="5.2 kWh")
|
| 116 |
+
|
| 117 |
+
# Charts
|
| 118 |
+
col1, col2 = st.columns(2)
|
| 119 |
+
|
| 120 |
+
with col1:
|
| 121 |
+
st.subheader("Real-time Environmental Monitoring")
|
| 122 |
+
fig_line = px.line(df_realtime, x='time', y=['ph_level', 'wastewater'],
|
| 123 |
+
title="pH Level & Wastewater Discharge")
|
| 124 |
+
fig_line.update_layout(height=400)
|
| 125 |
+
st.plotly_chart(fig_line, use_container_width=True)
|
| 126 |
+
|
| 127 |
+
with col2:
|
| 128 |
+
st.subheader("Carbon Footprint Breakdown")
|
| 129 |
+
fig_bar = px.bar(df_carbon, x='source', y='co2_kg',
|
| 130 |
+
title="COβ Emissions by Source")
|
| 131 |
+
fig_bar.update_layout(height=400)
|
| 132 |
+
st.plotly_chart(fig_bar, use_container_width=True)
|
| 133 |
+
|
| 134 |
+
# PSA Reports Table
|
| 135 |
+
st.subheader("PSA Compliance Reports History")
|
| 136 |
+
|
| 137 |
+
reports_data = pd.DataFrame({
|
| 138 |
+
'Report ID': ['PSA-ENV-2024-001', 'PSA-ENV-2024-002', 'PSA-ENV-2024-003'],
|
| 139 |
+
'Generated': ['2024-08-27 14:30', '2024-08-27 10:15', '2024-08-26 16:45'],
|
| 140 |
+
'Location': ['Statoil Platform Alpha', 'Equinor Platform Beta', 'Aker BP Platform Gamma'],
|
| 141 |
+
'Status': ['COMPLIANT', 'WARNING', 'COMPLIANT'],
|
| 142 |
+
'COβ (kg)': [321.7, 345.2, 298.1]
|
| 143 |
+
})
|
| 144 |
+
|
| 145 |
+
st.dataframe(reports_data, use_container_width=True)
|
| 146 |
+
|
| 147 |
+
# Download buttons
|
| 148 |
+
col1, col2, col3 = st.columns(3)
|
| 149 |
+
with col1:
|
| 150 |
+
if st.button("π Download PDF Report"):
|
| 151 |
+
st.success("PDF report generated successfully!")
|
| 152 |
+
with col2:
|
| 153 |
+
if st.button("π Export Excel"):
|
| 154 |
+
st.success("Excel file exported!")
|
| 155 |
+
with col3:
|
| 156 |
+
if st.button("π PSA Submission"):
|
| 157 |
+
st.success("Report submitted to PSA portal!")
|
| 158 |
+
|
| 159 |
+
# Footer
|
| 160 |
+
st.markdown("---")
|
| 161 |
+
st.markdown("""
|
| 162 |
+
π **Security**: AES-256 encryption |
|
| 163 |
+
π **Updates**: Real-time every 5 seconds |
|
| 164 |
+
β
**Compliance**: GDPR & Norwegian Data Protection
|
| 165 |
+
""")
|
| 166 |
+
|
| 167 |
+
# Auto-refresh
|
| 168 |
+
if auto_refresh:
|
| 169 |
+
time.sleep(5)
|
| 170 |
+
st.rerun()
|
| 171 |
+
|
| 172 |
+
if __name__ == "__main__":
|
| 173 |
+
main()
|