Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ __pycache__/
.env
# Raw third-party downloads stay outside the repository; see data/README.md.
raw/
# Working files of generate_mirrors; the merged result is data/train_mirrors.jsonl.
data/mirrors/
38 changes: 29 additions & 9 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ Canonical checkout: `/Volumes/SamsungSSD1/code/ai-text-detection-eval` on the Ma

## What this is

A small, reproducible experiment: can general-purpose decision models tell human writing from AI writing
with no training? It compiles a labelled text set, asks each model typed questions, and reports
separation, false positives and calibration. It is an evaluation harness, not a detector product.
A small, reproducible experiment on detecting AI-written text. It compiles labelled text sets and
compares three kinds of detector on them: decision models asked a typed question with no training
(Cloudflare Clef), statistical detectors that need no training (Binoculars, Fast-DetectGPT), and a
classifier fine-tuned on examples. It is an evaluation harness, not a detector product.

## Layout

- `src/detecteval/` — one module per pipeline stage: `fetch_raid`, `fetch_essays`, `generate`,
`build_dataset`, `score`, `report`; shared pieces in `config`, `hub`, `questions`, `metrics`, `textprep`.
- `data/` — the compiled evaluation set and the texts generated for this study (committed).
- `results/` — per-model scores, summary, report and figures (committed deliverables).
- Raw third-party downloads live outside Git in `RAW_DATA_DIR` (default `raw/`, ignored).
- `src/detecteval/` — one module per pipeline stage; shared pieces in `config`, `hub`, `questions`,
`metrics`, `textprep`, `items`.
- First study (texts of 150 words or fewer): `fetch_raid`, `fetch_essays`, `generate`, `build_dataset`,
`score`, `report`.
- Second study (long documents at several lengths): `fetch_raid_full`, `fetch_stackexchange`,
`build_long_set`, `generate_long`, `score_long`.
- Local detectors: `binoculars`; `generate_mirrors`, `train_data`, `train_classifier`, `classify`.
- Comparison of everything on held-out halves: `compare`, `compare_report`.
- `data/` — both evaluation sets, the texts generated for them, and the training mirrors (committed).
- `results/` — scores per detector, summaries, reports and figures (committed deliverables).
- Raw third-party downloads and trained model weights live outside Git in `RAW_DATA_DIR` (default
`raw/`, ignored).

## Conventions

Expand All @@ -26,6 +34,17 @@ separation, false positives and calibration. It is an evaluation harness, not a
- Model access goes through `hub.McpClient`. Tokens are read from a file named by `MCP_TOKEN_FILE`;
never print, log or commit a token, and keep `.env` untracked.
- Scoring and generation call paid APIs. Keep runs resumable and report the token cost.
- A request through an MCP gateway may be cut off after 60 seconds. `hub.WorkersAI.run_batch` gives each
item its own time limit so one slow item cannot lose the batch; keep single generations under that.
- A "key" is one text at one length: `<id>@short`, or `<id>@<words>` for the long set. Every detector
writes one row per key, so `compare` can join them.
- Long-set texts carry a `split`. Set thresholds and fit combinations on `calib`; report on `test`.
- Training data must never overlap evaluation data. `train_data` excludes every evaluation source
document, uses separate Stack Exchange sites, and leaves four current generators and all student
essays out of training. Keep those exclusions when changing either side.
- The local detectors need the optional `ml` dependency group (`uv sync --group ml`) and several
gigabytes of free memory. Run one at a time. CI does not install that group, so modules that need
PyTorch import it inside `main()` and tests cover only the NumPy parts.

## Setup and validation

