perbaiki logic error
Browse files
app.py
CHANGED
|
@@ -4,63 +4,109 @@ import gradio as gr
|
|
| 4 |
import os
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
# --- KONFIGURASI
|
| 8 |
-
MODEL_PATH = "archon_v1"
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
class ArchonBankEngine:
|
| 12 |
def __init__(self):
|
| 13 |
-
#
|
| 14 |
self.classifier = pipeline("text-classification", model=MODEL_PATH, tokenizer=MODEL_PATH)
|
| 15 |
-
self.
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
#
|
| 19 |
self.df_txn = pd.read_csv('transactions.csv', parse_dates=['date'])
|
| 20 |
self.df_cust = pd.read_csv('customers.csv')
|
| 21 |
self.df_bal = pd.read_csv('balances_revised.csv', parse_dates=['month'])
|
| 22 |
self.df_rep = pd.read_csv('repayments_revised.csv', parse_dates=['due_date'])
|
|
|
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
risk_level = "HIGH" if risk_score >= 0.7 else "LOW"
|
| 37 |
-
|
| 38 |
-
# 4. Tahap 5: NBO Engine (Action Layer)
|
| 39 |
-
action = "restructuring_suggestion" if risk_level == "HIGH" else "promote_saving"
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
return {
|
| 45 |
"ID Nasabah": customer_id,
|
| 46 |
"Level Risiko": risk_level,
|
| 47 |
"Rekomendasi Aksi": action,
|
|
|
|
| 48 |
"Penjelasan": summary
|
| 49 |
}
|
| 50 |
|
|
|
|
| 51 |
engine = ArchonBankEngine()
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return engine.run_pipeline(cust_id)
|
| 56 |
|
| 57 |
demo = gr.Interface(
|
| 58 |
-
fn=
|
| 59 |
-
inputs=gr.Textbox(label="
|
| 60 |
outputs="json",
|
| 61 |
-
title="🛡️ Archon-AI
|
| 62 |
-
description="Sistem automasi
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
demo.launch()
|
|
|
|
| 4 |
import os
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# --- KONFIGURASI PATH & MODEL ---
|
| 8 |
+
MODEL_PATH = "archon_v1"
|
| 9 |
+
# Kategori wajib sesuai instruksi Fase 2
|
| 10 |
+
ESSENTIAL_CATS = {'groceries', 'utilities', 'transport', 'healthcare', 'education'}
|
| 11 |
+
DISCRETIONARY_CATS = {'restaurant', 'cafe', 'entertainment', 'fashion', 'online_shopping', 'travel'}
|
| 12 |
|
| 13 |
class ArchonBankEngine:
|
| 14 |
def __init__(self):
|
| 15 |
+
# Pilar 1: AI Classifier (Fase 2)
|
| 16 |
self.classifier = pipeline("text-classification", model=MODEL_PATH, tokenizer=MODEL_PATH)
|
| 17 |
+
self.load_all_data()
|
| 18 |
|
| 19 |
+
def load_all_data(self):
|
| 20 |
+
# FASE 1: DATA FOUNDATION
|
| 21 |
self.df_txn = pd.read_csv('transactions.csv', parse_dates=['date'])
|
| 22 |
self.df_cust = pd.read_csv('customers.csv')
|
| 23 |
self.df_bal = pd.read_csv('balances_revised.csv', parse_dates=['month'])
|
| 24 |
self.df_rep = pd.read_csv('repayments_revised.csv', parse_dates=['due_date'])
|
| 25 |
+
self.df_off = pd.read_csv('offers.csv')
|
| 26 |
|
| 27 |
+
def analyze_customer(self, customer_id):
|
| 28 |
+
# --- PRE-PROCESSING DATA NASABAH ---
|
| 29 |
+
user_txn = self.df_txn[self.df_txn['customer_id'] == customer_id].copy()
|
| 30 |
+
user_bal = self.df_bal[self.df_bal['customer_id'] == customer_id].sort_values('month')
|
| 31 |
+
user_rep = self.df_rep[self.df_rep['customer_id'] == customer_id]
|
| 32 |
+
user_info = self.df_cust[self.df_cust['customer_id'] == customer_id].iloc[0]
|
| 33 |
+
|
| 34 |
+
if user_txn.empty: return {"Error": "Data Nasabah Tidak Ditemukan"}
|
| 35 |
+
|
| 36 |
+
# --- FASE 2: TRANSACTION INTELLIGENCE (Automasi AI) ---
|
| 37 |
+
# AI menentukan kategori dari deskripsi transaksi yang ambigu
|
| 38 |
+
user_txn['merchant_category'] = user_txn['raw_description'].apply(lambda x: self.classifier(x)[0]['label'])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Penentuan expense_type sesuai aturan Fase 2
|
| 41 |
+
def set_expense_type(cat):
|
| 42 |
+
if any(k in cat.lower() for k in ESSENTIAL_CATS): return 'essential'
|
| 43 |
+
return 'discretionary'
|
| 44 |
+
user_txn['expense_type'] = user_txn['merchant_category'].apply(set_expense_type)
|
| 45 |
+
|
| 46 |
+
# --- FASE 3 & 4: RISK SCORING (Early Warning System) ---
|
| 47 |
+
# 1. Expense Ratio (Bulan terakhir)
|
| 48 |
+
income = user_txn[user_txn['transaction_type'] == 'credit']['amount'].sum()
|
| 49 |
+
expense = user_txn[user_txn['transaction_type'] == 'debit']['amount'].sum()
|
| 50 |
+
er = expense / income if income > 0 else 1.0
|
| 51 |
+
er_score = 1 if er > 0.8 else (0.5 if er > 0.5 else 0)
|
| 52 |
+
|
| 53 |
+
# 2. Balance Trend (Bulan ini vs bulan lalu)
|
| 54 |
+
if len(user_bal) >= 2:
|
| 55 |
+
bt = -1 if user_bal.iloc[-1]['avg_balance'] < user_bal.iloc[-2]['avg_balance'] else 1
|
| 56 |
+
else: bt = 0
|
| 57 |
+
bt_score = 1 if bt == -1 else 0
|
| 58 |
+
|
| 59 |
+
# 3. Overdraft & Missed Payment
|
| 60 |
+
od_score = 1 if (user_bal['min_balance'] <= 0).any() else 0
|
| 61 |
+
mp_score = 1 if (user_rep['status'] == 'late').any() else 0
|
| 62 |
+
|
| 63 |
+
# HITUNG FINAL RISK SCORE (Bobot Fix: 30%, 20%, 20%, 20%, 10%)
|
| 64 |
+
risk_score = (0.3 * er_score) + (0.2 * bt_score) + (0.2 * od_score) + (0.2 * mp_score) + 0.1
|
| 65 |
+
risk_level = "HIGH" if risk_score >= 0.7 else ("MEDIUM" if risk_score >= 0.4 else "LOW")
|
| 66 |
+
|
| 67 |
+
# --- FASE 5: NBO ENGINE ---
|
| 68 |
+
# Menentukan aksi sesuai kriteria Fase 5
|
| 69 |
+
disc_ratio = user_txn[user_txn['expense_type'] == 'discretionary']['amount'].sum() / expense if expense > 0 else 0
|
| 70 |
|
| 71 |
+
if risk_level == "HIGH":
|
| 72 |
+
action = "restructuring_suggestion"
|
| 73 |
+
reason = "REPAYMENT_RISK_DETECTED"
|
| 74 |
+
elif risk_level == "MEDIUM" and disc_ratio > 0.4:
|
| 75 |
+
action = "budgeting_alert"
|
| 76 |
+
reason = "HIGH_DISCRETIONARY_SPENDING"
|
| 77 |
+
elif risk_level == "LOW" and disc_ratio <= 0.4:
|
| 78 |
+
action = "promote_saving"
|
| 79 |
+
reason = "STABLE_CASHFLOW"
|
| 80 |
+
else:
|
| 81 |
+
action = "no_action"
|
| 82 |
+
reason = "COOLDOWN_ACTIVE"
|
| 83 |
+
|
| 84 |
+
# --- FASE 6: EXPLAINABLE SUMMARY ---
|
| 85 |
+
summary = f"Nasabah memiliki risiko {risk_level}. "
|
| 86 |
+
if er_score == 1: summary += "Pengeluaran sangat tinggi (>80% pendapatan). "
|
| 87 |
+
if bt_score == 1: summary += "Tren saldo menurun signifikan. "
|
| 88 |
+
if mp_score == 1: summary += "Terdeteksi riwayat telat bayar."
|
| 89 |
+
|
| 90 |
return {
|
| 91 |
"ID Nasabah": customer_id,
|
| 92 |
"Level Risiko": risk_level,
|
| 93 |
"Rekomendasi Aksi": action,
|
| 94 |
+
"Alasan": reason,
|
| 95 |
"Penjelasan": summary
|
| 96 |
}
|
| 97 |
|
| 98 |
+
# --- INTEGRASI KE DASHBOARD ---
|
| 99 |
engine = ArchonBankEngine()
|
| 100 |
|
| 101 |
+
def run_app(cust_id):
|
| 102 |
+
return engine.analyze_customer(cust_id)
|
|
|
|
| 103 |
|
| 104 |
demo = gr.Interface(
|
| 105 |
+
fn=run_app,
|
| 106 |
+
inputs=gr.Textbox(label="Input Customer ID (C0001 - C0120)"),
|
| 107 |
outputs="json",
|
| 108 |
+
title="🛡️ Archon-AI Production Engine",
|
| 109 |
+
description="Sistem automasi perbankan sesuai instruksi Arahan Pembuatan AI Archon."
|
| 110 |
)
|
| 111 |
|
| 112 |
+
demo.launch()
|
|
|