Skip to content

Commit

Permalink
output
Browse files Browse the repository at this point in the history
  • Loading branch information
Akegarasu committed Mar 19, 2023
1 parent d3be275 commit ed07c53
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions modules/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,20 @@ def limit_round(self):
def save_history(self):
s = [{"q": i[0], "o": i[1]} for i in self.history]
filename = f"history-{int(time.time())}.json"
with open(os.path.join("outputs", "save", filename), "w", encoding="utf-8") as f:
p = os.path.join("outputs", "save", filename)
with open(p, "w", encoding="utf-8") as f:
f.write(json.dumps(s, ensure_ascii=False))
return f"Successful saved to: {p}"

def save_as_md(self):
filename = f"history-{int(time.time())}.md"
p = os.path.join("outputs", "markdown", filename)
output = ""
for i in self.history:
output += f"# 我: {i[0]}\n\nChatGLM: {i[1]}\n\n"
with open(os.path.join("outputs", "markdown", filename), "w", encoding="utf-8") as f:
with open(p, "w", encoding="utf-8") as f:
f.write(output)
return f"Successful saved to: {p}"

def load_history(self, file):
try:
Expand Down
10 changes: 7 additions & 3 deletions modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def clear_history():

def apply_max_round_click(max_round):
ctx.max_rounds = max_round
return f"Applied: max round {ctx.max_rounds}"


def create_ui():
Expand Down Expand Up @@ -66,6 +67,9 @@ def create_ui():
with gr.Row():
save_md_btn = gr.Button("保存为 MarkDown")

with gr.Row():
cmd_output = gr.Textbox(label="Command Output")

with gr.Column(scale=7):
chatbot = gr.Chatbot(elem_id="chat-box", show_label=False).style(height=800)
with gr.Row():
Expand All @@ -88,15 +92,15 @@ def create_ui():
clear.click(clear_history, outputs=[chatbot])
clear_input.click(lambda x: "", inputs=[input_message], outputs=[input_message])

save_his_btn.click(ctx.save_history)
save_md_btn.click(ctx.save_as_md)
save_his_btn.click(ctx.save_history, outputs=[cmd_output])
save_md_btn.click(ctx.save_as_md, outputs=[cmd_output])
load_his_btn.upload(ctx.load_history, inputs=[
load_his_btn,
], outputs=[
chatbot
])

apply_max_rounds.click(apply_max_round_click, inputs=[max_rounds])
apply_max_rounds.click(apply_max_round_click, inputs=[max_rounds], outputs=[cmd_output])

interfaces = [
(chat_interface, "Chat", "chat"),
Expand Down

0 comments on commit ed07c53

Please sign in to comment.