File size: 2,940 Bytes
22e1b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from transformers import pipeline
from difflib import Differ
from transformers import AutoModelForSequenceClassification,AutoTokenizer
from deployment import preprocess, detect
import gradio as gr

ner_pipeline = pipeline("ner")


def ner(text):
    output = ner_pipeline(text)
    output = [
        {'entity': 'I-LOC', 'score': 0.9995369, 'index': 2, 'word': 'Chicago', 'start': 5, 'end': 12},
        {'entity': 'I-PER', 'score': 0.99527764, 'index': 8, 'word': 'Joe', 'start': 38, 'end': 41}
        ]
    print(output)
    return {"text": text, "entities": output}

def diff_texts(text1, text2):
    d = Differ()
    return [
        (token[2:], token[0] if token[0] != " " else None)
        for token in d.compare(text1, text2)
    ]
    
out = diff_texts(
    "The quick brown fox jumped over the lazy dogs.",
    "The fast brown fox jumps over lazy dogs.")
print(out)


def separate_characters_with_mask(text, mask):
  """Separates characters in a string and pairs them with a mask sign.

  Args:
    text: The input string.

  Returns:
    A list of tuples, where each tuple contains a character and a mask.
  """

  return [(char, mask) for char in text]


def detect_ai_text(text):
    text = preprocess(text)
    result = detect(text,tokenizer,model,device)
    print(result)
    output = separate_characters_with_mask(text, result)
    return output

# init
device = 'cpu' # use 'cuda:0' if GPU is available
# model_dir = "nealcly/detection-longformer" # model in our paper
model_dir = "yaful/MAGE" # model in the online demo
tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModelForSequenceClassification.from_pretrained(model_dir).to(device)
examples = ["Apple's new credit card will begin a preview roll out today and will become available to all iPhone owners in the US later this month. A random selection of people will be allowed to go through the application process, which involves entering personal details which are sent to Goldman Sachs and TransUnion. Applications are approved or declined in less than a minute. The Apple Card is meant to be broadly accessible to every iPhone user, so the approval requirements will not be as strict as other credit cards. Once the application has been approved, users will be able to use the card immediately from the Apple Wallet app. The physical titanium card can be requested during setup for free, and it can be activated with NFC once it arrives."]

demo = gr.Interface(detect_ai_text,
             gr.Textbox(
                 label="input text",
                 placeholder="Enter text here...",
                 lines=5,
                 ),
             gr.HighlightedText(
                 label="AI-text detection",
                 combine_adjacent=True,
                 show_legend=True,
                 color_map={"machine-generated": "red", "human-written": "green"}
                 ),
             examples=examples)

demo.launch(share=True)