Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,14 @@ import tiktoken
|
|
| 3 |
|
| 4 |
import datetime
|
| 5 |
import json
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
openai.api_key = os.getenv('API_KEY')
|
| 9 |
openai.request_times = 0
|
| 10 |
|
|
|
|
|
|
|
| 11 |
def ask(question, history, behavior):
|
| 12 |
openai.request_times += 1
|
| 13 |
print(f"request times {openai.request_times}: {datetime.datetime.now()}: {question}")
|
|
@@ -65,54 +68,65 @@ def forget_long_term(messages, max_num_tokens=4000):
|
|
| 65 |
messages = messages[1:]
|
| 66 |
return messages
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
import gradio as gr
|
| 70 |
|
| 71 |
|
| 72 |
def to_md(content):
|
| 73 |
is_inside_code_block = False
|
|
|
|
| 74 |
output_spans = []
|
| 75 |
for i in range(len(content)):
|
| 76 |
-
if content[i]=="\n"
|
| 77 |
-
if
|
| 78 |
-
output_spans.append("\n")
|
| 79 |
-
else:
|
| 80 |
output_spans.append("<br>")
|
|
|
|
|
|
|
| 81 |
elif content[i]=="`":
|
| 82 |
-
|
| 83 |
-
if
|
|
|
|
| 84 |
is_inside_code_block = not is_inside_code_block
|
| 85 |
-
|
| 86 |
-
if is_inside_code_block:
|
| 87 |
-
if len(output_spans)==0:
|
| 88 |
-
output_spans.append("```")
|
| 89 |
-
elif output_spans[-1]=="<br>":
|
| 90 |
-
output_spans[-1] = "\n"
|
| 91 |
-
output_spans.append("```")
|
| 92 |
-
elif output_spans[-1].endswith("\n"):
|
| 93 |
-
output_spans.append("```")
|
| 94 |
-
else:
|
| 95 |
-
output_spans.append("\n```")
|
| 96 |
-
|
| 97 |
-
if i+1<len(content) and content[i+1]!="\n":
|
| 98 |
-
output_spans.append("\n")
|
| 99 |
-
else:
|
| 100 |
-
if output_spans[-1].endswith("\n"):
|
| 101 |
-
output_spans.append("```")
|
| 102 |
-
else:
|
| 103 |
-
output_spans.append("\n```")
|
| 104 |
-
|
| 105 |
-
if i+1<len(content) and content[i+1]!="\n":
|
| 106 |
-
output_spans.append("\n")
|
| 107 |
else:
|
| 108 |
output_spans.append(content[i])
|
| 109 |
return "".join(output_spans)
|
| 110 |
|
| 111 |
|
| 112 |
def predict(question, history=[], behavior=[]):
|
|
|
|
|
|
|
| 113 |
history = ask(question, history, behavior)
|
| 114 |
response = [(to_md(history[i]),to_md(history[i+1])) for i in range(0,len(history)-1,2)]
|
| 115 |
-
return "", history, response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
with gr.Blocks() as demo:
|
|
@@ -159,12 +173,15 @@ with gr.Blocks() as demo:
|
|
| 159 |
txt = gr.Textbox(show_label=False, placeholder="输入你想让ChatGPT回答的问题").style(container=False)
|
| 160 |
with gr.Row():
|
| 161 |
button_gen = gr.Button("Submit")
|
|
|
|
| 162 |
button_clr = gr.Button("Clear")
|
| 163 |
-
|
|
|
|
| 164 |
gr.Examples(examples=examples_bhv, inputs=bhv, label="Examples for setting behavior")
|
| 165 |
gr.Examples(examples=examples_txt, inputs=txt, label="Examples for asking question")
|
| 166 |
txt.submit(predict, [txt, state, behavior], [txt, state, chatbot])
|
| 167 |
-
button_gen.click(fn=predict, inputs=[txt, state, behavior], outputs=[txt, state, chatbot])
|
|
|
|
| 168 |
button_clr.click(fn=lambda :([],[]), inputs=None, outputs=[chatbot, state])
|
| 169 |
|
| 170 |
demo.queue(concurrency_count=3, max_size=10)
|
|
|
|
| 3 |
|
| 4 |
import datetime
|
| 5 |
import json
|
| 6 |
+
import time
|
| 7 |
import os
|
| 8 |
|
| 9 |
openai.api_key = os.getenv('API_KEY')
|
| 10 |
openai.request_times = 0
|
| 11 |
|
| 12 |
+
all_dialogue = []
|
| 13 |
+
|
| 14 |
def ask(question, history, behavior):
|
| 15 |
openai.request_times += 1
|
| 16 |
print(f"request times {openai.request_times}: {datetime.datetime.now()}: {question}")
|
|
|
|
| 68 |
messages = messages[1:]
|
| 69 |
return messages
|
| 70 |
|
| 71 |
+
def record_dialogue(history):
|
| 72 |
+
dialogue = json.dumps(history, ensure_ascii=False)
|
| 73 |
+
for i in range(len(all_dialogue)):
|
| 74 |
+
if dialogue[1:-1].startswith(all_dialogue[i][1:-1]):
|
| 75 |
+
all_dialogue[i] = dialogue
|
| 76 |
+
return
|
| 77 |
+
all_dialogue.append(dialogue)
|
| 78 |
+
return
|
| 79 |
+
|
| 80 |
|
| 81 |
import gradio as gr
|
| 82 |
|
| 83 |
|
| 84 |
def to_md(content):
|
| 85 |
is_inside_code_block = False
|
| 86 |
+
count_backtick = 0
|
| 87 |
output_spans = []
|
| 88 |
for i in range(len(content)):
|
| 89 |
+
if content[i]=="\n":
|
| 90 |
+
if not is_inside_code_block:
|
|
|
|
|
|
|
| 91 |
output_spans.append("<br>")
|
| 92 |
+
else:
|
| 93 |
+
output_spans.append("\n\n")
|
| 94 |
elif content[i]=="`":
|
| 95 |
+
count_backtick += 1
|
| 96 |
+
if count_backtick == 3:
|
| 97 |
+
count_backtick = 0
|
| 98 |
is_inside_code_block = not is_inside_code_block
|
| 99 |
+
output_spans.append(content[i])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
else:
|
| 101 |
output_spans.append(content[i])
|
| 102 |
return "".join(output_spans)
|
| 103 |
|
| 104 |
|
| 105 |
def predict(question, history=[], behavior=[]):
|
| 106 |
+
if question.startswith(f"{openai.api_key}:"):
|
| 107 |
+
return adminInstruct(question, history)
|
| 108 |
history = ask(question, history, behavior)
|
| 109 |
response = [(to_md(history[i]),to_md(history[i+1])) for i in range(0,len(history)-1,2)]
|
| 110 |
+
return "", history, response, gr.File.update(value=None, visible=False)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def retry(question, history=[], behavior=[]):
|
| 114 |
+
if len(history)<2:
|
| 115 |
+
return "", history, [], gr.File.update(value=None, visible=False)
|
| 116 |
+
question = history[-2]
|
| 117 |
+
history = history[:-2]
|
| 118 |
+
return predict(question, history, behavior)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def adminInstruct(question, history):
|
| 122 |
+
if "download all dialogue" in question:
|
| 123 |
+
filename = f"./all_dialogue_{len(all_dialogue)}.jsonl"
|
| 124 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 125 |
+
for dialogue in all_dialogue:
|
| 126 |
+
f.write(dialogue + "\n")
|
| 127 |
+
response = [(to_md(history[i]),to_md(history[i+1])) for i in range(0,len(history)-1,2)]
|
| 128 |
+
return "", history, response, gr.File.update(value=filename, visible=True)
|
| 129 |
+
return "", history, response, gr.File.update(value=None, visible=False)
|
| 130 |
|
| 131 |
|
| 132 |
with gr.Blocks() as demo:
|
|
|
|
| 173 |
txt = gr.Textbox(show_label=False, placeholder="输入你想让ChatGPT回答的问题").style(container=False)
|
| 174 |
with gr.Row():
|
| 175 |
button_gen = gr.Button("Submit")
|
| 176 |
+
button_rtr = gr.Button("Retry")
|
| 177 |
button_clr = gr.Button("Clear")
|
| 178 |
+
|
| 179 |
+
downloadfile = gr.File(None, interactive=False, show_label=False, visible=False)
|
| 180 |
gr.Examples(examples=examples_bhv, inputs=bhv, label="Examples for setting behavior")
|
| 181 |
gr.Examples(examples=examples_txt, inputs=txt, label="Examples for asking question")
|
| 182 |
txt.submit(predict, [txt, state, behavior], [txt, state, chatbot])
|
| 183 |
+
button_gen.click(fn=predict, inputs=[txt, state, behavior], outputs=[txt, state, chatbot, downloadfile])
|
| 184 |
+
button_rtr.click(fn=retry, inputs=[txt, state, behavior], outputs=[txt, state, chatbot, downloadfile])
|
| 185 |
button_clr.click(fn=lambda :([],[]), inputs=None, outputs=[chatbot, state])
|
| 186 |
|
| 187 |
demo.queue(concurrency_count=3, max_size=10)
|