Spaces:
Sleeping
Sleeping
Download src/human_feedback_learning.py from frigate-global/financial-risk-api: direct link, hf CLI and curl.
- Browser
- Download file 3.58 kB
-
https://huggingface.co/spaces/frigate-global/financial-risk-api/resolve/main/src/human_feedback_learning.py
- Command line
-
hf download hf://spaces/frigate-global/financial-risk-api/src/human_feedback_learning.py
-
curl -L -o human_feedback_learning.py https://huggingface.co/spaces/frigate-global/financial-risk-api/resolve/main/src/human_feedback_learning.py
3.58 kB
| import os | |
| import uuid | |
| import sqlite3 | |
| import pandas as pd | |
| import numpy as np | |
| class HumanFeedbackEngine: | |
| """Manages underwriter overrides, logs feedback to database, and calculates expert disagreement metrics.""" | |
| def __init__(self, db_path): | |
| self.db_path = db_path | |
| def log_override(self, user_id, analyst_id, action_taken, original_decision, new_decision, original_limit, new_limit, reviewer_confidence=1.0): | |
| """Logs manual reviewer override adjustments.""" | |
| override_id = str(uuid.uuid4())[:18] | |
| conn = sqlite3.connect(self.db_path) | |
| cursor = conn.cursor() | |
| cursor.execute(""" | |
| INSERT INTO human_overrides ( | |
| override_id, user_id, analyst_id, action_taken, original_decision, new_decision, | |
| original_credit_limit, new_credit_limit, reviewer_confidence | |
| ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) | |
| """, ( | |
| override_id, user_id, analyst_id, action_taken, original_decision, new_decision, | |
| original_limit, new_limit, reviewer_confidence | |
| )) | |
| conn.commit() | |
| conn.close() | |
| return override_id | |
| def analyze_override_patterns(self): | |
| """Calculates override stats: override rate, mean adjustment size, | |
| and consensus metrics (model-reviewer disagreement rate). | |
| """ | |
| conn = sqlite3.connect(self.db_path) | |
| df = pd.read_sql_query("SELECT * FROM human_overrides", conn) | |
| conn.close() | |
| if df.empty: | |
| return { | |
| "total_overrides": 0, | |
| "disagreement_rate": 0.0, | |
| "mean_limit_delta": 0.0, | |
| "reviewer_consensus_score": 1.0 | |
| } | |
| disagreements = df[df["original_decision"] != df["new_decision"]] | |
| disagreement_rate = len(disagreements) / len(df) | |
| limit_delta = df["new_credit_limit"] - df["original_credit_limit"] | |
| mean_delta = float(limit_delta.mean()) | |
| # Reviewer confidence weight consensus | |
| consensus_score = float(df["reviewer_confidence"].mean()) | |
| return { | |
| "total_overrides": len(df), | |
| "disagreement_rate": disagreement_rate, | |
| "mean_limit_delta": mean_delta, | |
| "reviewer_consensus_score": consensus_score | |
| } | |
| def compute_feedback_decision_offsets(self): | |
| """Calculates decision boundary adjustments based on human overrides. | |
| If underwriters continuously override high-risk rejections to approved | |
| for specific profiles, we widen the approval risk thresholds. If they | |
| override approvals to rejected, we tighten the thresholds. | |
| """ | |
| conn = sqlite3.connect(self.db_path) | |
| df = pd.read_sql_query("SELECT * FROM human_overrides", conn) | |
| conn.close() | |
| if df.empty: | |
| return 0.0 | |
| # Count overrides | |
| approvals_overridden_to_reject = len(df[(df["original_decision"] == "APPROVED") & (df["new_decision"] == "REJECTED")]) | |
| rejects_overridden_to_approve = len(df[(df["original_decision"] == "REJECTED") & (df["new_decision"] == "APPROVED")]) | |
| # If rejects overridden to approve is higher, we can slightly relax thresholds (positive offset) | |
| # If approvals overridden to reject is higher, we tighten (negative offset) | |
| net_overrides = rejects_overridden_to_approve - approvals_overridden_to_reject | |
| offset = np.clip(net_overrides * 0.005, -0.05, 0.05) | |
| return float(offset) | |