BridgeEight commited on
Commit
5dedc73
1 Parent(s): 8dac1b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -105
app.py CHANGED
@@ -1,128 +1,71 @@
1
- import os,random
2
- os.system('sh install_lmdeploy.sh')
3
- import gradio as gr
4
- from lmdeploy.serve.gradio.app import *
5
- os.system('sh download.sh')
6
 
7
- InterFace.async_engine = AsyncEngine(model_path='internlm2-chat-20b-4bits',
8
- instance_num=2,
9
- tp=1)
10
-
11
-
12
- async def reset_local_demo(instruction_txtbox: gr.Textbox,
13
- state_chatbot: gr.State, request: gr.Request):
14
- """reset the session.
15
-
16
- Args:
17
- instruction_txtbox (str): user's prompt
18
- state_chatbot (Sequence): the chatting history
19
- request (gr.Request): the request from a user
20
- """
21
- state_chatbot = []
22
-
23
- return (
24
- state_chatbot,
25
- state_chatbot,
26
- gr.Textbox.update(value=''),
27
- )
28
-
29
-
30
- async def cancel_local_demo(state_chatbot: gr.State, cancel_btn: gr.Button,
31
- reset_btn: gr.Button, request: gr.Request):
32
- """stop the session.
33
-
34
- Args:
35
- instruction_txtbox (str): user's prompt
36
- state_chatbot (Sequence): the chatting history
37
- request (gr.Request): the request from a user
38
- """
39
- return (state_chatbot, disable_btn, disable_btn)
40
-
41
- async def chat_stream_demo(
42
- instruction: str,
43
- state_chatbot: Sequence,
44
- cancel_btn: gr.Button,
45
- reset_btn: gr.Button,
46
- request: gr.Request,
47
- ):
48
- """Chat with AI assistant.
49
-
50
- Args:
51
- instruction (str): user's prompt
52
- state_chatbot (Sequence): the chatting history
53
- request (gr.Request): the request from a user
54
- """
55
- session_id = random.randint(0,100000)
56
- bot_summarized_response = ''
57
- state_chatbot = state_chatbot + [(instruction, None)]
58
- messages = []
59
- for item in state_chatbot:
60
- messages.append(dict(role='user', content=item[0]))
61
- if item[1] is not None:
62
- messages.append(dict(role='assistant', content=item[1]))
63
-
64
- yield (state_chatbot, state_chatbot, disable_btn, disable_btn,
65
- f'{bot_summarized_response}'.strip())
66
-
67
- async for outputs in InterFace.async_engine.generate(
68
- messages,
69
- session_id,
70
- stream_response=True,
71
- sequence_start=True,
72
- sequence_end=True):
73
- response = outputs.response
74
- if outputs.finish_reason == 'length':
75
- gr.Warning('WARNING: exceed session max length.'
76
- ' Please restart the session by reset button.')
77
- if outputs.generate_token_len < 0:
78
- gr.Warning('WARNING: running on the old session.'
79
- ' Please restart the session by reset button.')
80
- if state_chatbot[-1][-1] is None:
81
- state_chatbot[-1] = (state_chatbot[-1][0], response)
82
- else:
83
- state_chatbot[-1] = (state_chatbot[-1][0],
84
- state_chatbot[-1][1] + response
85
- ) # piece by piece
86
- yield (state_chatbot, state_chatbot, disable_btn, disable_btn,
87
- f'{bot_summarized_response}'.strip())
88
-
89
- yield (state_chatbot, state_chatbot, disable_btn, disable_btn,
90
- f'{bot_summarized_response}'.strip())
91
 
 
 
 
 
 
92
 
93
  with gr.Blocks(css=CSS, theme=THEME) as demo:
94
  state_chatbot = gr.State([])
 
95
 
96
  with gr.Column(elem_id='container'):
97
  gr.Markdown('## LMDeploy Playground')
98
 
99
  chatbot = gr.Chatbot(
100
  elem_id='chatbot',
101
- label=InterFace.async_engine.tm_model.model_name)
102
  instruction_txtbox = gr.Textbox(
103
  placeholder='Please input the instruction',
104
  label='Instruction')
105
  with gr.Row():
