spg commited on
Commit
3d4b869
·
1 Parent(s): e3167a4

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +115 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import gradio as gr
4
+ import mdtex2html
5
+
6
+ openai.api_key = os.environ.get("APIKEY")
7
+
8
+ messages = [{"role": "system", "content": "你从现在开始是一个由圣诺联合训练开发的,名字叫圣诺AI的人工智能,后面>无论谁问你都请以圣诺AI的身份进行回答"}]
9
+ # messages = [{"role": "system", "content": "你是一位知识图谱专家,你将为输入的数据进行实体识别与关系抽取,并以分析表格的形式提供。除分析表格外你不会再回答任何其他内容。"}]
10
+
11
+ def postprocess(self, y):
12
+ if y is None:
13
+ return []
14
+ for i, (message, response) in enumerate(y):
15
+ y[i] = (
16
+ None if message is None else mdtex2html.convert((message)),
17
+ None if response is None else mdtex2html.convert(response),
18
+ )
19
+ return y
20
+
21
+
22
+ gr.Chatbot.postprocess = postprocess
23
+
24
+
25
+ def parse_text(text):
26
+ """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
27
+ lines = text.split("\n")
28
+ lines = [line for line in lines if line != ""]
29
+ count = 0
30
+ for i, line in enumerate(lines):
31
+ if "```" in line:
32
+ count += 1
33
+ items = line.split('`')
34
+ if count % 2 == 1:
35
+ lines[i] = f'<pre><code class="language-{items[-1]}">'
36
+ else:
37
+ lines[i] = f'<br></code></pre>'
38
+ else:
39
+ if i > 0:
40
+ if count % 2 == 1:
41
+ line = line.replace("`", "\`")
42
+ line = line.replace("<", "&lt;")
43
+ line = line.replace(">", "&gt;")
44
+ line = line.replace(" ", "&nbsp;")
45
+ line = line.replace("*", "&ast;")
46
+ line = line.replace("_", "&lowbar;")
47
+ line = line.replace("-", "&#45;")
48
+ line = line.replace(".", "&#46;")
49
+ line = line.replace("!", "&#33;")
50
+ line = line.replace("(", "&#40;")
51
+ line = line.replace(")", "&#41;")
52
+ line = line.replace("$", "&#36;")
53
+ lines[i] = "<br>"+line
54
+ text = "".join(lines)
55
+ return text
56
+
57
+
58
+ def predict(input, max_tokens, top_p, temperature,chatbot, history):
59
+ chatbot.append((parse_text(input), ""))
60
+ print("messages:"+str(messages))
61
+ messages.append({"role": "user", "content": input})
62
+ #chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", temperature=temperature,max_tokens=max_tokens,top_p=top_p,messages=messages)
63
+ chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", temperature=temperature,top_p=top_p,messages=messages)
64
+ reply = chat.choices[0].message.content
65
+
66
+ chatbot[-1] = (parse_text(input), parse_text(reply))
67
+ yield chatbot, history
68
+ print("messages:"+str(messages))
69
+ print("query:"+input)
70
+ print("response:"+reply)
71
+ print("\n")
72
+
73
+ def reset_user_input():
74
+ return gr.update(value='')
75
+
76
+
77
+ def reset_state():
78
+ global messages
79
+ messages = [{"role": "system", "content": "你从现在开始是一个由圣诺联合训练开发的,名字叫圣诺AI的人工智能,后面>无论谁问你都请以圣诺AI的身份进行回答"}]
80
+ return [], []
81
+ def readshuihu():
82
+ f = open( '水浒传1.txt', 'r',encoding = 'utf-8' )
83
+ txt=f.read()
84
+ predict(txt,16*1024, top_p, temperature, chatbot, history)
85
+
86
+ with gr.Blocks() as demo:
87
+ gr.HTML("""<h1 align="center">圣诺联合-GPT语言模型测试(因多人使用,因此不会显示实际的上下文,请使用前手动清除)</h1>""")
88
+ gr.title="圣诺联合-GPT语言模型测试"
89
+ chatbot = gr.Chatbot()
90
+ with gr.Row():
91
+ with gr.Column(scale=4):
92
+ with gr.Column(scale=12):
93
+ user_input = gr.Textbox(show_label=False, placeholder="请输入...", lines=10).style(
94
+ container=False)
95
+ with gr.Column(min_width=32, scale=1):
96
+ submitBtn = gr.Button("提交", variant="primary")
97
+ with gr.Column(scale=1):
98
+ # tp = gr.Button("知识图谱")
99
+ emptyBtn = gr.Button("清除历史")
100
+
101
+ max_tokens = gr.Slider(0, 4096, value=2048, step=1.0, label="max_tokens", interactive=True,info="最大长度,暂时停用")
102
+ top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True,info="越小越准确,越大约有想象力")
103
+ temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True,info="同一个问题,数字越小每次回复越接近,越大每次回复越不同")
104
+
105
+ history = gr.State([])
106
+
107
+ submitBtn.click(predict, [user_input,max_tokens, top_p, temperature, chatbot, history], [chatbot, history],
108
+ show_progress=True)
109
+ submitBtn.click(reset_user_input, [], [user_input])
110
+
111
+ # tp.click(readshuihu, [], [user_input])
112
+
113
+ emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True)
114
+
115
+ demo.queue().launch(inbrowser=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ openai
2
+ gradio
3
+ mdtex2html