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