Spaces:
Running
Running
让保存的html对话文件能够显示代码高亮
Browse files- crazy_functions/对话历史存档.py +22 -2
crazy_functions/对话历史存档.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from toolbox import CatchException, update_ui
|
2 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
|
|
3 |
|
4 |
def write_chat_to_file(chatbot, history=None, file_name=None):
|
5 |
"""
|
@@ -11,6 +12,8 @@ def write_chat_to_file(chatbot, history=None, file_name=None):
|
|
11 |
file_name = 'chatGPT对话历史' + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.html'
|
12 |
os.makedirs('./gpt_log/', exist_ok=True)
|
13 |
with open(f'./gpt_log/{file_name}', 'w', encoding='utf8') as f:
|
|
|
|
|
14 |
for i, contents in enumerate(chatbot):
|
15 |
for j, content in enumerate(contents):
|
16 |
try: # 这个bug没找到触发条件,暂时先这样顶一下
|
@@ -26,14 +29,31 @@ def write_chat_to_file(chatbot, history=None, file_name=None):
|
|
26 |
for h in history:
|
27 |
f.write("\n>>>" + h)
|
28 |
f.write('</code>')
|
29 |
-
|
30 |
res = '对话历史写入:' + os.path.abspath(f'./gpt_log/{file_name}')
|
31 |
print(res)
|
32 |
return res
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def read_file_to_chat(chatbot, history, file_name):
|
35 |
with open(file_name, 'r', encoding='utf8') as f:
|
36 |
file_content = f.read()
|
|
|
|
|
|
|
37 |
html, history = file_content.split('<hr color="blue"> \n\n raw chat context:\n')
|
38 |
history = history.strip('<code>')
|
39 |
history = history.strip('</code>')
|
@@ -87,7 +107,7 @@ def 载入对话历史存档(txt, llm_kwargs, plugin_kwargs, chatbot, history, s
|
|
87 |
if not success:
|
88 |
if txt == "": txt = '空空如也的输入栏'
|
89 |
import glob
|
90 |
-
local_history = "<br/>".join(["`"+hide_cwd(f)+"`" for f in glob.glob(f'gpt_log/**/chatGPT对话历史*.html', recursive=True)])
|
91 |
chatbot.append([f"正在查找对话历史文件(html格式): {txt}", f"找不到任何html文件: {txt}。但本地存储了以下历史文件,您可以将任意一个文件路径粘贴到输入区,然后重试:<br/>{local_history}"])
|
92 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
93 |
return
|
|
|
1 |
from toolbox import CatchException, update_ui
|
2 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
3 |
+
import re
|
4 |
|
5 |
def write_chat_to_file(chatbot, history=None, file_name=None):
|
6 |
"""
|
|
|
12 |
file_name = 'chatGPT对话历史' + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.html'
|
13 |
os.makedirs('./gpt_log/', exist_ok=True)
|
14 |
with open(f'./gpt_log/{file_name}', 'w', encoding='utf8') as f:
|
15 |
+
from theme import advanced_css
|
16 |
+
f.write(f'<head><title>对话历史</title><style>{advanced_css}</style></head>')
|
17 |
for i, contents in enumerate(chatbot):
|
18 |
for j, content in enumerate(contents):
|
19 |
try: # 这个bug没找到触发条件,暂时先这样顶一下
|
|
|
29 |
for h in history:
|
30 |
f.write("\n>>>" + h)
|
31 |
f.write('</code>')
|
|
|
32 |
res = '对话历史写入:' + os.path.abspath(f'./gpt_log/{file_name}')
|
33 |
print(res)
|
34 |
return res
|
35 |
|
36 |
+
def gen_file_preview(file_name):
|
37 |
+
try:
|
38 |
+
with open(file_name, 'r', encoding='utf8') as f:
|
39 |
+
file_content = f.read()
|
40 |
+
# pattern to match the text between <head> and </head>
|
41 |
+
pattern = re.compile(r'<head>.*?</head>', flags=re.DOTALL)
|
42 |
+
file_content = re.sub(pattern, '', file_content)
|
43 |
+
html, history = file_content.split('<hr color="blue"> \n\n raw chat context:\n')
|
44 |
+
history = history.strip('<code>')
|
45 |
+
history = history.strip('</code>')
|
46 |
+
history = history.split("\n>>>")
|
47 |
+
return list(filter(lambda x:x!="", history))[0][:100]
|
48 |
+
except:
|
49 |
+
return ""
|
50 |
+
|
51 |
def read_file_to_chat(chatbot, history, file_name):
|
52 |
with open(file_name, 'r', encoding='utf8') as f:
|
53 |
file_content = f.read()
|
54 |
+
# pattern to match the text between <head> and </head>
|
55 |
+
pattern = re.compile(r'<head>.*?</head>', flags=re.DOTALL)
|
56 |
+
file_content = re.sub(pattern, '', file_content)
|
57 |
html, history = file_content.split('<hr color="blue"> \n\n raw chat context:\n')
|
58 |
history = history.strip('<code>')
|
59 |
history = history.strip('</code>')
|
|
|
107 |
if not success:
|
108 |
if txt == "": txt = '空空如也的输入栏'
|
109 |
import glob
|
110 |
+
local_history = "<br/>".join(["`"+hide_cwd(f)+f" ({gen_file_preview(f)})"+"`" for f in glob.glob(f'gpt_log/**/chatGPT对话历史*.html', recursive=True)])
|
111 |
chatbot.append([f"正在查找对话历史文件(html格式): {txt}", f"找不到任何html文件: {txt}。但本地存储了以下历史文件,您可以将任意一个文件路径粘贴到输入区,然后重试:<br/>{local_history}"])
|
112 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
113 |
return
|