Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import xgboost as xgb
|
| 5 |
import shap
|
| 6 |
-
import plotly.express as px
|
| 7 |
from sklearn.datasets import make_classification
|
| 8 |
|
| 9 |
-
# 1. ๊ฐ์ FDS ๋ฐ์ดํฐ ๋ฐ ๋ชจ๋ธ ์ค๋น
|
| 10 |
-
# ์ค๋ฌด์๋ค์ด ์ง๊ด์ ์ผ๋ก ์ดํดํ ์ ์๋ ํต์ฌ FDS ํผ์ฒ ์ ์
|
| 11 |
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5,
|
| 12 |
n_redundant=2, random_state=42)
|
| 13 |
|
|
@@ -22,7 +31,6 @@ model = xgb.XGBClassifier(random_state=42).fit(X, y)
|
|
| 22 |
explainer = shap.TreeExplainer(model)
|
| 23 |
|
| 24 |
def fds_predict(*args):
|
| 25 |
-
# ์
๋ ฅ ๋ฐ์ดํฐ๋ฅผ DataFrame์ผ๋ก ๋ณํ
|
| 26 |
input_df = pd.DataFrame([args], columns=feature_names)
|
| 27 |
|
| 28 |
# 1. ์ด์๊ฑฐ๋ ํ๋ฅ ๋ฐ ํ์ ์์ธก
|
|
@@ -38,16 +46,12 @@ def fds_predict(*args):
|
|
| 38 |
# 2. SHAP ๊ฐ ๊ณ์ฐ (XAI ์ค๋ช
๋ ฅ ์ถ์ถ)
|
| 39 |
shap_values = explainer.shap_values(input_df)
|
| 40 |
|
| 41 |
-
# 3. Plotly
|
| 42 |
shap_df = pd.DataFrame({
|
| 43 |
'ํน์ฑ(Feature)': feature_names,
|
| 44 |
'๊ธฐ์ฌ๋(SHAP Value)': shap_values[0]
|
| 45 |
-
})
|
| 46 |
-
|
| 47 |
-
# ๊ธฐ์ฌ๋ ์ ๋๊ฐ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ฑฐ๋ ์์น ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ์ฌ ๊ฐ๋
์ฑ ํ๋ณด
|
| 48 |
-
shap_df = shap_df.sort_values(by='๊ธฐ์ฌ๋(SHAP Value)', ascending=True)
|
| 49 |
|
| 50 |
-
# ์ํ๋๋ฅผ ๋์ด๋ ์์(์์)์ ๋ฎ์ถ๋ ์์(์์)๋ฅผ ์ง๊ด์ ์ผ๋ก ๋ณด์ฌ์ฃผ๋ ์ปฌ๋ฌ์
|
| 51 |
fig = px.bar(
|
| 52 |
shap_df,
|
| 53 |
x='๊ธฐ์ฌ๋(SHAP Value)',
|
|
@@ -55,11 +59,10 @@ def fds_predict(*args):
|
|
| 55 |
orientation='h',
|
| 56 |
title="<b>๐ก AI ํ์ ํต์ฌ ์์ธ ๋ถ์ (XAI ์๋ช
๋ชจ๋ธ)</b>",
|
| 57 |
color='๊ธฐ์ฌ๋(SHAP Value)',
|
| 58 |
-
color_continuous_scale='RdBu_r',
|
| 59 |
template='plotly_white'
|
| 60 |
)
|
| 61 |
|
| 62 |
-
# ๋ ์ด์์ ๋ฏธ์ธ ์กฐ์ (์ค๋ฌด ๋์๋ณด๋ ๋๋ ๊ฐ์กฐ)
|
| 63 |
fig.update_layout(
|
| 64 |
title_font_size=16,
|
| 65 |
xaxis_title="์ํ ๊ธฐ์ฌ๋ (์ ์ ๋ฐฉํฅ <---> ์ด์ ๋ฐฉํฅ)",
|
|
@@ -71,7 +74,7 @@ def fds_predict(*args):
|
|
| 71 |
|
| 72 |
return prediction, f"{round(prob * 100, 2)}%", fig
|
| 73 |
|
| 74 |
-
# 3. Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 75 |
with gr.Blocks(title="FDS 5์ธ๋ ํ์ง ๋ฐ ์๋ช
๋ฐ๋ชจ") as demo:
|
| 76 |
gr.Markdown("## ๐ก๏ธ FDS 5์ธ๋: AI ๊ธฐ๋ฐ ์ค์๊ฐ ํ์ง ๋ฐ XAI ์๋ช
๋์๋ณด๋")
|
| 77 |
gr.Markdown(
|
|
@@ -92,7 +95,7 @@ with gr.Blocks(title="FDS 5์ธ๋ ํ์ง ๋ฐ ์๋ช
๋ฐ๋ชจ") as demo:
|
|
| 92 |
gr.Radio([0, 1], label="์ผ๊ฐ์๊ฐ๋์ฌ๋ถ (0:์ฃผ๊ฐ, 1:์ฌ์ผ)", value=1),
|
| 93 |
gr.Slider(0, 500000, label="ํ์๋จ๊ฐ์ฐจ์ด", value=350000),
|
| 94 |
gr.Slider(0, 5, label="๋ก๊ทธ์ธ์คํจํ์", value=3),
|
| 95 |
-
gr.Slider(0, 1, label="๋น์ ์ํจํด_์ค์ฝ์ด (
|
| 96 |
]
|
| 97 |
submit_btn = gr.Button("์ค์๊ฐ ์ค์ฝ์ด๋ง ์คํ", variant="primary")
|
| 98 |
|
|
@@ -102,10 +105,8 @@ with gr.Blocks(title="FDS 5์ธ๋ ํ์ง ๋ฐ ์๋ช
๋ฐ๋ชจ") as demo:
|
|
| 102 |
res_label = gr.Textbox(label="์ต์ข
ํต์ ์กฐ์น", interactive=False)
|
| 103 |
res_prob = gr.Textbox(label="์ด์ ๊ฑฐ๋ ํ๋ฅ (Risk Score)", interactive=False)
|
| 104 |
|
| 105 |
-
# Plotly ์ปดํฌ๋ํธ๋ก ๋ณ๊ฒฝ๋์ด ๋ง์ฐ์ค ์ค๋ฒ ์ธํฐ๋์
์ง์
|
| 106 |
res_plot = gr.Plot(label="๋ณ์๋ณ ๊ธฐ์ฌ๋ (SHAP Analysis)")
|
| 107 |
|
| 108 |
-
# ๋ฒํผ ํด๋ฆญ ์ ํจ์ ์ฐ๊ฒฐ
|
| 109 |
submit_btn.click(
|
| 110 |
fn=fds_predict,
|
| 111 |
inputs=inputs,
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
# [๋ฐฉ์ด ์ฝ๋] Hugging Face ํ๊ฒฝ์์ ๊ฐ๋ ๋ฐ์ํ๋ ํจํค์ง ๋๋ฝ ์๋ฌ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํ ๋์ ์ค์น
|
| 5 |
+
try:
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
except ModuleNotFoundError:
|
| 8 |
+
print("โ ๏ธ Plotly๊ฐ ์ค์น๋์ง ์์ ๋์ ์ค์น๋ฅผ ์์ํฉ๋๋ค...")
|
| 9 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "plotly"])
|
| 10 |
+
import plotly.express as px
|
| 11 |
+
|
| 12 |
import gradio as gr
|
| 13 |
import pandas as pd
|
| 14 |
import numpy as np
|
| 15 |
import xgboost as xgb
|
| 16 |
import shap
|
|
|
|
| 17 |
from sklearn.datasets import make_classification
|
| 18 |
|
| 19 |
+
# 1. ๊ฐ์ FDS ๋ฐ์ดํฐ ๋ฐ ๋ชจ๋ธ ์ค๋น
|
|
|
|
| 20 |
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5,
|
| 21 |
n_redundant=2, random_state=42)
|
| 22 |
|
|
|
|
| 31 |
explainer = shap.TreeExplainer(model)
|
| 32 |
|
| 33 |
def fds_predict(*args):
|
|
|
|
| 34 |
input_df = pd.DataFrame([args], columns=feature_names)
|
| 35 |
|
| 36 |
# 1. ์ด์๊ฑฐ๋ ํ๋ฅ ๋ฐ ํ์ ์์ธก
|
|
|
|
| 46 |
# 2. SHAP ๊ฐ ๊ณ์ฐ (XAI ์ค๋ช
๋ ฅ ์ถ์ถ)
|
| 47 |
shap_values = explainer.shap_values(input_df)
|
| 48 |
|
| 49 |
+
# 3. Plotly ์๊ฐํ (ํ๊ธ ๊นจ์ง ๋ฐฉ์ง ๋ฐ ์ธํฐ๋์
์ ๊ณต)
|
| 50 |
shap_df = pd.DataFrame({
|
| 51 |
'ํน์ฑ(Feature)': feature_names,
|
| 52 |
'๊ธฐ์ฌ๋(SHAP Value)': shap_values[0]
|
| 53 |
+
}).sort_values(by='๊ธฐ์ฌ๋(SHAP Value)', ascending=True)
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
| 55 |
fig = px.bar(
|
| 56 |
shap_df,
|
| 57 |
x='๊ธฐ์ฌ๋(SHAP Value)',
|
|
|
|
| 59 |
orientation='h',
|
| 60 |
title="<b>๐ก AI ํ์ ํต์ฌ ์์ธ ๋ถ์ (XAI ์๋ช
๋ชจ๋ธ)</b>",
|
| 61 |
color='๊ธฐ์ฌ๋(SHAP Value)',
|
| 62 |
+
color_continuous_scale='RdBu_r',
|
| 63 |
template='plotly_white'
|
| 64 |
)
|
| 65 |
|
|
|
|
| 66 |
fig.update_layout(
|
| 67 |
title_font_size=16,
|
| 68 |
xaxis_title="์ํ ๊ธฐ์ฌ๋ (์ ์ ๋ฐฉํฅ <---> ์ด์ ๋ฐฉํฅ)",
|
|
|
|
| 74 |
|
| 75 |
return prediction, f"{round(prob * 100, 2)}%", fig
|
| 76 |
|
| 77 |
+
# 3. Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 78 |
with gr.Blocks(title="FDS 5์ธ๋ ํ์ง ๋ฐ ์๋ช
๋ฐ๋ชจ") as demo:
|
| 79 |
gr.Markdown("## ๐ก๏ธ FDS 5์ธ๋: AI ๊ธฐ๋ฐ ์ค์๊ฐ ํ์ง ๋ฐ XAI ์๋ช
๋์๋ณด๋")
|
| 80 |
gr.Markdown(
|
|
|
|
| 95 |
gr.Radio([0, 1], label="์ผ๊ฐ์๊ฐ๋์ฌ๋ถ (0:์ฃผ๊ฐ, 1:์ฌ์ผ)", value=1),
|
| 96 |
gr.Slider(0, 500000, label="ํ์๋จ๊ฐ์ฐจ์ด", value=350000),
|
| 97 |
gr.Slider(0, 5, label="๋ก๊ทธ์ธ์คํจํ์", value=3),
|
| 98 |
+
gr.Slider(0, 1, label="๋น์ ์ํจํด_์ค์ฝ์ด (ํ์๋ถ์)", value=0.78),
|
| 99 |
]
|
| 100 |
submit_btn = gr.Button("์ค์๊ฐ ์ค์ฝ์ด๋ง ์คํ", variant="primary")
|
| 101 |
|
|
|
|
| 105 |
res_label = gr.Textbox(label="์ต์ข
ํต์ ์กฐ์น", interactive=False)
|
| 106 |
res_prob = gr.Textbox(label="์ด์ ๊ฑฐ๋ ํ๋ฅ (Risk Score)", interactive=False)
|
| 107 |
|
|
|
|
| 108 |
res_plot = gr.Plot(label="๋ณ์๋ณ ๊ธฐ์ฌ๋ (SHAP Analysis)")
|
| 109 |
|
|
|
|
| 110 |
submit_btn.click(
|
| 111 |
fn=fds_predict,
|
| 112 |
inputs=inputs,
|