






'''simple docstring'''

import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import require_keras_nlp, require_tf, slow


if is_tf_available():
  import tensorflow as tf

if is_keras_nlp_available():
  from transformers.models.gpta import TFGPTaTokenizer


__a:			Optional[int]	       =       ["""gpt2"""]
__a:			str	       =       """gpt2"""

if is_tf_available():



  class       UpperCAmelCase  (						tf.Module ):





         '''simple docstring'''



         def __init__(						self     ,       __lowerCAmelCase			)  ->  Any:
          super().__init__()
          lowercase__    :   Tuple						= tokenizer
          lowercase__    :   Tuple						= AutoConfig.from_pretrained(__lowerCAmelCase			)
          lowercase__    :   int						= TFGPTaLMHeadModel.from_config(__lowerCAmelCase			)


         @tf.function(input_signature=(tf.TensorSpec((None,)     ,       tf.string     ,       name='''text'''			),)			)
         def _lowerCAmelCase(						self     ,       __lowerCAmelCase			)  ->  List[str]:
          lowercase__    :   Dict						= self.tokenizer(__lowerCAmelCase			)
          lowercase__    :   List[str]						= tokenized['''input_ids'''].to_tensor()

          lowercase__    :   Tuple						= tf.cast(input_ids_dense > 0     ,       tf.intaa			)
          # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN])

          lowercase__    :   int						= self.model(input_ids=__lowerCAmelCase     ,       attention_mask=__lowerCAmelCase			)['''logits''']

          return outputs



@require_tf
@require_keras_nlp
class       UpperCAmelCase  (						unittest.TestCase ):





       '''simple docstring'''



       def _lowerCAmelCase(						self			)  ->  List[str]:
        super().setUp()

        lowercase__    :   List[Any]						= [GPTaTokenizer.from_pretrained(__lowerCAmelCase			) for checkpoint in (TOKENIZER_CHECKPOINTS)]
        lowercase__    :   List[Any]						= [TFGPTaTokenizer.from_pretrained(__lowerCAmelCase			) for checkpoint in TOKENIZER_CHECKPOINTS]
        assert len(self.tokenizers			) == len(self.tf_tokenizers			)

        lowercase__    :   str						= [
            '''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ċ, ꝼ''',
        ]
        lowercase__    :   Any						= list(zip(self.test_sentences     ,       self.test_sentences[::-1]			)			)

       def _lowerCAmelCase(						self			)  ->  List[Any]:
        for tokenizer, tf_tokenizer in zip(self.tokenizers     ,       self.tf_tokenizers			):
         for test_inputs in self.test_sentences:
          lowercase__    :   str						= tokenizer([test_inputs]     ,       return_tensors='''tf'''			)
          lowercase__    :   Dict						= tf_tokenizer([test_inputs]			)

          for key in python_outputs.keys():
           # convert them to numpy to avoid messing with ragged tensors
           lowercase__    :   List[Any]						= python_outputs[key].numpy()
           lowercase__    :   List[str]						= tf_outputs[key].numpy()

           self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape			)			)
           self.assertTrue(tf.reduce_all(tf.cast(__lowerCAmelCase     ,       tf.intaa			) == tf_outputs_values			)			)

       @slow
       def _lowerCAmelCase(						self			)  ->  Optional[Any]:
        for tf_tokenizer in self.tf_tokenizers:
         lowercase__    :   Optional[Any]						= tf.function(__lowerCAmelCase			)
         for test_inputs in self.test_sentences:
          lowercase__    :   List[str]						= tf.constant(__lowerCAmelCase			)
          lowercase__    :   Optional[Any]						= compiled_tokenizer(__lowerCAmelCase			)
          lowercase__    :   Union[str, Any]						= tf_tokenizer(__lowerCAmelCase			)

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

       @slow
       def _lowerCAmelCase(						self			)  ->  Dict:
        for tf_tokenizer in self.tf_tokenizers:
         lowercase__    :   Any						= ModelToSave(tokenizer=__lowerCAmelCase			)
         lowercase__    :   str						= tf.convert_to_tensor([self.test_sentences[0]]			)
         lowercase__    :   Optional[int]						= model.serving(__lowerCAmelCase			)  # Build model with some sample inputs
         with TemporaryDirectory() as tempdir:
          lowercase__    :   Tuple						= Path(__lowerCAmelCase			) / '''saved.model'''
          tf.saved_model.save(__lowerCAmelCase     ,       __lowerCAmelCase     ,       signatures={'''serving_default''': model.serving}			)
          lowercase__    :   Dict						= tf.saved_model.load(__lowerCAmelCase			)
         lowercase__    :   int						= loaded_model.signatures['''serving_default'''](__lowerCAmelCase			)['''output_0''']
         # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
         self.assertTrue(tf.reduce_all(out == loaded_output			)			)

       @slow
       def _lowerCAmelCase(						self			)  ->  Union[str, Any]:
        for tf_tokenizer in self.tf_tokenizers:
         lowercase__    :   List[Any]						= tf.convert_to_tensor([self.test_sentences[0]]			)
         lowercase__    :   Tuple						= tf_tokenizer(__lowerCAmelCase			)  # Build model with some sample inputs

         lowercase__    :   Dict						= tf_tokenizer.get_config()
         lowercase__    :   int						= TFGPTaTokenizer.from_config(__lowerCAmelCase			)
         lowercase__    :   Tuple						= model_from_config(__lowerCAmelCase			)

         for key in from_config_output.keys():
          self.assertTrue(tf.reduce_all(from_config_output[key] == out[key]			)			)


       @slow
       def _lowerCAmelCase(						self			)  ->  List[str]:
        for tf_tokenizer in self.tf_tokenizers:
         # for the test to run
         lowercase__    :   List[str]						= 123123

         for max_length in [3, 5, 1024]:
          lowercase__    :   List[str]						= tf.convert_to_tensor([self.test_sentences[0]]			)
          lowercase__    :   Optional[Any]						= tf_tokenizer(__lowerCAmelCase     ,       max_length=__lowerCAmelCase			)

          lowercase__    :   List[str]						= out['''input_ids'''].numpy().shape[1]

          assert out_length == max_length


