Instructions to use matthewagi/AI-Detector-0.8B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use matthewagi/AI-Detector-0.8B with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download matthewagi/AI-Detector-0.8B --local-dir AI-Detector-0.8B
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Download gpu/run.py from matthewagi/AI-Detector-0.8B: direct link, hf CLI and curl.
- Browser
- Download file 3.99 kB
-
https://huggingface.co/matthewagi/AI-Detector-0.8B/resolve/main/gpu/run.py
- Command line
-
hf download hf://matthewagi/AI-Detector-0.8B/gpu/run.py
-
curl -L -o run.py https://huggingface.co/matthewagi/AI-Detector-0.8B/resolve/main/gpu/run.py
3.99 kB
| """Read a JSONL file of texts and save logits in original document order.""" | |
| import argparse | |
| import json | |
| import os | |
| from pathlib import Path | |
| import tempfile | |
| import numpy as np | |
| ROOT = Path(__file__).absolute().parents[1] | |
| GPU = Path(__file__).absolute().parent | |
| def plan(lengths, copies, budget, limit, padded): | |
| rows, used = [], 0 | |
| for index in sorted(range(len(lengths)), key=lengths.__getitem__): | |
| size = lengths[index] * copies | |
| width = (size + 63) // 64 * 64 if padded else size | |
| if width > budget: | |
| raise ValueError( | |
| f"Document {index} requires {width} positions; budget is {budget}." | |
| ) | |
| cost = width * (len(rows) + 1) if padded else used + size | |
| if rows and (cost > budget or len(rows) == limit): | |
| yield rows | |
| rows, used = [], 0 | |
| rows.append(index) | |
| used += size | |
| if rows: | |
| yield rows | |
| def save_scores(rows, groups, score, output): | |
| offsets = np.r_[0, np.cumsum([len(row) for row in rows], dtype=np.int64)] | |
| output.parent.mkdir(parents=True, exist_ok=True) | |
| with tempfile.TemporaryDirectory(dir=output.parent) as directory: | |
| logits = np.lib.format.open_memmap( | |
| Path(directory) / "scores.npy", | |
| mode="w+", | |
| dtype=np.float32, | |
| shape=(int(offsets[-1]), 3), | |
| ) | |
| for group in groups: | |
| result = score([rows[index] for index in group]) | |
| if ( | |
| result.shape != (sum(len(rows[index]) for index in group), 3) | |
| or not np.isfinite(result).all() | |
| ): | |
| raise ValueError("Unexpected token score shape or nonfinite scores.") | |
| start = 0 | |
| for index in group: | |
| end = start + len(rows[index]) | |
| logits[offsets[index] : offsets[index + 1]] = result[start:end] | |
| start = end | |
| with (Path(directory) / "output.npz").open("wb") as f: | |
| np.savez(f, offsets=offsets, input_ids=np.concatenate(rows), logits=logits) | |
| os.replace(Path(directory) / "output.npz", output) | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument( | |
| "input", type=Path, help='JSONL with one {"text": "..."} per line' | |
| ) | |
| parser.add_argument("output", type=Path, help="NumPy .npz file") | |
| parser.add_argument("--copies", choices=[1, 2], type=int, default=2) | |
| args = parser.parse_args() | |
| from transformers import AutoTokenizer | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| "Qwen/Qwen3.5-0.8B", revision="2fc06364715b967f1860aea9cf38778875588b17" | |
| ) | |
| texts = [json.loads(line)["text"] for line in args.input.read_text().splitlines()] | |
| if not texts or any( | |
| not isinstance(text, str) or not text.strip() for text in texts | |
| ): | |
| parser.error( | |
| "Supply at least one document and a nonempty text string on every line." | |
| ) | |
| rows = tokenizer(texts, add_special_tokens=False, verbose=False)["input_ids"] | |
| if not all(rows): | |
| parser.error("A document tokenized to zero tokens.") | |
| config = json.loads((GPU / "profile.json").read_text()) | |
| groups = list( | |
| plan( | |
| [len(r) for r in rows], | |
| args.copies, | |
| config["batch_tokens"], | |
| config["max_documents"], | |
| False, | |
| ) | |
| ) | |
| import torch | |
| if torch.cuda.get_device_capability() != (9, 0): | |
| raise RuntimeError("The mixed-FP8 runner requires a Hopper SM90 GPU.") | |
| from worker import Worker | |
| worker = Worker(ROOT / "model.safetensors") | |
| def score(batch): | |
| lengths = [len(row) * args.copies for row in batch] | |
| ids = torch.tensor( | |
| [t for row in batch for t in row * args.copies], device="cuda" | |
| ) | |
| return worker(ids, lengths, args.copies).cpu().numpy() | |
| save_scores(rows, groups, score, args.output) | |
| if __name__ == "__main__": | |
| main() | |