Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
import transformers
|
| 5 |
+
from transformers import (
|
| 6 |
+
AutoModelForSequenceClassification,
|
| 7 |
+
AutoTokenizer,
|
| 8 |
+
)
|
| 9 |
+
from deployment import preprocess, detect
|
| 10 |
+
|
| 11 |
+
device = 'cpu'
|
| 12 |
+
model_dir = "nealcly/detection-longformer"
|
| 13 |
+
|
| 14 |
+
# load the Longformer detector
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 16 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_dir).to(device)
|
| 17 |
+
|
| 18 |
+
def detect(input_text,device,th=-3.08583984375):
|
| 19 |
+
label2decisions = {
|
| 20 |
+
0: "machine-generated",
|
| 21 |
+
1: "human-written",
|
| 22 |
+
}
|
| 23 |
+
tokenize_input = tokenizer(input_text)
|
| 24 |
+
tensor_input = torch.tensor([tokenize_input["input_ids"]]).to(device)
|
| 25 |
+
outputs = model(tensor_input)
|
| 26 |
+
is_machine = -outputs.logits[0][0].item()
|
| 27 |
+
if is_machine < th:
|
| 28 |
+
decision = 0
|
| 29 |
+
else:
|
| 30 |
+
decision = 1
|
| 31 |
+
|
| 32 |
+
return label2decisions[decision]
|
| 33 |
+
|
| 34 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 35 |
+
iface.launch()
|