import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from datetime import date, timedelta
import sqlite3
from io import BytesIO
import base64
# Database Setup (in-memory for demo; use PostgreSQL for prod)
@st.cache_resource
def init_db():
conn = sqlite3.connect(':memory:')
# Model Inventory
conn.execute('''
CREATE TABLE models (
id INTEGER PRIMARY KEY, name TEXT, owner TEXT, version TEXT, risk_tier TEXT,
data_sources TEXT, deploy_date DATE, status TEXT
)''')
# Sample data
sample_models = pd.DataFrame({
'name': ['FraudDetect v2', 'ForecastPro', 'ChatCFO'],
'owner': ['Risk Team', 'FP&A', 'Exec'],
'version': ['2.1', '1.0', 'beta'],
'risk_tier': ['High', 'Med', 'Low'],
'data_sources': ['TXN DB', 'ERP', 'Emails'],
'deploy_date': ['2024-01-15', '2024-03-01', '2024-06-01'],
'status': ['Active', 'Active', 'Testing']
})
sample_models.to_sql('models', conn, index=False)
# Control Tests
conn.execute('''
CREATE TABLE tests (
id INTEGER PRIMARY KEY, model_id INTEGER, test_date DATE, control_type TEXT,
pass_fail TEXT, evidence TEXT
)''')
# Exceptions
conn.execute('''
CREATE TABLE exceptions (
id INTEGER PRIMARY KEY, model_id INTEGER, desc TEXT, priority TEXT,
status TEXT, triage_date DATE
)''')
return conn
conn = init_db()
# Sidebar Navigation
st.sidebar.title("CFO AI Guardrails")
page = st.sidebar.selectbox("Navigate", [
"Model Inventory", "Control Testing", "Exception Triage",
"Regulator Summaries", "Investor Comms", "Enablement Toolkit"
])
# Page 1: Model Inventory
if page == "Model Inventory":
st.header("🗂️ Model Inventory")
models = pd.read_sql("SELECT * FROM models", conn)
st.dataframe(models, use_container_width=True)
# Add Model
with st.expander("➕ Add New Model"):
col1, col2, col3 = st.columns(3)
new_name = col1.text_input("Name")
new_owner = col2.text_input("Owner")
new_risk = col3.selectbox("Risk Tier", ['Low', 'Med', 'High'])
if st.button("Add"):
conn.execute("INSERT INTO models (name, owner, risk_tier, deploy_date, status) VALUES (?, ?, ?, ?, ?)",
(new_name, new_owner, new_risk, date.today(), 'Active'))
st.success("Added!")
st.rerun()
# Risk Heatmap
fig = px.scatter(models, x='deploy_date', y='name', color='risk_tier', size='owner',
title="Model Risk Overview", hover_data=['data_sources'])
st.plotly_chart(fig)
# Page 2: Control Testing Cadence
elif page == "Control Testing":
st.header("🔄 Control Testing Cadence")
tests = pd.read_sql("SELECT * FROM tests", conn)
if tests.empty:
st.info("Add tests via inventory or manually.")
# Calendar-like Gantt
fig = go.Figure()
fig.add_trace(go.Bar(name="Tests Due", x=tests['test_date'], y=tests['control_type'], orientation='h'))
st.plotly_chart(fig)
# Compliance Gauge
compliance_score = 85 # Mock calc: (pass / total) * 100
fig_gauge = go.Figure(go.Indicator(
mode="gauge+number", value=compliance_score, domain={'x': [0, 1], 'y': [0, 1]},
title={'text': "Compliance %"}, gauge={'axis': {'range': [None, 100]}}
))
st.plotly_chart(fig_gauge)
# Page 3: Exception Triage
elif page == "Exception Triage":
st.header("🚨 Exception Triage")
ex_df = pd.read_sql("SELECT * FROM exceptions", conn)
st.dataframe(ex_df)
# Kanban
col1, col2, col3 = st.columns(3)
with col1:
st.subheader("New")
# Filter logic here
with col2:
st.subheader("In Progress")
with col3:
st.subheader("Resolved")
# Page 4: Regulator-Ready Summaries
elif page == "Regulator Summaries":
st.header("📋 Regulator-Ready Summaries")
if st.button("Generate Risk Register PDF"):
# Mock PDF gen (use reportlab in prod)
st.download_button("Download PDF", data=b"Mock PDF Content", file_name="ai_risk_register.pdf")
# Page 5: Investor Communications
elif page == "Investor Comms":
st.header("💼 Investor Communications")
st.info("Template: 'Our AI controls cover 100% high-risk models with quarterly audits.'")
st.download_button("Download Slide Deck", data=b"Mock PPTX", file_name="AI_Guardrails.pptx")
# Page 6: Enablement Toolkit
elif page == "Enablement Toolkit":
st.header("🧰 Enablement Toolkit")
st.markdown("""
- [Guardrail Checklist](https://example.com/checklist.pdf)
- [Policy Template](https://example.com/policy.docx)
- Training: Embed video/IPython notebook here.
""")
# Footer
st.sidebar.markdown("---")
st.sidebar.info("Built by Sherlock | Trailblazer Labs")