Spaces:
Runtime error
Runtime error
File size: 12,353 Bytes
e4bd7f9 fabe555 e4bd7f9 8bf1f58 e4bd7f9 2cc0bbc e4bd7f9 2cc0bbc e4bd7f9 2cc0bbc e4bd7f9 8bf1f58 2cc0bbc e4bd7f9 a6888db e4bd7f9 fabe555 e4bd7f9 |
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 |
import argparse
import os
import random
# import sys
# import os
#
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# sys.path.append(BASE_DIR)
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import gradio as gr
from constants.constant import LIGHTER_COLOR_MAP_HEX
# NOTE: Must import LlamaTokenizer before `bubogpt.common.config`
# otherwise, it will cause seg fault when `llama_tokenizer.decode` is called
from grounding_model import GroundingModule
from match import MatchModule
from bubogpt.common.config import Config
from bubogpt.common.dist_utils import get_rank
from bubogpt.common.registry import registry
from eval_scripts.conversation import Chat, CONV_X, DummyChat
# NOTE&TODO: put this before bubogpt import will cause circular import
# possibly because `imagebind` imports `bubogpt` and `bubogpt` also imports `imagebind`
from imagebind.models.image_bind import ModalityType
# from ner import NERModule
from tagging_model import TaggingModule
def parse_args():
parser = argparse.ArgumentParser(description="Qualitative")
parser.add_argument("--cfg-path", help="path to configuration file.", default='./eval_configs/mmgpt4_eval.yaml')
parser.add_argument("--dummy", action="store_true", help="Debug Mode")
parser.add_argument("--gpu-id", type=int, default=0, help="specify the gpu to load the model.")
parser.add_argument(
"--options",
nargs="+",
help="override some settings in the used config, the key-value pair "
"in xxx=yyy format will be merged into config file (deprecate), "
"change to --cfg-options instead.",
)
parser.add_argument("--ground-all", action="store_true")
args = parser.parse_args()
return args
def setup_seeds(config):
seed = config.run_cfg.seed + get_rank()
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
cudnn.benchmark = False
cudnn.deterministic = True
# ========================================
# Model Initialization
# ========================================
print('Initializing Chat')
args = parse_args()
assert args.dummy or (args.cfg_path is not None), "Invalid Config! Set --dummy or configurate the cfg_path!"
device = 'cuda:{}'.format(args.gpu_id) if torch.cuda.is_available() else 'cpu'
if not args.dummy:
cfg = Config(args)
# Create processors
vis_processor_cfg = cfg.datasets_cfg.default.vis_processor.eval
aud_processor_cfg = cfg.datasets_cfg.default.audio_processor.eval
vis_processor = registry.get_processor_class(vis_processor_cfg.name).from_config(vis_processor_cfg)
aud_processor = registry.get_processor_class(aud_processor_cfg.name).from_config(aud_processor_cfg)
processors = {ModalityType.VISION: vis_processor, ModalityType.AUDIO: aud_processor}
# Create model
model_config = cfg.model_cfg
model_config.device_8bit = device
model_cls = registry.get_model_class(model_config.arch)
model = model_cls.from_config(model_config).to(device)
chat = Chat(model, processors, device=device)
else:
model = None
chat = DummyChat()
match = MatchModule(model='gpt-3.5-turbo')
tagging_module = TaggingModule(device=device)
grounding_dino = GroundingModule(device=device)
print('Initialization Finished')
# ========================================
# Gradio Setting
# ========================================
def gradio_reset(chat_state, emb_list):
if chat_state is not None:
chat_state.messages = []
if emb_list is not None:
emb_list = []
return None, gr.update(value=None, interactive=True), gr.update(value=None, interactive=False), \
gr.update(value=None, interactive=True), \
gr.update(placeholder='Please upload your image/audio first', interactive=False), \
gr.update(value=None), \
gr.update(value="Upload & Start Chat", interactive=True), \
chat_state, emb_list, gr.update(value={})
def upload_x(gr_img, gr_aud, chat_state):
if gr_img is None and gr_aud is None:
return None, None, None, gr.update(interactive=True), chat_state, None, {}
chat_state = CONV_X.copy()
emb_list = []
if gr_img is not None:
chat.upload_img(gr_img, chat_state, emb_list)
state = {
'tags': tagging_module(gr_img)
}
# print(state)
else:
state = {}
if gr_aud is not None:
chat.upload_aud(gr_aud, chat_state, emb_list)
return gr.update(interactive=False), gr.update(interactive=False), \
gr.update(interactive=True, placeholder='Type and press Enter'), \
gr.update(value="Start Chatting", interactive=False), \
chat_state, emb_list, state
def gradio_ask(user_message, chatbot, chat_state, text_output, last_answer):
if len(user_message) == 0:
return gr.update(interactive=True, placeholder='Input should not be empty!'), chatbot, chat_state, \
gr.update(value=None, color_map=None, show_legend=False), gr.update(value=None)
if last_answer is not None:
chatbot[-1][1] = last_answer
chat.ask(user_message, chat_state)
if text_output is not None:
os.makedirs('results', exist_ok=True)
# print("****** Text output is:", text_output)
chatbot[-1][1] = ''.join(map(lambda x: x[0], text_output))
chatbot = chatbot + [[user_message, None]]
return '', chatbot, chat_state, gr.update(value=None, color_map=None, show_legend=False), gr.update(value=None)
def gradio_answer(image, chatbot, chat_state, emb_list, num_beams, temperature, entity_state):
llm_message = chat.answer(conversation=chat_state,
emb_list=emb_list,
num_beams=num_beams,
temperature=temperature,
max_new_tokens=300,
max_length=2000)[0]
if image is not None:
# new_entity_state = entity_state.value()
# new_entity_state.update({"answer": llm_message})
entity_state["answer"] = llm_message
rich_text, match_state, color_map = match(llm_message, entity_state)
print("Original Color Map: ", color_map)
color_map = {key: LIGHTER_COLOR_MAP_HEX[color_map[key]] for key in color_map}
print("Modified Color Map: ", color_map)
chatbot[-1][1] = "The answer can be found in the textbox below and I'm trying my best to highlight the " \
"corresponding region on the image."
# new_entity_state.update({"match_state": match_state})
entity_state['match_state'] = match_state # item_id -> local_id
new_grounded_image = grounding_dino.draw(image, entity_state)
show_legend = bool(match_state)
print('gradio_answer ==> current state: ', entity_state)
# if args.ground_all:
# ground_img, local_results = grounding_dino.prompt2mask(image,
# '.'.join(map(lambda x: x, state['entity'])),
# state=state)
# else:
# ground_img = None
return chatbot, chat_state, emb_list, \
gr.update(value=rich_text, color_map=color_map, show_legend=show_legend), \
gr.update(value=entity_state), \
gr.update(value=llm_message), gr.update(value=new_grounded_image)
else:
chatbot[-1][1] = llm_message
return chatbot, chat_state, emb_list, \
gr.update(value=None), \
entity_state, \
gr.update(value=None), gr.update(value=None)
def grounding_fn(image, chatbot, entity_state):
# print("Grounding fn: ", entity_state)
if image and entity_state:
ground_img, local_results = grounding_dino.prompt2mask2(
image, ','.join(map(lambda x: x, entity_state['tags'])), state=entity_state
)
entity_state['grounding'] = {
'full': ground_img,
'local': local_results
}
print('grounding_fn ==> current state: ', entity_state)
return chatbot, gr.update(value=ground_img, interactive=False), entity_state
return chatbot, gr.update(value=None, interactive=False), entity_state
def select_fn(image, ground_img, entity_state, evt: gr.SelectData):
if image is None:
return gr.update(value=None, interactive=False)
item, label = evt.value[0], evt.value[1]
if label is None:
return ground_img
print('select_fn ==> current state: ', entity_state)
if 'grounding' not in entity_state:
ground_img, local_results = grounding_dino.prompt2mask2(image,
','.join(map(lambda x: x[0], entity_state['tags'])),
state=entity_state)
entity_state['grounding'] = {
'full': ground_img,
'local': local_results
}
# local_img = entity_state['grounding']['local'][entity]['image']
# print("DEBUG INFO: ", entity_state)
local_img = grounding_dino.draw(image, entity_state, item.lower())
return gr.update(value=local_img, interactive=False)
title = """<h1 align="center">Demo of BuboGPT</h1>"""
description = """<h3>This is the demo of BuboGPT. Upload and start chatting!</h3>"""
# article = """<p><a href='https://minigpt-4.github.io'><img src='https://img.shields.io/badge/Project-Page-Green'></a></p><p><a href='https://github.com/Vision-CAIR/MiniGPT-4'><img src='https://img.shields.io/badge/Github-Code-blue'></a></p><p><a href='https://raw.githubusercontent.com/Vision-CAIR/MiniGPT-4/main/MiniGPT_4.pdf'><img src='https://img.shields.io/badge/Paper-PDF-red'></a></p>
# """
# TODO show examples below
with gr.Blocks() as demo:
gr.Markdown(title)
gr.Markdown(description)
# gr.Markdown(article)
with gr.Row():
with gr.Column(scale=0.5):
image = gr.Image(type="pil")
grounded_image = gr.Image(type="pil", interactive=False)
audio = gr.Audio()
upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
clear = gr.Button("Restart")
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",
)
with gr.Column():
chat_state = gr.State()
last_answer = gr.State()
entity_state = gr.State(value={})
emb_list = gr.State()
chatbot = gr.Chatbot(label='BuboGPT')
text_output = gr.HighlightedText(value=None, label="Response", show_legend=False)
text_input = gr.Textbox(label='User', placeholder='Please upload your image/audio first', interactive=False)
upload_button.click(
upload_x, [image, audio, chat_state],
[image, audio, text_input, upload_button, chat_state, emb_list, entity_state]).then(
grounding_fn,
[image, chatbot, entity_state],
[chatbot, grounded_image, entity_state]
)
text_input.submit(gradio_ask,
[text_input, chatbot, chat_state, text_output, last_answer],
[text_input, chatbot, chat_state, text_output, last_answer]
).then(
gradio_answer,
[image, chatbot, chat_state, emb_list, num_beams, temperature, entity_state],
[chatbot, chat_state, emb_list, text_output, entity_state, last_answer, grounded_image]
)
clear.click(gradio_reset,
[chat_state, emb_list],
[chatbot, image, grounded_image, audio, text_input, text_output,
upload_button, chat_state, emb_list, entity_state],
queue=False)
text_output.select(
select_fn,
[image, grounded_image, entity_state],
[grounded_image]
)
demo.launch(enable_queue=True)
|