import gradio as gr import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns # 5세대 FDS의 핵심: AI 모델의 판단 근거 시각화 함수 def predict_fraud(user_id, amount, location, time_of_day): # 실제 환경에서는 여기서 학습된 모델(XGBoost, PyG 등)을 로드하여 사용합니다. # 본 데모에서는 로직에 따른 시뮬레이션 결과를 반환합니다. score = 0 reasons = [] # 가상의 탐지 로직 (5세대 특성 반영: 행동 패턴 분석) if amount > 5000: score += 40 reasons.append("평소 거래 금액 대비 과다 지출") if location == "Overseas": score += 30 reasons.append("비정상적 접속 위치 (해외)") if time_of_day == "Dawn (00-05)": score += 20 reasons.append("취약 시간대 거래") risk_level = "High" if score >= 70 else "Medium" if score >= 40 else "Low" result_text = f"결과: {risk_level} (위험 점수: {score}/100)" # 시각화 데이터 생성 fig, ax = plt.subplots(figsize=(6, 4)) features = ['Amount', 'Location', 'Time', 'History'] values = [amount/100, 30 if location == "Overseas" else 10, 20 if "Dawn" in time_of_day else 5, 10] sns.barplot(x=features, y=values, ax=ax, palette="viridis") ax.set_title("Feature Importance for this Transaction") ax.set_ylabel("Risk Contribution") return result_text, "\n".join(reasons) if reasons else "특이사항 없음", fig # Gradio 인터페이스 구성 with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("# 🛡️ FDS 5세대 (AI-Driven) 탐지 데모") gr.Markdown("사용자의 거래 패턴을 분석하여 실시간으로 이상거래를 탐지합니다.") with gr.Row(): with gr.Column(): user_id = gr.Textbox(label="User ID", placeholder="USER_1234") amount = gr.Number(label="거래 금액 ($)", value=100) location = gr.Dropdown(["Domestic", "Overseas"], label="접속 지역", value="Domestic") time_of_day = gr.Radio(["Day (06-18)", "Night (19-23)", "Dawn (00-05)"], label="거래 시간대", value="Day (06-18)") btn = gr.Button("이상거래 검사 실행", variant="primary") with gr.Column(): output_res = gr.Textbox(label="탐지 결과") output_reason = gr.Textbox(label="주요 탐지 사유 (XAI)") output_plot = gr.Plot(label="위험 요소 분석 그래프") btn.click(predict_fraud, inputs=[user_id, amount, location, time_of_day], outputs=[output_res, output_reason, output_plot]) if __name__ == "__main__": demo.launch()