Spaces:
Sleeping
Sleeping
花菜
lihuacai
commited on
Commit
•
a093438
1
Parent(s):
48e9f50
feat: 使用log代替print (#166)
Browse files* feat: 保存历史对话,json支持中文,和格式化json
* feat: 使用log代替print
---------
Co-authored-by: lihuacai <huacai2.li@tcl.com>
- ChuanhuChatbot.py +4 -2
- utils.py +38 -35
ChuanhuChatbot.py
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
# -*- coding:utf-8 -*-
|
2 |
import gradio as gr
|
3 |
import os
|
|
|
4 |
import sys
|
5 |
import argparse
|
6 |
from utils import *
|
7 |
from presets import *
|
8 |
|
|
|
9 |
|
10 |
my_api_key = "" # 在这里输入你的 API 密钥
|
11 |
|
@@ -20,7 +22,7 @@ authflag = False
|
|
20 |
if dockerflag:
|
21 |
my_api_key = os.environ.get('my_api_key')
|
22 |
if my_api_key == "empty":
|
23 |
-
|
24 |
sys.exit(1)
|
25 |
#auth
|
26 |
username = os.environ.get('USERNAME')
|
@@ -140,7 +142,7 @@ with gr.Blocks(css=customCSS) as demo:
|
|
140 |
|
141 |
templateApplyBtn.click(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt], [systemPromptTxt], show_progress=True)
|
142 |
|
143 |
-
|
144 |
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
145 |
demo.title = "川虎ChatGPT 🚀"
|
146 |
|
|
|
1 |
# -*- coding:utf-8 -*-
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
+
import logging
|
5 |
import sys
|
6 |
import argparse
|
7 |
from utils import *
|
8 |
from presets import *
|
9 |
|
10 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s")
|
11 |
|
12 |
my_api_key = "" # 在这里输入你的 API 密钥
|
13 |
|
|
|
22 |
if dockerflag:
|
23 |
my_api_key = os.environ.get('my_api_key')
|
24 |
if my_api_key == "empty":
|
25 |
+
logging.error("Please give a api key!")
|
26 |
sys.exit(1)
|
27 |
#auth
|
28 |
username = os.environ.get('USERNAME')
|
|
|
142 |
|
143 |
templateApplyBtn.click(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt], [systemPromptTxt], show_progress=True)
|
144 |
|
145 |
+
logging.info(colorama.Back.GREEN + "\n川虎的温馨提示:访问 http://localhost:7860 查看界面" + colorama.Style.RESET_ALL)
|
146 |
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
147 |
demo.title = "川虎ChatGPT 🚀"
|
148 |
|
utils.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
# -*- coding:utf-8 -*-
|
2 |
from __future__ import annotations
|
3 |
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple, Type
|
|
|
4 |
import json
|
5 |
import gradio as gr
|
6 |
# import openai
|
@@ -16,6 +17,8 @@ import tiktoken
|
|
16 |
from tqdm import tqdm
|
17 |
import colorama
|
18 |
|
|
|
|
|
19 |
if TYPE_CHECKING:
|
20 |
from typing import TypedDict
|
21 |
|
@@ -128,7 +131,7 @@ def stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, all_
|
|
128 |
def get_return_value():
|
129 |
return chatbot, history, status_text, all_token_counts
|
130 |
|
131 |
-
|
132 |
partial_words = ""
|
133 |
counter = 0
|
134 |
status_text = "开始实时传输回答……"
|
@@ -142,7 +145,7 @@ def stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, all_
|
|
142 |
else:
|
143 |
user_token_count = count_token(inputs)
|
144 |
all_token_counts.append(user_token_count)
|
145 |
-
|
146 |
yield get_return_value()
|
147 |
try:
|
148 |
response = get_response(openai_api_key, system_prompt, history, temperature, top_p, True, selected_model)
|
@@ -170,7 +173,7 @@ def stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, all_
|
|
170 |
try:
|
171 |
chunk = json.loads(chunk[6:])
|
172 |
except json.JSONDecodeError:
|
173 |
-
|
174 |
error_json_str += chunk
|
175 |
status_text = f"JSON解析错误。请重置对话。收到的内容: {error_json_str}"
|
176 |
yield get_return_value()
|
@@ -195,7 +198,7 @@ def stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, all_
|
|
195 |
|
196 |
|
197 |
def predict_all(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, selected_model):
|
198 |
-
|
199 |
history.append(construct_user(inputs))
|
200 |
history.append(construct_assistant(""))
|
201 |
chatbot.append((parse_text(inputs), ""))
|
@@ -222,10 +225,10 @@ def predict_all(openai_api_key, system_prompt, history, inputs, chatbot, all_tok
|
|
222 |
|
223 |
|
224 |
def predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, stream=False, selected_model = MODELS[0], should_check_token_count = True): # repetition_penalty, top_k
|
225 |
-
|
226 |
if len(openai_api_key) != 51:
|
227 |
status_text = standard_error_msg + no_apikey_msg
|
228 |
-
|
229 |
chatbot.append((parse_text(inputs), ""))
|
230 |
if len(history) == 0:
|
231 |
history.append(construct_user(inputs))
|
@@ -237,23 +240,23 @@ def predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_c
|
|
237 |
return
|
238 |
yield chatbot, history, "开始生成回答……", all_token_counts
|
239 |
if stream:
|
240 |
-
|
241 |
iter = stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, selected_model)
|
242 |
for chatbot, history, status_text, all_token_counts in iter:
|
243 |
yield chatbot, history, status_text, all_token_counts
|
244 |
else:
|
245 |
-
|
246 |
chatbot, history, status_text, all_token_counts = predict_all(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, selected_model)
|
247 |
yield chatbot, history, status_text, all_token_counts
|
248 |
-
|
249 |
if len(history) > 1 and history[-1]['content'] != inputs:
|
250 |
-
|
251 |
if stream:
|
252 |
max_token = max_token_streaming
|
253 |
else:
|
254 |
max_token = max_token_all
|
255 |
if sum(all_token_counts) > max_token and should_check_token_count:
|
256 |
-
|
257 |
iter = reduce_token_size(openai_api_key, system_prompt, history, chatbot, all_token_counts, top_p, temperature, stream=False, hidden=True)
|
258 |
for chatbot, history, status_text, all_token_counts in iter:
|
259 |
status_text = f"Token 达到上限,已自动降低Token计数至 {status_text}"
|
@@ -261,7 +264,7 @@ def predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_c
|
|
261 |
|
262 |
|
263 |
def retry(openai_api_key, system_prompt, history, chatbot, token_count, top_p, temperature, stream=False, selected_model = MODELS[0]):
|
264 |
-
|
265 |
if len(history) == 0:
|
266 |
yield chatbot, history, f"{standard_error_msg}上下文是空的", token_count
|
267 |
return
|
@@ -269,13 +272,13 @@ def retry(openai_api_key, system_prompt, history, chatbot, token_count, top_p, t
|
|
269 |
inputs = history.pop()["content"]
|
270 |
token_count.pop()
|
271 |
iter = predict(openai_api_key, system_prompt, history, inputs, chatbot, token_count, top_p, temperature, stream=stream, selected_model=selected_model)
|
272 |
-
|
273 |
for x in iter:
|
274 |
yield x
|
275 |
|
276 |
|
277 |
def reduce_token_size(openai_api_key, system_prompt, history, chatbot, token_count, top_p, temperature, stream=False, hidden=False, selected_model = MODELS[0]):
|
278 |
-
|
279 |
iter = predict(openai_api_key, system_prompt, history, summarize_prompt, chatbot, token_count, top_p, temperature, stream=stream, selected_model = selected_model, should_check_token_count=False)
|
280 |
for chatbot, history, status_text, previous_token_count in iter:
|
281 |
history = history[-2:]
|
@@ -283,49 +286,49 @@ def reduce_token_size(openai_api_key, system_prompt, history, chatbot, token_cou
|
|
283 |
if hidden:
|
284 |
chatbot.pop()
|
285 |
yield chatbot, history, construct_token_message(sum(token_count), stream=stream), token_count
|
286 |
-
|
287 |
|
288 |
|
289 |
def delete_last_conversation(chatbot, history, previous_token_count):
|
290 |
if len(chatbot) > 0 and standard_error_msg in chatbot[-1][1]:
|
291 |
-
|
292 |
chatbot.pop()
|
293 |
return chatbot, history
|
294 |
if len(history) > 0:
|
295 |
-
|
296 |
history.pop()
|
297 |
history.pop()
|
298 |
if len(chatbot) > 0:
|
299 |
-
|
300 |
chatbot.pop()
|
301 |
if len(previous_token_count) > 0:
|
302 |
-
|
303 |
previous_token_count.pop()
|
304 |
return chatbot, history, previous_token_count, construct_token_message(sum(previous_token_count))
|
305 |
|
306 |
|
307 |
def save_chat_history(filename, system, history, chatbot):
|
308 |
-
|
309 |
if filename == "":
|
310 |
return
|
311 |
if not filename.endswith(".json"):
|
312 |
filename += ".json"
|
313 |
os.makedirs(HISTORY_DIR, exist_ok=True)
|
314 |
json_s = {"system": system, "history": history, "chatbot": chatbot}
|
315 |
-
|
316 |
with open(os.path.join(HISTORY_DIR, filename), "w") as f:
|
317 |
-
json.dump(json_s, f)
|
318 |
-
|
319 |
|
320 |
|
321 |
def load_chat_history(filename, system, history, chatbot):
|
322 |
-
|
323 |
try:
|
324 |
with open(os.path.join(HISTORY_DIR, filename), "r") as f:
|
325 |
json_s = json.load(f)
|
326 |
try:
|
327 |
if type(json_s["history"][0]) == str:
|
328 |
-
|
329 |
new_history = []
|
330 |
for index, item in enumerate(json_s["history"]):
|
331 |
if index % 2 == 0:
|
@@ -333,21 +336,21 @@ def load_chat_history(filename, system, history, chatbot):
|
|
333 |
else:
|
334 |
new_history.append(construct_assistant(item))
|
335 |
json_s["history"] = new_history
|
336 |
-
|
337 |
except:
|
338 |
# 没有对话历史
|
339 |
pass
|
340 |
-
|
341 |
return filename, json_s["system"], json_s["history"], json_s["chatbot"]
|
342 |
except FileNotFoundError:
|
343 |
-
|
344 |
return filename, system, history, chatbot
|
345 |
|
346 |
def sorted_by_pinyin(list):
|
347 |
return sorted(list, key=lambda char: lazy_pinyin(char)[0][0])
|
348 |
|
349 |
def get_file_names(dir, plain=False, filetypes=[".json"]):
|
350 |
-
|
351 |
files = []
|
352 |
try:
|
353 |
for type in filetypes:
|
@@ -363,13 +366,13 @@ def get_file_names(dir, plain=False, filetypes=[".json"]):
|
|
363 |
return gr.Dropdown.update(choices=files)
|
364 |
|
365 |
def get_history_names(plain=False):
|
366 |
-
|
367 |
return get_file_names(HISTORY_DIR, plain)
|
368 |
|
369 |
def load_template(filename, mode=0):
|
370 |
-
|
371 |
lines = []
|
372 |
-
|
373 |
if filename.endswith(".json"):
|
374 |
with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as f:
|
375 |
lines = json.load(f)
|
@@ -388,18 +391,18 @@ def load_template(filename, mode=0):
|
|
388 |
return {row[0]:row[1] for row in lines}, gr.Dropdown.update(choices=choices, value=choices[0])
|
389 |
|
390 |
def get_template_names(plain=False):
|
391 |
-
|
392 |
return get_file_names(TEMPLATES_DIR, plain, filetypes=[".csv", "json"])
|
393 |
|
394 |
def get_template_content(templates, selection, original_system_prompt):
|
395 |
-
|
396 |
try:
|
397 |
return templates[selection]
|
398 |
except:
|
399 |
return original_system_prompt
|
400 |
|
401 |
def reset_state():
|
402 |
-
|
403 |
return [], [], [], construct_token_message(0)
|
404 |
|
405 |
def reset_textbox():
|
|
|
1 |
# -*- coding:utf-8 -*-
|
2 |
from __future__ import annotations
|
3 |
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple, Type
|
4 |
+
import logging
|
5 |
import json
|
6 |
import gradio as gr
|
7 |
# import openai
|
|
|
17 |
from tqdm import tqdm
|
18 |
import colorama
|
19 |
|
20 |
+
# logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s")
|
21 |
+
|
22 |
if TYPE_CHECKING:
|
23 |
from typing import TypedDict
|
24 |
|
|
|
131 |
def get_return_value():
|
132 |
return chatbot, history, status_text, all_token_counts
|
133 |
|
134 |
+
logging.info("实时回答模式")
|
135 |
partial_words = ""
|
136 |
counter = 0
|
137 |
status_text = "开始实时传输回答……"
|
|
|
145 |
else:
|
146 |
user_token_count = count_token(inputs)
|
147 |
all_token_counts.append(user_token_count)
|
148 |
+
logging.info(f"输入token计数: {user_token_count}")
|
149 |
yield get_return_value()
|
150 |
try:
|
151 |
response = get_response(openai_api_key, system_prompt, history, temperature, top_p, True, selected_model)
|
|
|
173 |
try:
|
174 |
chunk = json.loads(chunk[6:])
|
175 |
except json.JSONDecodeError:
|
176 |
+
logging.info(chunk)
|
177 |
error_json_str += chunk
|
178 |
status_text = f"JSON解析错误。请重置对话。收到的内容: {error_json_str}"
|
179 |
yield get_return_value()
|
|
|
198 |
|
199 |
|
200 |
def predict_all(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, selected_model):
|
201 |
+
logging.info("一次性回答模式")
|
202 |
history.append(construct_user(inputs))
|
203 |
history.append(construct_assistant(""))
|
204 |
chatbot.append((parse_text(inputs), ""))
|
|
|
225 |
|
226 |
|
227 |
def predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, stream=False, selected_model = MODELS[0], should_check_token_count = True): # repetition_penalty, top_k
|
228 |
+
logging.info("输入为:" +colorama.Fore.BLUE + f"{inputs}" + colorama.Style.RESET_ALL)
|
229 |
if len(openai_api_key) != 51:
|
230 |
status_text = standard_error_msg + no_apikey_msg
|
231 |
+
logging.info(status_text)
|
232 |
chatbot.append((parse_text(inputs), ""))
|
233 |
if len(history) == 0:
|
234 |
history.append(construct_user(inputs))
|
|
|
240 |
return
|
241 |
yield chatbot, history, "开始生成回答……", all_token_counts
|
242 |
if stream:
|
243 |
+
logging.info("使用流式传输")
|
244 |
iter = stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, selected_model)
|
245 |
for chatbot, history, status_text, all_token_counts in iter:
|
246 |
yield chatbot, history, status_text, all_token_counts
|
247 |
else:
|
248 |
+
logging.info("不使用流式传输")
|
249 |
chatbot, history, status_text, all_token_counts = predict_all(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, selected_model)
|
250 |
yield chatbot, history, status_text, all_token_counts
|
251 |
+
logging.info(f"传输完毕。当前token计数为{all_token_counts}")
|
252 |
if len(history) > 1 and history[-1]['content'] != inputs:
|
253 |
+
logging.info("回答为:" +colorama.Fore.BLUE + f"{history[-1]['content']}" + colorama.Style.RESET_ALL)
|
254 |
if stream:
|
255 |
max_token = max_token_streaming
|
256 |
else:
|
257 |
max_token = max_token_all
|
258 |
if sum(all_token_counts) > max_token and should_check_token_count:
|
259 |
+
logging.info(f"精简token中{all_token_counts}/{max_token}")
|
260 |
iter = reduce_token_size(openai_api_key, system_prompt, history, chatbot, all_token_counts, top_p, temperature, stream=False, hidden=True)
|
261 |
for chatbot, history, status_text, all_token_counts in iter:
|
262 |
status_text = f"Token 达到上限,已自动降低Token计数至 {status_text}"
|
|
|
264 |
|
265 |
|
266 |
def retry(openai_api_key, system_prompt, history, chatbot, token_count, top_p, temperature, stream=False, selected_model = MODELS[0]):
|
267 |
+
logging.info("重试中……")
|
268 |
if len(history) == 0:
|
269 |
yield chatbot, history, f"{standard_error_msg}上下文是空的", token_count
|
270 |
return
|
|
|
272 |
inputs = history.pop()["content"]
|
273 |
token_count.pop()
|
274 |
iter = predict(openai_api_key, system_prompt, history, inputs, chatbot, token_count, top_p, temperature, stream=stream, selected_model=selected_model)
|
275 |
+
logging.info("重试完毕")
|
276 |
for x in iter:
|
277 |
yield x
|
278 |
|
279 |
|
280 |
def reduce_token_size(openai_api_key, system_prompt, history, chatbot, token_count, top_p, temperature, stream=False, hidden=False, selected_model = MODELS[0]):
|
281 |
+
logging.info("开始减少token数量……")
|
282 |
iter = predict(openai_api_key, system_prompt, history, summarize_prompt, chatbot, token_count, top_p, temperature, stream=stream, selected_model = selected_model, should_check_token_count=False)
|
283 |
for chatbot, history, status_text, previous_token_count in iter:
|
284 |
history = history[-2:]
|
|
|
286 |
if hidden:
|
287 |
chatbot.pop()
|
288 |
yield chatbot, history, construct_token_message(sum(token_count), stream=stream), token_count
|
289 |
+
logging.info("减少token数量完毕")
|
290 |
|
291 |
|
292 |
def delete_last_conversation(chatbot, history, previous_token_count):
|
293 |
if len(chatbot) > 0 and standard_error_msg in chatbot[-1][1]:
|
294 |
+
logging.info("由于包含报错信息,只删除chatbot记录")
|
295 |
chatbot.pop()
|
296 |
return chatbot, history
|
297 |
if len(history) > 0:
|
298 |
+
logging.info("删除了一组对话历史")
|
299 |
history.pop()
|
300 |
history.pop()
|
301 |
if len(chatbot) > 0:
|
302 |
+
logging.info("删除了一组chatbot对话")
|
303 |
chatbot.pop()
|
304 |
if len(previous_token_count) > 0:
|
305 |
+
logging.info("删除了一组对话的token计数记录")
|
306 |
previous_token_count.pop()
|
307 |
return chatbot, history, previous_token_count, construct_token_message(sum(previous_token_count))
|
308 |
|
309 |
|
310 |
def save_chat_history(filename, system, history, chatbot):
|
311 |
+
logging.info("保存对话历史中……")
|
312 |
if filename == "":
|
313 |
return
|
314 |
if not filename.endswith(".json"):
|
315 |
filename += ".json"
|
316 |
os.makedirs(HISTORY_DIR, exist_ok=True)
|
317 |
json_s = {"system": system, "history": history, "chatbot": chatbot}
|
318 |
+
logging.info(json_s)
|
319 |
with open(os.path.join(HISTORY_DIR, filename), "w") as f:
|
320 |
+
json.dump(json_s, f, ensure_ascii=False, indent=4)
|
321 |
+
logging.info("保存对话历史完毕")
|
322 |
|
323 |
|
324 |
def load_chat_history(filename, system, history, chatbot):
|
325 |
+
logging.info("加载对话历史中……")
|
326 |
try:
|
327 |
with open(os.path.join(HISTORY_DIR, filename), "r") as f:
|
328 |
json_s = json.load(f)
|
329 |
try:
|
330 |
if type(json_s["history"][0]) == str:
|
331 |
+
logging.info("历史记录格式为旧版,正在转换……")
|
332 |
new_history = []
|
333 |
for index, item in enumerate(json_s["history"]):
|
334 |
if index % 2 == 0:
|
|
|
336 |
else:
|
337 |
new_history.append(construct_assistant(item))
|
338 |
json_s["history"] = new_history
|
339 |
+
logging.info(new_history)
|
340 |
except:
|
341 |
# 没有对话历史
|
342 |
pass
|
343 |
+
logging.info("加载对话历史完毕")
|
344 |
return filename, json_s["system"], json_s["history"], json_s["chatbot"]
|
345 |
except FileNotFoundError:
|
346 |
+
logging.info("没有找到对话历史文件,不执行任何操作")
|
347 |
return filename, system, history, chatbot
|
348 |
|
349 |
def sorted_by_pinyin(list):
|
350 |
return sorted(list, key=lambda char: lazy_pinyin(char)[0][0])
|
351 |
|
352 |
def get_file_names(dir, plain=False, filetypes=[".json"]):
|
353 |
+
logging.info(f"获取文件名列表,目录为{dir},文件类型为{filetypes},是否为纯文本列表{plain}")
|
354 |
files = []
|
355 |
try:
|
356 |
for type in filetypes:
|
|
|
366 |
return gr.Dropdown.update(choices=files)
|
367 |
|
368 |
def get_history_names(plain=False):
|
369 |
+
logging.info("获取历史记录文件名列表")
|
370 |
return get_file_names(HISTORY_DIR, plain)
|
371 |
|
372 |
def load_template(filename, mode=0):
|
373 |
+
logging.info(f"加载模板文件{filename},模式为{mode}(0为返回字典和下拉菜单,1为返回下拉菜单,2为返回字典)")
|
374 |
lines = []
|
375 |
+
logging.info("Loading template...")
|
376 |
if filename.endswith(".json"):
|
377 |
with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as f:
|
378 |
lines = json.load(f)
|
|
|
391 |
return {row[0]:row[1] for row in lines}, gr.Dropdown.update(choices=choices, value=choices[0])
|
392 |
|
393 |
def get_template_names(plain=False):
|
394 |
+
logging.info("获取模板文件名列表")
|
395 |
return get_file_names(TEMPLATES_DIR, plain, filetypes=[".csv", "json"])
|
396 |
|
397 |
def get_template_content(templates, selection, original_system_prompt):
|
398 |
+
logging.info(f"应用模板中,选择为{selection},原始系统提示为{original_system_prompt}")
|
399 |
try:
|
400 |
return templates[selection]
|
401 |
except:
|
402 |
return original_system_prompt
|
403 |
|
404 |
def reset_state():
|
405 |
+
logging.info("重置状态")
|
406 |
return [], [], [], construct_token_message(0)
|
407 |
|
408 |
def reset_textbox():
|