Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +39 -81
src/streamlit_app.py
CHANGED
|
@@ -2,18 +2,15 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import google.generativeai as genai
|
| 5 |
-
import json, re
|
| 6 |
-
import os
|
| 7 |
-
from datetime import datetime
|
| 8 |
|
| 9 |
# ====== CONFIG ======
|
| 10 |
st.set_page_config(page_title="ECL Risk Analyzer", layout="wide")
|
| 11 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 12 |
|
| 13 |
-
# ======
|
| 14 |
@st.cache_data
|
| 15 |
def process_loan_data(df: pd.DataFrame):
|
| 16 |
-
"""Compute PD, LGD, EAD, and ECL by loan_intent."""
|
| 17 |
df = df.dropna(subset=["loan_intent", "credit_score", "loan_amnt", "loan_status"])
|
| 18 |
df["loan_status"] = df["loan_status"].astype(int)
|
| 19 |
group = df.groupby("loan_intent")
|
|
@@ -23,21 +20,17 @@ def process_loan_data(df: pd.DataFrame):
|
|
| 23 |
ecl_seg = pd_seg * lgd_seg * ead_seg
|
| 24 |
ecl_df = pd.concat([pd_seg, lgd_seg, ead_seg, ecl_seg], axis=1)
|
| 25 |
ecl_df.columns = ["PD", "LGD", "EAD", "ECL"]
|
| 26 |
-
|
| 27 |
-
return ecl_df
|
| 28 |
-
|
| 29 |
-
import re, json
|
| 30 |
|
| 31 |
def get_gemini_decision(segment, pd_val, lgd_val, ead_val, ecl_val):
|
| 32 |
-
"""Gemini-backed risk decision, with
|
| 33 |
model = genai.GenerativeModel("gemini-2.0-flash-lite")
|
| 34 |
|
| 35 |
system_prompt = (
|
| 36 |
"You are a financial risk advisor. "
|
| 37 |
-
"Return only JSON
|
| 38 |
-
'
|
| 39 |
)
|
| 40 |
-
|
| 41 |
user_prompt = f"""
|
| 42 |
Segment: {segment}
|
| 43 |
PD: {pd_val:.3f}
|
|
@@ -59,96 +52,61 @@ Respond with one JSON object only.
|
|
| 59 |
generation_config={"temperature": 0.1}
|
| 60 |
)
|
| 61 |
text = resp.text.strip()
|
| 62 |
-
|
| 63 |
-
# --- Strip Markdown wrappers like ```json ... ```
|
| 64 |
text = re.sub(r"^```json", "", text)
|
| 65 |
text = re.sub(r"^```", "", text)
|
| 66 |
text = re.sub(r"```$", "", text)
|
| 67 |
-
text = text.strip()
|
| 68 |
-
|
| 69 |
-
# --- Extract only JSON substring ---
|
| 70 |
match = re.search(r"\{.*\}", text, re.DOTALL)
|
| 71 |
if match:
|
| 72 |
text = match.group(0)
|
| 73 |
-
|
| 74 |
-
# --- Load and validate ---
|
| 75 |
data = json.loads(text)
|
| 76 |
-
if not isinstance(data, dict):
|
| 77 |
-
raise ValueError("Parsed non-dict JSON")
|
| 78 |
-
|
| 79 |
-
for k in ["action", "rationale", "confidence"]:
|
| 80 |
-
data.setdefault(k, None)
|
| 81 |
return data
|
| 82 |
-
|
| 83 |
except Exception as e:
|
| 84 |
-
# Log what Gemini returned for debugging
|
| 85 |
st.warning(f"⚠️ Gemini output parse failed: {e}")
|
| 86 |
st.text_area("Raw Gemini output", value=resp.text if 'resp' in locals() else "No response", height=150)
|
| 87 |
return {"action": "maintain", "rationale": "Fallback - parse failure", "confidence": 0.0}
|
| 88 |
|
| 89 |
# ====== UI ======
|
| 90 |
st.title("📊 Expected Credit Loss (ECL) Risk Dashboard")
|
| 91 |
-
st.write("Upload your **
|
| 92 |
|
| 93 |
uploaded = st.file_uploader("Upload CSV dataset", type=["csv"])
|
| 94 |
|
| 95 |
if uploaded:
|
| 96 |
df = pd.read_csv(uploaded)
|
| 97 |
-
st.success("Dataset loaded successfully.")
|
| 98 |
-
st.dataframe(df.head())
|
| 99 |
-
|
| 100 |
ecl_df = process_loan_data(df)
|
| 101 |
-
st.
|
| 102 |
st.dataframe(ecl_df, use_container_width=True, hide_index=True)
|
| 103 |
|
| 104 |
-
# ---
|
| 105 |
-
st.
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
st.
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
"
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
"ECL": row["ECL"],
|
| 135 |
-
"PD": row["PD"]
|
| 136 |
-
})
|
| 137 |
-
result_df = pd.DataFrame(decisions)
|
| 138 |
-
result_df["Timestamp"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 139 |
-
st.dataframe(result_df, use_container_width=True, hide_index=True)
|
| 140 |
-
|
| 141 |
-
# --- Plot action summary ---
|
| 142 |
-
st.subheader("Recommended Actions Distribution")
|
| 143 |
-
fig3, ax3 = plt.subplots(figsize=(6, 4))
|
| 144 |
-
action_counts = result_df["Action"].value_counts()
|
| 145 |
-
ax3.pie(action_counts, labels=action_counts.index, autopct="%1.1f%%", startangle=140)
|
| 146 |
-
ax3.set_title("Recommended Actions per Segment")
|
| 147 |
-
st.pyplot(fig3)
|
| 148 |
-
|
| 149 |
-
# Option to export report
|
| 150 |
-
csv_out = result_df.to_csv(index=False).encode("utf-8")
|
| 151 |
-
st.download_button("Download ECL + Decision Report", csv_out, "ECL_Decisions.csv", "text/csv")
|
| 152 |
|
| 153 |
else:
|
| 154 |
-
st.info("Upload
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import google.generativeai as genai
|
| 5 |
+
import json, os, re
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# ====== CONFIG ======
|
| 8 |
st.set_page_config(page_title="ECL Risk Analyzer", layout="wide")
|
| 9 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 10 |
|
| 11 |
+
# ====== HELPERS ======
|
| 12 |
@st.cache_data
|
| 13 |
def process_loan_data(df: pd.DataFrame):
|
|
|
|
| 14 |
df = df.dropna(subset=["loan_intent", "credit_score", "loan_amnt", "loan_status"])
|
| 15 |
df["loan_status"] = df["loan_status"].astype(int)
|
| 16 |
group = df.groupby("loan_intent")
|
|
|
|
| 20 |
ecl_seg = pd_seg * lgd_seg * ead_seg
|
| 21 |
ecl_df = pd.concat([pd_seg, lgd_seg, ead_seg, ecl_seg], axis=1)
|
| 22 |
ecl_df.columns = ["PD", "LGD", "EAD", "ECL"]
|
| 23 |
+
return ecl_df.reset_index()
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def get_gemini_decision(segment, pd_val, lgd_val, ead_val, ecl_val):
|
| 26 |
+
"""Gemini-backed risk decision, single-segment call with robust parsing."""
|
| 27 |
model = genai.GenerativeModel("gemini-2.0-flash-lite")
|
| 28 |
|
| 29 |
system_prompt = (
|
| 30 |
"You are a financial risk advisor. "
|
| 31 |
+
"Return only JSON. "
|
| 32 |
+
'Schema: {"action":"increase_interest"|"reduce_disbursement"|"maintain","rationale":"string","confidence":float}'
|
| 33 |
)
|
|
|
|
| 34 |
user_prompt = f"""
|
| 35 |
Segment: {segment}
|
| 36 |
PD: {pd_val:.3f}
|
|
|
|
| 52 |
generation_config={"temperature": 0.1}
|
| 53 |
)
|
| 54 |
text = resp.text.strip()
|
|
|
|
|
|
|
| 55 |
text = re.sub(r"^```json", "", text)
|
| 56 |
text = re.sub(r"^```", "", text)
|
| 57 |
text = re.sub(r"```$", "", text)
|
|
|
|
|
|
|
|
|
|
| 58 |
match = re.search(r"\{.*\}", text, re.DOTALL)
|
| 59 |
if match:
|
| 60 |
text = match.group(0)
|
|
|
|
|
|
|
| 61 |
data = json.loads(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return data
|
|
|
|
| 63 |
except Exception as e:
|
|
|
|
| 64 |
st.warning(f"⚠️ Gemini output parse failed: {e}")
|
| 65 |
st.text_area("Raw Gemini output", value=resp.text if 'resp' in locals() else "No response", height=150)
|
| 66 |
return {"action": "maintain", "rationale": "Fallback - parse failure", "confidence": 0.0}
|
| 67 |
|
| 68 |
# ====== UI ======
|
| 69 |
st.title("📊 Expected Credit Loss (ECL) Risk Dashboard")
|
| 70 |
+
st.write("Upload your **loan dataset**, review segment-level ECL metrics, and analyze one segment at a time with Gemini.")
|
| 71 |
|
| 72 |
uploaded = st.file_uploader("Upload CSV dataset", type=["csv"])
|
| 73 |
|
| 74 |
if uploaded:
|
| 75 |
df = pd.read_csv(uploaded)
|
|
|
|
|
|
|
|
|
|
| 76 |
ecl_df = process_loan_data(df)
|
| 77 |
+
st.success("Dataset processed successfully.")
|
| 78 |
st.dataframe(ecl_df, use_container_width=True, hide_index=True)
|
| 79 |
|
| 80 |
+
# --- Visual overview ---
|
| 81 |
+
col1, col2 = st.columns(2)
|
| 82 |
+
with col1:
|
| 83 |
+
st.subheader("ECL by Segment")
|
| 84 |
+
fig, ax = plt.subplots(figsize=(6, 3))
|
| 85 |
+
ax.bar(ecl_df["loan_intent"], ecl_df["ECL"])
|
| 86 |
+
ax.set_xlabel("Segment"); ax.set_ylabel("ECL")
|
| 87 |
+
plt.xticks(rotation=45)
|
| 88 |
+
st.pyplot(fig)
|
| 89 |
+
with col2:
|
| 90 |
+
st.subheader("PD by Segment")
|
| 91 |
+
fig2, ax2 = plt.subplots(figsize=(6, 3))
|
| 92 |
+
ax2.bar(ecl_df["loan_intent"], ecl_df["PD"], color="gray")
|
| 93 |
+
ax2.set_xlabel("Segment"); ax2.set_ylabel("PD")
|
| 94 |
+
plt.xticks(rotation=45)
|
| 95 |
+
st.pyplot(fig2)
|
| 96 |
+
|
| 97 |
+
# --- Segment selection ---
|
| 98 |
+
st.subheader("Analyze Specific Segment")
|
| 99 |
+
segments = ecl_df["loan_intent"].unique().tolist()
|
| 100 |
+
selected_segment = st.selectbox("Choose a segment:", segments)
|
| 101 |
+
|
| 102 |
+
row = ecl_df[ecl_df["loan_intent"] == selected_segment].iloc[0]
|
| 103 |
+
st.write(f"**PD:** {row.PD:.3f} | **LGD:** {row.LGD:.3f} | **EAD:** {row.EAD:,.0f} | **ECL:** {row.ECL:,.0f}")
|
| 104 |
+
|
| 105 |
+
if st.button("Generate Gemini Decision"):
|
| 106 |
+
with st.spinner("Querying Gemini..."):
|
| 107 |
+
decision = get_gemini_decision(row["loan_intent"], row["PD"], row["LGD"], row["EAD"], row["ECL"])
|
| 108 |
+
st.success("Gemini Decision:")
|
| 109 |
+
st.json(decision)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
else:
|
| 112 |
+
st.info("Upload a CSV file to begin.")
|