Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```
|
| 2 |
+
import transformers
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoConfig,
|
| 5 |
+
AutoModelForSequenceClassification,
|
| 6 |
+
AutoTokenizer,
|
| 7 |
+
DataCollatorWithPadding,
|
| 8 |
+
EvalPrediction,
|
| 9 |
+
HfArgumentParser,
|
| 10 |
+
PretrainedConfig,
|
| 11 |
+
Trainer,
|
| 12 |
+
TrainingArguments,
|
| 13 |
+
default_data_collator,
|
| 14 |
+
set_seed,
|
| 15 |
+
)
|
| 16 |
+
import torch
|
| 17 |
+
|
| 18 |
+
model_dir = "nealcly/detection-longformer"
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 20 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_dir).to('cuda:0')
|
| 21 |
+
threshold = 3.08583984375
|
| 22 |
+
label2decisions = {
|
| 23 |
+
0: "machine-generated",
|
| 24 |
+
1: "human-written",
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def detect(input_text):
|
| 28 |
+
tokenize_input = tokenizer(input_text)
|
| 29 |
+
tensor_input = torch.tensor([tokenize_input["input_ids"]]).to('cuda:0')
|
| 30 |
+
outputs = model(tensor_input)
|
| 31 |
+
is_machine = -outputs.logits[0][0].item() + threshold
|
| 32 |
+
if is_machine > 0:
|
| 33 |
+
decision = 0
|
| 34 |
+
else:
|
| 35 |
+
decision = 1
|
| 36 |
+
print(f"The text is {label2decisions[decision]}.")
|
| 37 |
+
```
|