File size: 6,608 Bytes
d7f4607
 
 
 
 
 
76ecb56
 
d7f4607
 
 
 
 
 
 
 
 
 
8b1a796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7f4607
 
36cc3ec
 
8b1a796
36cc3ec
 
 
 
 
 
8b1a796
 
36cc3ec
 
 
 
 
 
5f029c0
36cc3ec
5f029c0
36cc3ec
 
5f029c0
36cc3ec
 
 
 
 
 
8b1a796
36cc3ec
8b1a796
36cc3ec
5f029c0
 
8b1a796
 
 
 
 
 
 
 
36cc3ec
 
 
8b1a796
 
 
 
5f029c0
 
 
 
8b1a796
5f029c0
8b1a796
 
36cc3ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f029c0
36cc3ec
 
 
5f029c0
36cc3ec
 
 
5f029c0
36cc3ec
 
 
5f029c0
36cc3ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f029c0
36cc3ec
 
 
 
 
 
 
 
 
5f029c0
36cc3ec
 
 
 
 
 
8b1a796
36cc3ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f029c0
 
 
 
 
 
 
 
 
 
 
 
8b1a796
5f029c0
 
 
 
 
 
 
 
 
 
 
 
 
 
8b1a796
5f029c0
 
 
 
 
 
 
 
 
36cc3ec
 
 
 
5f029c0
8b1a796
 
 
 
5f029c0
 
 
8b1a796
 
 
 
36cc3ec
8b1a796
36cc3ec
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
---
language:
  - en
pipeline_tag: text-classification
library_name: peft
base_model: microsoft/deberta-v3-large
datasets:
  - stealthcode/ai-detection
tags:
  - lora
  - ai-detection
  - binary-classification
  - deberta-v3-large
metrics:
  - accuracy
  - f1
  - auroc
  - average_precision
model-index:
  - name: AI Detector LoRA (DeBERTa-v3-large)
    results:
      - task:
          type: text-classification
          name: AI Text Detection
        dataset:
          name: stealthcode/ai-detection
          type: stealthcode/ai-detection
        metrics:
          - type: auroc
            value: 0.9985
          - type: f1
            value: 0.9812
          - type: accuracy
            value: 0.9814
---

# AI Detector LoRA (DeBERTa-v3-large)

LoRA adapter for binary AI-text vs Human-text detection, trained on ~2.7M English samples
(`label: 1 = AI, 0 = Human`) using `microsoft/deberta-v3-large` as the base model.

- **Base model:** `microsoft/deberta-v3-large`
- **Task:** Binary classification (AI vs Human)
- **Head:** Single-logit + `BCEWithLogitsLoss`
- **Adapter type:** LoRA (`peft`)
- **Hardware:** 8 x RTX 5090, bf16, multi-GPU
- **Final decision threshold:** **0.8697** (max-F1 on calibration set)

---

## Files in this repo

- `adapter/` – LoRA weights saved with `peft_model.save_pretrained(...)`
- `merged_model/` – fully merged model (base + LoRA) for standalone use
- `threshold.json` – chosen deployment threshold and validation F1
- `calibration.json` – temperature scaling parameters and calibration metrics
- `results.json` – hyperparameters, validation threshold search, test metrics
- `training_log_history.csv` – raw Trainer log history
- `predictions_calib.csv` – calibration-set probabilities and labels
- `predictions_test.csv` – test probabilities and labels
- `figures/` – training and evaluation plots
- `README.md` – this file

---

## Metrics (test set, n=279,241)

Using threshold **0.8697**:

| Metric                 | Value  |
| ---------------------- | ------ |
| AUROC                  | 0.9985 |
| Average Precision (AP) | 0.9985 |
| F1                     | 0.9812 |
| Accuracy               | 0.9814 |
| Precision (AI)         | 0.9902 |
| Recall (AI)            | 0.9724 |
| Precision (Human)      | 0.9728 |
| Recall (Human)         | 0.9904 |

Confusion matrix (test):

- **True Negatives (Human correctly)**: 138,276
- **False Positives (Human → AI)**: 1,345
- **False Negatives (AI → Human)**: 3,859
- **True Positives (AI correctly)**: 135,761

### Calibration

