Spaces:
Paused
Paused
Delete services/services
Browse files
services/services/bot_bert.py
DELETED
|
@@ -1,91 +0,0 @@
|
|
| 1 |
-
# =========================
|
| 2 |
-
# 🤖 BOT DETECTION (BERT)
|
| 3 |
-
# =========================
|
| 4 |
-
|
| 5 |
-
from transformers import pipeline
|
| 6 |
-
|
| 7 |
-
# =========================
|
| 8 |
-
# LOAD MODEL (SAFE)
|
| 9 |
-
# =========================
|
| 10 |
-
try:
|
| 11 |
-
bot_model = pipeline(
|
| 12 |
-
"text-classification",
|
| 13 |
-
model="unitary/toxic-bert", # ringan & cepat
|
| 14 |
-
device=-1 # CPU (HF aman)
|
| 15 |
-
)
|
| 16 |
-
print("✅ BERT bot model loaded")
|
| 17 |
-
|
| 18 |
-
except Exception as e:
|
| 19 |
-
print("⚠️ BERT model gagal load:", e)
|
| 20 |
-
bot_model = None
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# =========================
|
| 24 |
-
# 🔥 FALLBACK RULE-BASED
|
| 25 |
-
# =========================
|
| 26 |
-
def fallback_bot_detection(text):
|
| 27 |
-
score = 0
|
| 28 |
-
|
| 29 |
-
if len(text) < 20:
|
| 30 |
-
score += 1
|
| 31 |
-
|
| 32 |
-
if text.count("!") > 2:
|
| 33 |
-
score += 1
|
| 34 |
-
|
| 35 |
-
if len(set(text.split())) < 4:
|
| 36 |
-
score += 1
|
| 37 |
-
|
| 38 |
-
if text.isupper():
|
| 39 |
-
score += 1
|
| 40 |
-
|
| 41 |
-
label = "Bot" if score >= 2 else "Human"
|
| 42 |
-
|
| 43 |
-
return {
|
| 44 |
-
"text": text,
|
| 45 |
-
"score": score / 4,
|
| 46 |
-
"label": label,
|
| 47 |
-
"method": "fallback"
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# =========================
|
| 52 |
-
# 🔥 MAIN FUNCTION
|
| 53 |
-
# =========================
|
| 54 |
-
def detect_bot_bert(texts):
|
| 55 |
-
results = []
|
| 56 |
-
|
| 57 |
-
# 🔥 jika model gagal → fallback semua
|
| 58 |
-
if bot_model is None:
|
| 59 |
-
print("⚠️ fallback mode aktif")
|
| 60 |
-
return [fallback_bot_detection(t) for t in texts[:20]]
|
| 61 |
-
|
| 62 |
-
try:
|
| 63 |
-
for t in texts[:20]:
|
| 64 |
-
|
| 65 |
-
# batasi panjang (biar tidak error)
|
| 66 |
-
t_clean = t[:512]
|
| 67 |
-
|
| 68 |
-
res = bot_model(t_clean)[0]
|
| 69 |
-
|
| 70 |
-
score = float(res["score"])
|
| 71 |
-
|
| 72 |
-
# interpretasi label
|
| 73 |
-
if res["label"].lower() in ["toxic", "toxic"]:
|
| 74 |
-
label = "Bot" if score > 0.6 else "Human"
|
| 75 |
-
else:
|
| 76 |
-
label = "Human"
|
| 77 |
-
|
| 78 |
-
results.append({
|
| 79 |
-
"text": t,
|
| 80 |
-
"score": round(score, 3),
|
| 81 |
-
"label": label,
|
| 82 |
-
"method": "bert"
|
| 83 |
-
})
|
| 84 |
-
|
| 85 |
-
return results
|
| 86 |
-
|
| 87 |
-
except Exception as e:
|
| 88 |
-
print("❌ BERT inference error:", e)
|
| 89 |
-
|
| 90 |
-
# fallback kalau inference gagal
|
| 91 |
-
return [fallback_bot_detection(t) for t in texts[:20]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|