| import gradio as gr |
| import pandas as pd |
| import numpy as np |
| import matplotlib.pyplot as plt |
| import seaborn as sns |
|
|
| |
| def predict_fraud(user_id, amount, location, time_of_day): |
| |
| |
| |
| score = 0 |
| reasons = [] |
| |
| |
| 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 |
|
|
| |
| 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() |