Spaces:
Sleeping
Sleeping
Chuan Hu
commited on
Commit
•
3861a8e
1
Parent(s):
e8d851b
增加保存/加载对话历史记录功能
Browse files- ChuanhuChatbot.py +35 -3
ChuanhuChatbot.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
@@ -44,7 +45,7 @@ def get_response(system, context, raw = False):
|
|
44 |
message = response["choices"][0]["message"]["content"]
|
45 |
|
46 |
message_with_stats = f'{message}\n\n================\n\n{statistics}'
|
47 |
-
# message_with_stats =
|
48 |
|
49 |
return message, parse_text(message_with_stats)
|
50 |
|
@@ -85,12 +86,33 @@ def reduce_token(chatbot, system, context):
|
|
85 |
statistics = f'本次对话Tokens用量【{response["usage"]["completion_tokens"]+12+12+8} / 4096】'
|
86 |
optmz_str = markdown.markdown( f'好的,我们之前聊了:{response["choices"][0]["message"]["content"]}\n\n================\n\n{statistics}' )
|
87 |
chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
|
88 |
-
|
89 |
context = []
|
90 |
context.append({"role": "user", "content": "我们之前聊了什么?"})
|
91 |
context.append({"role": "assistant", "content": f'我们之前聊了:{response["choices"][0]["message"]["content"]}'})
|
92 |
return chatbot, context
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
def reset_state():
|
96 |
return [], []
|
@@ -103,6 +125,7 @@ with gr.Blocks() as demo:
|
|
103 |
chatbot = gr.Chatbot().style(color_map=("#1D51EE", "#585A5B"))
|
104 |
context = gr.State([])
|
105 |
systemPrompt = gr.State(update_system(initial_prompt))
|
|
|
106 |
|
107 |
with gr.Row():
|
108 |
with gr.Column(scale=12):
|
@@ -114,9 +137,16 @@ with gr.Blocks() as demo:
|
|
114 |
retryBtn = gr.Button("🔄 重新生成")
|
115 |
delLastBtn = gr.Button("🗑️ 删除上条对话")
|
116 |
reduceTokenBtn = gr.Button("♻️ 优化Tokens")
|
117 |
-
|
118 |
newSystemPrompt = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=True)
|
119 |
systemPromptDisplay = gr.Textbox(show_label=True, value=initial_prompt, interactive=False, label="目前的 System prompt").style(container=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
txt.submit(predict, [chatbot, txt, systemPrompt, context], [chatbot, context], show_progress=True)
|
122 |
txt.submit(lambda :"", None, txt)
|
@@ -129,6 +159,8 @@ with gr.Blocks() as demo:
|
|
129 |
retryBtn.click(retry, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
|
130 |
delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
|
131 |
reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
|
|
|
|
|
132 |
|
133 |
|
134 |
demo.launch()
|
|
|
1 |
+
import json
|
2 |
import gradio as gr
|
3 |
import openai
|
4 |
import os
|
|
|
45 |
message = response["choices"][0]["message"]["content"]
|
46 |
|
47 |
message_with_stats = f'{message}\n\n================\n\n{statistics}'
|
48 |
+
# message_with_stats = .markdown(message_with_stats)
|
49 |
|
50 |
return message, parse_text(message_with_stats)
|
51 |
|
|
|
86 |
statistics = f'本次对话Tokens用量【{response["usage"]["completion_tokens"]+12+12+8} / 4096】'
|
87 |
optmz_str = markdown.markdown( f'好的,我们之前聊了:{response["choices"][0]["message"]["content"]}\n\n================\n\n{statistics}' )
|
88 |
chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
|
89 |
+
|
90 |
context = []
|
91 |
context.append({"role": "user", "content": "我们之前聊了什么?"})
|
92 |
context.append({"role": "assistant", "content": f'我们之前聊了:{response["choices"][0]["message"]["content"]}'})
|
93 |
return chatbot, context
|
94 |
|
95 |
+
def save_chat_history(filepath, system, context):
|
96 |
+
if filepath == "":
|
97 |
+
return
|
98 |
+
history = {"system": system, "context": context}
|
99 |
+
with open(f"{filepath}.json", "w") as f:
|
100 |
+
json.dump(history, f)
|
101 |
+
|
102 |
+
def load_chat_history(fileobj):
|
103 |
+
with open(fileobj.name, "r") as f:
|
104 |
+
history = json.load(f)
|
105 |
+
context = history["context"]
|
106 |
+
chathistory = []
|
107 |
+
for i in range(0, len(context), 2):
|
108 |
+
chathistory.append((parse_text(context[i]["content"]), parse_text(context[i+1]["content"])))
|
109 |
+
return chathistory , history["system"], context, history["system"]["content"]
|
110 |
+
|
111 |
+
def get_history_names():
|
112 |
+
with open("history.json", "r") as f:
|
113 |
+
history = json.load(f)
|
114 |
+
return list(history.keys())
|
115 |
+
|
116 |
|
117 |
def reset_state():
|
118 |
return [], []
|
|
|
125 |
chatbot = gr.Chatbot().style(color_map=("#1D51EE", "#585A5B"))
|
126 |
context = gr.State([])
|
127 |
systemPrompt = gr.State(update_system(initial_prompt))
|
128 |
+
topic = gr.State("未命名对话历史记录")
|
129 |
|
130 |
with gr.Row():
|
131 |
with gr.Column(scale=12):
|
|
|
137 |
retryBtn = gr.Button("🔄 重新生成")
|
138 |
delLastBtn = gr.Button("🗑️ 删除上条对话")
|
139 |
reduceTokenBtn = gr.Button("♻️ 优化Tokens")
|
|
|
140 |
newSystemPrompt = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=True)
|
141 |
systemPromptDisplay = gr.Textbox(show_label=True, value=initial_prompt, interactive=False, label="目前的 System prompt").style(container=True)
|
142 |
+
with gr.Accordion(label="保存/加载对话历史记录(在文本框中输入文件名,点击“保存对话”按钮,历史记录文件会被存储到本地)", open=False):
|
143 |
+
with gr.Column():
|
144 |
+
with gr.Row():
|
145 |
+
with gr.Column(scale=6):
|
146 |
+
saveFileName = gr.Textbox(show_label=True, placeholder=f"在这里输入保存的文件名...", label="保存对话", value="对话历史记录").style(container=True)
|
147 |
+
with gr.Column(scale=1):
|
148 |
+
saveBtn = gr.Button("💾 保存对话")
|
149 |
+
uploadBtn = gr.UploadButton("📂 读取对话", file_count="single", file_types=["json"])
|
150 |
|
151 |
txt.submit(predict, [chatbot, txt, systemPrompt, context], [chatbot, context], show_progress=True)
|
152 |
txt.submit(lambda :"", None, txt)
|
|
|
159 |
retryBtn.click(retry, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
|
160 |
delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
|
161 |
reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
|
162 |
+
uploadBtn.upload(load_chat_history, uploadBtn, [chatbot, systemPrompt, context, systemPromptDisplay], show_progress=True)
|
163 |
+
saveBtn.click(save_chat_history, [saveFileName, systemPrompt, context], None, show_progress=True)
|
164 |
|
165 |
|
166 |
demo.launch()
|