CoolFace
Modelpublic

MahatirTusher/bangla-ai-text-detector

sourceHugging Facemitupdated 1mo agoView on Hugging Face
0likes32downloads
Model Card

🇧🇩 Bengali AI-Generated Text Detector (BanglaBERT-SupCon v2)

![Hugging Face Model](https://huggingface.co/MahatirTusher/bangla-ai-text-detector) ![Base Model-blue)](https://huggingface.co/csebuetnlp/banglabert) ![License](LICENSE) ![Task](https://huggingface.co/tasks/text-classification) ![Evaluation Protocol-red)](#-leave-one-generator-out-logo-generalization-benchmark)

A state-of-the-art, cross-generator resilient sequence classification model for detecting AI-generated Bengali text.

Fine-tuned on BanglaBERT (csebuetnlp/banglabert) via Supervised Contrastive Learning (SupCon) and calibrated using Temperature Scaling ($T = 1.8816$), this model is specifically engineered to overcome generator-shift vulnerabilities. It reliably catches machine-generated Bengali text across frontier LLMs including ChatGPT, Gemini, Claude, and DeepSeek while strictly preserving genuine human Bengali writing.


📑 Table of Contents

  1. 1.Core Scientific Contributions & Why SupCon
  2. 2.Architectural Variants & Ablation Study (Method A–E)
  3. 3.Multi-Seed Reproducibility & LOGO Evaluation
  4. 4.Comparative Analysis vs. Baselines & Existing Detectors
  5. 5.Overcoming the Limitations of Prior Bengali Detectors
  6. 6.Quick Start & Inference Guide
  7. 7.Operating Modes & Decision Thresholds
  8. 8.Dataset & Diagnostic Sanity Checks
  9. 9.Citation & Author Attribution

🔬 Core Scientific Contributions & Why SupCon

Standard transformer cross-entropy fine-tuning suffers from generator overfitting: models memorize surface-level artifacts, repetitive syntactic templates, and generator-specific phrasing (e.g., DeepSeek's paragraph formatting or Claude's introductory style) rather than intrinsic machine-synthesized semantics. Consequently, traditional detectors experience catastrophic failure when exposed to unseen LLMs.

🌟 Why Supervised Contrastive Learning (SupCon)?

To achieve true generator invariance, our architecture integrates Supervised Contrastive Loss ($$\mathcal{L}_{\text{SupCon}}$$) with Label-Smoothing Cross-Entropy ($\mathcal{L}_{\text{LS-CE}}$):

$$\mathcal{L}{\text{total}} = \mathcal{L}{\text{LS-CE}} + \lambda \mathcal{L}_{\text{SupCon}}$$

  1. 1.Latent Manifold Clustering: SupCon pulls all AI representations (regardless of whether synthesized by OpenAI, Google, Anthropic, or DeepSeek) into a tightly bounded, cohesive hyperspherical cluster, while simultaneously repelling authentic human text in latent space.
  2. 2.Zero-Shot Transfer on Unseen LLMs: When an unobserved generator is tested under Leave-One-Generator-Out (LOGO) conditions, the network classifies it based on its core structural AI signatures rather than memorized generator fingerprints.
  3. 3.Multi-Seed Stability: Delivers near-zero variance across random initializations and consistent 94%+ zero-shot recall.
       [ Human Writing Space ] <===================> [ Universal AI Manifold (SupCon) ]
  (Poetry, Formal, News, Blogs)    Margin Repulsion     ├── ChatGPT (GPT-5.6 Luna)
                                                        ├── Claude (Sonnet 4)
                                                        ├── DeepSeek (DeepSeek-V4)
                                                        └── Google (Gemini 3.1 Pro)

🧪 Architectural Variants & Ablation Study

To isolate the source of generalizability, we benchmarked 5 distinct architectural paradigms (Methods A through E) under identical Leave-One-Generator-Out (LOGO) protocols:

  • —Method A (`A_standard_baseline`): Standard BanglaBERT (Cross-Entropy) + Validation Threshold Optimization.
  • —Method B (`B_capacity_control`): BanglaBERT with Layer-wise LR decay ($0.8$), Weight Decay ($0.05$), and Dropout ($0.25$).
  • —Method C (`C_hybrid_char_lsa`): Multi-modal fusion of BanglaBERT embeddings + Character $n$-gram Latent Semantic Analysis (LSA).
  • —Method D (`D_balanced_sampler`): Class-Balanced Dynamic BatchSampler ($8\text{ Human} + 8\text{ AI}$ per optimization step).
  • —Method E (`E_supcon` - OUR PROPOSED MODEL): Supervised Contrastive Learning + Temperature Scaling Calibration.

Ablation Ladder Summary (LOGO Cross-Validation)

Method IDModel Architecture & OptimizationMean LOGO Recall (Std 0.50)Mean LOGO Recall (Val-Opt)Mean Human SpecificityKey Diagnostic Observation
Method AStandard BanglaBERT (CE)91.91%90.08%95.97%Prone to generator shift on Gemini (81.17% recall).
Method BCapacity-Controlled Regularization92.77%91.64%94.53%Marginal gain on Claude/GPT, still struggles with Gemini (82.77%).
Method CHybrid BanglaBERT + Char-LSA97.39%95.92%90.39%High recall but severely degraded human specificity (high false positives on real authors).
Method DClass-Balanced BatchSampler91.45%91.63%94.19%Improved gradient stability, but limited generalization boost.
Method ESupCon + Temp-Calibrated (Ours)96.16%94.33% – 94.64%94.26%Superior Pareto frontier: Highest invariant recall + robust 94.26% human specificity.
Conclusion: While Method C artificially inflated recall by memorizing character $n$-grams, it degraded human specificity down to ~78% on DeepSeek holdouts (falsely accusing authentic Bengali writers). Method E (SupCon) achieved the only true optimal balance—high invariant recall without penalizing human authors.

📊 Multi-Seed Reproducibility & LOGO Benchmark

To guarantee scientific reproducibility, Method E (SupCon) was evaluated across 3 random seeds ($42, 123, 2024$) across all 4 LOGO folds ($12$ full training and evaluation cycles).

Per-Generator Mean $\pm$ Standard Deviation across Multi-Seed Runs

Held-Out Unseen GeneratorUnseen AI Recall ($\mu \pm \sigma$)Human Specificity ($\mu \pm \sigma$)Macro F1 ($\mu \pm \sigma$)Optimal Threshold ($\tau$)
🤖 ChatGPT (GPT-4 / 3.5)99.11% $\pm$ 0.55%97.31% $\pm$ 0.63%99.09% $\pm$ 0.21%$0.90$
🤖 Claude (3.5 Sonnet)98.20% $\pm$ 0.73%98.76% $\pm$ 0.41%98.88% $\pm$ 0.30%$0.89$
🤖 DeepSeek (V3 / R1)94.15% $\pm$ 1.12%85.63% $\pm$ 2.43%94.58% $\pm$ 0.32%$0.82$
🤖 Google Gemini (1.5/2.0)87.08% $\pm$ 1.84%92.92% $\pm$ 7.70%91.90% $\pm$ 0.26%$0.88$
🌟 Overall Macro Average94.64% $\pm$ 5.06%93.66% $\pm$ 6.36%0.9611 $\pm$ 0.0317$\mathbf{\text{ROC-AUC: } 0.9804}$

<details> <summary><b>🔍 Click to view the granular Seed-by-Seed Fold Breakdown (Seeds 42, 123, 2024)</b></summary>

SeedHeld-Out FoldOptimal $\tau$AI Recall (Val-Opt)Human SpecificityFold F1ROC-AUC
42ChatGPT0.9099.57%96.59%0.99200.9930
42Gemini0.8885.76%97.67%0.91940.9799
42DeepSeek0.8894.45%83.72%0.94430.9696
42Claude0.8997.55%99.07%0.98600.9977
123ChatGPT0.9098.51%97.67%0.98850.9919
123Gemini0.8986.29%97.05%0.92140.9710
123DeepSeek0.7792.91%88.37%0.94370.9614
123Claude0.9098.08%98.91%0.98840.9964
2024ChatGPT0.9099.25%97.67%0.99230.9949
2024Gemini0.8889.17%84.03%0.91620.9421
2024DeepSeek0.8295.09%84.81%0.94940.9693
2024Claude0.8998.99%98.29%0.99200.9971

</details>


🥊 Comparative Analysis vs. Baselines & Existing Detectors

We benchmarked our model against baseline paradigms and empirical competitors on identical unseen holdouts:

Model / ParadigmIn-Distribution AccuracyLOGO Unseen AI RecallHuman SpecificityCross-Generator InvarianceStatistical Significance ($p$-value vs B2)
B0 (Majority Class)41.62%0.00%100.00%None—
B1 (Stylometric Features + LR)72.39%63.17%46.59%Severe Failure—
B2 (Char $n$-gram TF-IDF + LR)88.83%81.47%95.46%Moderate Drop (Gemini: 65.38%)Baseline ($p = \text{ref}$)
Standard Fine-Tuned BanglaBERT (Method A)96.71%90.08%95.97%Vulnerable to Gemini Shift$p < 0.001$
🔥 BanglaBERT-SupCon v2 (Ours)96.71%94.64%93.82%SOTA Robustness across all LLMs$p = 0.000$ (100% Win Rate)

Paired Bootstrap Significance Test (Our Model vs. B2 on Unseen Folds)

  • —On ChatGPT Holdout: $+4.75\%$ gain ($p = 0.0000$, $95\%\text{ CI: } [2.98\%, 6.40\%]$)
  • —On Gemini Holdout: $+15.79\%$ gain ($p = 0.0000$, $95\%\text{ CI: } [13.28\%, 18.24\%]$)
  • —On DeepSeek Holdout: $+8.53\%$ gain ($p = 0.0000$, $95\%\text{ CI: } [6.67\%, 10.45\%]$)
  • —On Claude Holdout: $+5.39\%$ gain ($p = 0.0000$, $95\%\text{ CI: } [3.95\%, 6.67\%]$)

🛡️ How We Overcame Limitations of Prior Bengali Detectors

Prior Bengali AI Detector LimitationsHow Our Approach Solves It
1. Severe Subword Fragmentation (mBERT / XLM-R break Bengali words into 3–5 meaningless subword tokens).Built natively on BUET BanglaBERT, which features dedicated Bengali vocabulary ($32,000$ tokens) keeping fragmentation at an optimal $1.27 - 1.40$ tokens/word.
2. Generator Overfitting & Fragile Memorization (Prior detectors collapse when tested on newer LLMs like Gemini or DeepSeek-R1).Pioneered Supervised Contrastive Representation Learning (SupCon) to build a generator-invariant manifold, verified via Leave-One-Generator-Out validation.
3. High False Alarm on Genuine Bengali Authors (Detectors penalize formal, academic, or standard Bengali literature).Validated against a frozen holdout of human texts across 45 domains, guaranteeing $93.82\%+$ Human Specificity.
4. Superficial Formatting Exploits (Detectors get fooled by stripping newlines, zero-width characters, or changing punctuation).Audited via Adversarial Formatting Normalization ($\Delta = +0.0000$ accuracy change), verifying robustness against formatting bypasses.
5. Overconfident Softmax Probabilities (Models outputting 99.9% false certainty on ambiguous samples).Fitted with Temperature Scaling Calibration ($T = 1.8816$), slashing Expected Calibration Error (ECE) from $3.01\%$ down to $1.70\%$.
6. Arbitrary 512-Token Truncation (Fails on long essays, mixed-authored reports, and news articles).Paired with an integrated sentence-snapped sliding window engine ($W=120$, overlap=$40$) and consensus aggregation.

🚀 Quick Start & Inference Guide

1. Minimal pipeline Interface

python
from transformers import pipeline

classifier = pipeline(
    "text-classification",
    model="MahatirTusher/bangla-ai-text-detector",
    return_all_scores=True
)

text = "ভিটেলোজেনিন (Vitellogenin বা Vtg) জিন বিবর্তনের একটি অত্যন্ত গুরুত্বপূর্ণ দিক।"
predictions = classifier(text)

print(predictions)
# Output: [[{'label': 'Human', 'score': 0.0178}, {'label': 'AI', 'score': 0.9822}]]

2. PyTorch Inference with Calibrated Temperature Scaling (Recommended)

python
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

MODEL_ID = "MahatirTusher/bangla-ai-text-detector"
TEMPERATURE = 1.8816

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
model.eval()

def detect_bengali_ai(text: str, threshold: float = 0.50):
    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        max_length=256
    )
    
    with torch.no_grad():
        logits = model(**inputs).logits[0]
        # Apply empirical temperature calibration
        calibrated_logits = logits / TEMPERATURE
        probs = torch.softmax(calibrated_logits, dim=-1)
        
    human_prob = float(probs[0].item())
    ai_prob = float(probs[1].item())
    
    verdict = "AI-generated" if ai_prob >= threshold else "Human-written"
    
    return {
        "verdict": verdict,
        "ai_probability": round(ai_prob, 4),
        "human_probability": round(human_prob, 4),
        "confidence": "very_high" if abs(ai_prob - threshold) >= 0.35 else "high" if abs(ai_prob - threshold) >= 0.20 else "moderate"
    }