106
- cancel_btn = gr.Button(value='Cancel', interactive=False, visible=False)
107
- reset_btn = gr.Button(value='Reset', interactive=False, visible=False)
108
-
109
- send_event = instruction_txtbox.submit(
110
- chat_stream_demo,
111
- [instruction_txtbox, state_chatbot, cancel_btn, reset_btn],
112
- [state_chatbot, chatbot, cancel_btn, reset_btn])
 
 
 
 
 
 
 
 
 
 
 
 
113
  instruction_txtbox.submit(
114
  lambda: gr.Textbox.update(value=''),
115
  [],
116
  [instruction_txtbox],
117
  )
118
- cancel_btn.click(cancel_local_demo,
119
- [state_chatbot, cancel_btn, reset_btn],
120
- [state_chatbot, cancel_btn, reset_btn],
121
- cancels=[send_event])
122
-
123
- reset_btn.click(reset_local_demo, [instruction_txtbox, state_chatbot],
 
 
124
  [state_chatbot, chatbot, instruction_txtbox],
125
  cancels=[send_event])
126
 
127
- # print(f'server is gonna mount on: http://{server_name}:{server_port}')
128
- demo.queue(concurrency_count=4, max_size=100).launch()
 
 
 
 
 
 
 
 
 
1
+ from lmdeploy.serve.gradio.turbomind_coupled import *
2
+ from lmdeploy.messages import TurbomindEngineConfig
 
 
 
3
 
4
+ backend_config = TurbomindEngineConfig(max_batch_size=1, cache_max_entry_count=0.05)
5
+ model_path = 'internlm/internlm2-chat-20b-4bits'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ InterFace.async_engine = AsyncEngine(
8
+ model_path=model_path,
9
+ backend='turbomind',
10
+ backend_config=backend_config,
11
+ tp=1)
12
 
13
  with gr.Blocks(css=CSS, theme=THEME) as demo:
14
  state_chatbot = gr.State([])
15
+ state_session_id = gr.State(0)
16
 
17
  with gr.Column(elem_id='container'):
18
  gr.Markdown('## LMDeploy Playground')
19
 
20
  chatbot = gr.Chatbot(
21
  elem_id='chatbot',
22
+ label=InterFace.async_engine.engine.model_name)
23
  instruction_txtbox = gr.Textbox(
24
  placeholder='Please input the instruction',
25
  label='Instruction')
26
  with gr.Row():
27
+ cancel_btn = gr.Button(value='Cancel', interactive=False)
28
+ reset_btn = gr.Button(value='Reset')
29
+ with gr.Row():
30
+ request_output_len = gr.Slider(1,
31
+ 2048,
32
+ value=512,
33
+ step=1,
34
+ label='Maximum new tokens')
35
+ top_p = gr.Slider(0.01, 1, value=0.8, step=0.01, label='Top_p')
36
+ temperature = gr.Slider(0.01,
37
+ 1.5,
38
+ value=0.7,
39
+ step=0.01,
40
+ label='Temperature')
41
+
42
+ send_event = instruction_txtbox.submit(chat_stream_local, [
43
+ instruction_txtbox, state_chatbot, cancel_btn, reset_btn,
44
+ state_session_id, top_p, temperature, request_output_len
45
+ ], [state_chatbot, chatbot, cancel_btn, reset_btn])
46
  instruction_txtbox.submit(
47
  lambda: gr.Textbox.update(value=''),
48
  [],
49
  [instruction_txtbox],
50
  )
51
+ cancel_btn.click(
52
+ cancel_local_func,
53
+ [state_chatbot, cancel_btn, reset_btn, state_session_id],
54
+ [state_chatbot, cancel_btn, reset_btn],
55
+ cancels=[send_event])
56
+
57
+ reset_btn.click(reset_local_func,
58
+ [instruction_txtbox, state_chatbot, state_session_id],
59
  [state_chatbot, chatbot, instruction_txtbox],
60
  cancels=[send_event])
61
 
62
+ def init():
63
+ with InterFace.lock:
64
+ InterFace.global_session_id += 1
65
+ new_session_id = InterFace.global_session_id
66
+ return new_session_id
67
+
68
+ demo.load(init, inputs=None, outputs=[state_session_id])
69
+
70
+ demo.queue(concurrency_count=InterFace.async_engine.instance_num,
71
+ max_size=100).launch()