Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from lmdeploy.serve.gradio.turbomind_coupled import *
|
| 2 |
+
from lmdeploy.messages import TurbomindEngineConfig
|
| 3 |
+
from lmdeploy import ChatTemplateConfig
|
| 4 |
+
|
| 5 |
+
chat_template = ChatTemplateConfig(model_name='internlm2-chat-7b', system='', eosys='', meta_instruction='')
|
| 6 |
+
backend_config = TurbomindEngineConfig(model_name='internlm2-chat-7b', max_batch_size=1, cache_max_entry_count=0.05)#, model_format='awq')
|
| 7 |
+
model_path = 'internlm/internlm2-math-7b'
|
| 8 |
+
|
| 9 |
+
InterFace.async_engine = AsyncEngine(
|
| 10 |
+
model_path=model_path,
|
| 11 |
+
backend='turbomind',
|
| 12 |
+
backend_config=backend_config,
|
| 13 |
+
chat_template_config=chat_template,
|
| 14 |
+
tp=1)
|
| 15 |
+
|
| 16 |
+
async def reset_local_func(instruction_txtbox: gr.Textbox,
|
| 17 |
+
state_chatbot: Sequence, session_id: int):
|
| 18 |
+
"""reset the session.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
instruction_txtbox (str): user's prompt
|
| 22 |
+
state_chatbot (Sequence): the chatting history
|
| 23 |
+
session_id (int): the session id
|
| 24 |
+
"""
|
| 25 |
+
state_chatbot = []
|
| 26 |
+
# end the session
|
| 27 |
+
with InterFace.lock:
|
| 28 |
+
InterFace.global_session_id += 1
|
| 29 |
+
session_id = InterFace.global_session_id
|
| 30 |
+
return (state_chatbot, state_chatbot, gr.Textbox.update(value=''), session_id)
|
| 31 |
+
|
| 32 |
+
async def cancel_local_func(state_chatbot: Sequence, cancel_btn: gr.Button,
|
| 33 |
+
reset_btn: gr.Button, session_id: int):
|
| 34 |
+
"""stop the session.
|
| 35 |
+
|
| 36 |
+
Args:
|
| 37 |
+
instruction_txtbox (str): user's prompt
|
| 38 |
+
state_chatbot (Sequence): the chatting history
|
| 39 |
+
cancel_btn (gr.Button): the cancel button
|
| 40 |
+
reset_btn (gr.Button): the reset button
|
| 41 |
+
session_id (int): the session id
|
| 42 |
+
"""
|
| 43 |
+
yield (state_chatbot, disable_btn, disable_btn, session_id)
|
| 44 |
+
InterFace.async_engine.stop_session(session_id)
|
| 45 |
+
# pytorch backend does not support resume chat history now
|
| 46 |
+
if InterFace.async_engine.backend == 'pytorch':
|
| 47 |
+
yield (state_chatbot, disable_btn, enable_btn, session_id)
|
| 48 |
+
else:
|
| 49 |
+
with InterFace.lock:
|
| 50 |
+
InterFace.global_session_id += 1
|
| 51 |
+
session_id = InterFace.global_session_id
|
| 52 |
+
messages = []
|
| 53 |
+
for qa in state_chatbot:
|
| 54 |
+
messages.append(dict(role='user', content=qa[0]))
|
| 55 |
+
if qa[1] is not None:
|
| 56 |
+
messages.append(dict(role='assistant', content=qa[1]))
|
| 57 |
+
gen_config = GenerationConfig(max_new_tokens=0)
|
| 58 |
+
async for out in InterFace.async_engine.generate(messages,
|
| 59 |
+
session_id,
|
| 60 |
+
gen_config=gen_config,
|
| 61 |
+
stream_response=True,
|
| 62 |
+
sequence_start=True,
|
| 63 |
+
sequence_end=False):
|
| 64 |
+
pass
|
| 65 |
+
yield (state_chatbot, disable_btn, enable_btn, session_id)
|
| 66 |
+
|
| 67 |
+
with gr.Blocks(css=CSS, theme=THEME) as demo:
|
| 68 |
+
state_chatbot = gr.State([])
|
| 69 |
+
state_session_id = gr.State(0)
|
| 70 |
+
|
| 71 |
+
with gr.Column(elem_id='container'):
|
| 72 |
+
gr.Markdown('## LMDeploy Playground')
|
| 73 |
+
|
| 74 |
+
chatbot = gr.Chatbot(
|
| 75 |
+
elem_id='chatbot',
|
| 76 |
+
label=InterFace.async_engine.engine.model_name)
|
| 77 |
+
instruction_txtbox = gr.Textbox(
|
| 78 |
+
placeholder='Please input the instruction',
|
| 79 |
+
label='Instruction')
|
| 80 |
+
with gr.Row():
|
| 81 |
+
cancel_btn = gr.Button(value='Cancel', interactive=False)
|
| 82 |
+
reset_btn = gr.Button(value='Reset')
|
| 83 |
+
with gr.Row():
|
| 84 |
+
request_output_len = gr.Slider(1,
|
| 85 |
+
2048,
|
| 86 |
+
value=1024,
|
| 87 |
+
step=1,
|
| 88 |
+
label='Maximum new tokens')
|
| 89 |
+
top_p = gr.Slider(0.01, 1, value=1.0, step=0.01, label='Top_p')
|
| 90 |
+
temperature = gr.Slider(0.01,
|
| 91 |
+
1.5,
|
| 92 |
+
value=0.01,
|
| 93 |
+
step=0.01,
|
| 94 |
+
label='Temperature')
|
| 95 |
+
|
| 96 |
+
send_event = instruction_txtbox.submit(chat_stream_local, [
|
| 97 |
+
instruction_txtbox, state_chatbot, cancel_btn, reset_btn,
|
| 98 |
+
state_session_id, top_p, temperature, request_output_len
|
| 99 |
+
], [state_chatbot, chatbot, cancel_btn, reset_btn])
|
| 100 |
+
instruction_txtbox.submit(
|
| 101 |
+
lambda: gr.Textbox.update(value=''),
|
| 102 |
+
[],
|
| 103 |
+
[instruction_txtbox],
|
| 104 |
+
)
|
| 105 |
+
cancel_btn.click(
|
| 106 |
+
cancel_local_func,
|
| 107 |
+
[state_chatbot, cancel_btn, reset_btn, state_session_id],
|
| 108 |
+
[state_chatbot, cancel_btn, reset_btn, state_session_id],
|
| 109 |
+
cancels=[send_event])
|
| 110 |
+
|
| 111 |
+
reset_btn.click(reset_local_func,
|
| 112 |
+
[instruction_txtbox, state_chatbot, state_session_id],
|
| 113 |
+
[state_chatbot, chatbot, instruction_txtbox, state_session_id],
|
| 114 |
+
cancels=[send_event])
|
| 115 |
+
|
| 116 |
+
def init():
|
| 117 |
+
with InterFace.lock:
|
| 118 |
+
InterFace.global_session_id += 1
|
| 119 |
+
new_session_id = InterFace.global_session_id
|
| 120 |
+
return new_session_id
|
| 121 |
+
|
| 122 |
+
demo.load(init, inputs=None, outputs=[state_session_id])
|
| 123 |
+
|
| 124 |
+
demo.queue(concurrency_count=InterFace.async_engine.instance_num,
|
| 125 |
+
max_size=100).launch()
|