Pectics commited on
Commit
d81f4b6
·
1 Parent(s): bd5a97e

大改UI界面,优化api调用逻辑

Browse files
Files changed (1) hide show
  1. app.py +191 -32
app.py CHANGED
@@ -1,9 +1,10 @@
1
  from spaces import GPU
2
 
3
  from threading import Thread
4
- from transformers import Qwen2VLForConditionalGeneration, Qwen2VLProcessor, TextIteratorStreamer, AutoProcessor, BatchFeature
5
  from qwen_vl_utils import process_vision_info
6
- from gradio import ChatInterface, Slider
 
7
 
8
  model_path = "Pectics/Softie-VL-7B-250123"
9
 
@@ -17,8 +18,40 @@ min_pixels = 256 * 28 * 28
17
  max_pixels = 1280 * 28 * 28
18
  processor: Qwen2VLProcessor = AutoProcessor.from_pretrained(model_path, min_pixels=min_pixels, max_pixels=max_pixels)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  @GPU
21
- def infer(
22
  inputs: tuple,
23
  max_tokens: int,
24
  temperature: float,
@@ -40,41 +73,167 @@ def infer(
40
  top_p=top_p,
41
  )
42
  Thread(target=model.generate, kwargs=kwargs).start()
43
- response = ""
44
  for token in streamer:
45
- response += token
46
- yield response
47
 
48
- def respond(
49
- message: str | list[object],
50
- history: list[object],
 
51
  max_tokens: int,
52
  temperature: float,
53
  top_p: float,
 
 
 
54
  ):
55
- print('message', message)
56
- print('history', history)
57
- if isinstance(message, str):
58
- if len(history) == 0 or history[0]['role'] != 'system':
59
- history.insert(0, {"role": "system", "content": """You are Softie, or 小软 in Chinese.
60
- You are an intelligent assistant developed by the School of Software at Hefei University of Technology.
61
- You like to chat with people and help them solve problems."""})
62
- history.append({"role": "user", "content": message})
63
- message = history
64
- text_inputs = processor.apply_chat_template(message, tokenize=False, add_generation_prompt=True)
65
- image_inputs, video_inputs = process_vision_info(message)
66
- for response in infer((text_inputs, image_inputs, video_inputs), max_tokens, temperature, top_p):
67
- yield response
68
-
69
- app = ChatInterface(
70
- respond,
71
- type="messages",
72
- additional_inputs=[
73
- Slider(minimum=1, maximum=2048, value=512, step=1, label="最大生成长度"),
74
- Slider(minimum=0.01, maximum=4.0, value=0.75, step=0.01, label="温度系数(Temperature)"),
75
- Slider(minimum=0.01, maximum=1.0, value=0.5, step=0.01, label="核取样系数(Top-p)"),
76
- ],
77
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  if __name__ == "__main__":
80
  app.launch()
 
1
  from spaces import GPU
2
 
3
  from threading import Thread
4
+ from transformers import Qwen2VLForConditionalGeneration, Qwen2VLProcessor, TextIteratorStreamer, AutoProcessor
5
  from qwen_vl_utils import process_vision_info
6
+
7
+ from gradio import ChatMessage, Chatbot, MultimodalTextbox, Slider, Checkbox, CheckboxGroup, Textbox, JSON, Blocks, Row, Column, Markdown, FileData
8
 
9
  model_path = "Pectics/Softie-VL-7B-250123"
10
 
 
18
  max_pixels = 1280 * 28 * 28
19
  processor: Qwen2VLProcessor = AutoProcessor.from_pretrained(model_path, min_pixels=min_pixels, max_pixels=max_pixels)
20
 
21
+ SYSTEM_PROMPT = """
22
+ You are Softie, or 小软 in Chinese.
23
+ You are an intelligent assistant developed by the School of Software at Hefei University of Technology.
24
+ You like to chat with people and help them solve problems.
25
+ You will interact with the user in the QQ group chat, and the user message you receive will satisfy the following format:
26
+ user_id: 用户ID
27
+ nickname: 用户昵称
28
+ content: 用户消息内容
29
+ You will directly output your reply content without the need to format it.
30
+ """.strip()
31
+
32
+ STREAMING_FLAG = False
33
+ STREAMING_STOP_FLAG = False
34
+
35
+ FORBIDDING_FLAG = False
36
+
37
+ def interrupt() -> None:
38
+ global STREAMING_FLAG
39
+ if STREAMING_FLAG:
40
+ global STREAMING_STOP_FLAG
41
+ STREAMING_STOP_FLAG = True
42
+
43
+ def callback(
44
+ input: dict,
45
+ history: list[ChatMessage],
46
+ messages: list
47
+ ) -> tuple[str, list[ChatMessage], list]:
48
+ if len(history) <= 1 or len(messages) <= 1:
49
+ return input["text"], history, messages
50
+ history.pop(); messages.pop(); messages.pop()
51
+ return history.pop()["content"], history, messages
52
+
53
  @GPU
54
+ def core_infer(
55
  inputs: tuple,
56
  max_tokens: int,
57
  temperature: float,
 
73
  top_p=top_p,
74
  )
75
  Thread(target=model.generate, kwargs=kwargs).start()
 
76
  for token in streamer:
77
+ yield token
 
78
 
79
+ def process_model(
80
+ messages: str | list[object],
81
+ user_id: str,
82
+ nickname: str,
83
  max_tokens: int,
84
  temperature: float,
85
  top_p: float,
86
+ use_tools: bool,
87
+ use_agents: bool,
88
+ *args: list,
89
  ):
90
+ global STREAMING_FLAG, STREAMING_STOP_FLAG, FORBIDDING_FLAG
91
+ if STREAMING_FLAG or FORBIDDING_FLAG or len(messages) <= 0 or messages[-1]["role"] != "user":
92
+ yield None, messages, args[0]
93
+ return
94
+ # embed user details
95
+ _msgs_copy = messages.copy()
96
+ if isinstance(_msgs_copy[-1]["content"], list):
97
+ if len(_msgs_copy[-1]["content"]) <= 0 or _msgs_copy[-1]["content"][-1]["type"] != "text":
98
+ _msgs_copy[-1]["content"].insert(0, {
99
+ "type": "text",
100
+ "text": f"""
101
+ user_id: {user_id}
102
+ nickname: {nickname}
103
+ content: """.lstrip(),
104
+ })
105
+ else:
106
+ _msgs_copy[-1]["content"][-1]["text"] = f"""
107
+ user_id: {user_id}
108
+ nickname: {nickname}
109
+ content: {_msgs_copy[-1]["content"][-1]["text"]}
110
+ """.strip()
111
+ else:
112
+ _msgs_copy[-1]["content"] = f"""
113
+ user_id: {user_id}
114
+ nickname: {nickname}
115
+ content: {_msgs_copy[-1]["content"]}
116
+ """.strip()
117
+ # process messages
118
+ text_inputs = processor.apply_chat_template(_msgs_copy, tokenize=False, add_generation_prompt=True)
119
+ image_inputs, video_inputs = process_vision_info(_msgs_copy)
120
+ response = ""
121
+ args[0].append(ChatMessage(role="assistant", content=""))
122
+ messages.append({"role": "assistant", "content": ""})
123
+ STREAMING_FLAG = True
124
+ for token in core_infer((text_inputs, image_inputs, video_inputs), max_tokens, temperature, top_p):
125
+ if STREAMING_STOP_FLAG:
126
+ response += "...(Interrupted)"
127
+ args[0][-1].content = response
128
+ messages[-1]["content"] = response
129
+ STREAMING_STOP_FLAG = False
130
+ yield response, messages, args[0]
131
+ break
132
+ response += token
133
+ args[0][-1].content = response
134
+ messages[-1]["content"] = response
135
+ yield response, messages, args[0]
136
+ STREAMING_FLAG = False
137
+
138
+ def process_input(
139
+ input: dict,
140
+ history: list[ChatMessage],
141
+ checkbox: list[str],
142
+ messages: str
143
+ ) -> tuple[str, list[ChatMessage], bool, bool, list]:
144
+ global STREAMING_FLAG, FORBIDDING_FLAG
145
+ if STREAMING_FLAG or not isinstance(input["text"], str) or input["text"].strip() == "":
146
+ FORBIDDING_FLAG = True
147
+ return (
148
+ input["text"],
149
+ history,
150
+ "允许使用工具" in checkbox,
151
+ "允许使用代理模型" in checkbox,
152
+ messages
153
+ )
154
+ FORBIDDING_FLAG = False
155
+ if len(history) <= 0:
156
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
157
+ history.append(ChatMessage(role="system", content=SYSTEM_PROMPT))
158
+ else:
159
+ if history[0]["role"] != "system":
160
+ history.insert(0, ChatMessage(role="system", content=SYSTEM_PROMPT))
161
+ if messages[0]["role"] != "system":
162
+ messages.insert(0, {"role": "system", "content": SYSTEM_PROMPT})
163
+ message = {"role": "user", "content": []}
164
+ while isinstance(input["files"], list) and len(input["files"]) > 0:
165
+ path = input["files"].pop(0)
166
+ message["content"].append({"type": "image", "image": f"file://{path}"}) # Qwen2VL format
167
+ history.append(ChatMessage(role="user", content=FileData(path=path)))
168
+ message["content"].append({"type": "text", "text": input["text"]})
169
+ if len(message["content"]) == 1 and message["content"][0]["type"] == "text":
170
+ message["content"] = message["content"][0]["text"]
171
+ history.append(ChatMessage(role="user", content=input["text"]))
172
+ messages.append(message)
173
+ return (
174
+ "",
175
+ history,
176
+ "允许使用工具" in checkbox,
177
+ "允许使用代理模型" in checkbox,
178
+ messages,
179
+ )
180
+
181
+ with Blocks() as app:
182
+ text_response = Textbox("", visible=False, interactive=False)
183
+ use_tools = Checkbox(visible=False, interactive=False)
184
+ use_agents = Checkbox(visible=False, interactive=False)
185
+ Markdown("# 小软Softie")
186
+ with Row():
187
+ with Column(scale=3, min_width=500):
188
+ chatbot = Chatbot(type="messages", avatar_images=(None, "avatar.jpg"), feedback_options=("Like", "Dislike"), scale=3, min_height=700, min_width=500, resizeable=True, show_label=False, show_copy_button=True, show_copy_all_button=True)
189
+ textbox = MultimodalTextbox(file_types=[".jpg", ".jpeg", ".png"], file_count="multiple", stop_btn=True, show_label=False, autofocus=False, placeholder="在此输入内容")
190
+ with Column(scale=1, min_width=300):
191
+ with Column(scale=0):
192
+ max_tokens = Slider(interactive=True, minimum=1, maximum=2048, value=512, step=1, label="max_tokens", info="最大生成长度")
193
+ temperature = Slider(interactive=True, minimum=0.01, maximum=4.0, value=0.75, step=0.01, label="temperature", info="温度系数")
194
+ top_p = Slider(interactive=True, minimum=0.01, maximum=1.0, value=0.5, step=0.01, label="top_p", info="核取样系数")
195
+ checkbox = CheckboxGroup(["允许使用工具", "允许使用代理模型"], label="options", info="功能选项(开发中)")
196
+ with Column(scale=0):
197
+ user_id = Textbox(value=123456789, label="user_id", info="用户ID")
198
+ nickname = Textbox(value="用户1234", label="nickname", info="用户昵称")
199
+ json_messages = JSON([], max_height=125, label="JSON消息格式")
200
+
201
+ chatbot.clear(
202
+ lambda: ("[]", ""),
203
+ outputs=[json_messages, text_response],
204
+ api_name=False,
205
+ show_api=False,
206
+ )
207
+ chatbot.retry(
208
+ callback,
209
+ [textbox, chatbot, json_messages],
210
+ [textbox, chatbot, json_messages],
211
+ api_name=False,
212
+ show_api=False,
213
+ )
214
+ chatbot.like(lambda: None, api_name=False, show_api=False)
215
+
216
+ textbox.submit(
217
+ process_input,
218
+ [textbox, chatbot, checkbox, json_messages],
219
+ [textbox, chatbot, use_tools, use_agents, json_messages],
220
+ queue=False,
221
+ api_name=False,
222
+ show_api=False,
223
+ show_progress="hidden",
224
+ trigger_mode="once",
225
+ ).then(
226
+ process_model,
227
+ [json_messages, user_id, nickname, max_tokens, temperature, top_p, use_tools, use_agents, chatbot],
228
+ [text_response, json_messages, chatbot],
229
+ queue=True,
230
+ api_name="api",
231
+ show_api=True,
232
+ show_progress="hidden",
233
+ trigger_mode="once",
234
+ )
235
+
236
+ textbox.stop(interrupt, api_name=False, show_api=False)
237
 
238
  if __name__ == "__main__":
239
  app.launch()