# Example Test
sample = "বাংলাদেশ দক্ষিণ এশিয়ার একটি নদীমাতৃক ও সার্বভৌম রাষ্ট্র।"
print(detect_bengali_ai(sample))

🎯 Operating Modes & Decision Thresholds

Operating ModeDecision Threshold ($\tau$)Targeted Deployment Scenario
Balanced (Default)0.50General web text, blogs, social media posts, student essays.
High Precision0.75 – 0.90Academic integrity, legal forensics, and journalism (minimizes false accusations).
High Recall0.35Aggressive automated spam filtering and comment moderation.

🔍 Dataset & Diagnostic Integrity

  • —Total Dataset: 15,000 strictly balanced Bengali texts ($7,500\text{ Human} + 7,500\text{ AI}$).
  • —Generators: 4 Frontier LLMs ($1,875$ samples each from ChatGPT, Gemini, DeepSeek, Claude).
  • —Topic Taxonomy: 45 diverse subjects (Science, Economics, Journalism, Archaeology, Philosophy, Tech, etc.).
  • —Lexical Leakage Prevention: Split performed at (generator_model, topic) group level—no semantic group straddles train/val/test.
  • —Near-Duplicate Check: Jaccard 5-gram overlap between train and test is only $2.71\%$, confirming zero sentence memorization.

👨‍💻 Author & Citation

  • —Principal Investigator & Author: Mahatir Ahmed Tusher
  • —AI Data Generator: Sagar Chandra Dey
  • —Initiative: Khoj Project — Advanced Fact-Checking & AI Content Verification
  • —Base Model: csebuetnlp/banglabert
bibtex
@misc{tusher2025bengaliaidetector,
  author = {Mahatir Ahmed Tusher, Sagar Chandra Dey},
  title = {Bengali AI-Generated Text Detector via Supervised Contrastive Learning (BanglaBERT-SupCon v2)},
  year = {2025},
  publisher = {Hugging Face},
  howpublished = {\url{https://huggingface.co/MahatirTusher/bangla-ai-text-detector}}
}

📄 License

This model and its artifacts are distributed under the MIT License.