File size: 9,526 Bytes
ff86237 07a61d3 5d8c940 07a61d3 ff86237 07a61d3 ff86237 07a61d3 5d8c940 1168fe0 5d8c940 1168fe0 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 e25b6d7 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 11f6ff1 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 ff86237 07a61d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | import gradio as gr
import torch
import torch.nn.functional as F
import os
import zipfile
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from peft import PeftModel
# ==========================================
# 1. Model Loading
# ==========================================
# [์์ ] LoRA ์ด๋ํฐ(adapter_config.json)๋ฅผ ํ์ธํด๋ณด๋ฉด ์ค์ base_model_name_or_path๋
# "monologg/koelectra-small-v3-discriminator" ์ด๋ฉฐ, task_type๋ "SEQ_CLS"(๋ถ๋ฅ)์
๋๋ค.
# Qwen ์์ฑํ LLM์ด ์๋๋ผ KoELECTRA ๊ธฐ๋ฐ "์ด์ง ๋ถ๋ฅ(์ ์/์ค์ ๋ณด)" ๋ชจ๋ธ์
๋๋ค.
BASE_MODEL = "monologg/koelectra-small-v3-discriminator"
LORA_WEIGHTS = ""
NUM_LABELS = 2 # safetensors์ classifier.out_proj shape์ด (2, 256) -> ํด๋์ค 2๊ฐ
LABEL_MAP = {
0: "์ ๋ขฐ ๊ฐ๋ฅ (๊ธฐํ ์ค์ ๋ณด๋ก ํ๋จ๋์ง ์์)",
1: "๊ธฐํ ์ค์ ๋ณด ๊ฐ๋ฅ์ฑ ์์ (์ฃผ์ ํ์)"
}
print("Loading model and tokenizer...")
try:
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
base_model = AutoModelForSequenceClassification.from_pretrained(
BASE_MODEL,
num_labels=NUM_LABELS,
torch_dtype=torch.float32,
)
model = PeftModel.from_pretrained(base_model, LORA_WEIGHTS)
model.eval()
MODEL_LOADED = True
print("LoRA Model successfully loaded!")
except Exception as e:
print(f"Warning: Model loading failed ({e}). Running in Simulation Mode.")
MODEL_LOADED = False
# ==========================================
# 2. Pipeline Algorithm & Inference Engine
# ==========================================
def run_pipeline(user_input):
"""
1. Text Preprocessing
2. Tokenizing
3. LoRA Model Inference (๋ถ๋ฅ)
4. XAI / Decision Guide Extraction
5. Attention / Keyword Map Analysis
6. Output Generation & Limitations
"""
if not user_input.strip():
return "์
๋ ฅ ํ
์คํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.", "N/A", "N/A", "N/A"
# Step 1: Preprocessing
cleaned_input = user_input.strip()
if MODEL_LOADED:
# Step 2: Tokenizing
inputs = tokenizer(cleaned_input, return_tensors="pt", truncation=True, max_length=512)
# Step 3: LoRA Model Inference (+ attention ์ถ๋ ฅ ์์ฒญ)
with torch.no_grad():
outputs = model(**inputs, output_attentions=True)
logits = outputs.logits
probs = F.softmax(logits, dim=-1)[0]
pred_id = int(torch.argmax(probs).item())
confidence = float(probs[pred_id].item())
generated_text = (
f"[๋ถ์ ๊ฒฐ๊ณผ]\n"
f"ํ๋จ: {LABEL_MAP.get(pred_id, f'ํด๋์ค {pred_id}')}\n"
f"์ ๋ขฐ๋(ํ๋ฅ ): {confidence * 100:.1f}%\n\n"
f"ํด๋์ค๋ณ ํ๋ฅ -> ์ ์: {probs[0]*100:.1f}% / ์ค์ ๋ณด ์์ฌ: {probs[1]*100:.1f}%"
)
# Step 4: Decision Guide (xAI) - ์ค์ ํ๋ฅ ๊ฐ ๊ธฐ๋ฐ
decision_guide = (
"๐ **xAI ํ๋จ ๊ฐ์ด๋ ์ถ์ถ**\n"
f"- ๋ชจ๋ธ ์์ธก ํด๋์ค: {pred_id} ({LABEL_MAP.get(pred_id, '')})\n"
f"- ์์ธก ์ ๋ขฐ๋: {confidence * 100:.1f}%\n"
f"- ํด๋์ค ํ๋ฅ ๋ถํฌ: ์ ์ {probs[0]*100:.1f}% / ์ค์ ๋ณด ์์ฌ {probs[1]*100:.1f}%"
)
# Step 5: Attention Map Analysis - ์ค์ ์ดํ
์
๊ฐ์ค์น ๊ธฐ๋ฐ (CLS -> ๊ฐ ํ ํฐ)
try:
# ๋ง์ง๋ง ๋ ์ด์ด์ ๋ชจ๋ ํค๋ ํ๊ท , [CLS] ํ ํฐ์ด ๊ฐ ํ ํฐ์ ์ค ๊ฐ์ค์น ์ฌ์ฉ
last_layer_attn = outputs.attentions[-1][0] # (num_heads, seq_len, seq_len)
cls_attn = last_layer_attn.mean(dim=0)[0] # (seq_len,) CLS -> ๊ฐ ํ ํฐ
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
scored = [
(tok, float(w)) for tok, w in zip(tokens, cls_attn.tolist())
if tok not in tokenizer.all_special_tokens
]
scored.sort(key=lambda x: x[1], reverse=True)
top_k = scored[:5]
lines = "\n".join(f"{i+1}. '{tok}' -> ๊ฐ์ค์น {w:.3f}" for i, (tok, w) in enumerate(top_k))
attention_analysis = "๐ **์ดํ
์
๋งต ํต์ฌ ๊ฐ์ค์น (Attention Weights)**\n" + (lines or "ํ์ํ ํ ํฐ์ด ์์ต๋๋ค.")
except Exception as e:
attention_analysis = f"๐ ์ดํ
์
๋ถ์ ์ค ์ค๋ฅ ๋ฐ์: {e}"
else:
# ๋ชจ๋ธ ๋ก๋ฉ ์์ฒด๊ฐ ์คํจํ์ ๋๋ง ์ฌ์ฉ๋๋ ์๋ฎฌ๋ ์ด์
์ถ๋ ฅ
generated_text = (
"[์๋ฎฌ๋ ์ด์
๊ฒฐ๊ณผ - ๋ชจ๋ธ ๋ฏธ๋ก๋ฉ]\n"
"ํ์ฌ ์๋ฒ์ LoRA ๊ฐ์ค์น ๋๋ base ๋ชจ๋ธ์ ๋ถ๋ฌ์ค์ง ๋ชปํด ์์ ์ถ๋ ฅ์ ํ์ํฉ๋๋ค.\n"
"requirements.txt ๋ฐ ๋ชจ๋ธ ๊ฒฝ๋ก ์ค์ ์ ํ์ธํด์ฃผ์ธ์."
)
decision_guide = "๐ ๋ชจ๋ธ์ด ๋ก๋ฉ๋์ง ์์ xAI ๊ฐ์ด๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค."
attention_analysis = "๐ ๋ชจ๋ธ์ด ๋ก๋ฉ๋์ง ์์ ์ดํ
์
๋ถ์์ ์ํํ ์ ์์ต๋๋ค."
# Step 6: Disclaimer & Limitations
disclaimer = (
"โ ๏ธ **Model Card ํ๊ณ ๊ณ ์ง ๋ฐ ์ฌ์ ์๋ด (Disclaimer)**\n"
"โข **์ฑ
์ ์ ์ธ**: ๋ณธ ๋ชจ๋ธ(Team 3, 2026)์ ๊ธฐํ๋ณํ ์ค์ ๋ณด ํ์ง ๋ชฉ์ ์ผ๋ก ๊ฐ๋ฐ๋์์ต๋๋ค.\n"
"โข **์๋๋ ์ฌ์ฉ**: ์ผ๋ฐ์ ์ธ ๊ธฐํ ์ ๋ณด ํ์ ๋ฐ ๊ต์ ํ๋จ ์ฐธ๊ณ ์ฉ.\n"
"โข **์ฌ์ฉ ๊ธ์ง**: ์ํ์ ํ๋จ, ๋ฒ์ ๊ท์ ๊ทผ๊ฑฐ, ์๋ ์ฐจ๋จ/์ ์ฌ ์์คํ
์ ๋จ๋
๊ทผ๊ฑฐ๋ก ์ฌ์ฉ ๋ถ๊ฐ.\n"
"โข **์ฝ์ ๋ฐ ํ๊ณ**: ๊ณต์ ๋ ฅ ์๋ ๊ธฐ๊ด์ ํ์ ์๋ฃ๊ฐ ์๋ ์์
๋ฏธ๋์ด ํน์ ์ ์ ์กฐ์ด, ๊ทน๋จ์ ๋น์ , ํ
์คํธ ํํ๊ฐ ์๊ณก๋ ์กฐ๊ฑด์์๋ ์คํ๋ฅ ์ด ์์นํ ์ ์์ต๋๋ค.\n"
"โข **์ด์ ์ ๊ธฐ**: ๊ฒฐ๊ณผ์ ๋ํ ์ด์ ์ ์ฒญ ๋ฐ ์ค๋ฅ ์ ๋ณด๋ ๊ฐ๋ฐํ(Team 3) ํต๋ก๋ฅผ ์ด์ฉํด ์ฃผ์ธ์."
)
return generated_text, decision_guide, attention_analysis, disclaimer
# ==========================================
# 3. Gradio Web Interface
# ==========================================
theme = gr.themes.Soft(
primary_hue="teal",
secondary_hue="slate",
)
custom_css = """
.model-card-box {
background-color: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 15px;
margin-bottom: 15px;
}
.disclaimer-box {
background-color: #fffbe2;
border-left: 4px solid #f59e0b;
padding: 12px;
border-radius: 4px;
margin-top: 10px;
}
"""
with gr.Blocks(theme=theme, css=custom_css, title="๊ธฐํ๋ณํ ์ค์ ๋ณด ํ์ง AI ๋ชจ๋ธ") as demo:
gr.Markdown(
"""
# ๐ ๊ธฐํ๋ณํ ์ค์ ๋ณด ํ์ง AI ๋ชจ๋ธ (Climate Misinfo Detector)
**Team 3 | ์ฑ
์์์ AI ํ๋ก์ ํธ (High School Module 3-9)**
LoRA ์ด๋ํฐ ๊ฐ์ค์น๊ฐ ์ ์ฉ๋ KoELECTRA ๊ธฐ๋ฐ ๋ถ๋ฅ AI ์๊ณ ๋ฆฌ์ฆ ์น ์ธํฐํ์ด์ค์
๋๋ค.
"""
)
with gr.Row():
# Left Column
with gr.Column(scale=1):
gr.Markdown("### ๐ฅ ์ฌ์ฉ์ ์
๋ ฅ (Input)")
user_input = gr.Textbox(
lines=5,
placeholder="๊ฒ์ฆํ๊ณ ์ถ์ ๊ธฐํ๋ณํ ๊ด๋ จ ๋ฌธ์ฅ์ด๋ ์ฃผ์ฅ์ ์
๋ ฅํ์ธ์...\n ์: ์ง๊ตฌ์จ๋ํ๋ ์ธ๊ฐ ํ๋ ๋๋ฌธ์ด ์๋๋ผ ์์ฐ์ ์ธ ์ฃผ๊ธฐ์ผ ๋ฟ์ด๋ค.",
label="์
๋ ฅ ํ
์คํธ (Text Preprocessing & Tokenizing)"
)
submit_btn = gr.Button("๐ AI ์ถ๋ก ๋ฐ ๋ถ์ ์คํ", variant="primary")
gr.Markdown("### ๐ Model Card ๊ฐ์")
gr.Markdown(
"""
<div class="model-card-box">
<b>โข ๋ชจ๋ธ๋ช
:</b> Team 3 LoRA Climate Misinfo LLM (KoELECTRA-small-v3 ๊ธฐ๋ฐ)<br>
<b>โข ํ์ต ๋ฐ์ดํฐ:</b> ๊ธฐํ ๋ฐ์ดํฐ๋ฒ ์ด์ค (2024~2026, 200๊ฑด ๊ฒ์์๋ฃ)<br>
<b>โข ์ฑ๋ฅ ์งํ:</b> Acc 85%, F1 0.82 (TP:100, TN:100, FP:10, FN:20)<br>
<b>โข ์ค๋ฆฌ ์ฒดํฌ:</b> ์ํ/๋ฒ๋ฅ ์๋ ํ๋จ ๊ธ์ง
</div>
"""
)
# Right Column
with gr.Column(scale=1):
gr.Markdown("### ๐ค ์ถ๋ก ๋ฐ XAI ๋ถ์ ๊ฒฐ๊ณผ (Output)")
output_text = gr.Textbox(
label="์ต์ข
ํ๋ณ ๊ฒฐ๊ณผ (Output Generation)",
lines=6,
interactive=False
)
with gr.Tabs():
with gr.TabItem("๐ ํ๋จ ๊ฐ์ด๋ (xAI)"):
xai_output = gr.Markdown("๋ถ์ ์คํ ํ ํ์๋ฉ๋๋ค.")
with gr.TabItem("๐ ์ดํ
์
๋งต ๋ถ์"):
attention_output = gr.Markdown("๋ถ์ ์คํ ํ ํ์๋ฉ๋๋ค.")
gr.Markdown("### โ ๏ธ ํ๊ณ ๊ณ ์ง ๋ฐ ์ฑ
์ ์ ์ธ (Model Card Section 5 & 6)")
disclaimer_output = gr.Markdown(
"""
<div class="disclaimer-box">
<b>โข ํ๊ณ ์ฌ์ ๊ณ ์ง:</b> ๋ณธ ๋ชจ๋ธ์ ์คํธ๋ ์ค ํ
์คํธ ๊ฒฐ๊ณผ ์คํ ์ํฉ ๋ฐ ์๊ณก ๋ฌธ๋งฅ์์ ์คํ ์ํ์ด ์กด์ฌํฉ๋๋ค.<br>
<b>โข ์ด์ ์ ๊ธฐ ํต๋ก:</b> ๊ฒฐ๊ณผ์ ๋ํ ๋ฌธ์ ๋ฐ ํผ๋๋ฐฑ์ ๊ณต์ ๋ชจ๋ 3-9 ์ด์์ ๊ธฐ ์ฐฝ๊ตฌ๋ฅผ ํตํด ์ ์ ๊ฐ๋ฅํฉ๋๋ค.
</div>
""",
elem_classes=["disclaimer-box"]
)
submit_btn.click(
fn=run_pipeline,
inputs=[user_input],
outputs=[output_text, xai_output, attention_output, disclaimer_output]
)
if __name__ == "__main__":
demo.launch() |