Spaces:
Sleeping
Sleeping
File size: 2,062 Bytes
1b30a9a | 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 | # =========================
# 🤖 BOT DETECTION (BERT)
# =========================
from transformers import pipeline
# =========================
# LOAD MODEL (SAFE)
# =========================
try:
bot_model = pipeline(
"text-classification",
model="unitary/toxic-bert", # ringan & cepat
device=-1 # CPU (HF aman)
)
print("✅ BERT bot model loaded")
except Exception as e:
print("⚠️ BERT model gagal load:", e)
bot_model = None
# =========================
# 🔥 FALLBACK RULE-BASED
# =========================
def fallback_bot_detection(text):
score = 0
if len(text) < 20:
score += 1
if text.count("!") > 2:
score += 1
if len(set(text.split())) < 4:
score += 1
if text.isupper():
score += 1
label = "Bot" if score >= 2 else "Human"
return {
"text": text,
"score": score / 4,
"label": label,
"method": "fallback"
}
# =========================
# 🔥 MAIN FUNCTION
# =========================
def detect_bot_bert(texts):
results = []
# 🔥 jika model gagal → fallback semua
if bot_model is None:
print("⚠️ fallback mode aktif")
return [fallback_bot_detection(t) for t in texts[:20]]
try:
for t in texts[:20]:
# batasi panjang (biar tidak error)
t_clean = t[:512]
res = bot_model(t_clean)[0]
score = float(res["score"])
# interpretasi label
if res["label"].lower() in ["toxic", "toxic"]:
label = "Bot" if score > 0.6 else "Human"
else:
label = "Human"
results.append({
"text": t,
"score": round(score, 3),
"label": label,
"method": "bert"
})
return results
except Exception as e:
print("❌ BERT inference error:", e)
# fallback kalau inference gagal
return [fallback_bot_detection(t) for t in texts[:20]] |