FDS_TEST1 / app.py
eunji73's picture
Rename 5์„ธ๋Œ€ FDS to app.py
558f8d5 verified
Raw
History Blame Contribute Delete
2.72 kB
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()