unsubscribe commited on
Commit
c5ef9d5
1 Parent(s): 8b316c4

update app.py by importing lmdeploy

Browse files
Files changed (1) hide show
  1. app.py +122 -63
app.py CHANGED
@@ -1,63 +1,122 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)