Spaces:
Running
Running
abdullahmeda
commited on
initial commit
Browse files- .gitattributes +3 -0
- app.py +143 -0
- data/english.pickle +3 -0
- data/gpt2-large-model +3 -0
- data/gpt2-medium-model +3 -0
- data/gpt2-small-model +3 -0
- requirements.txt +10 -0
.gitattributes
CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
data/gpt2-large-model filter=lfs diff=lfs merge=lfs -text
|
37 |
+
data/gpt2-medium-model filter=lfs diff=lfs merge=lfs -text
|
38 |
+
data/gpt2-small-model filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import joblib
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
import pandas as pd
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
from nltk.data import load as nltk_load
|
9 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
10 |
+
|
11 |
+
|
12 |
+
NLTK = nltk_load('data/english.pickle')
|
13 |
+
sent_cut_en = NLTK.tokenize
|
14 |
+
clf = joblib.load(f'data/gpt2-large-model', 'rb')
|
15 |
+
|
16 |
+
model_id = 'gpt2-large'
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
18 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
19 |
+
|
20 |
+
CROSS_ENTROPY = torch.nn.CrossEntropyLoss(reduction='none')
|
21 |
+
|
22 |
+
|
23 |
+
def gpt2_features(text, tokenizer, model, sent_cut):
|
24 |
+
# Tokenize
|
25 |
+
input_max_length = tokenizer.model_max_length - 2
|
26 |
+
token_ids, offsets = list(), list()
|
27 |
+
sentences = sent_cut(text)
|
28 |
+
for s in sentences:
|
29 |
+
tokens = tokenizer.tokenize(s)
|
30 |
+
ids = tokenizer.convert_tokens_to_ids(tokens)
|
31 |
+
difference = len(token_ids) + len(ids) - input_max_length
|
32 |
+
if difference > 0:
|
33 |
+
ids = ids[:-difference]
|
34 |
+
offsets.append((len(token_ids), len(token_ids) + len(ids)))
|
35 |
+
token_ids.extend(ids)
|
36 |
+
if difference >= 0:
|
37 |
+
break
|
38 |
+
|
39 |
+
input_ids = torch.tensor([tokenizer.bos_token_id] + token_ids)
|
40 |
+
logits = model(input_ids).logits
|
41 |
+
# Shift so that n-1 predict n
|
42 |
+
shift_logits = logits[:-1].contiguous()
|
43 |
+
shift_target = input_ids[1:].contiguous()
|
44 |
+
loss = CROSS_ENTROPY(shift_logits, shift_target)
|
45 |
+
|
46 |
+
all_probs = torch.softmax(shift_logits, dim=-1)
|
47 |
+
sorted_ids = torch.argsort(all_probs, dim=-1, descending=True) # stable=True
|
48 |
+
expanded_tokens = shift_target.unsqueeze(-1).expand_as(sorted_ids)
|
49 |
+
indices = torch.where(sorted_ids == expanded_tokens)
|
50 |
+
rank = indices[-1]
|
51 |
+
counter = [
|
52 |
+
rank < 10,
|
53 |
+
(rank >= 10) & (rank < 100),
|
54 |
+
(rank >= 100) & (rank < 1000),
|
55 |
+
rank >= 1000
|
56 |
+
]
|
57 |
+
counter = [c.long().sum(-1).item() for c in counter]
|
58 |
+
|
59 |
+
|
60 |
+
# compute different-level ppl
|
61 |
+
text_ppl = loss.mean().exp().item()
|
62 |
+
sent_ppl = list()
|
63 |
+
for start, end in offsets:
|
64 |
+
nll = loss[start: end].sum() / (end - start)
|
65 |
+
sent_ppl.append(nll.exp().item())
|
66 |
+
max_sent_ppl = max(sent_ppl)
|
67 |
+
sent_ppl_avg = sum(sent_ppl) / len(sent_ppl)
|
68 |
+
if len(sent_ppl) > 1:
|
69 |
+
sent_ppl_std = torch.std(torch.tensor(sent_ppl)).item()
|
70 |
+
else:
|
71 |
+
sent_ppl_std = 0
|
72 |
+
|
73 |
+
mask = torch.tensor([1] * loss.size(0))
|
74 |
+
step_ppl = loss.cumsum(dim=-1).div(mask.cumsum(dim=-1)).exp()
|
75 |
+
max_step_ppl = step_ppl.max(dim=-1)[0].item()
|
76 |
+
step_ppl_avg = step_ppl.sum(dim=-1).div(loss.size(0)).item()
|
77 |
+
if step_ppl.size(0) > 1:
|
78 |
+
step_ppl_std = step_ppl.std().item()
|
79 |
+
else:
|
80 |
+
step_ppl_std = 0
|
81 |
+
ppls = [
|
82 |
+
text_ppl, max_sent_ppl, sent_ppl_avg, sent_ppl_std,
|
83 |
+
max_step_ppl, step_ppl_avg, step_ppl_std
|
84 |
+
]
|
85 |
+
return ppls + counter # type: ignore
|
86 |
+
|
87 |
+
|
88 |
+
def predict(features, classifier, id_to_label):
|
89 |
+
x = np.asarray([features])
|
90 |
+
pred = classifier.predict(x)[0]
|
91 |
+
prob = classifier.predict_proba(x)[0, pred]
|
92 |
+
return [id_to_label[pred], prob]
|
93 |
+
|
94 |
+
|
95 |
+
def predict(text):
|
96 |
+
with torch.no_grad():
|
97 |
+
feats = gpt2_features(text, tokenizer, model, sent_cut_en)
|
98 |
+
out = predict(*feats, clf, ['Human Written', 'LLM Generated'])
|
99 |
+
return out
|
100 |
+
|
101 |
+
|
102 |
+
with gr.Blocks() as demo:
|
103 |
+
gr.Markdown(
|
104 |
+
"""
|
105 |
+
## ChatGPT Detector 🔬 (Linguistic version / 语言学版)
|
106 |
+
|
107 |
+
Visit our project on Github: [chatgpt-comparison-detection project](https://github.com/Hello-SimpleAI/chatgpt-comparison-detection)<br>
|
108 |
+
欢迎在 Github 上关注我们的 [ChatGPT 对比与检测项目](https://github.com/Hello-SimpleAI/chatgpt-comparison-detection)<br>
|
109 |
+
We provide three kinds of detectors, all in Bilingual / 我们提供了三个版本的检测器,且都支持中英文:
|
110 |
+
- [QA version / 问答版](https://www.modelscope.cn/studios/simpleai/chatgpt-detector-qa)<br>
|
111 |
+
detect whether an **answer** is generated by ChatGPT for certain **question**, using PLM-based classifiers / 判断某个**问题的回答**是否由ChatGPT生成,使用基于PTM的分类器来开发;
|
112 |
+
- [Sinlge-text version / 独立文本版](https://www.modelscope.cn/studios/simpleai/chatgpt-detector-single)<br>
|
113 |
+
detect whether a piece of text is ChatGPT generated, using PLM-based classifiers / 判断**单条文本**是否由ChatGPT生成,使用基于PTM的分类器来开发;
|
114 |
+
- [**Linguistic version / 语言学版** (👈 Current / 当前使用)](https://www.modelscope.cn/studios/simpleai/chatgpt-detector-ling)<br>
|
115 |
+
detect whether a piece of text is ChatGPT generated, using linguistic features / 判断**单条文本**是否由ChatGPT生成,使用基于语言学特征的模型来开发;
|
116 |
+
|
117 |
+
"""
|
118 |
+
)
|
119 |
+
|
120 |
+
gr.Markdown(
|
121 |
+
"""
|
122 |
+
## Introduction:
|
123 |
+
Two Logistic regression models trained with two kinds of features:
|
124 |
+
1. [GLTR](https://aclanthology.org/P19-3019) Test-2, Language model predict token rank top-k buckets, top 10, 10-100, 100-1000, 1000+.
|
125 |
+
2. PPL-based, text ppl, sentence ppl, etc.
|
126 |
+
|
127 |
+
English LM is [GPT2-small](https://huggingface.co/gpt2).
|
128 |
+
|
129 |
+
Note: Providing more text to the `Text` box can make the prediction more accurate!
|
130 |
+
"""
|
131 |
+
)
|
132 |
+
a1 = gr.Textbox(
|
133 |
+
lines=5, label='Text',
|
134 |
+
value="There are a few things that can help protect your credit card information from being misused when you give it to a restaurant or any other business:\n\nEncryption: Many businesses use encryption to protect your credit card information when it is being transmitted or stored. This means that the information is transformed into a code that is difficult for anyone to read without the right key."
|
135 |
+
)
|
136 |
+
button1 = gr.Button("🤖 Predict!")
|
137 |
+
gr.Markdown("GLTR")
|
138 |
+
label1_gltr = gr.Textbox(lines=1, label='GLTR Predicted Label 🎃')
|
139 |
+
score1_gltr = gr.Textbox(lines=1, label='GLTR Probability')
|
140 |
+
|
141 |
+
button1.click(predict, inputs=[a1], outputs=[label1_gltr, score1_gltr])
|
142 |
+
|
143 |
+
demo.launch()
|
data/english.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5cad3758596392364e3be9803dbd7ebeda384b68937b488a01365f5551bb942c
|
3 |
+
size 406697
|
data/gpt2-large-model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3f8a8c96268cfb0b109366d85a7d26f403aabcf5d411093596f670d384b1d7a9
|
3 |
+
size 918235392
|
data/gpt2-medium-model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:744b547d5540386486a4b6642be3a60b33d3d3ad12db513f82031cee5ebea6c7
|
3 |
+
size 932091008
|
data/gpt2-small-model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6aab895ebfc50e1168d70df9bb62837d14f8a78938943af1d1eaee280f427d15
|
3 |
+
size 976393968
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.15.0
|
2 |
+
joblib==1.3.2
|
3 |
+
nltk==3.8.1
|
4 |
+
numpy==1.26.3
|
5 |
+
pandas==2.2.0
|
6 |
+
torch==2.1.2
|
7 |
+
transformers==4.37.0
|
8 |
+
xgboost
|
9 |
+
lightgbm
|
10 |
+
catboost
|