




import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip 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 CLIPImageProcessor, CLIPProcessor

@require_vision
class  _lowerCamelCase						(			unittest.TestCase   ):

      """simple docstring"""

      def       snake_case	(     self					: Dict				):
          __UpperCamelCase  		=						tempfile.mkdtemp()

          # fmt: off
          __UpperCamelCase  		=						['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>''']
          # fmt: on
          __UpperCamelCase  		=						dict(zip(snake_case ,       range(len(snake_case				)				)				)				)
          __UpperCamelCase  		=						['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', '''''']
          __UpperCamelCase  		=						{'''unk_token''': '''<unk>'''}

          __UpperCamelCase  		=						os.path.join(self.tmpdirname ,       VOCAB_FILES_NAMES['''vocab_file''']				)
          __UpperCamelCase  		=						os.path.join(self.tmpdirname ,       VOCAB_FILES_NAMES['''merges_file''']				)
          with open(self.vocab_file ,       '''w''' ,       encoding='''utf-8'''				) as fp:
              fp.write(json.dumps(snake_case				) + '''\n'''				)
          with open(self.merges_file ,       '''w''' ,       encoding='''utf-8'''				) as fp:
              fp.write('''\n'''.join(snake_case				)				)

          __UpperCamelCase  		=						{
              '''do_resize''': True,
              '''size''': 20,
              '''do_center_crop''': True,
              '''crop_size''': 18,
              '''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],
          }
          __UpperCamelCase  		=						os.path.join(self.tmpdirname ,       snake_case				)
          with open(self.image_processor_file ,       '''w''' ,       encoding='''utf-8'''				) as fp:
              json.dump(snake_case ,       snake_case				)







      def       snake_case	(     self					: str ,       **snake_case					: Dict				):
          return CLIPTokenizer.from_pretrained(self.tmpdirname ,       **snake_case				)







      def       snake_case	(     self					: Union[str, Any] ,       **snake_case					: List[str]				):
          return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,       **snake_case				)







      def       snake_case	(     self					: Tuple ,       **snake_case					: Optional[int]				):
          return CLIPImageProcessor.from_pretrained(self.tmpdirname ,       **snake_case				)







      def       snake_case	(     self					: int				):
          shutil.rmtree(self.tmpdirname				)







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

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

          return image_inputs







      def       snake_case	(     self					: str				):
          __UpperCamelCase  		=						self.get_tokenizer()
          __UpperCamelCase  		=						self.get_rust_tokenizer()
          __UpperCamelCase  		=						self.get_image_processor()

          __UpperCamelCase  		=						CLIPProcessor(tokenizer=snake_case ,       image_processor=snake_case				)
          processor_slow.save_pretrained(self.tmpdirname				)
          __UpperCamelCase  		=						CLIPProcessor.from_pretrained(self.tmpdirname ,       use_fast=snake_case				)

          __UpperCamelCase  		=						CLIPProcessor(tokenizer=snake_case ,       image_processor=snake_case				)
          processor_fast.save_pretrained(self.tmpdirname				)
          __UpperCamelCase  		=						CLIPProcessor.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 ,       snake_case				)
          self.assertIsInstance(processor_fast.tokenizer ,       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 ,       snake_case				)
          self.assertIsInstance(processor_fast.image_processor ,       snake_case				)







      def       snake_case	(     self					: Any				):
          __UpperCamelCase  		=						CLIPProcessor(tokenizer=self.get_tokenizer() ,       image_processor=self.get_image_processor()				)
          processor.save_pretrained(self.tmpdirname				)

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

          __UpperCamelCase  		=						CLIPProcessor.from_pretrained(
              self.tmpdirname ,       bos_token='''(BOS)''' ,       eos_token='''(EOS)''' ,       do_normalize=snake_case ,       padding_value=1.0				)

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

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







      def       snake_case	(     self					: List[str]				):
          __UpperCamelCase  		=						self.get_image_processor()
          __UpperCamelCase  		=						self.get_tokenizer()

          __UpperCamelCase  		=						CLIPProcessor(tokenizer=snake_case ,       image_processor=snake_case				)

          __UpperCamelCase  		=						self.prepare_image_inputs()

          __UpperCamelCase  		=						image_processor(snake_case ,       return_tensors='''np'''				)
          __UpperCamelCase  		=						processor(images=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       snake_case	(     self					: str				):
          __UpperCamelCase  		=						self.get_image_processor()
          __UpperCamelCase  		=						self.get_tokenizer()

          __UpperCamelCase  		=						CLIPProcessor(tokenizer=snake_case ,       image_processor=snake_case				)

          __UpperCamelCase  		=						'''lower newer'''

          __UpperCamelCase  		=						processor(text=snake_case				)

          __UpperCamelCase  		=						tokenizer(snake_case				)

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







      def       snake_case	(     self					: str				):
          __UpperCamelCase  		=						self.get_image_processor()
          __UpperCamelCase  		=						self.get_tokenizer()

          __UpperCamelCase  		=						CLIPProcessor(tokenizer=snake_case ,       image_processor=snake_case				)

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

          __UpperCamelCase  		=						processor(text=snake_case ,       images=snake_case				)

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

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







      def       snake_case	(     self					: Any				):
          __UpperCamelCase  		=						self.get_image_processor()
          __UpperCamelCase  		=						self.get_tokenizer()

          __UpperCamelCase  		=						CLIPProcessor(tokenizer=snake_case ,       image_processor=snake_case				)

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

          __UpperCamelCase  		=						processor.batch_decode(snake_case				)
          __UpperCamelCase  		=						tokenizer.batch_decode(snake_case				)

          self.assertListEqual(snake_case ,       snake_case				)







      def       snake_case	(     self					: Tuple				):
          __UpperCamelCase  		=						self.get_image_processor()
          __UpperCamelCase  		=						self.get_tokenizer()

          __UpperCamelCase  		=						CLIPProcessor(tokenizer=snake_case ,       image_processor=snake_case				)

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

          __UpperCamelCase  		=						processor(text=snake_case ,       images=snake_case				)

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












import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_   		=     logging.get_logger(__name__)

a_   		=     {
    "microsoft/unispeech-large-1500h-cv": (
        "https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json"
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}

class  _lowerCamelCase						(			_SCREAMING_SNAKE_CASE   ):

      """simple docstring"""

      lowerCAmelCase__			: Dict    =					"unispeech"







      def __init__(     self					: str ,       snake_case					: Union[str, Any]=32 ,       snake_case					: Optional[Any]=768 ,       snake_case					: Dict=12 ,       snake_case					: Tuple=12 ,       snake_case					: Optional[Any]=3072 ,       snake_case					: Any="gelu" ,       snake_case					: Dict=0.1 ,       snake_case					: Tuple=0.1 ,       snake_case					: str=0.1 ,       snake_case					: Optional[int]=0.0 ,       snake_case					: Any=0.0 ,       snake_case					: Optional[Any]=0.1 ,       snake_case					: List[Any]=0.1 ,       snake_case					: Optional[int]=0.02 ,       snake_case					: List[str]=1E-5 ,       snake_case					: str="group" ,       snake_case					: List[Any]="gelu" ,       snake_case					: Union[str, Any]=(512, 512, 512, 512, 512, 512, 512) ,       snake_case					: List[Any]=(5, 2, 2, 2, 2, 2, 2) ,       snake_case					: Union[str, Any]=(10, 3, 3, 3, 3, 2, 2) ,       snake_case					: Tuple=False ,       snake_case					: Optional[int]=128 ,       snake_case					: List[str]=16 ,       snake_case					: List[str]=False ,       snake_case					: Dict=True ,       snake_case					: Optional[Any]=0.05 ,       snake_case					: Optional[Any]=10 ,       snake_case					: Union[str, Any]=2 ,       snake_case					: List[str]=0.0 ,       snake_case					: str=10 ,       snake_case					: int=0 ,       snake_case					: Tuple=320 ,       snake_case					: Any=2 ,       snake_case					: List[str]=0.1 ,       snake_case					: Optional[Any]=100 ,       snake_case					: List[Any]=256 ,       snake_case					: Union[str, Any]=256 ,       snake_case					: Any=0.1 ,       snake_case					: str="mean" ,       snake_case					: Union[str, Any]=False ,       snake_case					: str=False ,       snake_case					: Union[str, Any]=256 ,       snake_case					: Optional[Any]=80 ,       snake_case					: str=0 ,       snake_case					: int=1 ,       snake_case					: int=2 ,       snake_case					: Dict=0.5 ,       **snake_case					: Optional[int] ,       ):
          super().__init__(**snake_case ,       pad_token_id=snake_case ,       bos_token_id=snake_case ,       eos_token_id=snake_case				)
          __UpperCamelCase  		=						hidden_size
          __UpperCamelCase  		=						feat_extract_norm
          __UpperCamelCase  		=						feat_extract_activation
          __UpperCamelCase  		=						list(snake_case				)
          __UpperCamelCase  		=						list(snake_case				)
          __UpperCamelCase  		=						list(snake_case				)
          __UpperCamelCase  		=						conv_bias
          __UpperCamelCase  		=						num_conv_pos_embeddings
          __UpperCamelCase  		=						num_conv_pos_embedding_groups
          __UpperCamelCase  		=						len(self.conv_dim				)
          __UpperCamelCase  		=						num_hidden_layers
          __UpperCamelCase  		=						intermediate_size
          __UpperCamelCase  		=						hidden_act
          __UpperCamelCase  		=						num_attention_heads
          __UpperCamelCase  		=						hidden_dropout
          __UpperCamelCase  		=						attention_dropout
          __UpperCamelCase  		=						activation_dropout
          __UpperCamelCase  		=						feat_proj_dropout
          __UpperCamelCase  		=						final_dropout
          __UpperCamelCase  		=						layerdrop
          __UpperCamelCase  		=						layer_norm_eps
          __UpperCamelCase  		=						initializer_range
          __UpperCamelCase  		=						num_ctc_classes
          __UpperCamelCase  		=						vocab_size
          __UpperCamelCase  		=						do_stable_layer_norm
          __UpperCamelCase  		=						use_weighted_layer_sum
          __UpperCamelCase  		=						classifier_proj_size

          if (
              (len(self.conv_stride				) != self.num_feat_extract_layers)
              or (len(self.conv_kernel				) != self.num_feat_extract_layers)
              or (len(self.conv_dim				) != self.num_feat_extract_layers)
          ):
              raise ValueError(
                  '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
                  ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
                  F" {len(self.conv_dim				)}`, `len(config.conv_stride) = {len(self.conv_stride				)}`,"
                  F" `len(config.conv_kernel) = {len(self.conv_kernel				)}`."				)

          # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
          __UpperCamelCase  		=						apply_spec_augment
          __UpperCamelCase  		=						mask_time_prob
          __UpperCamelCase  		=						mask_time_length
          __UpperCamelCase  		=						mask_time_min_masks
          __UpperCamelCase  		=						mask_feature_prob
          __UpperCamelCase  		=						mask_feature_length
          __UpperCamelCase  		=						mask_feature_min_masks

          # parameters for pretraining with codevector quantized representations
          __UpperCamelCase  		=						num_codevectors_per_group
          __UpperCamelCase  		=						num_codevector_groups
          __UpperCamelCase  		=						contrastive_logits_temperature
          __UpperCamelCase  		=						feat_quantizer_dropout
          __UpperCamelCase  		=						num_negatives
          __UpperCamelCase  		=						codevector_dim
          __UpperCamelCase  		=						proj_codevector_dim
          __UpperCamelCase  		=						diversity_loss_weight

          # ctc loss
          __UpperCamelCase  		=						ctc_loss_reduction
          __UpperCamelCase  		=						ctc_zero_infinity

          # pretraining loss
          __UpperCamelCase  		=						replace_prob







      @property
      def       snake_case	(     self					: Dict				):
          return functools.reduce(operator.mul ,       self.conv_stride ,       1				)






