KevinNuNu commited on
Commit
38fe132
0 Parent(s):
Files changed (5) hide show
  1. README.md +13 -0
  2. app.py +128 -0
  3. download.sh +8 -0
  4. install_lmdeploy.sh +27 -0
  5. requirements.txt +1 -0
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: LMDeployDemo
3
+ emoji: 🌍
4
+ colorFrom: yellow
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.44.3
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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='turbomind-internlm-chat-20b-w4',
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, enable_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, enable_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, enable_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)
107
+ reset_btn = gr.Button(value='Reset')
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()
download.sh ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ # git clone [email protected]:lmdeploy/turbomind-internlm-chat-20b-w4
3
+ if [ ! -d "turbomind-internlm-chat-20b-w4" ]
4
+ then
5
+ echo "Downloading..."
6
+ git lfs clone https://huggingface.co/lmdeploy/turbomind-internlm-chat-20b-w4
7
+ fi
8
+ ls turbomind-internlm-chat-20b-w4
install_lmdeploy.sh ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # 安装lmdeploy
4
+ # 获取安装lmdeploy的位置下的lib文件夹路径
5
+ lmdeploy_dir=$(pip show lmdeploy | grep Location | cut -d' ' -f2)
6
+ lib_dir="${lmdeploy_dir}/lmdeploy/lib"
7
+
8
+ # 检查lib目录是否存在
9
+ if [ ! -d "$lib_dir" ]
10
+ then
11
+ echo "Lib directory does not exist at ${lib_dir}"
12
+ exit 1
13
+ fi
14
+
15
+ # 克隆lmdeploy的仓库
16
+ git clone https://github.com/InternLM/lmdeploy.git || exit 1
17
+
18
+ # 将lib文件夹拷贝到刚刚克隆的lmdeploy下
19
+ cp -r "$lib_dir" "lmdeploy/lmdeploy/" || exit 1
20
+
21
+ pip uninstall -y lmdeploy
22
+
23
+ # cd lmdeploy && git checkout v0.0.8-rc1 && cd ..
24
+ mv lmdeploy lmdeploy-backup
25
+ mv lmdeploy-backup/lmdeploy lmdeploy
26
+
27
+ echo "Script executed successfully"
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ lmdeploy