- **Method:** temperature scaling
- **Temperature (T):** 1.4437
- **Calibration set:** calibration
- Test ECE: 0.0075 → 0.0116 (after calibration)
- Test Brier: 0.0157 → 0.0156 (after calibration)

---

## Plots

### Training & validation

- Learning curves:

  ![Learning curves](./figures/fig_learning_curves.png)

- Eval metrics over time:

  ![Eval metrics](./figures/fig_eval_metrics.png)

### Validation set

- ROC:

  ![ROC (calib)](./figures/fig_roc_calib.png)

- Precision–Recall:

  ![PR (calib)](./figures/fig_pr_calib.png)

- Calibration curve:

  ![Calibration (calib)](./figures/fig_calibration_calib.png)

- F1 vs threshold:

  ![F1 vs threshold (calib)](./figures/fig_threshold_f1_calib.png)

### Test set

- ROC:

  ![ROC (test)](./figures/fig_roc_test.png)

- Precision–Recall:

  ![PR (test)](./figures/fig_pr_test.png)

- Calibration curve:

  ![Calibration (test)](./figures/fig_calibration_test.png)

- Confusion matrix:

  ![Confusion matrix (test)](./figures/fig_confusion_test.png)

---

## Usage

### Load base + LoRA adapter

```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from peft import PeftModel
import torch
import json

base_model_id = "microsoft/deberta-v3-large"
adapter_id    = "stealthcode/ai-detection"  # or local: "./adapter"

tokenizer = AutoTokenizer.from_pretrained(base_model_id)

base_model = AutoModelForSequenceClassification.from_pretrained(
    base_model_id,
    num_labels=1,  # single logit for BCEWithLogitsLoss
)
model = PeftModel.from_pretrained(base_model, adapter_id)
model.eval()
```

### Inference with threshold

```python
# load threshold
with open("threshold.json") as f:
    thr = json.load(f)["threshold"]  # 0.8697

def predict_proba(texts):
    enc = tokenizer(
        texts,
        padding=True,
        truncation=True,
        max_length=512,
        return_tensors="pt",
    )
    with torch.no_grad():
        logits = model(**enc).logits.squeeze(-1)
        probs = torch.sigmoid(logits)
    return probs.cpu().numpy()

def predict_label(texts, threshold=thr):
    probs = predict_proba(texts)
    return (probs >= threshold).astype(int)

# example
texts = ["Some example text to classify"]
probs = predict_proba(texts)
labels = predict_label(texts)
print(probs, labels)  # label 1 = AI, 0 = Human
```

### Load merged model (no PEFT required)

```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch, json

model_dir = "./merged_model"
tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModelForSequenceClassification.from_pretrained(model_dir)
model.eval()

with open("threshold.json") as f:
    thr = json.load(f)["threshold"]  # 0.8697

def predict_proba(texts):
    enc = tokenizer(texts, padding=True, truncation=True, max_length=512, return_tensors="pt")
    with torch.no_grad():
        logits = model(**enc).logits.squeeze(-1)
        probs = torch.sigmoid(logits)
    return probs.cpu().numpy()
```

### Optional: apply temperature scaling to logits

```python
import json
with open("calibration.json") as f:
    T = json.load(f)["temperature"]  # e.g., 1.4437

def predict_proba_calibrated(texts):
    enc = tokenizer(texts, padding=True, truncation=True, max_length=512, return_tensors="pt")
    with torch.no_grad():
        logits = model(**enc).logits.squeeze(-1)
        probs = torch.sigmoid(logits / T)
    return probs.cpu().numpy()
```

---

## Notes

- Classifier head is **trainable** together with LoRA layers (unfrozen after applying PEFT).
- **LoRA config:**
  - `r=32`, `alpha=128`, `dropout=0.0`
  - Target modules: `query_proj`, `key_proj`, `value_proj`
- **Training config:**

  - `bf16=True`
  - `optim="adamw_torch_fused"`
  - `lr_scheduler_type="cosine_with_restarts"`
  - `num_train_epochs=2`
  - `per_device_train_batch_size=8`, `gradient_accumulation_steps=4`
  - `max_grad_norm=0.5`

- Threshold `0.8697` was chosen as the **max-F1** point on the calibration set.
  You can adjust it if you prefer fewer false positives or fewer false negatives.