Spaces:
Running
Running
Commit
·
05eef7a
1
Parent(s):
ca877b2
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,42 +6,12 @@ from text_generation import Client
|
|
| 6 |
from conversation import get_default_conv_template
|
| 7 |
|
| 8 |
|
| 9 |
-
endpoint_url = os.environ.get("ENDPOINT_URL")
|
| 10 |
client = Client(endpoint_url, timeout=120)
|
| 11 |
eos_token = "</s>"
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
-
def generate_response(history, max_new_token=512, top_p=0.9, temperature=0.8, do_sample=True):
|
| 16 |
-
conv = get_default_conv_template("vicuna").copy()
|
| 17 |
-
roles = {"human": conv.roles[0], "gpt": conv.roles[1]} # map human to USER and gpt to ASSISTANT
|
| 18 |
-
for user, bot in history:
|
| 19 |
-
conv.append_message(roles['human'], user)
|
| 20 |
-
conv.append_message(roles["gpt"], bot)
|
| 21 |
-
msg = conv.get_prompt()
|
| 22 |
-
|
| 23 |
-
for response in client.generate_stream(
|
| 24 |
-
msg,
|
| 25 |
-
max_new_tokens=max_new_token,
|
| 26 |
-
top_p=top_p,
|
| 27 |
-
temperature=temperature,
|
| 28 |
-
do_sample=do_sample,
|
| 29 |
-
):
|
| 30 |
-
if not response.token.special:
|
| 31 |
-
yield response.token.text
|
| 32 |
-
|
| 33 |
-
# res = client.generate(
|
| 34 |
-
# msg,
|
| 35 |
-
# stop_sequences=["<|assistant|>", eos_token, "<|system|>", "<|user|>"],
|
| 36 |
-
# max_new_tokens=max_new_token,
|
| 37 |
-
# top_p=top_p,
|
| 38 |
-
# top_k=top_k,
|
| 39 |
-
# do_sample=do_sample,
|
| 40 |
-
# temperature=temperature,
|
| 41 |
-
# repetition_penalty=repetition_penalty,
|
| 42 |
-
# )
|
| 43 |
-
# return [("assistant", res.generated_text)]
|
| 44 |
-
#
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
chatbot = gr.Chatbot()
|
| 47 |
msg = gr.Textbox()
|
|
@@ -51,22 +21,128 @@ with gr.Blocks() as demo:
|
|
| 51 |
return "", history + [[user_message, None]]
|
| 52 |
|
| 53 |
def bot(history):
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
import ipdb
|
| 57 |
-
ipdb.set_trace()
|
| 58 |
-
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
| 59 |
history[-1][1] = ""
|
| 60 |
-
for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
history[-1][1] += character
|
|
|
|
| 62 |
time.sleep(0.05)
|
| 63 |
yield history
|
| 64 |
|
|
|
|
| 65 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 66 |
-
|
| 67 |
)
|
| 68 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 69 |
|
| 70 |
demo.queue()
|
| 71 |
demo.launch()
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from conversation import get_default_conv_template
|
| 7 |
|
| 8 |
|
| 9 |
+
endpoint_url = os.environ.get("ENDPOINT_URL", "http://127.0.0.1:8080")
|
| 10 |
client = Client(endpoint_url, timeout=120)
|
| 11 |
eos_token = "</s>"
|
| 12 |
|
| 13 |
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
with gr.Blocks() as demo:
|
| 16 |
chatbot = gr.Chatbot()
|
| 17 |
msg = gr.Textbox()
|
|
|
|
| 21 |
return "", history + [[user_message, None]]
|
| 22 |
|
| 23 |
def bot(history):
|
| 24 |
+
conv = get_default_conv_template("vicuna").copy()
|
| 25 |
+
roles = {"human": conv.roles[0], "gpt": conv.roles[1]} # map human to USER and gpt to ASSISTANT
|
| 26 |
+
for user, bot in history:
|
| 27 |
+
conv.append_message(roles['human'], user)
|
| 28 |
+
conv.append_message(roles["gpt"], bot)
|
| 29 |
+
msg = conv.get_prompt()
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
history[-1][1] = ""
|
| 32 |
+
for response in client.generate_stream(
|
| 33 |
+
msg,
|
| 34 |
+
max_new_tokens=512,
|
| 35 |
+
):
|
| 36 |
+
if not response.token.special:
|
| 37 |
+
character = response.token.text
|
| 38 |
+
history[-1][1] += character
|
| 39 |
+
yield history
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def generate_response(history, max_new_token=512, top_p=0.9, temperature=0.8, do_sample=True):
|
| 43 |
+
conv = get_default_conv_template("vicuna").copy()
|
| 44 |
+
roles = {"human": conv.roles[0], "gpt": conv.roles[1]} # map human to USER and gpt to ASSISTANT
|
| 45 |
+
for user, bot in history:
|
| 46 |
+
conv.append_message(roles['human'], user)
|
| 47 |
+
conv.append_message(roles["gpt"], bot)
|
| 48 |
+
msg = conv.get_prompt()
|
| 49 |
+
|
| 50 |
+
for response in client.generate_stream(
|
| 51 |
+
msg,
|
| 52 |
+
max_new_tokens=max_new_token,
|
| 53 |
+
top_p=top_p,
|
| 54 |
+
temperature=temperature,
|
| 55 |
+
do_sample=do_sample,
|
| 56 |
+
):
|
| 57 |
+
history[-1][1] = ""
|
| 58 |
+
# if not response.token.special:
|
| 59 |
+
character = response.token.text
|
| 60 |
history[-1][1] += character
|
| 61 |
+
print(history[-1][1])
|
| 62 |
time.sleep(0.05)
|
| 63 |
yield history
|
| 64 |
|
| 65 |
+
|
| 66 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 67 |
+
bot, chatbot, chatbot
|
| 68 |
)
|
| 69 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 70 |
|
| 71 |
demo.queue()
|
| 72 |
demo.launch()
|
| 73 |
|
| 74 |
+
#
|
| 75 |
+
# with gr.Blocks() as demo:
|
| 76 |
+
# chatbot = gr.Chatbot()
|
| 77 |
+
# with gr.Row():
|
| 78 |
+
# with gr.Column(scale=4):
|
| 79 |
+
# with gr.Column(scale=12):
|
| 80 |
+
# user_input = gr.Textbox(
|
| 81 |
+
# show_label=False,
|
| 82 |
+
# placeholder="Shift + Enter傳送...",
|
| 83 |
+
# lines=10).style(
|
| 84 |
+
# container=False)
|
| 85 |
+
# with gr.Column(min_width=32, scale=1):
|
| 86 |
+
# submitBtn = gr.Button("Submit", variant="primary")
|
| 87 |
+
# with gr.Column(scale=1):
|
| 88 |
+
# emptyBtn = gr.Button("Clear History")
|
| 89 |
+
# max_new_token = gr.Slider(
|
| 90 |
+
# 1,
|
| 91 |
+
# 1024,
|
| 92 |
+
# value=128,
|
| 93 |
+
# step=1.0,
|
| 94 |
+
# label="Maximum New Token Length",
|
| 95 |
+
# interactive=True)
|
| 96 |
+
# top_p = gr.Slider(0, 1, value=0.9, step=0.01,
|
| 97 |
+
# label="Top P", interactive=True)
|
| 98 |
+
# temperature = gr.Slider(
|
| 99 |
+
# 0,
|
| 100 |
+
# 1,
|
| 101 |
+
# value=0.5,
|
| 102 |
+
# step=0.01,
|
| 103 |
+
# label="Temperature",
|
| 104 |
+
# interactive=True)
|
| 105 |
+
# top_k = gr.Slider(1, 40, value=40, step=1,
|
| 106 |
+
# label="Top K", interactive=True)
|
| 107 |
+
# do_sample = gr.Checkbox(
|
| 108 |
+
# value=True,
|
| 109 |
+
# label="Do Sample",
|
| 110 |
+
# info="use random sample strategy",
|
| 111 |
+
# interactive=True)
|
| 112 |
+
# repetition_penalty = gr.Slider(
|
| 113 |
+
# 1.0,
|
| 114 |
+
# 3.0,
|
| 115 |
+
# value=1.1,
|
| 116 |
+
# step=0.1,
|
| 117 |
+
# label="Repetition Penalty",
|
| 118 |
+
# interactive=True)
|
| 119 |
+
#
|
| 120 |
+
# params = [user_input, chatbot]
|
| 121 |
+
# predict_params = [
|
| 122 |
+
# chatbot,
|
| 123 |
+
# max_new_token,
|
| 124 |
+
# top_p,
|
| 125 |
+
# temperature,
|
| 126 |
+
# top_k,
|
| 127 |
+
# do_sample,
|
| 128 |
+
# repetition_penalty]
|
| 129 |
+
#
|
| 130 |
+
# submitBtn.click(
|
| 131 |
+
# generate_response,
|
| 132 |
+
# [user_input, max_new_token, top_p, top_k, temperature, do_sample, repetition_penalty],
|
| 133 |
+
# [chatbot],
|
| 134 |
+
# queue=False
|
| 135 |
+
# )
|
| 136 |
+
#
|
| 137 |
+
# user_input.submit(
|
| 138 |
+
# generate_response,
|
| 139 |
+
# [user_input, max_new_token, top_p, top_k, temperature, do_sample, repetition_penalty],
|
| 140 |
+
# [chatbot],
|
| 141 |
+
# queue=False
|
| 142 |
+
# )
|
| 143 |
+
#
|
| 144 |
+
# submitBtn.click(lambda: None, [], [user_input])
|
| 145 |
+
#
|
| 146 |
+
# emptyBtn.click(lambda: chatbot.reset(), outputs=[chatbot], show_progress=True)
|
| 147 |
+
#
|
| 148 |
+
# demo.launch()
|