Spaces:
Sleeping
Sleeping
| # ========================= | |
| # π€ 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]] |