Views
No views yet
rasbt/human-vs-ai-50k. Human-written text has label 0 and AI-generated text has label 1.1hf download rasbt/ai-text-detector-gpt2-variable \
2 --local-dir models/ai-text-detector-gpt2-variable1import json
2from pathlib import Path
3
4import torch
5from transformers import AutoModelForSequenceClassification, AutoTokenizer
6
7
8model_dir = Path("models/ai-text-detector-gpt2-variable")
9metadata = json.loads(
10 (model_dir / "detector-config.json").read_text(encoding="utf-8")
11)
12tokenizer = AutoTokenizer.from_pretrained(model_dir)
13model = AutoModelForSequenceClassification.from_pretrained(model_dir)
14model.eval()
15
16text = "Paste the text to classify here."
17text_ids = tokenizer(
18 text,
19 add_special_tokens=False,
20 truncation=True,
21 max_length=metadata["max_text_length"],
22)["input_ids"]
23
24if metadata["readout_position"] == "fixed":
25 padding_length = metadata["context_length"] - len(text_ids) - 1
26 input_ids = (
27 text_ids
28 + [tokenizer.pad_token_id] * padding_length
29 + [tokenizer.eos_token_id]
30 )
31 attention_mask = [1] * len(text_ids) + [0] * padding_length + [1]
32else:
33 input_ids = text_ids + [tokenizer.eos_token_id]
34 attention_mask = [1] * len(input_ids)
35
36inputs = {
37 "input_ids": torch.tensor([input_ids]),
38 "attention_mask": torch.tensor([attention_mask]),
39}
40with torch.inference_mode():
41 logits = model(**inputs).logits / metadata["temperature"]
42 probabilities = logits.float().softmax(dim=-1)
43
44ai_index = metadata["label_mapping"]["ai"]
45ai_probability = probabilities[0, ai_index].item()
46print({"score": round(100 * ai_probability, 4)})detector-config.json contains the readout, calibration, and training metadata. The recommended inference implementation is provided in the rasbt/ai-detector repository because classification requires selecting the configured readout position.