





import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import (
					    AutoProcessor,
					    BertTokenizerFast,
					    BlipImageProcessor,
					    GPTaTokenizer,
					    InstructBlipProcessor,
					    PreTrainedTokenizerFast,
					)

@require_vision
class 							__SCREAMING_SNAKE_CASE       (  unittest.TestCase							):






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      tempfile.mkdtemp()

														UpperCamelCase__					  =      BlipImageProcessor()
														UpperCamelCase__					  =      GPTaTokenizer.from_pretrained("""hf-internal-testing/tiny-random-GPT2Model"""	)
														UpperCamelCase__					  =      BertTokenizerFast.from_pretrained("""hf-internal-testing/tiny-random-bert"""	)

														UpperCamelCase__					  =      InstructBlipProcessor(__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	)

														processor.save_pretrained(self.tmpdirname	)






							def 					_lowerCamelCase		(    self	,			**__lowerCAmelCase	):
														return AutoProcessor.from_pretrained(self.tmpdirname	,			**__lowerCAmelCase	).tokenizer






							def 					_lowerCamelCase		(    self	,			**__lowerCAmelCase	):
														return AutoProcessor.from_pretrained(self.tmpdirname	,			**__lowerCAmelCase	).image_processor






							def 					_lowerCamelCase		(    self	,			**__lowerCAmelCase	):
														return AutoProcessor.from_pretrained(self.tmpdirname	,			**__lowerCAmelCase	).qformer_tokenizer






							def 					_lowerCamelCase		(    self	):
														shutil.rmtree(self.tmpdirname	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      [np.random.randint(255	,			size=(3, 30, 400)	,			dtype=np.uinta	)]

														UpperCamelCase__					  =      [Image.fromarray(np.moveaxis(__lowerCAmelCase	,			0	,			-1	)	) for x in image_inputs]

														return image_inputs






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      InstructBlipProcessor(
														    tokenizer=self.get_tokenizer()	,			image_processor=self.get_image_processor()	,			qformer_tokenizer=self.get_qformer_tokenizer()	,			)
														processor.save_pretrained(self.tmpdirname	)

														UpperCamelCase__					  =      self.get_tokenizer(bos_token="""(BOS)"""	,			eos_token="""(EOS)"""	)
														UpperCamelCase__					  =      self.get_image_processor(do_normalize=__lowerCAmelCase	,			padding_value=1.0	)

														UpperCamelCase__					  =      InstructBlipProcessor.from_pretrained(
														    self.tmpdirname	,			bos_token="""(BOS)"""	,			eos_token="""(EOS)"""	,			do_normalize=__lowerCAmelCase	,			padding_value=1.0	)

														self.assertEqual(processor.tokenizer.get_vocab()	,			tokenizer_add_kwargs.get_vocab()	)
														self.assertIsInstance(processor.tokenizer	,			__lowerCAmelCase	)

														self.assertEqual(processor.image_processor.to_json_string()	,			image_processor_add_kwargs.to_json_string()	)
														self.assertIsInstance(processor.image_processor	,			__lowerCAmelCase	)
														self.assertIsInstance(processor.qformer_tokenizer	,			__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.get_image_processor()
														UpperCamelCase__					  =      self.get_tokenizer()
														UpperCamelCase__					  =      self.get_qformer_tokenizer()

														UpperCamelCase__					  =      InstructBlipProcessor(
														    tokenizer=__lowerCAmelCase	,			image_processor=__lowerCAmelCase	,			qformer_tokenizer=__lowerCAmelCase	)

														UpperCamelCase__					  =      self.prepare_image_inputs()

														UpperCamelCase__					  =      image_processor(__lowerCAmelCase	,			return_tensors="""np"""	)
														UpperCamelCase__					  =      processor(images=__lowerCAmelCase	,			return_tensors="""np"""	)

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()	,			input_processor[key].sum()	,			delta=1E-2	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.get_image_processor()
														UpperCamelCase__					  =      self.get_tokenizer()
														UpperCamelCase__					  =      self.get_qformer_tokenizer()

														UpperCamelCase__					  =      InstructBlipProcessor(
														    tokenizer=__lowerCAmelCase	,			image_processor=__lowerCAmelCase	,			qformer_tokenizer=__lowerCAmelCase	)

														UpperCamelCase__					  =      """lower newer"""

														UpperCamelCase__					  =      processor(text=__lowerCAmelCase	)

														UpperCamelCase__					  =      tokenizer(__lowerCAmelCase	,			return_token_type_ids=__lowerCAmelCase	)
														UpperCamelCase__					  =      qformer_tokenizer(__lowerCAmelCase	,			return_token_type_ids=__lowerCAmelCase	)

														for key in encoded_tokens.keys():
																					self.assertListEqual(encoded_tokens[key]	,			encoded_processor[key]	)

														for key in encoded_tokens_qformer.keys():
																					self.assertListEqual(encoded_tokens_qformer[key]	,			encoded_processor["""qformer_""" + key]	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.get_image_processor()
														UpperCamelCase__					  =      self.get_tokenizer()
														UpperCamelCase__					  =      self.get_qformer_tokenizer()

														UpperCamelCase__					  =      InstructBlipProcessor(
														    tokenizer=__lowerCAmelCase	,			image_processor=__lowerCAmelCase	,			qformer_tokenizer=__lowerCAmelCase	)

														UpperCamelCase__					  =      """lower newer"""
														UpperCamelCase__					  =      self.prepare_image_inputs()

														UpperCamelCase__					  =      processor(text=__lowerCAmelCase	,			images=__lowerCAmelCase	)

														self.assertListEqual(
														    list(inputs.keys()	)	,			["""input_ids""", """attention_mask""", """qformer_input_ids""", """qformer_attention_mask""", """pixel_values"""]	,			)

														# test if it raises when no input is passed
														with pytest.raises(__lowerCAmelCase	):
																					processor()






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.get_image_processor()
														UpperCamelCase__					  =      self.get_tokenizer()
														UpperCamelCase__					  =      self.get_qformer_tokenizer()

														UpperCamelCase__					  =      InstructBlipProcessor(
														    tokenizer=__lowerCAmelCase	,			image_processor=__lowerCAmelCase	,			qformer_tokenizer=__lowerCAmelCase	)

														UpperCamelCase__					  =      [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

														UpperCamelCase__					  =      processor.batch_decode(__lowerCAmelCase	)
														UpperCamelCase__					  =      tokenizer.batch_decode(__lowerCAmelCase	)

														self.assertListEqual(__lowerCAmelCase	,			__lowerCAmelCase	)







							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.get_image_processor()
														UpperCamelCase__					  =      self.get_tokenizer()
														UpperCamelCase__					  =      self.get_qformer_tokenizer()

														UpperCamelCase__					  =      InstructBlipProcessor(
														    tokenizer=__lowerCAmelCase	,			image_processor=__lowerCAmelCase	,			qformer_tokenizer=__lowerCAmelCase	)

														UpperCamelCase__					  =      """lower newer"""
														UpperCamelCase__					  =      self.prepare_image_inputs()

														UpperCamelCase__					  =      processor(text=__lowerCAmelCase	,			images=__lowerCAmelCase	)

														self.assertListEqual(
														    list(inputs.keys()	)	,			["""input_ids""", """attention_mask""", """qformer_input_ids""", """qformer_attention_mask""", """pixel_values"""]	,			)











import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
					from .tokenization_big_bird import BigBirdTokenizer
else:
					UpperCamelCase__           =			None

UpperCamelCase__           =			logging.get_logger(__name__)
UpperCamelCase__           =			{"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}

UpperCamelCase__           =			{
    "vocab_file": {
        "google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model",
        "google/bigbird-roberta-large": (
            "https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model"
        ),
        "google/bigbird-base-trivia-itc": (
            "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model"
        ),
    },
    "tokenizer_file": {
        "google/bigbird-roberta-base": (
            "https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json"
        ),
        "google/bigbird-roberta-large": (
            "https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json"
        ),
        "google/bigbird-base-trivia-itc": (
            "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json"
        ),
    },
}

UpperCamelCase__           =			{
    "google/bigbird-roberta-base": 4_096,
    "google/bigbird-roberta-large": 4_096,
    "google/bigbird-base-trivia-itc": 4_096,
}


UpperCamelCase__           =			"▁"

class 							__SCREAMING_SNAKE_CASE       (  _a							):
							snake_case  :   Optional[Any]      =							VOCAB_FILES_NAMES
							snake_case  :   List[str]      =							PRETRAINED_VOCAB_FILES_MAP
							snake_case  :   List[str]      =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							snake_case  :   int      =							BigBirdTokenizer
							snake_case  :   List[Any]      =							["""input_ids""", """attention_mask"""]
							snake_case  :   List[int]      =							[]






							def __init__(    self	,			__lowerCAmelCase=None	,			__lowerCAmelCase=None	,			__lowerCAmelCase="<unk>"	,			__lowerCAmelCase="<s>"	,			__lowerCAmelCase="</s>"	,			__lowerCAmelCase="<pad>"	,			__lowerCAmelCase="[SEP]"	,			__lowerCAmelCase="[MASK]"	,			__lowerCAmelCase="[CLS]"	,			**__lowerCAmelCase	,			):
														UpperCamelCase__					  =      AddedToken(__lowerCAmelCase	,			lstrip=__lowerCAmelCase	,			rstrip=__lowerCAmelCase	) if isinstance(__lowerCAmelCase	,			__lowerCAmelCase	) else bos_token
														UpperCamelCase__					  =      AddedToken(__lowerCAmelCase	,			lstrip=__lowerCAmelCase	,			rstrip=__lowerCAmelCase	) if isinstance(__lowerCAmelCase	,			__lowerCAmelCase	) else eos_token
														UpperCamelCase__					  =      AddedToken(__lowerCAmelCase	,			lstrip=__lowerCAmelCase	,			rstrip=__lowerCAmelCase	) if isinstance(__lowerCAmelCase	,			__lowerCAmelCase	) else unk_token
														UpperCamelCase__					  =      AddedToken(__lowerCAmelCase	,			lstrip=__lowerCAmelCase	,			rstrip=__lowerCAmelCase	) if isinstance(__lowerCAmelCase	,			__lowerCAmelCase	) else pad_token
														UpperCamelCase__					  =      AddedToken(__lowerCAmelCase	,			lstrip=__lowerCAmelCase	,			rstrip=__lowerCAmelCase	) if isinstance(__lowerCAmelCase	,			__lowerCAmelCase	) else cls_token
														UpperCamelCase__					  =      AddedToken(__lowerCAmelCase	,			lstrip=__lowerCAmelCase	,			rstrip=__lowerCAmelCase	) if isinstance(__lowerCAmelCase	,			__lowerCAmelCase	) else sep_token

														# Mask token behave like a normal word, i.e. include the space before it
														UpperCamelCase__					  =      AddedToken(__lowerCAmelCase	,			lstrip=__lowerCAmelCase	,			rstrip=__lowerCAmelCase	) if isinstance(__lowerCAmelCase	,			__lowerCAmelCase	) else mask_token

														super().__init__(
														    __lowerCAmelCase	,			tokenizer_file=__lowerCAmelCase	,			bos_token=__lowerCAmelCase	,			eos_token=__lowerCAmelCase	,			unk_token=__lowerCAmelCase	,			sep_token=__lowerCAmelCase	,			pad_token=__lowerCAmelCase	,			cls_token=__lowerCAmelCase	,			mask_token=__lowerCAmelCase	,			**__lowerCAmelCase	,			)

														UpperCamelCase__					  =      vocab_file
														UpperCamelCase__					  =      False if not self.vocab_file else True






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase = None	):
														UpperCamelCase__					  =      [self.sep_token_id]
														UpperCamelCase__					  =      [self.cls_token_id]
														if token_ids_a is None:
																					return cls + token_ids_a + sep
														return cls + token_ids_a + sep + token_ids_a + sep






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase = None	,			__lowerCAmelCase = False	):

														if already_has_special_tokens:
																					if token_ids_a is not None:
																												raise ValueError(
																												    """You should not supply a second sequence if the provided sequence of """
																												    """ids is already formatted with special tokens for the model."""	)
																					return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

														if token_ids_a is None:
																					return [1] + ([0] * len(__lowerCAmelCase	)) + [1]
														return [1] + ([0] * len(__lowerCAmelCase	)) + [1] + ([0] * len(__lowerCAmelCase	)) + [1]






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase = None	):
														UpperCamelCase__					  =      [self.sep_token_id]
														UpperCamelCase__					  =      [self.cls_token_id]

														if token_ids_a is None:
																					return len(cls + token_ids_a + sep	) * [0]
														return len(cls + token_ids_a + sep	) * [0] + len(token_ids_a + sep	) * [1]







							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase = None	):
														if not self.can_save_slow_tokenizer:
																					raise ValueError(
																					    """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """
																					    """tokenizer."""	)

														if not os.path.isdir(__lowerCAmelCase	):
																					logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""	)
																					return
														UpperCamelCase__					  =      os.path.join(
														    __lowerCAmelCase	,			(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]	)

														if os.path.abspath(self.vocab_file	) != os.path.abspath(__lowerCAmelCase	):
																					copyfile(self.vocab_file	,			__lowerCAmelCase	)

														return (out_vocab_file,)




