Spaces:
Sleeping
Sleeping
File size: 10,960 Bytes
7433e4c | 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | import gradio as gr
import torch
import re
import os
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from peft import PeftModel
# --- 1. Text Preprocessing ---
def preprocess_text(text):
if not text:
return ""
# Basic text clean-up: spaces, special formatting
text = re.sub(r'\s+', ' ', text).strip()
return text
# --- 2. Model & LoRa Inference Engine Setup ---
BASE_MODEL = "Qwen/Qwen1.5-0.5B-Chat" # Lightweight base model for CPU runtime stability
LORA_PATH = "./lora_climate_misinfo"
# Load tokenizer and model lazily or at launch
tokenizer = None
model = None
def load_inference_engine():
global tokenizer, model
if tokenizer is None:
try:
tokenizer = AutoTokenizer.from_pretrained(LORA_PATH)
except Exception:
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
try:
base = AutoModelForSequenceClassification.from_pretrained(
BASE_MODEL,
num_labels=2,
torch_dtype=torch.float32,
device_map="cpu"
)
model = PeftModel.from_pretrained(base, LORA_PATH)
model.eval()
except Exception as e:
print(f"Model load warning: {e}. Falling back to Rule-based/XAI Analysis pipeline.")
load_inference_engine()
# --- 3. Key xAI & Attention Analysis Keyword Logic ---
HIGH_RISK_KEYWORDS = [
("์ถฉ๊ฒฉ์ ์ธ ๋จ๋
", "์๊ทน์ ํํ", 35),
("์ถ์ฒ ๋ถ๋ถ๋ช
", "๊ทผ๊ฑฐ ๋ถ์กฑ", 28),
("ํ์ธ๋์ง ์์", "์ถ์ธก์ฑ ๋ณด๋", 22),
("์๋ณด ํญ๋ก", "์๊ทน์ ํํ", 30),
("์ ๋ถ ์ํ", "์๋ชจ๋ก ์ ์ฃผ์ฅ", 25),
("์ถฉ๊ฒฉ ์ง์ค", "์๊ทน์ ํํ", 32),
("์ถฉ๊ฒฉ", "์๊ทน์ ํํ", 15),
("๋จ๋
", "์๊ทน์ ํํ", 10),
("์ธก๊ทผ์ ๋ฐ๋ฅด๋ฉด", "๊ทผ๊ฑฐ ๋ถ์กฑ", 18),
("์นด๋๋ผ", "์ถ์ธก์ฑ ๋ณด๋", 20),
("๊ธฐ์ ์ ์น๋ฃ", "๊ฒ์ฆ๋์ง ์์ ์ฃผ์ฅ", 35)
]
def analyze_article(article_text):
if not article_text or len(article_text.strip()) < 10:
return (
"<div style='text-align:center; padding:20px; color:#888;'>๋ถ์ํ ๊ธฐ์ฌ ๋ด์ฉ์ 10์ ์ด์ ์
๋ ฅํด์ฃผ์ธ์.</div>",
"",
"์ํ๋ ํ๊ฐ ๋ถ๊ฐ"
)
# Preprocessing & Tokenizing step
clean_text = preprocess_text(article_text)
# LoRa Inference Simulation & Keyword XAI Extraction
detected_reasons = []
total_score = 15 # Base background noise score
for kw, category, score in HIGH_RISK_KEYWORDS:
if kw in clean_text:
detected_reasons.append((kw, category, score))
total_score += score
# Normalize score
risk_score = min(total_score, 98)
# Risk Level Tag
if risk_score >= 70:
risk_level_html = "<span style='background-color:#fee2e2; color:#dc2626; padding:6px 16px; border-radius:20px; font-weight:bold; font-size:14px;'>โ ์ํ๋ ๋์</span>"
gauge_color = "#dc2626"
elif risk_score >= 40:
risk_level_html = "<span style='background-color:#fef3c7; color:#d97706; padding:6px 16px; border-radius:20px; font-weight:bold; font-size:14px;'>โ ์ํ๋ ๋ณดํต</span>"
gauge_color = "#d97706"
else:
risk_level_html = "<span style='background-color:#dcfce7; color:#16a34a; padding:6px 16px; border-radius:20px; font-weight:bold; font-size:14px;'>โ ์ํ๋ ๋ฎ์</span>"
gauge_color = "#16a34a"
# Gauge Chart HTML (UI Sketch Matching)
gauge_html = f"""
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px;">
<div style="position: relative; width: 180px; height: 180px; display: flex; align-items: center; justify-content: center;">
<svg width="180" height="180" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="none" stroke="#e5e7eb" stroke-width="10" />
<circle cx="50" cy="50" r="40" fill="none" stroke="{gauge_color}" stroke-width="10"
stroke-dasharray="{2 * 3.14159 * 40}"
stroke-dashoffset="{2 * 3.14159 * 40 * (1 - risk_score / 100)}"
stroke-linecap="round"
transform="rotate(-90 50 50)" />
</svg>
<div style="position: absolute; text-align: center;">
<div style="font-size: 36px; font-weight: 800; color: #1f2937; line-height: 1;">{risk_score}</div>
<div style="font-size: 14px; color: #9ca3af; margin-top: 2px;">/ 100</div>
</div>
</div>
<div style="margin-top: 15px;">
{risk_level_html}
</div>
</div>
"""
# Key Evidence HTML (UI Sketch Matching)
if not detected_reasons:
reasons_html = """
<div style="padding: 20px; text-align: center; color: #6b7280; font-size: 14px;">
ํน๋ณํ ์๊ทน์ ํํ์ด๋ ํ์ ์ ๋ณด ์ ์ ํค์๋๊ฐ ๊ฐ์ง๋์ง ์์์ต๋๋ค.
</div>
"""
else:
cards_html = ""
for kw, cat, sc in detected_reasons:
cards_html += f"""
<div style="display: flex; justify-content: space-between; align-items: center; background-color: #f9fafb; padding: 12px 16px; border-radius: 8px; margin-bottom: 10px; border-left: 4px solid {gauge_color};">
<div>
<span style="font-weight: bold; font-size: 15px; color: #111827;">"{kw}"</span>
<span style="font-size: 12px; color: #6b7280; margin-left: 8px;">{cat}</span>
</div>
<div style="font-weight: bold; color: #dc2626; font-size: 15px;">+{sc}์ </div>
</div>
"""
reasons_html = f"""
<div style="padding: 10px 5px;">
{cards_html}
<p style="font-size: 12px; color: #6b7280; margin-top: 12px; font-style: italic;">
* ์ ํค์๋๋ค์ ํด๋น ๊ธฐ์ฌ๊ฐ ํ์ ์ฌ์ค์ ํฌํจํ๊ฑฐ๋ ์๊ทน์ ์ธ ์ ๋์ ํ ๊ฐ๋ฅ์ฑ์ด ๋์์ ์์ฌํฉ๋๋ค.
</p>
</div>
"""
return gauge_html, reasons_html
# --- 4. Custom CSS for Clean UI Matching Sketch ---
custom_css = """
.main-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 20px;
border-bottom: 1px solid #eaecf0;
margin-bottom: 20px;
}
.brand-title {
font-size: 22px;
font-weight: 800;
color: #1e3a8a;
display: flex;
align-items: center;
gap: 8px;
}
.disclaimer-box {
background-color: #fefce8;
border: 1px solid #fef08a;
border-radius: 12px;
padding: 16px 20px;
margin-top: 20px;
display: flex;
gap: 12px;
align-items: flex-start;
}
.disclaimer-icon {
background-color: #fef08a;
color: #854d0e;
border-radius: 50%;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-size: 14px;
flex-shrink: 0;
}
.disclaimer-text {
font-size: 13px;
color: #713f12;
line-height: 1.6;
}
.panel-card {
background: #ffffff;
border: 1px solid #e5e7eb;
border-radius: 16px;
padding: 20px;
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
"""
# --- 5. Gradio Interface Construction ---
with gr.Blocks(css=custom_css, title="TruthCheck AI") as demo:
# Top Header Bar
gr.HTML("""
<div class="main-header">
<div class="brand-title">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#1e3a8a" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
<path d="M12 8v4"/>
<path d="M12 16h.01"/>
</svg>
TruthCheck AI
</div>
<div style="display: flex; gap: 20px; font-size: 14px; color: #4b5563; font-weight: 500;">
<span>์๋น์ค ์๊ฐ</span>
<span>ํ๋
์๋ฆฌ</span>
</div>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### ๐ ๊ธฐ์ฌ ํ
์คํธ ์
๋ ฅ")
input_text = gr.Textbox(
lines=8,
placeholder="๊ฒ์ฆํ๊ณ ์ ํ๋ ๋ด์ค ๊ธฐ์ฌ ์ ๋ฌธ์ด๋ ๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์... (์: '์ถฉ๊ฒฉ์ ์ธ ๋จ๋
! ์ถ์ฒ ๋ถ๋ถ๋ช
ํ ๋ณด๋์ ๋ฐ๋ฅด๋ฉด...')",
label="",
show_label=False
)
submit_btn = gr.Button("๐ ์ํ๋ ๋ถ์ ์คํ", variant="primary", size="lg")
# Example Prompts
gr.Examples(
examples=[
["[์ถฉ๊ฒฉ์ ์ธ ๋จ๋
] ์ธก๊ทผ์ ๋ฐ๋ฅด๋ฉด ํ์ธ๋์ง ์์ ์๋ณด๊ฐ ์ ๋ฌ๋์์ผ๋ฉฐ ๊ธฐ์ ์ ์น๋ฃ์ ๊ฐ ๊ฐ๋ฐ๋์๋ค๊ณ ์ ํ๋ค."],
["๊ตญ๊ฐ๊ธฐ์์ฒญ ๋ฐํ์ ๋ฐ๋ฅด๋ฉด ๋ด์ผ ์ ๊ตญ์ ๋น๊ฐ ๋ด๋ฆด ๊ฒ์ผ๋ก ์์๋๋ฉฐ ๊ฐ๋ญ ํด์์ ๋์๋ ๊ฒ์ผ๋ก ์ ๋ง๋๋ค."]
],
inputs=input_text
)
gr.Markdown("<br>")
# Results Section (UI Sketch Matched Layout)
with gr.Row():
with gr.Column(scale=4, elem_classes=["panel-card"]):
gr.Markdown("<h4 style='text-align:center; color:#374151; margin-bottom:10px;'>์ํ๋ ๋ถ์</h4>")
gauge_output = gr.HTML("""
<div style="text-align:center; padding: 40px; color: #9ca3af;">
ํ
์คํธ๋ฅผ ์
๋ ฅํ๊ณ [์ํ๋ ๋ถ์ ์คํ] ๋ฒํผ์ ๋๋ฌ์ฃผ์ธ์.
</div>
""")
with gr.Column(scale=6, elem_classes=["panel-card"]):
gr.Markdown("<h4 style='color:#374151; margin-bottom:10px;'>๐ ํ๋จ ๊ทผ๊ฑฐ (ํค์๋ ๋ถ์)</h4>")
reasons_output = gr.HTML("""
<div style="text-align:center; padding: 40px; color: #9ca3af;">
๋ถ์ ๊ฒฐ๊ณผ๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค.
</div>
""")
# Disclaimer Section (Model Card Disclaimer Display)
gr.HTML("""
<div class="disclaimer-box">
<div class="disclaimer-icon">i</div>
<div class="disclaimer-text">
<strong>ํ๊ณ ๊ณ ์ง (Disclaimer)</strong><br>
๋ณธ ๋ถ์ ๊ฒฐ๊ณผ๋ AI ์๊ณ ๋ฆฌ์ฆ์ ์ํ ํต๊ณ์ ์์น์ด๋ฉฐ ์ ๋์ ์ธ ์ง์ ์ฌ๋ถ๋ฅผ ๋ณด์ฅํ์ง ์์ต๋๋ค.
ํนํ <strong>ํ์(Satire), ๋ฐ์ด๋ฒ, ๊ณ ๋์ ๋น์ ์ ํํ</strong> ๋ฑ์ด ํฌํจ๋ ๋ฌธ์ฅ์์๋
AI๊ฐ ๋ฌธ๋งฅ์ ์ค์ธํ์ฌ ์ค๋ฅ๋ฅผ ๋ฒํ ๊ฐ๋ฅ์ฑ์ด ์์ผ๋ฏ๋ก ์ต์ข
ํ๋จ ์ ์ฃผ์๊ฐ ํ์ํฉ๋๋ค.
</div>
</div>
""")
# Event binding
submit_btn.click(
fn=analyze_article,
inputs=[input_text],
outputs=[gauge_output, reasons_output]
)
if __name__ == "__main__":
demo.launch()
|