Added code to use roberta-openai-detector to compare results against

Signed-off-by: Jacob Torrey <jacob@thinkst.com>
pull/6/head
Jacob Torrey 2023-05-11 18:03:56 -06:00
rodzic 524279a09a
commit bab771c53a
3 zmienionych plików z 103 dodań i 0 usunięć

47
roberta_detect.py 100644
Wyświetl plik

@ -0,0 +1,47 @@
#!/usr/bin/env python3
# HuggingFace API test harness
import re
from typing import Optional, Tuple
from roberta_local import classify_text
def run_on_file_chunked(filename : str, chunk_size : int = 1024, fuzziness : int = 3) -> Optional[Tuple[str, float]]:
'''
Given a filename (and an optional chunk size) returns the score for the contents of that file.
This function chunks the file into at most chunk_size parts to score separately, then returns an average. This prevents a very large input
overwhelming the model.
'''
with open(filename, 'r') as fp:
contents = fp.read()
# Remove extra spaces and duplicate newlines.
contents = re.sub(' +', ' ', contents)
contents = re.sub('\t', '', contents)
contents = re.sub('\n+', '\n', contents)
contents = re.sub('\n ', '\n', contents)
start = 0
end = 0
chunks = []
while start + chunk_size < len(contents) and end != -1:
end = contents.rfind(' ', start, start + chunk_size + 1)
chunks.append(contents[start:end])
start = end + 1
chunks.append(contents[start:])
scores = []
for c in chunks:
scores.append(classify_text(c))
ssum : float = 0.0
for s in scores:
if s[0] == 'AI':
ssum -= s[1]
else:
ssum += s[1]
sa : float = ssum / len(scores)
if sa < 0:
return ('AI', abs(sa))
else:
return ('Human', abs(sa))

17
roberta_local.py 100644
Wyświetl plik

@ -0,0 +1,17 @@
#!/usr/bin/env python3
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
tokenizer = AutoTokenizer.from_pretrained("roberta-base-openai-detector")
model = AutoModelForSequenceClassification.from_pretrained("roberta-base-openai-detector")
def classify_text(s : str):
inputs = tokenizer(s, return_tensors='pt')
with torch.no_grad():
logits = model(**inputs).logits
pc = model.config.id2label[logits.argmax().item()]
conf = max(torch.softmax(logits, dim=1).tolist()[0])
if pc == 'Real':
return ('Human', conf)
return ('AI', conf)

Wyświetl plik

@ -0,0 +1,39 @@
#!/usr/bin/env python3
import pytest, os
from warnings import warn
from roberta_detect import run_on_file_chunked
AI_SAMPLE_DIR = 'samples/llm-generated/'
HUMAN_SAMPLE_DIR = 'samples/human-generated/'
ai_files = os.listdir(AI_SAMPLE_DIR)
human_files = os.listdir(HUMAN_SAMPLE_DIR)
CONFIDENCE_THRESHOLD : float = 0.00 # What confidence to treat as error vs warning
def test_training_file():
(classification, score) = run_on_file_chunked('ai-generated.txt')
assert classification == 'AI', 'The training corpus should always be detected as AI-generated... since it is (score: ' + str(round(score, 8)) + ')'
@pytest.mark.parametrize('f', human_files)
def test_human_samples(f):
(classification, score) = run_on_file_chunked(HUMAN_SAMPLE_DIR + f)
if score > CONFIDENCE_THRESHOLD:
assert classification == 'Human', f + ' is a human-generated file, misclassified as AI-generated with confidence ' + str(round(score, 8))
else:
if classification != 'Human':
warn("Misclassified " + f + " with score of: " + str(round(score, 8)))
else:
warn("Unable to confidently classify: " + f)
@pytest.mark.parametrize('f', ai_files)
def test_llm_sample(f):
(classification, score) = run_on_file_chunked(AI_SAMPLE_DIR + f)
if score > CONFIDENCE_THRESHOLD:
assert classification == 'AI', f + ' is an LLM-generated file, misclassified as human-generated with confidence ' + str(round(score, 8))
else:
if classification != 'AI':
warn("Misclassified " + f + " with score of: " + str(round(score, 8)))
else:
warn("Unable to confidently classify: " + f)