-
Notifications
You must be signed in to change notification settings - Fork 20
/
webui_demo.py
344 lines (287 loc) · 12.3 KB
/
webui_demo.py
1
2
3
4
5
6
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import argparse
import os
import random
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import gradio as gr
from PIL import Image
from transformers.generation import GenerationConfig
from lavis.common.config import Config
from lavis.common.dist_utils import get_rank
from lavis.common.registry import registry
from lavis.models import load_model_and_preprocess
from functools import partial
from copy import deepcopy
import cv2
def extract_frames(video_path, num_frames):
cap = cv2.VideoCapture(video_path)
total_num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
sampling_interval = int(total_num_frames / num_frames)
if sampling_interval == 0: # total_frames < target_frames, 逐帧提取
sampling_interval = 1
images = []
frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if frame_count % sampling_interval == 0:
frame = frame[:, :, ::-1] # BGR to RGB
images.append(Image.fromarray(frame).convert("RGB"))
frame_count += 1
if len(images) >= num_frames:
break
cap.release()
if len(images) == 0:
raise AssertionError(f"Video not found or no frames extracted: {video_path}")
return images
def disable_torch_init():
"""
Disable the redundant torch default initialization to accelerate model creation.
"""
setattr(torch.nn.Linear, "reset_parameters", lambda self: None)
setattr(torch.nn.LayerNorm, "reset_parameters", lambda self: None)
def _load_model_processor(args):
if args.cpu_only:
device_map = "cpu"
else:
device_map = 'cuda:{}'.format(args.gpu_id)
global load_model_and_preprocess
load_model_and_preprocess = partial(load_model_and_preprocess,is_eval=True,device=device_map)
model, vis_processors, _ = load_model_and_preprocess("minigpt4qwen", args.model_type, llm_device_map=args.llm_device_map)
model.load_checkpoint(args.checkpoint_path)
model.llm_model.transformer.bfloat16()
model.llm_model.lm_head.bfloat16()
generation_config = {
"chat_format": "chatml",
"eos_token_id": 151643,
"pad_token_id": 151643,
"max_window_size": 6144,
"max_new_tokens": 512,
"transformers_version": "4.31.0"
}
return model, vis_processors, generation_config
def parse_args():
parser = argparse.ArgumentParser(description="Demo")
parser.add_argument("--model-type",type=str,default='qwen7b_chat',choices=['qwen7b_chat','qwen14b_chat'])
parser.add_argument("-c", "--checkpoint-path", type=str,
help="Checkpoint name or path, default to %(default)r")
parser.add_argument("-s", "--seed", type=int, default=42, help="Random seed")
parser.add_argument("--cpu-only", action="store_true", help="Run demo with CPU only")
parser.add_argument("--gpu_id", type=int, default=0, help="specify the gpu to load the model.")
parser.add_argument("--llm_device_map", type=str, default="cpu")
args = parser.parse_args()
return args
# ========================================
# Model Initialization
# ========================================
print('Initializing Chat')
args = parse_args()
if torch.cuda.is_available() and not args.cpu_only:
device='cuda:{}'.format(args.gpu_id)
else:
device=torch.device('cpu')
disable_torch_init()
model, vis_processors, default_generation_config = _load_model_processor(args)
vis_processor = vis_processors["eval"]
print('Initialization Finished')
# ========================================
# Gradio Setting
# ========================================
def gradio_reset(history, img_list):
if history is not None:
history = []
if img_list is not None:
img_list = []
return None, \
gr.update(value=None, interactive=True, visible=True), \
gr.update(value=None, interactive=True, visible=False), \
gr.update(placeholder='Please upload your image first', interactive=False), \
gr.update(value="Upload & Start Chat", interactive=True), \
gr.update(value=None), \
history, \
img_list
def load_and_process_img(image,img_list):
if isinstance(image, str): # is a image path
raw_image = Image.open(image).convert('RGB')
image = vis_processor(raw_image)
elif isinstance(image, Image.Image):
raw_image = image
raw_image = raw_image.convert('RGB')
image = vis_processor(raw_image)
elif isinstance(image, torch.Tensor):
if len(image.shape) == 3:
image = image
else:
assert False, "the `image.ndim` must be 3"
img_list.append(image)
msg = "Received."
return msg
def load_and_process_video(video_path,img_list):
assert isinstance(video_path, str), "Input must be a path of video"
raw_images = extract_frames(video_path, num_frames=32) # hard-code the `num_frames`(32)
images = [vis_processor(raw_image) for raw_image in raw_images]
img_list.extend(images)
msg = "Received."
return msg
def upload_img(gr_img, text_input, history, img_list, img_prefix):
if gr_img is None:
return (
gr.update(),
gr.update(),
gr.update(),
history, img_list, img_prefix
)
llm_message = load_and_process_img(gr_img, img_list)
img_prefix = '<Img><ImageHere></Img>'
return gr.update(interactive=False), \
gr.update(interactive=True, placeholder='Type and press Enter'), \
gr.update(value="Start Chatting", interactive=False), \
history, \
img_list, \
img_prefix
# def upload_imgs(gr_imgs, text_input, history, img_list, img_prefix):
# if gr_imgs is None:
# return (
# gr.update(),
# gr.update(),
# gr.update(),
# history, img_list, img_prefix
# )
# for gr_img in gr_imgs:
# llm_message = load_and_process_img(gr_img, img_list)
# img_prefix = '<Img>' + '<ImageHere>' * len(gr_imgs) + '</Img>'
# return gr.update(interactive=False), \
# gr.update(interactive=True, placeholder='Type and press Enter'), \
# gr.update(value="Start Chatting", interactive=False), \
# history, \
# img_list, \
# img_prefix
def upload_video(video, text_input, history, img_list, img_prefix):
# Add your video processing logic here
if video is None:
return (
gr.update(),
gr.update(),
gr.update(),
history, img_list, img_prefix
)
llm_message = load_and_process_video(video, img_list)
img_prefix = f"<Img>{'<ImageHere>' * len(img_list)}</Img>"
return gr.update(interactive=False), \
gr.update(interactive=True, placeholder='Type and press Enter'), \
gr.update(value="Start Chatting", interactive=False), \
history, \
img_list, \
img_prefix
def gradio_ask(user_message, chatbot, img_prefix):
if len(user_message) == 0:
return gr.update(interactive=True, placeholder='Input should not be empty!'), chatbot, history
def get_ask(user_message, img_prefix):
return img_prefix + user_message
user_message = get_ask(user_message,img_prefix)
chatbot = chatbot + [[user_message, None]]
img_prefix = ""
return '', chatbot, img_prefix
def gradio_answer(chatbot, history, img_list, do_sample,num_beams, temperature, top_k, top_p):
generation_config = deepcopy(default_generation_config)
generation_config.update(
{
"do_sample": do_sample=='True',
"num_beams": num_beams,
'temperature': temperature,
"top_k": top_k,
"top_p": top_p,
}
)
global device
image_tensor = torch.stack(img_list).to(device)
generation_config = GenerationConfig.from_dict(generation_config)
global args
if args.cpu_only:
model.bfloat16()
response, history = model.chat(query=chatbot[-1][0], history=history, image_tensor=image_tensor.bfloat16(), generation_config=generation_config,verbose=True)
else:
with torch.cuda.amp.autocast(enabled=True,dtype=torch.bfloat16):
response, history = model.chat(query=chatbot[-1][0], history=history, image_tensor=image_tensor.bfloat16(), generation_config=generation_config,verbose=True)
chatbot[-1][1] = response
return chatbot, history, img_list
title = """<h1 align="center">Demo of MPPQwen</h1>"""
description = """<h3>This is the demo of MPPQwen, supporting {single-image/multi-image/video} {single-turn/multi-turn} conversation. Upload your images and start chatting! <br> To use
example questions, click example image, hit upload, and press enter in the chatbox. </h3>"""
from transformers.trainer_utils import set_seed
set_seed(args.seed)
#TODO show examples below
with gr.Blocks() as demo:
gr.Markdown(title)
gr.Markdown(description)
with gr.Row():
with gr.Column(scale=0.5):
mode = gr.Dropdown(choices=["Single Image", "Video"], label="Select Mode", value=None)
image_single = gr.Image(type="filepath", label="Upload Image", value=None)
video = gr.Video(label="Upload Video", value=None)
upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
clear = gr.Button("Restart 🔄")
do_sample = gr.components.Radio(['True', 'False'],
label='do_sample(If False, num_beams, temperature and so on cannot work!)',
value='False')
num_beams = gr.Slider(
minimum=1,
maximum=10,
value=1,
step=1,
interactive=True,
label="beam search numbers)",
)
temperature = gr.Slider(
minimum=0.1,
maximum=2.0,
value=1.0,
step=0.1,
interactive=True,
label="Temperature",
)
top_k = gr.Slider(
minimum=0,
maximum=5,
value=1,
step=1,
interactive=True,
label="Top_k",
)
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=1.0,
step=0.05,
interactive=True,
label="Top_p",
)
with gr.Column():
history = gr.State(value=[])
img_list = gr.State(value=[])
chatbot = gr.Chatbot(label='MPPQwen')
img_prefix = gr.State(value="")
text_input = gr.Textbox(label='User', placeholder='Please upload your image first', interactive=False)
# gr.Examples(examples=[
# ["examples/minigpt4_image_3.jpg", "描述下这幅图片"],
# ], inputs=[image, text_input])
def update_inputs(mode):
if mode == "Single Image":
return gr.update(visible=True), gr.update(visible=False)
elif mode == "Video":
return gr.update(visible=False), gr.update(visible=True)
else:
return gr.update(visible=True), gr.update(visible=True)
mode.change(update_inputs, inputs=mode, outputs=[image_single, video])
# print('Mode\t', mode.value)
upload_button.click(upload_img, [image_single, text_input, history, img_list, img_prefix], [image_single, text_input, upload_button, history, img_list, img_prefix])
upload_button.click(upload_video, [video, text_input, history, img_list, img_prefix], [video, text_input, upload_button, history, img_list, img_prefix])
# print(list(map(type,[text_input, chatbot, img_prefix])))
# print(list(map(type,[chatbot, history, img_list, do_sample, num_beams, temperature, top_k, top_p])))
text_input.submit(gradio_ask, [text_input, chatbot, img_prefix], [text_input, chatbot, img_prefix]).then(
gradio_answer, [chatbot, history, img_list, do_sample, num_beams, temperature, top_k, top_p], [chatbot, history, img_list]
)
clear.click(gradio_reset, [history, img_list], [chatbot, image_single, video, text_input, upload_button, mode, history, img_list], queue=False)
demo.launch(share=True,inbrowser=True)