MartinHeHeHe commited on
Commit
bde834c
·
1 Parent(s): 692c83c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -56
app.py CHANGED
@@ -1,57 +1,62 @@
1
  import os
2
- from typing import Iterator
3
-
4
- from text_generation import Client
5
-
6
- model_id = 'HuggingFaceH4/zephyr-7b-beta'
7
-
8
- API_URL = "https://api-inference.huggingface.co/models/" + model_id
9
- HF_TOKEN = os.environ.get("HF_READ_TOKEN", None)
10
-
11
- client = Client(
12
- API_URL,
13
- headers={"Authorization": f"Bearer {HF_TOKEN}"},
14
- )
15
- EOS_STRING = "</s>"
16
- EOT_STRING = "<EOT>"
17
-
18
-
19
- def get_prompt(message: str, chat_history: list[tuple[str, str]],
20
- system_prompt: str) -> str:
21
- texts = [f'<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
22
- # The first user input is _not_ stripped
23
- do_strip = False
24
- for user_input, response in chat_history:
25
- user_input = user_input.strip() if do_strip else user_input
26
- do_strip = True
27
- texts.append(f'{user_input} [/INST] {response.strip()} </s><s>[INST] ')
28
- message = message.strip() if do_strip else message
29
- texts.append(f'{message} [/INST]')
30
- return ''.join(texts)
31
-
32
-
33
- def run(message: str,
34
- chat_history: list[tuple[str, str]],
35
- system_prompt: str,
36
- max_new_tokens: int = 1024,
37
- temperature: float = 0.1,
38
- top_p: float = 0.9,
39
- top_k: int = 50) -> Iterator[str]:
40
- prompt = get_prompt(message, chat_history, system_prompt)
41
-
42
- generate_kwargs = dict(
43
- max_new_tokens=max_new_tokens,
44
- do_sample=True,
45
- top_p=top_p,
46
- top_k=top_k,
47
- temperature=temperature,
48
- )
49
- stream = client.generate_stream(prompt, **generate_kwargs)
50
- output = ""
51
- for response in stream:
52
- if any([end_token in response.token.text for end_token in [EOS_STRING, EOT_STRING]]):
53
- return output
54
- else:
55
- output += response.token.text
56
- yield output
57
- return output
 
 
 
 
 
 
1
  import os
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ # auth_token = os.environ.get("access_token")
6
+ pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
7
+ pipeline_zh = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta-chinese")
8
+
9
+
10
+
11
+ def predict_en(text):
12
+ res = pipeline_en(text)[0]
13
+ return res['label'],res['score']
14
+
15
+ def predict_zh(text):
16
+ res = pipeline_zh(text)[0]
17
+ return res['label'],res['score']
18
+
19
+
20
+
21
+
22
+ with gr.Blocks() as demo:
23
+ gr.Markdown("""
24
+ ## ChatGPT Detector 🔬 (Sinlge-text version)
25
+ Visit our project on Github: [chatgpt-comparison-detection project](https://github.com/Hello-SimpleAI/chatgpt-comparison-detection)<br>
26
+ 欢迎在 Github 上关注我们的 [ChatGPT 对比与检测项目](https://github.com/Hello-SimpleAI/chatgpt-comparison-detection)
27
+ We provide three kinds of detectors, all in Bilingual / 我们提供了三个版本的检测器,且都支持中英文:
28
+ - [**QA version / 问答版**](https://huggingface.co/spaces/Hello-SimpleAI/chatgpt-detector-qa)<br>
29
+ detect whether an **answer** is generated by ChatGPT for certain **question**, using PLM-based classifiers / 判断某个**问题的回答**是否由ChatGPT生成,使用基于PTM的分类器来开发;
30
+ - [Sinlge-text version / 独立文本版 (👈 Current / 当前使用)](https://huggingface.co/spaces/Hello-SimpleAI/chatgpt-detector-single)<br>
31
+ detect whether a piece of text is ChatGPT generated, using PLM-based classifiers / 判断**单条文本**是否由ChatGPT生成,使用基于PTM的分类器来开发;
32
+ - [Linguistic version / 语言学版](https://huggingface.co/spaces/Hello-SimpleAI/chatgpt-detector-ling)<br>
33
+ detect whether a piece of text is ChatGPT generated, using linguistic features / 判断**单条文本**是否由ChatGPT生成,使用基于语言学特征的模型来开发;
34
+
35
+
36
+ """)
37
+ with gr.Tab("English"):
38
+ gr.Markdown("""
39
+ Note: Providing more text to the `Text` box can make the prediction more accurate!
40
+ """)
41
+ t1 = gr.Textbox(lines=5, label='Text',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.")
42
+ button1 = gr.Button("🤖 Predict!")
43
+ label1 = gr.Textbox(lines=1, label='Predicted Label 🎃')
44
+ score1 = gr.Textbox(lines=1, label='Prob')
45
+ with gr.Tab("中文版"):
46
+ gr.Markdown("""
47
+ 注意: 在`文本`栏中输入更多的文本,可以让预测更准确哦!
48
+ """)
49
+ t2 = gr.Textbox(lines=5, label='文本',value="对于OpenAI大力出奇迹的工作,自然每个人都有自己的看点。我自己最欣赏的地方是ChatGPT如何解决 “AI校正(Alignment)“这个问题。这个问题也是我们课题组这两年在探索的学术问题之一。")
50
+ button2 = gr.Button("🤖 预测!")
51
+ label2 = gr.Textbox(lines=1, label='预测结果 🎃')
52
+ score2 = gr.Textbox(lines=1, label='模型概率')
53
+
54
+ button1.click(predict_en, inputs=[t1], outputs=[label1,score1], api_name='predict_en')
55
+ button2.click(predict_zh, inputs=[t2], outputs=[label2,score2], api_name='predict_zh')
56
+
57
+ # Page Count
58
+ gr.Markdown("""
59
+ <center><a href='https://clustrmaps.com/site/1bsdc' title='Visit tracker'><img src='//clustrmaps.com/map_v2.png?cl=080808&w=a&t=tt&d=NXQdnwxvIm27veMbB5F7oHNID09nhSvkBRZ_Aji9eIA&co=ffffff&ct=808080'/></a></center>
60
+ """)
61
+
62
+ demo.launch()