Spaces:
Runtime error
Runtime error
InternLM-Math
commited on
Commit
•
d21c507
1
Parent(s):
dc6726d
Update app.py
Browse files
app.py
CHANGED
@@ -1,177 +1,130 @@
|
|
1 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
2 |
-
import torch
|
3 |
-
import streamlit as st
|
4 |
-
from openxlab.model import download
|
5 |
-
from modelscope import snapshot_download
|
6 |
import os
|
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 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
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 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
#
|
134 |
-
#
|
135 |
-
|
136 |
-
# 1024,
|
137 |
-
# value=512,
|
138 |
-
# step=1,
|
139 |
-
# label='Maximum new tokens')
|
140 |
-
# top_p = gr.Slider(0.01, 1, value=1.0, step=0.01, label='Top_p')
|
141 |
-
# temperature = gr.Slider(0.01,
|
142 |
-
# 1.5,
|
143 |
-
# value=0.01,
|
144 |
-
# step=0.01,
|
145 |
-
# label='Temperature')
|
146 |
-
|
147 |
-
# send_event = instruction_txtbox.submit(chat_stream_local, [
|
148 |
-
# instruction_txtbox, state_chatbot, cancel_btn, reset_btn,
|
149 |
-
# state_session_id, top_p, temperature, request_output_len
|
150 |
-
# ], [state_chatbot, chatbot, cancel_btn, reset_btn])
|
151 |
-
# instruction_txtbox.submit(
|
152 |
-
# lambda: gr.Textbox.update(value=''),
|
153 |
-
# [],
|
154 |
-
# [instruction_txtbox],
|
155 |
-
# )
|
156 |
-
# cancel_btn.click(
|
157 |
-
# cancel_local_func,
|
158 |
-
# [state_chatbot, cancel_btn, reset_btn, state_session_id],
|
159 |
-
# [state_chatbot, cancel_btn, reset_btn, state_session_id],
|
160 |
-
# cancels=[send_event])
|
161 |
-
|
162 |
-
# reset_btn.click(reset_local_func,
|
163 |
-
# [instruction_txtbox, state_chatbot, state_session_id],
|
164 |
-
# [state_chatbot, chatbot, instruction_txtbox, state_session_id],
|
165 |
-
# cancels=[send_event])
|
166 |
-
|
167 |
-
# def init():
|
168 |
-
# with InterFace.lock:
|
169 |
-
# InterFace.global_session_id += 1
|
170 |
-
# new_session_id = InterFace.global_session_id
|
171 |
-
# return new_session_id
|
172 |
-
|
173 |
-
# demo.load(init, inputs=None, outputs=[state_session_id])
|
174 |
-
|
175 |
-
# # demo.queue(concurrency_count=InterFace.async_engine.instance_num,
|
176 |
-
# # max_size=100).launch()
|
177 |
-
# demo.queue(max_size=1000).launch(max_threads=InterFace.async_engine.instance_num)
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from lmdeploy import ChatTemplateConfig
|
7 |
+
|
8 |
+
chat_template = ChatTemplateConfig(model_name='internlm2-chat-7b', system='', eosys='', meta_instruction='')
|
9 |
+
backend_config = TurbomindEngineConfig(model_name='internlm2-chat-7b', max_batch_size=1, cache_max_entry_count=0.05)#, model_format='awq')
|
10 |
+
model_path = 'internlm/internlm2-math-7b'
|
11 |
+
|
12 |
+
InterFace.async_engine = AsyncEngine(
|
13 |
+
model_path=model_path,
|
14 |
+
backend='turbomind',
|
15 |
+
backend_config=backend_config,
|
16 |
+
chat_template_config=chat_template,
|
17 |
+
tp=1)
|
18 |
+
|
19 |
+
async def reset_local_func(instruction_txtbox: gr.Textbox,
|
20 |
+
state_chatbot: Sequence, session_id: int):
|
21 |
+
"""reset the session.
|
22 |
+
|
23 |
+
Args:
|
24 |
+
instruction_txtbox (str): user's prompt
|
25 |
+
state_chatbot (Sequence): the chatting history
|
26 |
+
session_id (int): the session id
|
27 |
+
"""
|
28 |
+
state_chatbot = []
|
29 |
+
# end the session
|
30 |
+
with InterFace.lock:
|
31 |
+
InterFace.global_session_id += 1
|
32 |
+
session_id = InterFace.global_session_id
|
33 |
+
return (state_chatbot, state_chatbot, gr.Textbox.update(value=''), session_id)
|
34 |
+
|
35 |
+
async def cancel_local_func(state_chatbot: Sequence, cancel_btn: gr.Button,
|
36 |
+
reset_btn: gr.Button, session_id: int):
|
37 |
+
"""stop the session.
|
38 |
+
|
39 |
+
Args:
|
40 |
+
instruction_txtbox (str): user's prompt
|
41 |
+
state_chatbot (Sequence): the chatting history
|
42 |
+
cancel_btn (gr.Button): the cancel button
|
43 |
+
reset_btn (gr.Button): the reset button
|
44 |
+
session_id (int): the session id
|
45 |
+
"""
|
46 |
+
yield (state_chatbot, disable_btn, disable_btn, session_id)
|
47 |
+
InterFace.async_engine.stop_session(session_id)
|
48 |
+
# pytorch backend does not support resume chat history now
|
49 |
+
if InterFace.async_engine.backend == 'pytorch':
|
50 |
+
yield (state_chatbot, disable_btn, enable_btn, session_id)
|
51 |
+
else:
|
52 |
+
with InterFace.lock:
|
53 |
+
InterFace.global_session_id += 1
|
54 |
+
session_id = InterFace.global_session_id
|
55 |
+
messages = []
|
56 |
+
for qa in state_chatbot:
|
57 |
+
messages.append(dict(role='user', content=qa[0]))
|
58 |
+
if qa[1] is not None:
|
59 |
+
messages.append(dict(role='assistant', content=qa[1]))
|
60 |
+
gen_config = GenerationConfig(max_new_tokens=0)
|
61 |
+
async for out in InterFace.async_engine.generate(messages,
|
62 |
+
session_id,
|
63 |
+
gen_config=gen_config,
|
64 |
+
stream_response=True,
|
65 |
+
sequence_start=True,
|
66 |
+
sequence_end=False):
|
67 |
+
pass
|
68 |
+
yield (state_chatbot, disable_btn, enable_btn, session_id)
|
69 |
+
|
70 |
+
with gr.Blocks(css=CSS, theme=THEME) as demo:
|
71 |
+
state_chatbot = gr.State([])
|
72 |
+
state_session_id = gr.State(0)
|
73 |
+
|
74 |
+
with gr.Column(elem_id='container'):
|
75 |
+
gr.Markdown('## LMDeploy Playground')
|
76 |
+
gr.Markdown('[InternLM Math GitHub Page](https://github.com/InternLM/InternLM-Math)')
|
77 |
+
|
78 |
+
chatbot = gr.Chatbot(
|
79 |
+
elem_id='chatbot',
|
80 |
+
label=InterFace.async_engine.engine.model_name)
|
81 |
+
instruction_txtbox = gr.Textbox(
|
82 |
+
placeholder='Please input the instruction',
|
83 |
+
label='Instruction')
|
84 |
+
with gr.Row():
|
85 |
+
cancel_btn = gr.Button(value='Cancel', interactive=False)
|
86 |
+
reset_btn = gr.Button(value='Reset')
|
87 |
+
with gr.Row():
|
88 |
+
request_output_len = gr.Slider(1,
|
89 |
+
2048,
|
90 |
+
value=1024,
|
91 |
+
step=1,
|
92 |
+
label='Maximum new tokens')
|
93 |
+
top_p = gr.Slider(0.01, 1, value=1.0, step=0.01, label='Top_p')
|
94 |
+
temperature = gr.Slider(0.01,
|
95 |
+
1.5,
|
96 |
+
value=0.01,
|
97 |
+
step=0.01,
|
98 |
+
label='Temperature')
|
99 |
+
|
100 |
+
send_event = instruction_txtbox.submit(chat_stream_local, [
|
101 |
+
instruction_txtbox, state_chatbot, cancel_btn, reset_btn,
|
102 |
+
state_session_id, top_p, temperature, request_output_len
|
103 |
+
], [state_chatbot, chatbot, cancel_btn, reset_btn])
|
104 |
+
instruction_txtbox.submit(
|
105 |
+
lambda: gr.Textbox.update(value=''),
|
106 |
+
[],
|
107 |
+
[instruction_txtbox],
|
108 |
+
)
|
109 |
+
cancel_btn.click(
|
110 |
+
cancel_local_func,
|
111 |
+
[state_chatbot, cancel_btn, reset_btn, state_session_id],
|
112 |
+
[state_chatbot, cancel_btn, reset_btn, state_session_id],
|
113 |
+
cancels=[send_event])
|
114 |
+
|
115 |
+
reset_btn.click(reset_local_func,
|
116 |
+
[instruction_txtbox, state_chatbot, state_session_id],
|
117 |
+
[state_chatbot, chatbot, instruction_txtbox, state_session_id],
|
118 |
+
cancels=[send_event])
|
119 |
+
|
120 |
+
def init():
|
121 |
+
with InterFace.lock:
|
122 |
+
InterFace.global_session_id += 1
|
123 |
+
new_session_id = InterFace.global_session_id
|
124 |
+
return new_session_id
|
125 |
+
|
126 |
+
demo.load(init, inputs=None, outputs=[state_session_id])
|
127 |
+
|
128 |
+
# demo.queue(concurrency_count=InterFace.async_engine.instance_num,
|
129 |
+
# max_size=100).launch()
|
130 |
+
demo.queue(max_size=1000).launch(max_threads=InterFace.async_engine.instance_num)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|