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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Alibaba Cloud.
|
2 |
+
#
|
3 |
+
# This source code is licensed under the license found in the
|
4 |
+
# LICENSE file in the root directory of this source tree.
|
5 |
+
|
6 |
+
"""Tokenization classes for QWen."""
|
7 |
+
|
8 |
+
import base64
|
9 |
+
import logging
|
10 |
+
import os
|
11 |
+
import requests
|
12 |
+
import unicodedata
|
13 |
+
from typing import Collection, Dict, List, Set, Tuple, Union, Any, Callable, Optional
|
14 |
+
|
15 |
+
import tiktoken
|
16 |
+
import numpy as np
|
17 |
+
from PIL import Image
|
18 |
+
from PIL import ImageFont
|
19 |
+
from PIL import ImageDraw
|
20 |
+
from transformers import PreTrainedTokenizer, AddedToken
|
21 |
+
from transformers.utils import try_to_load_from_cache
|
22 |
+
|
23 |
+
import matplotlib.colors as mcolors
|
24 |
+
from matplotlib.font_manager import FontProperties
|
25 |
+
|
26 |
+
logger = logging.getLogger(__name__)
|
27 |
+
|
28 |
+
|
29 |
+
VOCAB_FILES_NAMES = {"vocab_file": "qwen.tiktoken", "ttf": "SimSun.ttf"}
|
30 |
+
FONT_PATH = try_to_load_from_cache("Qwen/Qwen-VL-Chat", "SimSun.ttf")
|
31 |
+
if FONT_PATH is None:
|
32 |
+
if not os.path.exists("SimSun.ttf"):
|
33 |
+
ttf = requests.get("https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/SimSun.ttf")
|
34 |
+
open("SimSun.ttf", "wb").write(ttf.content)
|
35 |
+
FONT_PATH = "SimSun.ttf"
|
36 |
+
|
37 |
+
PAT_STR = r"""(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"""
|
38 |
+
ENDOFTEXT = "<|endoftext|>"
|
39 |
+
IMSTART = "<|im_start|>"
|
40 |
+
IMEND = "<|im_end|>"
|
41 |
+
# as the default behavior is changed to allow special tokens in
|
42 |
+
# regular texts, the surface forms of special tokens need to be
|
43 |
+
# as different as possible to minimize the impact
|
44 |
+
EXTRAS = tuple((f"<|extra_{i}|>" for i in range(205)))
|
45 |
+
SPECIAL_TOKENS = (
|
46 |
+
ENDOFTEXT,
|
47 |
+
IMSTART,
|
48 |
+
IMEND,
|
49 |
+
) + EXTRAS
|
50 |
+
IMG_TOKEN_SPAN = 256
|
51 |
+
|
52 |
+
|
53 |
+
def _load_tiktoken_bpe(tiktoken_bpe_file: str) -> Dict[bytes, int]:
|
54 |
+
with open(tiktoken_bpe_file, "rb") as f:
|
55 |
+
contents = f.read()
|
56 |
+
return {
|
57 |
+
base64.b64decode(token): int(rank)
|
58 |
+
for token, rank in (line.split() for line in contents.splitlines() if line)
|
59 |
+
}
|
60 |
+
|
61 |
+
def _list_find(
|
62 |
+
input_list: List[Any],
|
63 |
+
candidates: Tuple[Any],
|
64 |
+
start: int = 0,
|
65 |
+
):
|
66 |
+
for i in range(start, len(input_list)):
|
67 |
+
if input_list[i] in candidates:
|
68 |
+
return i
|
69 |
+
return -1
|
70 |
+
|
71 |
+
def _replace_closed_tag(
|
72 |
+
input_tokens: List[Any],
|
73 |
+
start_tags: Union[Any, Tuple[Any]],
|
74 |
+
end_tags: Union[Any, Tuple[Any]],
|
75 |
+
inclusive_replace_func: Callable,
|
76 |
+
exclusive_replace_func: Callable = lambda x: x,
|
77 |
+
):
|
78 |
+
if isinstance(start_tags, (str, int)):
|
79 |
+
start_tags = (start_tags,)
|
80 |
+
if isinstance(end_tags, (str, int)):
|
81 |
+
end_tags = (end_tags,)
|
82 |
+
assert len(start_tags) == len(end_tags)
|
83 |
+
|
84 |
+
output_tokens = []
|
85 |
+
end = 0
|
86 |
+
while True:
|
87 |
+
start = _list_find(input_tokens, start_tags, end)
|
88 |
+
if start == -1:
|
89 |
+
break
|
90 |
+
output_tokens.extend(exclusive_replace_func(input_tokens[end : start]))
|
91 |
+
tag_idx = start_tags.index(input_tokens[start])
|
92 |
+
end = _list_find(input_tokens, (end_tags[tag_idx],), start)
|
93 |
+
if end == -1:
|
94 |
+
raise ValueError("Unclosed image token")
|
95 |
+
output_tokens.extend(inclusive_replace_func(input_tokens[start : end + 1]))
|
96 |
+
end += 1
|
97 |
+
output_tokens.extend(exclusive_replace_func(input_tokens[end : ]))
|
98 |
+
return output_tokens
|
99 |
+
|
100 |
+
class QWenTokenizer(PreTrainedTokenizer):
|
101 |
+
"""QWen tokenizer."""
|
102 |
+
|
103 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
104 |
+
|
105 |
+
def __init__(
|
106 |
+
self,
|
107 |
+
vocab_file,
|
108 |
+
errors="replace",
|
109 |
+
image_start_tag='<img>',
|
110 |
+
image_end_tag='</img>',
|
111 |
+
image_pad_tag='<imgpad>',
|
112 |
+
ref_start_tag='<ref>',
|
113 |
+
ref_end_tag='</ref>',
|
114 |
+
box_start_tag='<box>',
|
115 |
+
box_end_tag='</box>',
|
116 |
+
quad_start_tag='<quad>',
|
117 |
+
quad_end_tag='</quad>',
|
118 |
+
**kwargs,
|
119 |
+
):
|
120 |
+
super().__init__(**kwargs)
|
121 |
+
self.image_start_tag = image_start_tag
|
122 |
+
self.image_end_tag = image_end_tag
|
123 |
+
self.image_pad_tag = image_pad_tag
|
124 |
+
self.ref_start_tag = ref_start_tag
|
125 |
+
self.ref_end_tag = ref_end_tag
|
126 |
+
self.box_start_tag = box_start_tag
|
127 |
+
self.box_end_tag = box_end_tag
|
128 |
+
self.quad_start_tag = quad_start_tag
|
129 |
+
self.quad_end_tag = quad_end_tag
|
130 |
+
self.IMAGE_ST = (
|
131 |
+
ref_start_tag, ref_end_tag,
|
132 |
+
box_start_tag, box_end_tag,
|
133 |
+
quad_start_tag, quad_end_tag,
|
134 |
+
image_start_tag, image_end_tag,
|
135 |
+
image_pad_tag
|
136 |
+
)
|
137 |
+
|
138 |
+
self.errors = errors # how to handle errors in decoding
|
139 |
+
|
140 |
+
self.mergeable_ranks = _load_tiktoken_bpe(vocab_file) # type: dict[bytes, int]
|
141 |
+
self.special_tokens = {
|
142 |
+
token: index
|
143 |
+
for index, token in enumerate(
|
144 |
+
SPECIAL_TOKENS + self.IMAGE_ST, start=len(self.mergeable_ranks)
|
145 |
+
)
|
146 |
+
}
|
147 |
+
self.img_start_id = self.special_tokens[self.image_start_tag]
|
148 |
+
self.img_end_id = self.special_tokens[self.image_end_tag]
|
149 |
+
self.img_pad_id = self.special_tokens[self.image_pad_tag]
|
150 |
+
self.ref_start_id = self.special_tokens[self.ref_start_tag]
|
151 |
+
self.ref_end_id = self.special_tokens[self.ref_end_tag]
|
152 |
+
self.box_start_id = self.special_tokens[self.box_start_tag]
|
153 |
+
self.box_end_id = self.special_tokens[self.box_end_tag]
|
154 |
+
self.quad_start_id = self.special_tokens[self.quad_start_tag]
|
155 |
+
self.quad_end_id = self.special_tokens[self.quad_end_tag]
|
156 |
+
self.image_special_tokens = set([
|
157 |
+
self.ref_start_id, self.ref_end_id, self.box_start_id, self.box_end_id,
|
158 |
+
self.quad_start_id, self.quad_end_id,
|
159 |
+
])
|
160 |
+
|
161 |
+
enc = tiktoken.Encoding(
|
162 |
+
"Qwen",
|
163 |
+
pat_str=PAT_STR,
|
164 |
+
mergeable_ranks=self.mergeable_ranks,
|
165 |
+
special_tokens=self.special_tokens,
|
166 |
+
)
|
167 |
+
assert (
|
168 |
+
len(self.mergeable_ranks) + len(self.special_tokens) == enc.n_vocab
|
169 |
+
), f"{len(self.mergeable_ranks) + len(self.special_tokens)} != {enc.n_vocab} in encoding"
|
170 |
+
|
171 |
+
self.decoder = {
|
172 |
+
v: k for k, v in self.mergeable_ranks.items()
|
173 |
+
} # type: dict[int, bytes|str]
|
174 |
+
self.decoder.update({v: k for k, v in self.special_tokens.items()})
|
175 |
+
|
176 |
+
self.tokenizer = enc # type: tiktoken.Encoding
|
177 |
+
|
178 |
+
self.eod_id = self.tokenizer.eot_token
|
179 |
+
self.im_start_id = self.special_tokens[IMSTART]
|
180 |
+
self.im_end_id = self.special_tokens[IMEND]
|
181 |
+
|
182 |
+
def __getstate__(self):
|
183 |
+
# for pickle lovers
|
184 |
+
state = self.__dict__.copy()
|
185 |
+
del state['tokenizer']
|
186 |
+
return state
|
187 |
+
|
188 |
+
def __setstate__(self, state):
|
189 |
+
# tokenizer is not python native; don't pass it; rebuild it
|
190 |
+
self.__dict__.update(state)
|
191 |
+
enc = tiktoken.Encoding(
|
192 |
+
"Qwen",
|
193 |
+
pat_str=PAT_STR,
|
194 |
+
mergeable_ranks=self.mergeable_ranks,
|
195 |
+
special_tokens=self.special_tokens,
|
196 |
+
)
|
197 |
+
self.tokenizer = enc
|
198 |
+
|
199 |
+
|
200 |
+
def __len__(self) -> int:
|
201 |
+
return self.tokenizer.n_vocab
|
202 |
+
|
203 |
+
def get_vocab(self) -> Dict[bytes, int]:
|
204 |
+
return self.mergeable_ranks
|
205 |
+
|
206 |
+
def convert_tokens_to_ids(
|
207 |
+
self, tokens: Union[bytes, str, List[Union[bytes, str]]]
|
208 |
+
) -> List[int]:
|
209 |
+
ids = []
|
210 |
+
if isinstance(tokens, (str, bytes)):
|
211 |
+
if tokens in self.special_tokens:
|
212 |
+
return self.special_tokens[tokens]
|
213 |
+
else:
|
214 |
+
return self.mergeable_ranks.get(tokens)
|
215 |
+
for token in tokens:
|
216 |
+
if token in self.special_tokens:
|
217 |
+
ids.append(self.special_tokens[token])
|
218 |
+
else:
|
219 |
+
ids.append(self.mergeable_ranks.get(token))
|
220 |
+
return ids
|
221 |
+
|
222 |
+
def _add_tokens(self, new_tokens: Union[List[str], List[AddedToken]], special_tokens: bool = False) -> int:
|
223 |
+
if not special_tokens and new_tokens:
|
224 |
+
raise ValueError('Adding regular tokens is not supported')
|
225 |
+
for token in new_tokens:
|
226 |
+
surface_form = token.content if isinstance(token, AddedToken) else token
|
227 |
+
if surface_form not in SPECIAL_TOKENS + self.IMAGE_ST:
|
228 |
+
raise ValueError('Adding unknown special tokens is not supported')
|
229 |
+
return 0
|
230 |
+
|
231 |
+
def save_vocabulary(self, save_directory: str, **kwargs) -> Tuple[str]:
|
232 |
+
"""
|
233 |
+
Save only the vocabulary of the tokenizer (vocabulary).
|
234 |
+
|
235 |
+
Returns:
|
236 |
+
`Tuple(str)`: Paths to the files saved.
|
237 |
+
"""
|
238 |
+
file_path = os.path.join(save_directory, "qwen.tiktoken")
|
239 |
+
with open(file_path, "w", encoding="utf8") as w:
|
240 |
+
for k, v in self.mergeable_ranks.items():
|
241 |
+
line = base64.b64encode(k).decode("utf8") + " " + str(v) + "\n"
|
242 |
+
w.write(line)
|
243 |
+
return (file_path,)
|
244 |
+
|
245 |
+
def tokenize(
|
246 |
+
self,
|
247 |
+
text: str,
|
248 |
+
allowed_special: Union[Set, str] = "all",
|
249 |
+
disallowed_special: Union[Collection, str] = (),
|
250 |
+
**kwargs,
|
251 |
+
) -> List[Union[bytes, str]]:
|
252 |
+
"""
|
253 |
+
Converts a string in a sequence of tokens.
|
254 |
+
|
255 |
+
Args:
|
256 |
+
text (`str`):
|
257 |
+
The sequence to be encoded.
|
258 |
+
allowed_special (`Literal["all"]` or `set`):
|
259 |
+
The surface forms of the tokens to be encoded as special tokens in regular texts.
|
260 |
+
Default to "all".
|
261 |
+
disallowed_special (`Literal["all"]` or `Collection`):
|
262 |
+
The surface forms of the tokens that should not be in regular texts and trigger errors.
|
263 |
+
Default to an empty tuple.
|
264 |
+
|
265 |
+
kwargs (additional keyword arguments, *optional*):
|
266 |
+
Will be passed to the underlying model specific encode method.
|
267 |
+
|
268 |
+
Returns:
|
269 |
+
`List[bytes|str]`: The list of tokens.
|
270 |
+
"""
|
271 |
+
tokens = []
|
272 |
+
text = unicodedata.normalize("NFC", text)
|
273 |
+
|
274 |
+
# this implementation takes a detour: text -> token id -> token surface forms
|
275 |
+
for t in self.tokenizer.encode(
|
276 |
+
text, allowed_special=allowed_special, disallowed_special=disallowed_special
|
277 |
+
):
|
278 |
+
tokens.append(self.decoder[t])
|
279 |
+
|
280 |
+
def _encode_imgurl(img_tokens):
|
281 |
+
assert img_tokens[0] == self.image_start_tag and img_tokens[-1] == self.image_end_tag
|
282 |
+
img_tokens = img_tokens[1:-1]
|
283 |
+
img_url = b''.join(img_tokens)
|
284 |
+
out_img_tokens = list(map(self.decoder.get, img_url))
|
285 |
+
if len(out_img_tokens) > IMG_TOKEN_SPAN:
|
286 |
+
raise ValueError("The content in {}..{} is too long".format(
|
287 |
+
self.image_start_tag, self.image_end_tag))
|
288 |
+
out_img_tokens.extend([self.image_pad_tag] * (IMG_TOKEN_SPAN - len(out_img_tokens)))
|
289 |
+
out_img_tokens = [self.image_start_tag] + out_img_tokens + [self.image_end_tag]
|
290 |
+
return out_img_tokens
|
291 |
+
|
292 |
+
return _replace_closed_tag(tokens, self.image_start_tag, self.image_end_tag, _encode_imgurl)
|
293 |
+
|
294 |
+
def convert_tokens_to_string(self, tokens: List[Union[bytes, str]]) -> str:
|
295 |
+
"""
|
296 |
+
Converts a sequence of tokens in a single string.
|
297 |
+
"""
|
298 |
+
text = ""
|
299 |
+
temp = b""
|
300 |
+
for t in tokens:
|
301 |
+
if isinstance(t, str):
|
302 |
+
if temp:
|
303 |
+
text += temp.decode("utf-8", errors=self.errors)
|
304 |
+
temp = b""
|
305 |
+
text += t
|
306 |
+
elif isinstance(t, bytes):
|
307 |
+
temp += t
|
308 |
+
else:
|
309 |
+
raise TypeError("token should only be of type types or str")
|
310 |
+
if temp:
|
311 |
+
text += temp.decode("utf-8", errors=self.errors)
|
312 |
+
return text
|
313 |
+
|
314 |
+
@property
|
315 |
+
def vocab_size(self):
|
316 |
+
return self.tokenizer.n_vocab
|
317 |
+
|
318 |
+
def _convert_id_to_token(self, index: int) -> Union[bytes, str]:
|
319 |
+
"""Converts an id to a token, special tokens included"""
|
320 |
+
if index in self.decoder:
|
321 |
+
return self.decoder[index]
|
322 |
+
raise ValueError("unknown ids")
|
323 |
+
|
324 |
+
def _convert_token_to_id(self, token: Union[bytes, str]) -> int:
|
325 |
+
"""Converts a token to an id using the vocab, special tokens included"""
|
326 |
+
if token in self.special_tokens:
|
327 |
+
return self.special_tokens[token]
|
328 |
+
if token in self.mergeable_ranks:
|
329 |
+
return self.mergeable_ranks[token]
|
330 |
+
raise ValueError("unknown token")
|
331 |
+
|
332 |
+
def _tokenize(self, text: str, **kwargs):
|
333 |
+
"""
|
334 |
+
Converts a string in a sequence of tokens (string), using the tokenizer. Split in words for word-based
|
335 |
+
vocabulary or sub-words for sub-word-based vocabularies (BPE/SentencePieces/WordPieces).
|
336 |
+
|
337 |
+
Do NOT take care of added tokens.
|
338 |
+
"""
|
339 |
+
raise NotImplementedError
|
340 |
+
|
341 |
+
def _decode(
|
342 |
+
self,
|
343 |
+
token_ids: Union[int, List[int]],
|
344 |
+
skip_special_tokens: bool = False,
|
345 |
+
errors: str = None,
|
346 |
+
**kwargs,
|
347 |
+
) -> str:
|
348 |
+
if isinstance(token_ids, int):
|
349 |
+
token_ids = [token_ids]
|
350 |
+
|
351 |
+
def _decode_imgurl(img_token_ids):
|
352 |
+
assert img_token_ids[0] == self.img_start_id and img_token_ids[-1] == self.img_end_id
|
353 |
+
img_token_ids = img_token_ids[1:-1]
|
354 |
+
img_token_ids = img_token_ids[ : img_token_ids.index(self.img_pad_id)]
|
355 |
+
img_url = bytes(img_token_ids).decode('utf-8')
|
356 |
+
return [self.img_start_id] + self.tokenizer.encode(img_url) + [self.img_end_id]
|
357 |
+
|
358 |
+
token_ids = _replace_closed_tag(token_ids, self.img_start_id, self.img_end_id, _decode_imgurl)
|
359 |
+
|
360 |
+
if skip_special_tokens:
|
361 |
+
if kwargs.get('keep_image_special', False):
|
362 |
+
token_ids = [i for i in token_ids if i < self.eod_id
|
363 |
+
or i in self.image_special_tokens]
|
364 |
+
else:
|
365 |
+
token_ids = [i for i in token_ids if i < self.eod_id]
|
366 |
+
return self.tokenizer.decode(token_ids, errors=errors or self.errors)
|
367 |
+
|
368 |
+
def to_list_format(self, text: str):
|
369 |
+
text = unicodedata.normalize("NFC", text)
|
370 |
+
token_ids = self.tokenizer.encode(
|
371 |
+
text, allowed_special=set(self.IMAGE_ST + (ENDOFTEXT,)))
|
372 |
+
|
373 |
+
def _encode_vl_info(tokens):
|
374 |
+
if len(tokens) == 0:
|
375 |
+
return []
|
376 |
+
if tokens[0] == self.img_start_id and tokens[-1] == self.img_end_id:
|
377 |
+
key = 'image'
|
378 |
+
elif tokens[0] == self.ref_start_id and tokens[-1] == self.ref_end_id:
|
379 |
+
key = 'ref'
|
380 |
+
elif tokens[0] == self.box_start_id and tokens[-1] == self.box_end_id:
|
381 |
+
key = 'box'
|
382 |
+
elif tokens[0] == self.quad_start_id and tokens[-1] == self.quad_end_id:
|
383 |
+
key = 'quad'
|
384 |
+
else:
|
385 |
+
_tobytes = lambda x: x.encode('utf-8') if isinstance(x, str) else x
|
386 |
+
return [{'text': b''.join(map(_tobytes, map(self.decoder.get, tokens))).decode('utf-8')}]
|
387 |
+
_tobytes = lambda x: x.encode('utf-8') if isinstance(x, str) else x
|
388 |
+
val = b''.join(map(_tobytes, map(self.decoder.get, tokens[1:-1]))).decode('utf-8')
|
389 |
+
return [{key: val}]
|
390 |
+
|
391 |
+
return _replace_closed_tag(
|
392 |
+
token_ids,
|
393 |
+
(self.img_start_id, self.ref_start_id, self.box_start_id, self.quad_start_id),
|
394 |
+
(self.img_end_id, self.ref_end_id, self.box_end_id, self.quad_end_id),
|
395 |
+
_encode_vl_info,
|
396 |
+
_encode_vl_info,
|
397 |
+
)
|
398 |
+
|
399 |
+
def from_list_format(self, list_format: List[Dict], base_prompt="你是一个搞笑专家,非常了解网络和现实生活当中的各种梗和流行语,热衷于用幽默感给大家带来欢乐。\n"):
|
400 |
+
text = ''
|
401 |
+
num_images = 0
|
402 |
+
for ele in list_format:
|
403 |
+
if 'image' in ele:
|
404 |
+
num_images += 1
|
405 |
+
text += f'Picture {num_images}: '
|
406 |
+
text += self.image_start_tag + ele['image'] + self.image_end_tag
|
407 |
+
text += '\n'
|
408 |
+
elif 'text' in ele:
|
409 |
+
text += ele['text']
|
410 |
+
elif 'box' in ele:
|
411 |
+
if 'ref' in ele:
|
412 |
+
text += self.ref_start_tag + ele['ref'] + self.ref_end_tag
|
413 |
+
for box in ele['box']:
|
414 |
+
text += self.box_start_tag + '(%d,%d),(%d,%d)' % (box[0], box[1], box[2], box[3]) + self.box_end_tag
|
415 |
+
else:
|
416 |
+
raise ValueError("Unsupport element: " + str(ele))
|
417 |
+
return base_prompt + text
|
418 |
+
|
419 |
+
def _fetch_latest_picture(self, response, history):
|
420 |
+
if history is None:
|
421 |
+
history = []
|
422 |
+
_history = history + [(response, None)]
|
423 |
+
for q, r in _history[::-1]:
|
424 |
+
for ele in self.to_list_format(q)[::-1]:
|
425 |
+
if 'image' in ele:
|
426 |
+
return ele['image']
|
427 |
+
return None
|
428 |
+
|
429 |
+
def _fetch_all_box_with_ref(self, text):
|
430 |
+
list_format = self.to_list_format(text)
|
431 |
+
output = []
|
432 |
+
for i, ele in enumerate(list_format):
|
433 |
+
if 'box' in ele:
|
434 |
+
bbox = tuple(map(int, ele['box'].replace('(', '').replace(')', '').split(',')))
|
435 |
+
assert len(bbox) == 4
|
436 |
+
output.append({'box': bbox})
|
437 |
+
if i > 0 and 'ref' in list_format[i-1]:
|
438 |
+
output[-1]['ref'] = list_format[i-1]['ref'].strip()
|
439 |
+
return output
|
440 |
+
|
441 |
+
def draw_bbox_on_latest_picture(
|
442 |
+
self,
|
443 |
+
response,
|
444 |
+
history=None,
|
445 |
+
) -> Optional[Image.Image]:
|
446 |
+
image = self._fetch_latest_picture(response, history)
|
447 |
+
if image is None:
|
448 |
+
return None
|
449 |
+
if image.startswith("http://") or image.startswith("https://"):
|
450 |
+
image = Image.open(requests.get(image, stream=True).raw).convert("RGB")
|
451 |
+
h, w = image.height, image.width
|
452 |
+
else:
|
453 |
+
image = np.asarray(Image.open(image).convert("RGB"))
|
454 |
+
h, w = image.shape[0], image.shape[1]
|
455 |
+
visualizer = Visualizer(image)
|
456 |
+
|
457 |
+
boxes = self._fetch_all_box_with_ref(response)
|
458 |
+
if not boxes:
|
459 |
+
return None
|
460 |
+
color = random.choice([_ for _ in mcolors.TABLEAU_COLORS.keys()]) # init color
|
461 |
+
for box in boxes:
|
462 |
+
if 'ref' in box: # random new color for new refexps
|
463 |
+
color = random.choice([_ for _ in mcolors.TABLEAU_COLORS.keys()])
|
464 |
+
x1, y1, x2, y2 = box['box']
|
465 |
+
x1, y1, x2, y2 = (int(x1 / 1000 * w), int(y1 / 1000 * h), int(x2 / 1000 * w), int(y2 / 1000 * h))
|
466 |
+
visualizer.draw_box((x1, y1, x2, y2), alpha=1, edge_color=color)
|
467 |
+
if 'ref' in box:
|
468 |
+
visualizer.draw_text(box['ref'], (x1, y1), color=color, horizontal_alignment="left")
|
469 |
+
return visualizer.output
|
470 |
+
|
471 |
+
|
472 |
+
import colorsys
|
473 |
+
import logging
|
474 |
+
import math
|
475 |
+
import numpy as np
|
476 |
+
import matplotlib as mpl
|
477 |
+
import matplotlib.colors as mplc
|
478 |
+
import matplotlib.figure as mplfigure
|
479 |
+
import torch
|
480 |
+
from matplotlib.backends.backend_agg import FigureCanvasAgg
|
481 |
+
from PIL import Image
|
482 |
+
import random
|
483 |
+
|
484 |
+
logger = logging.getLogger(__name__)
|
485 |
+
|
486 |
+
|
487 |
+
class VisImage:
|
488 |
+
def __init__(self, img, scale=1.0):
|
489 |
+
self.img = img
|
490 |
+
self.scale = scale
|
491 |
+
self.width, self.height = img.shape[1], img.shape[0]
|
492 |
+
self._setup_figure(img)
|
493 |
+
|
494 |
+
def _setup_figure(self, img):
|
495 |
+
fig = mplfigure.Figure(frameon=False)
|
496 |
+
self.dpi = fig.get_dpi()
|
497 |
+
# add a small 1e-2 to avoid precision lost due to matplotlib's truncation
|
498 |
+
# (https://github.com/matplotlib/matplotlib/issues/15363)
|
499 |
+
fig.set_size_inches(
|
500 |
+
(self.width * self.scale + 1e-2) / self.dpi,
|
501 |
+
(self.height * self.scale + 1e-2) / self.dpi,
|
502 |
+
)
|
503 |
+
self.canvas = FigureCanvasAgg(fig)
|
504 |
+
# self.canvas = mpl.backends.backend_cairo.FigureCanvasCairo(fig)
|
505 |
+
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0])
|
506 |
+
ax.axis("off")
|
507 |
+
self.fig = fig
|
508 |
+
self.ax = ax
|
509 |
+
self.reset_image(img)
|
510 |
+
|
511 |
+
def reset_image(self, img):
|
512 |
+
img = img.astype("uint8")
|
513 |
+
self.ax.imshow(img, extent=(0, self.width, self.height, 0), interpolation="nearest")
|
514 |
+
|
515 |
+
def save(self, filepath):
|
516 |
+
self.fig.savefig(filepath)
|
517 |
+
|
518 |
+
def get_image(self):
|
519 |
+
canvas = self.canvas
|
520 |
+
s, (width, height) = canvas.print_to_buffer()
|
521 |
+
|
522 |
+
buffer = np.frombuffer(s, dtype="uint8")
|
523 |
+
|
524 |
+
img_rgba = buffer.reshape(height, width, 4)
|
525 |
+
rgb, alpha = np.split(img_rgba, [3], axis=2)
|
526 |
+
return rgb.astype("uint8")
|
527 |
+
|
528 |
+
|
529 |
+
class Visualizer:
|
530 |
+
def __init__(self, img_rgb, metadata=None, scale=1.0):
|
531 |
+
self.img = np.asarray(img_rgb).clip(0, 255).astype(np.uint8)
|
532 |
+
self.font_path = FONT_PATH
|
533 |
+
self.output = VisImage(self.img, scale=scale)
|
534 |
+
self.cpu_device = torch.device("cpu")
|
535 |
+
|
536 |
+
# too small texts are useless, therefore clamp to 14
|
537 |
+
self._default_font_size = max(
|
538 |
+
np.sqrt(self.output.height * self.output.width) // 30, 15 // scale
|
539 |
+
)
|
540 |
+
|
541 |
+
def draw_text(
|
542 |
+
self,
|
543 |
+
text,
|
544 |
+
position,
|
545 |
+
*,
|
546 |
+
font_size=None,
|
547 |
+
color="g",
|
548 |
+
horizontal_alignment="center",
|
549 |
+
rotation=0,
|
550 |
+
):
|
551 |
+
if not font_size:
|
552 |
+
font_size = self._default_font_size
|
553 |
+
|
554 |
+
# since the text background is dark, we don't want the text to be dark
|
555 |
+
color = np.maximum(list(mplc.to_rgb(color)), 0.2)
|
556 |
+
color[np.argmax(color)] = max(0.8, np.max(color))
|
557 |
+
|
558 |
+
x, y = position
|
559 |
+
self.output.ax.text(
|
560 |
+
x,
|
561 |
+
y,
|
562 |
+
text,
|
563 |
+
size=font_size * self.output.scale,
|
564 |
+
fontproperties=FontProperties(fname=self.font_path),
|
565 |
+
bbox={"facecolor": "black", "alpha": 0.8, "pad": 0.7, "edgecolor": "none"},
|
566 |
+
verticalalignment="top",
|
567 |
+
horizontalalignment=horizontal_alignment,
|
568 |
+
color=color,
|
569 |
+
zorder=10,
|
570 |
+
rotation=rotation,
|
571 |
+
)
|
572 |
+
return self.output
|
573 |
+
|
574 |
+
def draw_box(self, box_coord, alpha=0.5, edge_color="g", line_style="-"):
|
575 |
+
|
576 |
+
x0, y0, x1, y1 = box_coord
|
577 |
+
width = x1 - x0
|
578 |
+
height = y1 - y0
|
579 |
+
|
580 |
+
linewidth = max(self._default_font_size / 4, 1)
|
581 |
+
|
582 |
+
self.output.ax.add_patch(
|
583 |
+
mpl.patches.Rectangle(
|
584 |
+
(x0, y0),
|
585 |
+
width,
|
586 |
+
height,
|
587 |
+
fill=False,
|
588 |
+
edgecolor=edge_color,
|
589 |
+
linewidth=linewidth * self.output.scale,
|
590 |
+
alpha=alpha,
|
591 |
+
linestyle=line_style,
|
592 |
+
)
|
593 |
+
)
|
594 |
+
return self.output
|
595 |
+
|
596 |
+
def get_output(self):
|
597 |
+
|
598 |
+
return self.output
|
tokenizer_config.json
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
"additional_special_tokens": [],
|
4 |
"auto_map": {
|
5 |
"AutoTokenizer": [
|
6 |
-
"
|
7 |
null
|
8 |
]
|
9 |
},
|
|
|
3 |
"additional_special_tokens": [],
|
4 |
"auto_map": {
|
5 |
"AutoTokenizer": [
|
6 |
+
"tokenization_qwen.QWenTokenizer",
|
7 |
null
|
8 |
]
|
9 |
},
|