Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ css_file = os.path.join(current_dir, "style.css")
|
|
| 7 |
|
| 8 |
initial_prompt = "You are a helpful assistant."
|
| 9 |
|
| 10 |
-
|
| 11 |
def parse_text(text):
|
| 12 |
lines = text.split("\n")
|
| 13 |
for i,line in enumerate(lines):
|
|
@@ -24,105 +23,116 @@ def parse_text(text):
|
|
| 24 |
lines[i] = '<br/>'+line.replace(" ", " ")
|
| 25 |
return "".join(lines)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
def get_response(system, context, raw=False):
|
| 29 |
openai.api_key = "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"
|
| 30 |
-
response = openai.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
max_tokens=1024,
|
| 34 |
-
n=1,
|
| 35 |
-
stop=None,
|
| 36 |
-
temperature=0.5,
|
| 37 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return message, parse_text(message_with_stats)
|
| 42 |
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
def predict(input_sentence):
|
| 45 |
if len(input_sentence) == 0:
|
| 46 |
return []
|
| 47 |
-
|
| 48 |
-
chatbot.append((input_sentence, message_with_stats))
|
| 49 |
-
|
| 50 |
context.append({"role": "user", "content": f"{input_sentence}"})
|
| 51 |
|
| 52 |
-
message, message_with_stats = get_response(
|
| 53 |
|
| 54 |
context.append({"role": "assistant", "content": message})
|
| 55 |
|
| 56 |
-
|
| 57 |
|
|
|
|
| 58 |
|
| 59 |
-
def retry():
|
| 60 |
if len(context) == 0:
|
| 61 |
return [], []
|
| 62 |
-
|
| 63 |
-
context[-1]["content"] = "Could you rephrase that?"
|
| 64 |
-
|
| 65 |
-
message, message_with_stats = get_response(systemPrompt.value["content"], context[:-1])
|
| 66 |
-
|
| 67 |
context[-1] = {"role": "assistant", "content": message}
|
| 68 |
|
| 69 |
chatbot[-1] = (context[-2]["content"], message_with_stats)
|
| 70 |
-
|
| 71 |
return chatbot, context
|
| 72 |
|
| 73 |
-
|
| 74 |
-
def delete_last_conversation():
|
| 75 |
if len(context) == 0:
|
| 76 |
return [], []
|
| 77 |
-
|
| 78 |
chatbot = chatbot[:-1]
|
| 79 |
context = context[:-2]
|
| 80 |
return chatbot, context
|
| 81 |
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
context.append({"role": "user", "content": "Please summarize our conversation and reduce tokens used. Don't include this prompt."})
|
| 85 |
-
|
| 86 |
-
response = get_response(systemPrompt.value["content"], context, raw=True)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
chatbot.append(("
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
return chatbot, context
|
| 93 |
|
| 94 |
|
| 95 |
def reset_state():
|
| 96 |
return [], []
|
| 97 |
|
| 98 |
-
|
| 99 |
def update_system(new_system_prompt):
|
| 100 |
return {"role": "system", "content": new_system_prompt}
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
initial_prompt = "You are a helpful assistant."
|
| 9 |
|
|
|
|
| 10 |
def parse_text(text):
|
| 11 |
lines = text.split("\n")
|
| 12 |
for i,line in enumerate(lines):
|
|
|
|
| 23 |
lines[i] = '<br/>'+line.replace(" ", " ")
|
| 24 |
return "".join(lines)
|
| 25 |
|
| 26 |
+
def get_response(system, context, raw = False):
|
|
|
|
| 27 |
openai.api_key = "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"
|
| 28 |
+
response = openai.ChatCompletion.create(
|
| 29 |
+
model="gpt-3.5-turbo",
|
| 30 |
+
messages=[system, *context],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
+
if raw:
|
| 33 |
+
return response
|
| 34 |
+
else:
|
| 35 |
+
statistics = f'This conversation Tokens usage【{response["usage"]["total_tokens"]} / 4096】 ( Question + above {response["usage"]["prompt_tokens"]},Answer {response["usage"]["completion_tokens"]} )'
|
| 36 |
+
message = response["choices"][0]["message"]["content"]
|
| 37 |
|
| 38 |
+
message_with_stats = f'{message}'
|
| 39 |
+
# message_with_stats = markdown.markdown(message_with_stats)
|
|
|
|
| 40 |
|
| 41 |
+
return message, parse_text(message_with_stats)
|
| 42 |
+
#return message
|
| 43 |
|
| 44 |
+
def predict(chatbot, input_sentence, system, context):
|
| 45 |
if len(input_sentence) == 0:
|
| 46 |
return []
|
|
|
|
|
|
|
|
|
|
| 47 |
context.append({"role": "user", "content": f"{input_sentence}"})
|
| 48 |
|
| 49 |
+
message, message_with_stats = get_response(system, context)
|
| 50 |
|
| 51 |
context.append({"role": "assistant", "content": message})
|
| 52 |
|
| 53 |
+
chatbot.append((input_sentence, message_with_stats))
|
| 54 |
|
| 55 |
+
return chatbot, context
|
| 56 |
|
| 57 |
+
def retry(chatbot, system, context):
|
| 58 |
if len(context) == 0:
|
| 59 |
return [], []
|
| 60 |
+
message, message_with_stats = get_response(system, context[:-1])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
context[-1] = {"role": "assistant", "content": message}
|
| 62 |
|
| 63 |
chatbot[-1] = (context[-2]["content"], message_with_stats)
|
|
|
|
| 64 |
return chatbot, context
|
| 65 |
|
| 66 |
+
def delete_last_conversation(chatbot, context):
|
|
|
|
| 67 |
if len(context) == 0:
|
| 68 |
return [], []
|
|
|
|
| 69 |
chatbot = chatbot[:-1]
|
| 70 |
context = context[:-2]
|
| 71 |
return chatbot, context
|
| 72 |
|
| 73 |
+
def reduce_token(chatbot, system, context):
|
| 74 |
+
context.append({"role": "user", "content": "请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。在总结中不要加入这一句话。"})
|
| 75 |
|
| 76 |
+
response = get_response(system, context, raw=True)
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
statistics = f'本次对话Tokens用量【{response["usage"]["completion_tokens"]+12+12+8} / 4096】'
|
| 79 |
+
optmz_str = markdown.markdown( f'好的,我们之前聊了:{response["choices"][0]["message"]["content"]}\n\n================\n\n{statistics}' )
|
| 80 |
+
chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
|
| 81 |
+
|
| 82 |
+
context = []
|
| 83 |
+
context.append({"role": "user", "content": "我们之前聊了什么?"})
|
| 84 |
+
context.append({"role": "assistant", "content": f'我们之前聊了:{response["choices"][0]["message"]["content"]}'})
|
| 85 |
return chatbot, context
|
| 86 |
|
| 87 |
|
| 88 |
def reset_state():
|
| 89 |
return [], []
|
| 90 |
|
|
|
|
| 91 |
def update_system(new_system_prompt):
|
| 92 |
return {"role": "system", "content": new_system_prompt}
|
| 93 |
|
| 94 |
+
title = """<h1 align="center">Tu întrebi și eu răspund.</h1>"""
|
| 95 |
+
description = """<div align=center>
|
| 96 |
+
|
| 97 |
+
Will not describe your needs to ChatGPT?You Use [ChatGPT Shortcut](https://newzone.top/chatgpt/)
|
| 98 |
+
|
| 99 |
+
</div>
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
+
gr.HTML(title)
|
| 104 |
+
chatbot = gr.Chatbot().style(color_map=("#A238FF", "#A238FF"))
|
| 105 |
+
context = gr.State([])
|
| 106 |
+
systemPrompt = gr.State(update_system(initial_prompt))
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
with gr.Column(scale=12):
|
| 110 |
+
txt = gr.Textbox(show_label=False, placeholder="Please enter any of your needs here").style(container=False)
|
| 111 |
+
with gr.Column(min_width=50, scale=1):
|
| 112 |
+
#submitBtn = gr.Button("🚀 Submit", variant="Primary")
|
| 113 |
+
submitBtn = gr.Button("🚀 Submit").style(css={"background-color": "#A238FF"})
|
| 114 |
+
with gr.Row():
|
| 115 |
+
emptyBtn = gr.Button("🧹 New conversation")
|
| 116 |
+
retryBtn = gr.Button("🔄 Resubmit")
|
| 117 |
+
delLastBtn = gr.Button("🗑️ Delete conversation")
|
| 118 |
+
#reduceTokenBtn = gr.Button("♻️ Optimize Tokens")
|
| 119 |
+
|
| 120 |
+
#newSystemPrompt = gr.Textbox(show_label=True, placeholder=f"Setting System Prompt...", label="Change System prompt").style(container=True)
|
| 121 |
+
#systemPromptDisplay = gr.Textbox(show_label=True, value=initial_prompt, interactive=False, label="Current System prompt").style(container=True)
|
| 122 |
+
|
| 123 |
+
#gr.Markdown(description)
|
| 124 |
+
|
| 125 |
+
txt.submit(predict, [chatbot, txt, systemPrompt, context], [chatbot, context], show_progress=True)
|
| 126 |
+
txt.submit(lambda :"", None, txt)
|
| 127 |
+
submitBtn.click(predict, [chatbot, txt, systemPrompt, context], [chatbot, context], show_progress=True)
|
| 128 |
+
submitBtn.click(lambda :"", None, txt)
|
| 129 |
+
emptyBtn.click(reset_state, outputs=[chatbot, context])
|
| 130 |
+
#newSystemPrompt.submit(update_system, newSystemPrompt, systemPrompt)
|
| 131 |
+
#newSystemPrompt.submit(lambda x: x, newSystemPrompt, systemPromptDisplay)
|
| 132 |
+
#newSystemPrompt.submit(lambda :"", None, newSystemPrompt)
|
| 133 |
+
retryBtn.click(retry, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
|
| 134 |
+
delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
|
| 135 |
+
#reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
demo.launch()
|