





import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
     from PIL import Image

     from transformers import AlignProcessor, EfficientNetImageProcessor



@require_vision
class 				_UpperCAmelCase    (    unittest.TestCase    ):

  """simple docstring"""


  def 				lowercase		(				self :		int     )							->						int:
     __lowerCAmelCase								=	tempfile.mkdtemp()

     __lowerCAmelCase								=	[
         '[UNK]',
         '[CLS]',
         '[SEP]',
         '[PAD]',
         '[MASK]',
         'want',
         '##want',
         '##ed',
         'wa',
         'un',
         'runn',
         '##ing',
         ',',
         'low',
         'lowest',
     ]
     __lowerCAmelCase								=	os.path.join(self.tmpdirname					,  VOCAB_FILES_NAMES['vocab_file']     )
     with open(self.vocab_file					,  'w'					,  encoding='utf-8'     ) as vocab_writer:
        vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]     )     )

     __lowerCAmelCase								=	{
         'do_resize': True,
         'size': 2_0,
         'do_center_crop': True,
         'crop_size': 1_8,
         'do_normalize': True,
         'image_mean': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
         'image_std': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
     }
     __lowerCAmelCase								=	os.path.join(self.tmpdirname					,  SCREAMING_SNAKE_CASE_     )
     with open(self.image_processor_file					,  'w'					,  encoding='utf-8'     ) as fp:
        json.dump(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_     )






  def 				lowercase		(				self :		int					,  **lowerCAmelCase_ :		Tuple     )							->						List[str]:
     return BertTokenizer.from_pretrained(self.tmpdirname					,  **SCREAMING_SNAKE_CASE_     )






  def 				lowercase		(				self :		Dict					,  **lowerCAmelCase_ :		List[str]     )							->						Tuple:
     return BertTokenizerFast.from_pretrained(self.tmpdirname					,  **SCREAMING_SNAKE_CASE_     )






  def 				lowercase		(				self :		List[Any]					,  **lowerCAmelCase_ :		List[Any]     )							->						Dict:
     return EfficientNetImageProcessor.from_pretrained(self.tmpdirname					,  **SCREAMING_SNAKE_CASE_     )






  def 				lowercase		(				self :		str     )							->						List[str]:
     shutil.rmtree(self.tmpdirname     )






  def 				lowercase		(				self :		Any     )							->						Optional[Any]:
     __lowerCAmelCase								=	[np.random.randint(2_5_5					,  size=(3, 3_0, 4_0_0)					,  dtype=np.uinta     )]
     __lowerCAmelCase								=	[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE_					,  0					,  -1     )     ) for x in image_inputs]
     return image_inputs






  def 				lowercase		(				self :		Dict     )							->						int:
     __lowerCAmelCase								=	self.get_tokenizer()
     __lowerCAmelCase								=	self.get_rust_tokenizer()
     __lowerCAmelCase								=	self.get_image_processor()

     __lowerCAmelCase								=	AlignProcessor(tokenizer=SCREAMING_SNAKE_CASE_					,  image_processor=SCREAMING_SNAKE_CASE_     )
     processor_slow.save_pretrained(self.tmpdirname     )
     __lowerCAmelCase								=	AlignProcessor.from_pretrained(self.tmpdirname					,  use_fast=SCREAMING_SNAKE_CASE_     )

     __lowerCAmelCase								=	AlignProcessor(tokenizer=SCREAMING_SNAKE_CASE_					,  image_processor=SCREAMING_SNAKE_CASE_     )
     processor_fast.save_pretrained(self.tmpdirname     )
     __lowerCAmelCase								=	AlignProcessor.from_pretrained(self.tmpdirname     )

     self.assertEqual(processor_slow.tokenizer.get_vocab()					,  tokenizer_slow.get_vocab()     )
     self.assertEqual(processor_fast.tokenizer.get_vocab()					,  tokenizer_fast.get_vocab()     )
     self.assertEqual(tokenizer_slow.get_vocab()					,  tokenizer_fast.get_vocab()     )
     self.assertIsInstance(processor_slow.tokenizer					,  SCREAMING_SNAKE_CASE_     )
     self.assertIsInstance(processor_fast.tokenizer					,  SCREAMING_SNAKE_CASE_     )

     self.assertEqual(processor_slow.image_processor.to_json_string()					,  image_processor.to_json_string()     )
     self.assertEqual(processor_fast.image_processor.to_json_string()					,  image_processor.to_json_string()     )
     self.assertIsInstance(processor_slow.image_processor					,  SCREAMING_SNAKE_CASE_     )
     self.assertIsInstance(processor_fast.image_processor					,  SCREAMING_SNAKE_CASE_     )






  def 				lowercase		(				self :		List[str]     )							->						str:
     __lowerCAmelCase								=	AlignProcessor(tokenizer=self.get_tokenizer()					,  image_processor=self.get_image_processor()     )
     processor.save_pretrained(self.tmpdirname     )

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

     __lowerCAmelCase								=	AlignProcessor.from_pretrained(
         self.tmpdirname					,  bos_token='(BOS)'					,  eos_token='(EOS)'					,  do_normalize=SCREAMING_SNAKE_CASE_					,  padding_value=1.0     )

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

     self.assertEqual(processor.image_processor.to_json_string()					,  image_processor_add_kwargs.to_json_string()     )
     self.assertIsInstance(processor.image_processor					,  SCREAMING_SNAKE_CASE_     )






  def 				lowercase		(				self :		Dict     )							->						int:
     __lowerCAmelCase								=	self.get_image_processor()
     __lowerCAmelCase								=	self.get_tokenizer()

     __lowerCAmelCase								=	AlignProcessor(tokenizer=SCREAMING_SNAKE_CASE_					,  image_processor=SCREAMING_SNAKE_CASE_     )

     __lowerCAmelCase								=	self.prepare_image_inputs()

     __lowerCAmelCase								=	image_processor(SCREAMING_SNAKE_CASE_					,  return_tensors='np'     )
     __lowerCAmelCase								=	processor(images=SCREAMING_SNAKE_CASE_					,  return_tensors='np'     )

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






  def 				lowercase		(				self :		List[Any]     )							->						Union[str, Any]:
     __lowerCAmelCase								=	self.get_image_processor()
     __lowerCAmelCase								=	self.get_tokenizer()

     __lowerCAmelCase								=	AlignProcessor(tokenizer=SCREAMING_SNAKE_CASE_					,  image_processor=SCREAMING_SNAKE_CASE_     )

     __lowerCAmelCase								=	'lower newer'

     __lowerCAmelCase								=	processor(text=SCREAMING_SNAKE_CASE_     )

     __lowerCAmelCase								=	tokenizer(SCREAMING_SNAKE_CASE_					,  padding='max_length'					,  max_length=6_4     )

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






  def 				lowercase		(				self :		Tuple     )							->						int:
     __lowerCAmelCase								=	self.get_image_processor()
     __lowerCAmelCase								=	self.get_tokenizer()

     __lowerCAmelCase								=	AlignProcessor(tokenizer=SCREAMING_SNAKE_CASE_					,  image_processor=SCREAMING_SNAKE_CASE_     )

     __lowerCAmelCase								=	'lower newer'
     __lowerCAmelCase								=	self.prepare_image_inputs()

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

     self.assertListEqual(list(inputs.keys()     )					,  ['input_ids', 'token_type_ids', 'attention_mask', 'pixel_values']     )

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






  def 				lowercase		(				self :		str     )							->						str:
     __lowerCAmelCase								=	self.get_image_processor()
     __lowerCAmelCase								=	self.get_tokenizer()

     __lowerCAmelCase								=	AlignProcessor(tokenizer=SCREAMING_SNAKE_CASE_					,  image_processor=SCREAMING_SNAKE_CASE_     )

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

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

     self.assertListEqual(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_     )






  def 				lowercase		(				self :		List[Any]     )							->						List[str]:
     __lowerCAmelCase								=	self.get_image_processor()
     __lowerCAmelCase								=	self.get_tokenizer()

     __lowerCAmelCase								=	AlignProcessor(tokenizer=SCREAMING_SNAKE_CASE_					,  image_processor=SCREAMING_SNAKE_CASE_     )

     __lowerCAmelCase								=	'lower newer'
     __lowerCAmelCase								=	self.prepare_image_inputs()

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

     self.assertListEqual(list(inputs.keys()     )					,  processor.model_input_names     )





'''simple docstring'''



from . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)






