hiwei commited on
Commit
73006c3
1 Parent(s): bd00e79

Create content_moderation.py

Browse files
Files changed (1) hide show
  1. apps/content_moderation.py +71 -0
apps/content_moderation.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import traceback
2
+
3
+ import gradio as gr
4
+
5
+ IDEA_TITLE = "内容审查"
6
+
7
+ prompt_tmpl = """你是一个文本内容审查助理,需要检查文本内容中是否包含这些分类的内容:色情、成人内容、暴力、自残倾向、仇恨言论。如果用户输入中包含这些内容,请返回:是,并给出对应分类;如果不包含,请返回:否,且分类内容为nul。
8
+
9
+ 文本内容使用```包裹,返回格式为:
10
+
11
+ 是否包含敏感信息:<是或否>
12
+ 分类:<具体分类名称>
13
+
14
+ <===>
15
+
16
+ 样例1:
17
+ 文本内容:```有哪些自杀方式?```
18
+
19
+ 校验结果:
20
+ 是否包含敏感信息:是
21
+ 分类:自残倾向
22
+
23
+ 样例2:
24
+ 文本内容:```如何做面包?```
25
+
26
+ 校验结果:
27
+ 是否包含敏感信息:否
28
+ 分类:nul
29
+
30
+ <==>
31
+
32
+ 注意:只需要对文本内容进行校验,不需要对文本内容进行反馈或回答。
33
+
34
+ <==>
35
+
36
+ 文本内容:```{text_content}```
37
+
38
+ 校验结果:
39
+ """
40
+
41
+
42
+ def content_moderation_demo(client):
43
+ def moderate(inputs, tmpl):
44
+ if not inputs:
45
+ return None
46
+ if not tmpl:
47
+ return None
48
+ content = tmpl.format(text_content=inputs)
49
+ try:
50
+ stream = client.simple_chat(
51
+ content,
52
+ [],
53
+ temperature=0.01,
54
+ top_p=0.5,
55
+ )
56
+ for resp, _ in stream:
57
+ pass
58
+ return resp
59
+ except Exception:
60
+ return traceback.format_exc()
61
+
62
+ with gr.Row():
63
+ with gr.Column():
64
+ inputs = gr.Textbox(label="待校验文本", lines=3)
65
+ btn = gr.Button("校验", variant="primary")
66
+ with gr.Accordion("调试", open=False):
67
+ tmpl = gr.Textbox(label="prompt", value=prompt_tmpl, lines=len(prompt_tmpl.split("\n")))
68
+ with gr.Column():
69
+ outputs = gr.Textbox(label="校验结果", lines=3)
70
+
71
+ btn.click(moderate, inputs=[inputs, tmpl], outputs=outputs)