Expand All @@ -34,7 +53,8 @@ uv sync
bash ci/run.sh # ruff + pytest; the same entry point CI uses
```

Tests cover the metrics and text preparation only. They do not call any model.
Tests cover the metrics, text preparation, half assignment and the detector statistics. They do not call
any model and do not need PyTorch.

## Workflow

Expand Down
256 changes: 183 additions & 73 deletions README.md

Large diffs are not rendered by default.

41 changes: 38 additions & 3 deletions THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Third-party data
# Third-party data and models

`data/eval_set.jsonl` contains excerpts (at most 150 words each) from two published datasets, both
released under the MIT licence by their authors.
`data/eval_set.jsonl` and `data/long_set.jsonl` contain excerpts (at most 150 and 500 words each) from
two published datasets released under the MIT licence, and `data/long_set.jsonl` also contains Stack
Exchange posts under CC BY-SA 4.0. Sources, licences and the methods implemented here are listed below.

## RAID

Expand All @@ -21,6 +22,40 @@ Used for: TOEFL essays by non-native writers, the same essays polished by GPT-4,
(groups `essay_nonnative`, `toefl_gpt4_polished`, `essay_native`). The student essays originate from the
Hewlett Foundation ASAP corpus; their anonymisation tokens were replaced with plain fillers here.

## Stack Exchange

Questions from Stack Exchange network sites, fetched through the public API, licensed
[CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/). Used for the human groups
`forum_recent` and `forum_2019` in `data/long_set.jsonl`. Each record carries the attribution the licence
asks for: the post's `link`, its `author` display name, its `title` and `created` date. The texts were
changed as follows: code blocks, quotations and tables were removed, HTML was stripped, whitespace was
collapsed and the text was cut to at most 500 words. Because these records are adapted CC BY-SA material,
those records remain under CC BY-SA 4.0 whatever licence the rest of the repository takes.

The group `polished_2026` contains model-edited versions of some of those posts and of some student
essays; the forum-derived ones are likewise adaptations under CC BY-SA 4.0.

Posts used only for training the classifier (2017-2018, from a separate list of sites) are fetched by
`fetch_stackexchange` and are not redistributed here.

## Methods implemented from published papers

No code was copied from these projects; the statistics are implemented from the papers' descriptions.

- Hans, A., Schwarzschild, A., Cherepanova, V., Kazemi, H., Saha, A., Goldblum, M., Geiping, J., and
Goldstein, T. "Spotting LLMs with Binoculars: Zero-Shot Detection of Machine-Generated Text." ICML 2024.
- Bao, G., Zhao, Y., Teng, Z., Yang, L., and Zhang, Y. "Fast-DetectGPT: Efficient Zero-Shot Detection of
Machine-Generated Text via Conditional Probability Curvature." ICLR 2024.
- Emi, B., and Spero, M. "Technical Report on the Pangram AI-Generated Text Classifier." 2024. The idea of
training on machine "mirrors" of human documents comes from this report.

## Models downloaded at run time

Not redistributed here; each is fetched from Hugging Face when the local detectors run.

- `Qwen/Qwen2.5-0.5B` and `Qwen/Qwen2.5-0.5B-Instruct` (Apache-2.0), the model pair for Binoculars.
- `distilbert/distilroberta-base` (Apache-2.0), the encoder fine-tuned into the classifier.

## MIT licence text

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
Expand Down
47 changes: 36 additions & 11 deletions data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,46 @@

| File | What it is |
|---|---|
| `eval_set.jsonl` | The compiled evaluation set: one JSON object per text, already trimmed and normalised. This is what the models score. |
| `generated.jsonl` | Raw outputs generated for this study (prompt, generator, untrimmed text), before trimming. |
| `eval_set.jsonl` | First evaluation set: 836 texts of at most 150 words, already trimmed and normalised. |
| `generated.jsonl` | Raw model outputs written for the first set (prompt, generator, untrimmed text). |
| `long_set.jsonl` | Second evaluation set: documents of 300 words or more, kept up to 500 words, so each can be scored at 75, 150, 300 and 500 words. Every text is assigned to a `calib` or `test` half. |
| `generated_long.jsonl` | Raw model outputs written for the second set. So far fresh texts only (79 of 160); humanized rewrites and polished human texts are planned. |
| `train_mirrors.jsonl` | Texts written by four current models on the titles of human training documents (264 of 1,000 planned). Used only to train the classifier; never part of an evaluation set. |

## `eval_set.jsonl` fields
## Fields shared by both evaluation sets

- `id` — stable identifier.
- `label` — `human`, `ai`, or `mixed` (human-written, then polished by GPT-4).
- `group` — where the text comes from (see the table in the top-level README).
- `label` — `human`, `ai`, or `mixed` (written by a person, then polished by a model).
- `group` — where the text comes from (see the tables in the top-level README).
- `source`, `domain`, `generator` — dataset, genre, and the model that wrote it (`human` for people).
- `rewriter` — for humanized texts, the model that rewrote it.
- `source_id` — RAID source document; human and AI texts with the same value share a title.
- `words`, `text` — word count and the text as scored.
- `rewriter` — for humanized and polished texts, the model that rewrote it.
- `source_id` — the source document; human and AI texts with the same value share a title.
- `words`, `text` — word count and the text as stored.

## Extra fields in `long_set.jsonl`

- `split` — `calib` or `test`. Thresholds and combination weights are set on `calib`; results are reported
on `test`. Texts that share a `source_id` share a half.
- `title` — the title the text was written for (empty for essays).
- `site`, `link`, `author`, `created` — for forum posts: the Stack Exchange site, the post's URL, its
author's display name and its date. These are the attribution required by CC BY-SA 4.0.
- `rewritten_from` appears in `generated_long.jsonl`: the id of the text a rewrite started from.

A text counts for a length when it has at least 85% of that many words. All documents fill the 75-, 150-
and 300-word lengths; fewer reach 500 words.

## Not in the repository

Raw third-party downloads (about 2.4 GB of RAID parquet files and the original essay JSON files) are
kept outside Git. `python -m detecteval.fetch_raid` and `python -m detecteval.fetch_essays` fetch them
again into `RAW_DATA_DIR` (default `raw/`). See `THIRD_PARTY_NOTICES.md` for sources and licences.
Raw third-party downloads are kept outside Git in `RAW_DATA_DIR` (default `raw/`):

| Download | Size | Fetched by |
|---|---|---|
| RAID parquet files, four domains, with adversarial attacks | 2.4 GB | `python -m detecteval.fetch_raid` |
| RAID full non-adversarial training split, eight domains | 0.8 GB (plus 0.4 GB as parquet) | `python -m detecteval.fetch_raid_full` |
| Essay sets from Liang et al. | under 1 MB | `python -m detecteval.fetch_essays` |
| Stack Exchange posts for evaluation and training | about 10 MB | `python -m detecteval.fetch_stackexchange` |
| Trained classifier weights | about 0.6 GB per model | `python -m detecteval.train_classifier` |

The Stack Exchange fetch takes "recent" to mean "from the day after the Clef models were released until
today", so running it later returns a different, larger sample. See `THIRD_PARTY_NOTICES.md` for sources
and licences.
Loading
Loading