'''simple docstring'''

from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
  from PIL import Image

  from ..image_utils import load_image

if is_torch_available():
  from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
  from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
  from ..tf_utils import stable_softmax

__a:			Optional[Any]	       =       logging.get_logger(__name__)



@add_end_docstrings(a__ )
class       UpperCAmelCase  (						a__ ):





       '''simple docstring'''



       def __init__(						self     ,       **__lowerCAmelCase			)  ->  int:
        super().__init__(**__lowerCAmelCase			)

        requires_backends(self     ,       '''vision'''			)
        self.check_model_type(
            TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
            if self.framework == '''tf'''
            else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING			)

       def __call__(						self     ,       __lowerCAmelCase     ,       **__lowerCAmelCase			)  ->  List[str]:
        return super().__call__(__lowerCAmelCase     ,       **__lowerCAmelCase			)

       def _lowerCAmelCase(						self     ,       **__lowerCAmelCase			)  ->  Optional[Any]:
        lowercase__    :   str						= {}
        if "candidate_labels" in kwargs:
         lowercase__    :   str						= kwargs['''candidate_labels''']
        if "hypothesis_template" in kwargs:
         lowercase__    :   str						= kwargs['''hypothesis_template''']

        return preprocess_params, {}, {}

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase=None     ,       __lowerCAmelCase="This is a photo of {}."			)  ->  Any:
        lowercase__    :   Union[str, Any]						= load_image(__lowerCAmelCase			)
        lowercase__    :   List[str]						= self.image_processor(images=[image]     ,       return_tensors=self.framework			)
        lowercase__    :   Union[str, Any]						= candidate_labels
        lowercase__    :   int						= [hypothesis_template.format(__lowerCAmelCase			) for x in candidate_labels]
        lowercase__    :   Any						= self.tokenizer(__lowerCAmelCase     ,       return_tensors=self.framework     ,       padding=__lowerCAmelCase			)
        lowercase__    :   Any						= [text_inputs]
        return inputs

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase			)  ->  Optional[Any]:
        lowercase__    :   Any						= model_inputs.pop('''candidate_labels'''			)
        lowercase__    :   int						= model_inputs.pop('''text_inputs'''			)
        if isinstance(text_inputs[0]     ,       __lowerCAmelCase			):
         lowercase__    :   Union[str, Any]						= text_inputs[0]
        else:
         # Batching case.
         lowercase__    :   Optional[Any]						= text_inputs[0][0]

        lowercase__    :   Any						= self.model(**__lowerCAmelCase     ,       **__lowerCAmelCase			)

        lowercase__    :   Any						= {
            '''candidate_labels''': candidate_labels,
            '''logits''': outputs.logits_per_image,
        }
        return model_outputs


       def _lowerCAmelCase(						self     ,       __lowerCAmelCase			)  ->  List[str]:
        lowercase__    :   Union[str, Any]						= model_outputs.pop('''candidate_labels'''			)
        lowercase__    :   Optional[int]						= model_outputs['''logits'''][0]
        if self.framework == "pt":
         lowercase__    :   Optional[int]						= logits.softmax(dim=-1			).squeeze(-1			)
         lowercase__    :   Any						= probs.tolist()
         if not isinstance(__lowerCAmelCase     ,       __lowerCAmelCase			):
          lowercase__    :   Dict						= [scores]
        elif self.framework == "tf":
         lowercase__    :   List[Any]						= stable_softmax(__lowerCAmelCase     ,       axis=-1			)
         lowercase__    :   str						= probs.numpy().tolist()
        else:
         raise ValueError(F"""Unsupported framework: {self.framework}"""			)

        lowercase__    :   Optional[int]						= [
            {'''score''': score, '''label''': candidate_label}
            for score, candidate_label in sorted(zip(__lowerCAmelCase     ,       __lowerCAmelCase			)     ,       key=lambda __lowerCAmelCase			: -x[0]			)
        ]
        return result

