-
Notifications
You must be signed in to change notification settings - Fork 14
/
webui_demo.py
241 lines (202 loc) · 8.73 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
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
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), \
gr.update(placeholder='Please upload your image first', interactive=False),\
gr.update(value="Upload & Start Chat", interactive=True), \
history, \
img_list
def upload_img(gr_img, text_input, history, img_list, img_prefix):
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).unsqueeze(0).to(device)
elif isinstance(image, Image.Image):
raw_image = image
raw_image = raw_image.convert('RGB')
image = vis_processor(raw_image).unsqueeze(0).to(device)
elif isinstance(image, torch.Tensor):
if len(image.shape) == 3:
image = image.unsqueeze(0)
image = image.to(device)
img_list.append(image)
msg = "Received."
return msg
if gr_img is None:
return None, None, gr.update(interactive=True), history, None
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 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,
}
)
image_tensor = img_list[0] # 如果想支持多图情况:torch.stack(img_list).to(self.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 MiniGPT4Qwen</h1>"""
description = """<h3>This is the demo of MiniGPT4Qwen. 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):
image = gr.Image(type="pil")
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='MiniGPT4Qwen')
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])
upload_button.click(upload_img, [image, text_input, history,img_list, img_prefix], [image, 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, text_input, upload_button, history, img_list], queue=False)
demo.launch(share=True,inbrowser=True)