






'''simple docstring'''
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
     import tensorflow as tf

if is_tensorflow_text_available():
     from transformers.models.bert import TFBertTokenizer


a_					= ['bert-base-uncased', 'bert-base-cased']
a_					= 'hf-internal-testing/tiny-bert-tf-only'

if is_tf_available():






     class      __SCREAMING_SNAKE_CASE      (			tf.keras.Model				):




      def __init__(      self	:							Union[str, Any]					,      __lowercase	:							List[Any]      )	->						Any:
            super().__init__()
            SCREAMING_SNAKE_CASE__     :				str									=tokenizer
            SCREAMING_SNAKE_CASE__     :				Optional[int]									=AutoConfig.from_pretrained(_lowerCAmelCase      )
            SCREAMING_SNAKE_CASE__     :				int									=TFAutoModel.from_config(_lowerCAmelCase      )




      def      __magic_name__	(      self	:							Tuple					,      __lowercase	:							Optional[int]      )	->						Union[str, Any]:
            SCREAMING_SNAKE_CASE__     :				List[str]									=self.tokenizer(_lowerCAmelCase      )
            SCREAMING_SNAKE_CASE__     :				List[Any]									=self.bert(**_lowerCAmelCase      )
            return out["pooler_output"]






@require_tf
@require_tensorflow_text
class      __SCREAMING_SNAKE_CASE      (			unittest.TestCase				):




 def      __magic_name__	(      self	:							Optional[Any]      )	->						List[Any]:
       super().setUp()

       SCREAMING_SNAKE_CASE__     :				List[Any]									=[
           BertTokenizer.from_pretrained(_lowerCAmelCase      ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
       ]  # repeat for when fast_bert_tokenizer=false
       SCREAMING_SNAKE_CASE__     :				Any									=[TFBertTokenizer.from_pretrained(_lowerCAmelCase      ) for checkpoint in TOKENIZER_CHECKPOINTS] + [
           TFBertTokenizer.from_pretrained(_lowerCAmelCase					,      use_fast_bert_tokenizer=_lowerCAmelCase      )
           for checkpoint in TOKENIZER_CHECKPOINTS
       ]
       assert len(self.tokenizers      ) == len(self.tf_tokenizers      )

       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=[
           '''This is a straightforward English test sentence.''',
           '''This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.''',
           '''Now we\'re going to add some Chinese: 一 二 三 一二三''',
           '''And some much more rare Chinese: 齉 堃 齉堃''',
           '''Je vais aussi écrire en français pour tester les accents''',
           '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''',
       ]
       SCREAMING_SNAKE_CASE__     :				Any									=list(zip(self.test_sentences					,      self.test_sentences[::-1]      )      )




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Tuple:
       for tokenizer, tf_tokenizer in zip(self.tokenizers					,      self.tf_tokenizers      ):
             for test_inputs in (self.test_sentences, self.paired_sentences):
                   SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=tokenizer(_lowerCAmelCase					,      return_tensors='''tf'''					,      padding='''longest'''      )
                   SCREAMING_SNAKE_CASE__     :				Any									=tf_tokenizer(_lowerCAmelCase      )

                   for key in python_outputs.keys():
                         self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape      )      )
                         self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]					,      tf.intaa      ) == tf_outputs[key]      )      )




 @slow
 def      __magic_name__	(      self	:							List[str]      )	->						Any:
       for tf_tokenizer in self.tf_tokenizers:
             SCREAMING_SNAKE_CASE__     :				Optional[Any]									=tf_tokenizer(self.paired_sentences      )
             SCREAMING_SNAKE_CASE__     :				Tuple									=tf_tokenizer(
                 text=[sentence[0] for sentence in self.paired_sentences]					,      text_pair=[sentence[1] for sentence in self.paired_sentences]					,      )
             for key in merged_outputs.keys():
                   self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]					,      tf.intaa      ) == separated_outputs[key]      )      )




 @slow
 def      __magic_name__	(      self	:							Optional[Any]      )	->						int:
       for tf_tokenizer in self.tf_tokenizers:
             SCREAMING_SNAKE_CASE__     :				Optional[Any]									=tf.function(_lowerCAmelCase      )
             for test_inputs in (self.test_sentences, self.paired_sentences):
                   SCREAMING_SNAKE_CASE__     :				int									=tf.constant(_lowerCAmelCase      )
                   SCREAMING_SNAKE_CASE__     :				Any									=compiled_tokenizer(_lowerCAmelCase      )
                   SCREAMING_SNAKE_CASE__     :				int									=tf_tokenizer(_lowerCAmelCase      )

                   for key in eager_outputs.keys():
                         self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]      )      )




 @slow
 def      __magic_name__	(      self	:							List[Any]      )	->						List[str]:
       for tf_tokenizer in self.tf_tokenizers:
             SCREAMING_SNAKE_CASE__     :				str									=ModelToSave(tokenizer=_lowerCAmelCase      )
             SCREAMING_SNAKE_CASE__     :				Dict									=tf.convert_to_tensor(self.test_sentences      )
             SCREAMING_SNAKE_CASE__     :				int									=model(_lowerCAmelCase      )  # Build model with some sample inputs
             with TemporaryDirectory() as tempdir:
                   SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=Path(_lowerCAmelCase      ) / '''saved.model'''
                   model.save(_lowerCAmelCase      )
                   SCREAMING_SNAKE_CASE__     :				Tuple									=tf.keras.models.load_model(_lowerCAmelCase      )
             SCREAMING_SNAKE_CASE__     :				List[str]									=loaded_model(_lowerCAmelCase      )
             # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
             self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output      )      )					,      1e-5      )







'''simple docstring'''
import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
  from PIL import Image
else:






  class      __SCREAMING_SNAKE_CASE      :




   @staticmethod
   def      __magic_name__	(      *__lowercase	:							int					,      **__lowercase	:							Optional[Any]      )	->						Optional[Any]:
         pass






@is_pipeline_test
@require_vision
class      __SCREAMING_SNAKE_CASE      (			unittest.TestCase				):




 @require_torch
 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      candidate_labels=['''a''', '''b''', '''c''']      )

       # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
       # python and torch versions.
       self.assertIn(
           nested_simplify(__lowercase      )					,      [
               [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}],
               [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''c'''}, {'''score''': 0.333, '''label''': '''b'''}],
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				int									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      framework='''tf'''      )
       SCREAMING_SNAKE_CASE__     :				Dict									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Tuple									=image_classifier(__lowercase					,      candidate_labels=['''a''', '''b''', '''c''']      )

       self.assertEqual(
           nested_simplify(__lowercase      )					,      [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}]					,      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @slow
 @require_torch
 def      __magic_name__	(      self	:							List[Any]      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=pipeline(
           task='''zero-shot-image-classification'''					,      model='''openai/clip-vit-base-patch32'''					,      )
       # This is an image of 2 cats with remotes and no planes
       SCREAMING_SNAKE_CASE__     :				Any									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )

       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=image_classifier([image] * 5					,      candidate_labels=['''cat''', '''plane''', '''remote''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5					,      )




 @slow
 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				str									=pipeline(
           task='''zero-shot-image-classification'''					,      model='''openai/clip-vit-base-patch32'''					,      framework='''tf'''      )
       # This is an image of 2 cats with remotes and no planes
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_classifier([image] * 5					,      candidate_labels=['''cat''', '''plane''', '''remote''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5					,      )