plslokeshreddy's picture
Initial release
758867f
Raw History Blame Contribute Delete
1.36 kB
"""Command line: python detect.py --file essay.txt (or --text "...", or --file texts.jsonl --jsonl with one {"text": ...} per line)
Everything is loaded from this folder: the encoder weights, the two heads, the tokenizer and the scoring constants."""
import argparse, json, os
import torch
from transformers import AutoModel, AutoTokenizer
HERE = os.path.dirname(os.path.abspath(__file__))
ap = argparse.ArgumentParser(); ap.add_argument('--text'); ap.add_argument('--file'); ap.add_argument('--jsonl', action='store_true')
ap.add_argument('--model', default=HERE); ap.add_argument('--batch_size', type=int, default=8)
a = ap.parse_args()
tok = AutoTokenizer.from_pretrained(a.model, trust_remote_code=True)
model = AutoModel.from_pretrained(a.model, trust_remote_code=True).to('cuda' if torch.cuda.is_available() else 'cpu').eval()
if a.jsonl:
for line in open(a.file):
r = model.analyze(json.loads(line)['text'], tok, a.batch_size); r.pop('text_scored', None); print(json.dumps(r), flush=True)
else:
r = model.analyze(a.text if a.text is not None else open(a.file).read(), tok, a.batch_size)
print('Verdict:', r.get('verdict'), '| share:', r.get('share'), '| words:', r.get('words'))
for s in r.get('segments', []):
print(f" [{s['label']:>12} {s['confidence']:.2f}] {r['text_scored'][s['start']:s['end']][:100]!r}")