jeanlee/kmhas_korean_hate_speech
Viewer โข Updated โข 110k โข 1.98k โข 24
Qwen/Qwen3-Embedding-0.6B์ ๋๊ฒฐ ์๋ฒ ๋ฉ ๊ณต๊ฐ๊ณผ **2์ค ๋ฐฉ์ด์ (์ง์ค ์์ค 0.1ms ์ฐจ๋จ + AI ๋ฌธ๋งฅ ์ค์บ)**์ ํตํด 50ms ์ด๋ด์ ํ๊ตญ์ด ๋น์์ด๋ฅผ ํ์งํ๊ณ , 4๊ฐ๊ตญ(ํยท๋ฏธยท์ผยท์ค) ๊ฐ์ธ์ ๋ณด(PII)์ ํด๋ผ์ฐ๋ API ํค๋ฅผ ๋ง์คํนํ๋ ์ด๊ณ ์ ๊ฐ๋ ๋ชจ๋ธ์
๋๋ค.
pip install torch sentence-transformers huggingface_hub
import torch
import torch.nn as nn
import torch.nn.functional as F
from sentence_transformers import SentenceTransformer
from huggingface_hub import hf_hub_download
device = "cuda" if torch.cuda.is_available() else "cpu"
# 1. Qwen3 ์๋ฒ ๋ฉ ๋ฐฑ๋ณธ ๋ก๋
embed_model = SentenceTransformer("Qwen/Qwen3-Embedding-0.6B")
# 2. ๊ฒฐ์ ํค๋(Probe) ์ ์
class CalibratedJevProbe(nn.Module):
def __init__(self, in_features=1024):
super().__init__()
self.net = nn.Sequential(
nn.LayerNorm(in_features),
nn.Linear(in_features, 256),
nn.GELU(),
nn.Dropout(0.15),
nn.Linear(256, 64),
nn.GELU(),
nn.Linear(64, 2)
)
def forward(self, z):
return self.net(z)
# 3. ํ๊น
ํ์ด์ค์์ ๊ฐ์ค์น ์๋ ๋ค์ด๋ก๋ & ๋ก๋
weights_path = hf_hub_download(
repo_id="jaichang/korean-toxicity-pii-guard-qwen3",
filename="jev_probe_weights.pt"
)
probe = CalibratedJevProbe(in_features=1024).to(device)
probe.load_state_dict(torch.load(weights_path, map_location=device))
probe.eval()
# 4. ์ค์๊ฐ ๋น์์ด ํ์
INSTRUCT_PREFIX = "Instruct: Classify whether the Korean text contains profanity, insults, or toxicity.\nQuery: "
def check_toxicity(text: str):
with torch.no_grad():
emb = embed_model.encode([INSTRUCT_PREFIX + text], convert_to_tensor=True, normalize_embeddings=True).to(device).float()
logits = probe(emb)
toxic_prob = F.softmax(logits, dim=-1)[0, 1].item()
if toxic_prob >= 0.75: return f"๐จ ์ฐจ๋จ (์ ํดํ๋ฅ : {toxic_prob*100:.1f}%)"
elif toxic_prob >= 0.30: return f"โ ๏ธ ๊ฒํ (์ ํดํ๋ฅ : {toxic_prob*100:.1f}%)"
else: return f"โ
ํต๊ณผ (์ ํดํ๋ฅ : {toxic_prob*100:.1f}%)"
# ํ
์คํธ
print(check_toxicity("๊ฐ1์2๋ผ์ผ ์์ํด๋ผ")) # ๐จ ์ฐจ๋จ
print(check_toxicity("์๋ก์ด ํ๋ก์ ํธ์ ์๋ฐ์ ์ ์ฐ์ต๋๋ค.")) # โ
ํต๊ณผ
| ์ ๋ ฅ ๋ฌธ์ฅ ์ ํ | ๋ํ ์์ | ํ์ ๊ฒฐ๊ณผ | ์ง์ฐ ์๊ฐ |
|---|---|---|---|
| ์ ์ ๋์์ด์์ด (์คํ ์ ๋ก) | ์๋ฐ์ ์ ์ฐ์ต๋๋ค, 18์ ํฌ๋ ํ์ค, ๊ฐ๋๋ฆฌ |
โ ํต๊ณผ (0.3% ๋ฏธ๋ง) | ~50ms |
| ์๋ชจ/ํน์๋ฌธ์ ๋ณํ ์ ํ | ๊ฐ1์2๋ผ์ผ, ใ
ใ
์ด์ด์๋ค, ใ
ใด ์ง์ฆ |
๐จ 100% ์ฆ์ ์ฐจ๋จ | 0.1ms |
| ์ธ์ ๊ณต๊ฒฉ ํธ์นญ | ์ธ์ ๊ทธ๋ ๊ฒ ์ด์ง ๋ง๋ผ ์ฐ๋ ๊ธฐ์ผ |
๐จ ์ฆ์ ์ฐจ๋จ | 0.1ms |
| ์ง์ค์ ํจ๋ฅ/์์ค | ๊บผ์ ธ ๋ณ์ ์ ๋ฅ์ณ |
๐จ ์ฆ์ ์ฐจ๋จ | 0.1ms |
sk-), Google Cloud(AIzaSy), AWS(AKIA), GitHub(ghp_), SSH Private Key