Tuchuanhuhuhu commited on
Commit
bd419af
1 Parent(s): 811a42d

彻底,永久,一次性解决所有引号和LaTex问题

Browse files
Files changed (3) hide show
  1. ChuanhuChatbot.py +2 -256
  2. requirements.txt +1 -0
  3. utils.py +0 -0
ChuanhuChatbot.py CHANGED
@@ -1,23 +1,13 @@
1
- import json
2
  import gradio as gr
3
  # import openai
4
  import os
5
  import sys
6
- import traceback
7
- import requests
8
- # import markdown
9
- import csv
10
- from utils import ChuanhuChatbot
11
 
12
  my_api_key = "" # 在这里输入你的 API 密钥
13
  HIDE_MY_KEY = False # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True
14
 
15
- initial_prompt = "You are a helpful assistant."
16
- API_URL = "https://api.openai.com/v1/chat/completions"
17
- HISTORY_DIR = "history"
18
- TEMPLATES_DIR = "templates"
19
-
20
-
21
 
22
  #if we are running in Docker
23
  if os.environ.get('dockerrun') == 'yes':
@@ -38,250 +28,6 @@ if dockerflag:
38
  else:
39
  authflag = True
40
 
41
-
42
- def parse_text(text):
43
- lines = text.split("\n")
44
- lines = [line for line in lines if line != ""]
45
- count = 0
46
- firstline = False
47
- for i, line in enumerate(lines):
48
- if "```" in line:
49
- count += 1
50
- items = line.split('`')
51
- if count % 2 == 1:
52
- lines[i] = f'<pre><code class="language-{items[-1]}">'
53
- else:
54
- lines[i] = f'<br></code></pre>'
55
- else:
56
- if i > 0:
57
- if count % 2 == 1:
58
- # line = line.replace("‘", "'")
59
- # line = line.replace("“", '"')
60
- line = line.replace("`", "\`")
61
- # line = line.replace("\"", "`\"`")
62
- # line = line.replace("\'", "`\'`")
63
- # line = line.replace("'``'", "''")
64
- # line = line.replace("&", "&amp;")
65
- line = line.replace("<", "&lt;")
66
- line = line.replace(">", "&gt;")
67
- line = line.replace(" ", "&nbsp;")
68
- line = line.replace("*", "&ast;")
69
- line = line.replace("_", "&lowbar;")
70
- line = line.replace("-", "&#45;")
71
- line = line.replace(".", "&#46;")
72
- line = line.replace("!", "&#33;")
73
- line = line.replace("(", "&#40;")
74
- line = line.replace(")", "&#41;")
75
- line = line.replace("$", "&#36;")
76
- lines[i] = "<br>"+line
77
- text = "".join(lines)
78
- return text
79
-
80
- def predict(inputs, top_p, temperature, openai_api_key, chatbot=[], history=[], system_prompt=initial_prompt, retry=False, summary=False, retry_on_crash = False, stream = True): # repetition_penalty, top_k
81
-
82
- if retry_on_crash:
83
- retry = True
84
-
85
- headers = {
86
- "Content-Type": "application/json",
87
- "Authorization": f"Bearer {openai_api_key}"
88
- }
89
-
90
- chat_counter = len(history) // 2
91
-
92
- print(f"chat_counter - {chat_counter}")
93
-
94
- messages = []
95
- if chat_counter:
96
- for index in range(0, 2*chat_counter, 2):
97
- temp1 = {}
98
- temp1["role"] = "user"
99
- temp1["content"] = history[index]
100
- temp2 = {}
101
- temp2["role"] = "assistant"
102
- temp2["content"] = history[index+1]
103
- if temp1["content"] != "":
104
- if temp2["content"] != "" or retry:
105
- messages.append(temp1)
106
- messages.append(temp2)
107
- else:
108
- messages[-1]['content'] = temp2['content']
109
- if retry and chat_counter:
110
- if retry_on_crash:
111
- messages = messages[-6:]
112
- messages.pop()
113
- elif summary:
114
- history = [*[i["content"] for i in messages[-2:]], "我们刚刚聊了什么?"]
115
- messages.append(compose_user(
116
- "请帮我总结一下上述对话的内容,实现减少字数的同时,保证对话的质量。在总结中不要加入这一句话。"))
117
- else:
118
- temp3 = {}
119
- temp3["role"] = "user"
120
- temp3["content"] = inputs
121
- messages.append(temp3)
122
- chat_counter += 1
123
- messages = [compose_system(system_prompt), *messages]
124
- # messages
125
- payload = {
126
- "model": "gpt-3.5-turbo",
127
- "messages": messages, # [{"role": "user", "content": f"{inputs}"}],
128
- "temperature": temperature, # 1.0,
129
- "top_p": top_p, # 1.0,
130
- "n": 1,
131
- "stream": stream,
132
- "presence_penalty": 0,
133
- "frequency_penalty": 0,
134
- }
135
-
136
- if not summary:
137
- history.append(inputs)
138
- else:
139
- print("精简中...")
140
-
141
- print(f"payload: {payload}")
142
- # make a POST request to the API endpoint using the requests.post method, passing in stream=True
143
- try:
144
- response = requests.post(API_URL, headers=headers, json=payload, stream=True)
145
- except:
146
- history.append("")
147
- chatbot.append(inputs, "")
148
- yield history, chatbot, f"出现了网络错误"
149
- return
150
-
151
- token_counter = 0
152
- partial_words = ""
153
-
154
- counter = 0
155
- if stream:
156
- chatbot.append((parse_text(history[-1]), ""))
157
- for chunk in response.iter_lines():
158
- if counter == 0:
159
- counter += 1
160
- continue
161
- counter += 1
162
- # check whether each line is non-empty
163
- if chunk:
164
- # decode each line as response data is in bytes
165
- try:
166
- if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
167
- chunkjson = json.loads(chunk.decode()[6:])
168
- status_text = f"id: {chunkjson['id']}, finish_reason: {chunkjson['choices'][0]['finish_reason']}"
169
- yield chatbot, history, status_text
170
- break
171
- except Exception as e:
172
- traceback.print_exc()
173
- if not retry_on_crash:
174
- print("正在尝试使用缩短的context重新生成……")
175
- chatbot.pop()
176
- history.append("")
177
- yield next(predict(inputs, top_p, temperature, openai_api_key, chatbot, history, system_prompt, retry, summary=False, retry_on_crash=True, stream=False))
178
- else:
179
- msg = "☹️发生了错误:生成失败,请检查网络"
180
- print(msg)
181
- history.append(inputs, "")
182
- chatbot.append(inputs, msg)
183
- yield chatbot, history, "status: ERROR"
184
- break
185
- chunkjson = json.loads(chunk.decode()[6:])
186
- status_text = f"id: {chunkjson['id']}, finish_reason: {chunkjson['choices'][0]['finish_reason']}"
187
- partial_words = partial_words + \
188
- json.loads(chunk.decode()[6:])[
189
- 'choices'][0]["delta"]["content"]
190
- if token_counter == 0:
191
- history.append(" " + partial_words)
192
- else:
193
- history[-1] = partial_words
194
- chatbot[-1] = (parse_text(history[-2]), parse_text(history[-1]))
195
- token_counter += 1
196
- yield chatbot, history, status_text
197
- else:
198
- try:
199
- responsejson = json.loads(response.text)
200
- content = responsejson["choices"][0]["message"]["content"]
201
- history.append(content)
202
- chatbot.append((parse_text(history[-2]), parse_text(content)))
203
- status_text = "精简完成"
204
- except:
205
- chatbot.append((parse_text(history[-1]), "☹️发生了错误,请检查网络连接或者稍后再试。"))
206
- status_text = "status: ERROR"
207
- yield chatbot, history, status_text
208
-
209
-
210
-
211
- def delete_last_conversation(chatbot, history):
212
- if "☹️发生了错误" in chatbot[-1][1]:
213
- chatbot.pop()
214
- print(history)
215
- return chatbot, history
216
- history.pop()
217
- history.pop()
218
- print(history)
219
- return chatbot, history
220
-
221
- def save_chat_history(filename, system, history, chatbot):
222
- if filename == "":
223
- return
224
- if not filename.endswith(".json"):
225
- filename += ".json"
226
- os.makedirs(HISTORY_DIR, exist_ok=True)
227
- json_s = {"system": system, "history": history, "chatbot": chatbot}
228
- print(json_s)
229
- with open(os.path.join(HISTORY_DIR, filename), "w") as f:
230
- json.dump(json_s, f)
231
-
232
-
233
- def load_chat_history(filename):
234
- with open(os.path.join(HISTORY_DIR, filename), "r") as f:
235
- json_s = json.load(f)
236
- print(json_s)
237
- return filename, json_s["system"], json_s["history"], json_s["chatbot"]
238
-
239
-
240
- def get_file_names(dir, plain=False, filetype=".json"):
241
- # find all json files in the current directory and return their names
242
- try:
243
- files = sorted([f for f in os.listdir(dir) if f.endswith(filetype)])
244
- except FileNotFoundError:
245
- files = []
246
- if plain:
247
- return files
248
- else:
249
- return gr.Dropdown.update(choices=files)
250
-
251
- def get_history_names(plain=False):
252
- return get_file_names(HISTORY_DIR, plain)
253
-
254
- def load_template(filename, mode=0):
255
- lines = []
256
- with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as csvfile:
257
- reader = csv.reader(csvfile)
258
- lines = list(reader)
259
- lines = lines[1:]
260
- if mode == 1:
261
- return sorted([row[0] for row in lines])
262
- elif mode == 2:
263
- return {row[0]:row[1] for row in lines}
264
- else:
265
- return {row[0]:row[1] for row in lines}, gr.Dropdown.update(choices=sorted([row[0] for row in lines]))
266
-
267
- def get_template_names(plain=False):
268
- return get_file_names(TEMPLATES_DIR, plain, filetype=".csv")
269
-
270
- def reset_state():
271
- return [], []
272
-
273
-
274
- def compose_system(system_prompt):
275
- return {"role": "system", "content": system_prompt}
276
-
277
-
278
- def compose_user(user_input):
279
- return {"role": "user", "content": user_input}
280
-
281
-
282
- def reset_textbox():
283
- return gr.update(value='')
284
-
285
  title = """<h1 align="center">川虎ChatGPT 🚀</h1>"""
286
  description = """<div align=center>
287
 
 
 
1
  import gradio as gr
2
  # import openai
3
  import os
4
  import sys
5
+ from utils import *
 
 
 
 
6
 
7
  my_api_key = "" # 在这里输入你的 API 密钥
8
  HIDE_MY_KEY = False # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True
9
 
10
+ gr.Chatbot.postprocess = postprocess
 
 
 
 
 
11
 
12
  #if we are running in Docker
13
  if os.environ.get('dockerrun') == 'yes':
 
28
  else:
29
  authflag = True
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  title = """<h1 align="center">川虎ChatGPT 🚀</h1>"""
32
  description = """<div align=center>
33
 
requirements.txt CHANGED
@@ -1 +1,2 @@
1
  gradio
 
 
1
  gradio
2
+ mdtex2html
utils.py CHANGED
The diff for this file is too large to render. See raw diff