ableman82 commited on
Commit
869c6cb
ยท
verified ยท
1 Parent(s): 3724d07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
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 ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ (5์„ธ๋Œ€ FDS ๊ด€์ œ ๋Œ€์‹œ๋ณด๋“œ ์ปจ์…‰)
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="๋น„์ •์ƒํŒจํ„ด_์Šค์ฝ”์–ด (๋งˆ์šฐ์Šค/ํ‚ค๋ณด๋“œ ํ–‰์œ„๋ถ„์„)", value=0.78),
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,