Spaces:
Running
Running
abdullahmeda
commited on
Commit
•
6c5f0d7
1
Parent(s):
f08b7b6
Update app.py
Browse files
app.py
CHANGED
@@ -21,6 +21,23 @@ clf = joblib.load(f'data/gpt2-large-model')
|
|
21 |
|
22 |
CROSS_ENTROPY = torch.nn.CrossEntropyLoss(reduction='none')
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def gpt2_features(text, tokenizer, model, sent_cut):
|
26 |
# Tokenize
|
@@ -87,7 +104,7 @@ def gpt2_features(text, tokenizer, model, sent_cut):
|
|
87 |
return ppls + counter # type: ignore
|
88 |
|
89 |
|
90 |
-
def
|
91 |
x = np.asarray([features])
|
92 |
pred = classifier.predict(x)[0]
|
93 |
prob = classifier.predict_proba(x)[0, pred]
|
@@ -97,50 +114,44 @@ def predict(features, classifier, id_to_label):
|
|
97 |
def predict(text):
|
98 |
with torch.no_grad():
|
99 |
feats = gpt2_features(text, tokenizer, model, sent_cut_en)
|
100 |
-
out =
|
101 |
return out
|
102 |
|
103 |
|
104 |
-
print("Building Gradio Interface...")
|
105 |
with gr.Blocks() as demo:
|
106 |
gr.Markdown(
|
107 |
"""
|
108 |
-
##
|
109 |
-
|
110 |
-
Visit our project on Github: [chatgpt-comparison-detection project](https://github.com/Hello-SimpleAI/chatgpt-comparison-detection)<br>
|
111 |
-
欢迎在 Github 上关注我们的 [ChatGPT 对比与检测项目](https://github.com/Hello-SimpleAI/chatgpt-comparison-detection)<br>
|
112 |
-
We provide three kinds of detectors, all in Bilingual / 我们提供了三个版本的检测器,且都支持中英文:
|
113 |
-
- [QA version / 问答版](https://www.modelscope.cn/studios/simpleai/chatgpt-detector-qa)<br>
|
114 |
-
detect whether an **answer** is generated by ChatGPT for certain **question**, using PLM-based classifiers / 判断某个**问题的回答**是否由ChatGPT生成,使用基于PTM的分类器来开发;
|
115 |
-
- [Sinlge-text version / 独立文本版](https://www.modelscope.cn/studios/simpleai/chatgpt-detector-single)<br>
|
116 |
-
detect whether a piece of text is ChatGPT generated, using PLM-based classifiers / 判断**单条文本**是否由ChatGPT生成,使用基于PTM的分类器来开发;
|
117 |
-
- [**Linguistic version / 语言学版** (👈 Current / 当前使用)](https://www.modelscope.cn/studios/simpleai/chatgpt-detector-ling)<br>
|
118 |
-
detect whether a piece of text is ChatGPT generated, using linguistic features / 判断**单条文本**是否由ChatGPT生成,使用基于语言学特征的模型来开发;
|
119 |
|
120 |
-
|
121 |
-
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
## Introduction:
|
126 |
-
Two Logistic regression models trained with two kinds of features:
|
127 |
-
1. [GLTR](https://aclanthology.org/P19-3019) Test-2, Language model predict token rank top-k buckets, top 10, 10-100, 100-1000, 1000+.
|
128 |
-
2. PPL-based, text ppl, sentence ppl, etc.
|
129 |
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
"""
|
134 |
)
|
135 |
-
a1 = gr.Textbox(
|
136 |
-
lines=5, label='Text',
|
137 |
-
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."
|
138 |
-
)
|
139 |
button1 = gr.Button("🤖 Predict!")
|
140 |
-
gr.Markdown("
|
141 |
-
|
142 |
-
|
143 |
|
144 |
-
button1.click(predict, inputs=[a1], outputs=[
|
145 |
|
146 |
demo.launch()
|
|
|
21 |
|
22 |
CROSS_ENTROPY = torch.nn.CrossEntropyLoss(reduction='none')
|
23 |
|
24 |
+
example = """\
|
25 |
+
The perplexity (PPL) is commonly used as a metric for evaluating the performance of language models (LM). It is defined as the \
|
26 |
+
exponential of the negative average log-likelihood of the text under the LM. A lower PPL indicates that the language model is more confident \
|
27 |
+
in its predictions, and is therefore considered to be a better model. The training of LMs is carried out on large-scale text corpora, it can \
|
28 |
+
be considered that it has learned some common language patterns and text structures. Therefore, PPL can be used to measure how well a text \
|
29 |
+
conforms to common characteristics.
|
30 |
+
|
31 |
+
I used all variants of the open-source GPT-2 model except xl size to compute the PPL (both text-level and sentence-level PPLs) of the collected \
|
32 |
+
texts. It is observed that, regardless of whether it is at the text level or the sentence level, the content generated by LLMs have relatively \
|
33 |
+
lower PPLs compared to the text written by humans. LLM captured common patterns and structures in the text it was trained on, and is very good at \
|
34 |
+
reproducing them. As a result, text generated by LLMs have relatively concentrated low PPLs.
|
35 |
+
|
36 |
+
Humans have the ability to express themselves in a wide variety of ways, depending on the context, audience, and purpose of the text they are \
|
37 |
+
writing. This can include using creative or imaginative elements, such as metaphors, similes, and unique word choices, which can make it more \
|
38 |
+
difficult for GPT2 to predict. The PPL distributions of text written by humans and text generated by LLMs are shown in the figure below.\
|
39 |
+
"""
|
40 |
+
|
41 |
|
42 |
def gpt2_features(text, tokenizer, model, sent_cut):
|
43 |
# Tokenize
|
|
|
104 |
return ppls + counter # type: ignore
|
105 |
|
106 |
|
107 |
+
def predict_out(features, classifier, id_to_label):
|
108 |
x = np.asarray([features])
|
109 |
pred = classifier.predict(x)[0]
|
110 |
prob = classifier.predict_proba(x)[0, pred]
|
|
|
114 |
def predict(text):
|
115 |
with torch.no_grad():
|
116 |
feats = gpt2_features(text, tokenizer, model, sent_cut_en)
|
117 |
+
out = predict_out(feats, clf, ['Human Written', 'LLM Generated'])
|
118 |
return out
|
119 |
|
120 |
|
|
|
121 |
with gr.Blocks() as demo:
|
122 |
gr.Markdown(
|
123 |
"""
|
124 |
+
## Detect text generated using LLMs 🤖
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
+
Linguistic features such as Perplexity and other SOTA methods such as GLTR were used to classify between Human written and LLM Generated \
|
127 |
+
texts. This solution scored an ROC of 0.956 and 8th position in the DAIGT LLM Competition on Kaggle. Fork of and credits to this github repo
|
128 |
|
129 |
+
Competition: [https://www.kaggle.com/competitions/llm-detect-ai-generated-text/leaderboard](https://www.kaggle.com/competitions/llm-detect-ai-generated-text/leaderboard)
|
130 |
+
Solution WriteUp: [https://www.kaggle.com/competitions/llm-detect-ai-generated-text/discussion/470224](https://www.kaggle.com/competitions/llm-detect-ai-generated-text/discussion/470224)
|
|
|
|
|
|
|
|
|
131 |
|
132 |
+
### Linguistic Analysis: Language Model Perplexity
|
133 |
+
The perplexity (PPL) is commonly used as a metric for evaluating the performance of language models (LM). It is defined as the exponential \
|
134 |
+
of the negative average log-likelihood of the text under the LM. A lower PPL indicates that the language model is more confident in its \
|
135 |
+
predictions, and is therefore considered to be a better model. The training of LMs is carried out on large-scale text corpora, it can \
|
136 |
+
be considered that it has learned some common language patterns and text structures. Therefore, PPL can be used to measure how \
|
137 |
+
well a text conforms to common characteristics.
|
138 |
|
139 |
+
### GLTR: Giant Language Model Test Room
|
140 |
+
This idea originates from the following paper: arxiv.org/pdf/1906.04043.pdf. It studies 3 tests to compute features of an input text. Their \
|
141 |
+
major assumption is that to generate fluent and natural-looking text, most decoding strategies sample high probability tokens from the head \
|
142 |
+
of the distribution. I selected the most powerful Test-2 feature, which is the number of tokens in the Top-10, Top-100, Top-1000, and 1000+ \
|
143 |
+
ranks from the LM predicted probability distributions.
|
144 |
+
|
145 |
+
### Modelling
|
146 |
+
Scikit-learn's VotingClassifier consisting of XGBClassifier, LGBMClassifier, CatBoostClassifier and RandomForestClassifier with default parameters
|
147 |
"""
|
148 |
)
|
149 |
+
a1 = gr.Textbox( lines=7, label='Text', value=example )
|
|
|
|
|
|
|
150 |
button1 = gr.Button("🤖 Predict!")
|
151 |
+
gr.Markdown("Prediction:")
|
152 |
+
label1 = gr.Textbox(lines=1, label='Predicted Label')
|
153 |
+
score1 = gr.Textbox(lines=1, label='Predicted Probability')
|
154 |
|
155 |
+
button1.click(predict, inputs=[a1], outputs=[label1, score1])
|
156 |
|
157 |
demo.launch()
|