


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   :					Tuple	=      logging.get_logger(__name__)







@add_end_docstrings(lowercase__)
class     _lowercase				(							lowercase__):


       """simple docstring"""




       def __init__(     self							:    str      ,   **__lowerCamelCase							:    List[Any] ):


             '''simple docstring'''



             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							:    Dict      ,   __lowerCamelCase							:    Union[str, List[str], "Image", List["Image"]]      ,   **__lowerCamelCase							:    Optional[int] ):


             '''simple docstring'''



             return super().__call__(__lowerCamelCase      ,   **__lowerCamelCase )





       def 		lowerCAmelCase  (     self							:    int      ,   **__lowerCamelCase							:    Dict ):


             '''simple docstring'''



             lowerCamelCase__	:       Union[str, Any]					=						{}
             if "candidate_labels" in kwargs:
                   lowerCamelCase__	:       Optional[Any]					=						kwargs["candidate_labels"]
             if "hypothesis_template" in kwargs:
                   lowerCamelCase__	:       str					=						kwargs["hypothesis_template"]

             return preprocess_params, {}, {}





       def 		lowerCAmelCase  (     self							:    str      ,   __lowerCamelCase							:    Tuple      ,   __lowerCamelCase							:    List[Any]=None      ,   __lowerCamelCase							:    Optional[Any]="This is a photo of {}." ):


             '''simple docstring'''



             lowerCamelCase__	:       int					=						load_image(__lowerCamelCase )
             lowerCamelCase__	:       List[str]					=						self.image_processor(images=[image]      ,   return_tensors=self.framework )
             lowerCamelCase__	:       List[Any]					=						candidate_labels
             lowerCamelCase__	:       Any					=						[hypothesis_template.format(__lowerCamelCase ) for x in candidate_labels]
             lowerCamelCase__	:       int					=						self.tokenizer(__lowerCamelCase      ,   return_tensors=self.framework      ,   padding=__lowerCamelCase )
             lowerCamelCase__	:       str					=						[text_inputs]
             return inputs





       def 		lowerCAmelCase  (     self							:    int      ,   __lowerCamelCase							:    int ):


             '''simple docstring'''



             lowerCamelCase__	:       Optional[Any]					=						model_inputs.pop("candidate_labels" )
             lowerCamelCase__	:       List[str]					=						model_inputs.pop("text_inputs" )
             if isinstance(text_inputs[0]      ,   __lowerCamelCase ):
                   lowerCamelCase__	:       Optional[Any]					=						text_inputs[0]
             else:
                   # Batching case.
                   lowerCamelCase__	:       Union[str, Any]					=						text_inputs[0][0]

             lowerCamelCase__	:       Optional[int]					=						self.model(**__lowerCamelCase      ,   **__lowerCamelCase )

             lowerCamelCase__	:       List[Any]					=						{
                 "candidate_labels": candidate_labels,
                 "logits": outputs.logits_per_image,
             }
             return model_outputs





       def 		lowerCAmelCase  (     self							:    Dict      ,   __lowerCamelCase							:    Optional[int] ):


             '''simple docstring'''



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

             lowerCamelCase__	:       str					=						[
                 {"score": score, "label": candidate_label}
                 for score, candidate_label in sorted(zip(__lowerCamelCase      ,   __lowerCamelCase )      ,   key=lambda __lowerCamelCase : -x[0] )
             ]
             return result






import unittest

from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
@require_sentencepiece
@slow  # see https://github.com/huggingface/transformers/issues/11457
class     _lowercase				(							lowercase__							,						unittest.TestCase):


       """simple docstring"""




       A__      		=    BarthezTokenizer
       A__      		=    BarthezTokenizerFast
       A__      		=    True
       A__      		=    True





       def 		lowerCAmelCase  (     self							:    int ):


             '''simple docstring'''



             super().setUp()

             lowerCamelCase__	:       List[str]					=						BarthezTokenizerFast.from_pretrained("moussaKam/mbarthez" )
             tokenizer.save_pretrained(self.tmpdirname )
             tokenizer.save_pretrained(self.tmpdirname      ,   legacy_format=__lowerCamelCase )
             lowerCamelCase__	:       Tuple					=						tokenizer





       def 		lowerCAmelCase  (     self							:    Dict ):


             '''simple docstring'''



             lowerCamelCase__	:       Any					=						"<pad>"
             lowerCamelCase__	:       Tuple					=						1

             self.assertEqual(self.get_tokenizer()._convert_token_to_id(__lowerCamelCase )      ,   __lowerCamelCase )
             self.assertEqual(self.get_tokenizer()._convert_id_to_token(__lowerCamelCase )      ,   __lowerCamelCase )





       def 		lowerCAmelCase  (     self							:    Dict ):


             '''simple docstring'''



             lowerCamelCase__	:       Dict					=						list(self.get_tokenizer().get_vocab().keys() )

             self.assertEqual(vocab_keys[0]      ,   "<s>" )
             self.assertEqual(vocab_keys[1]      ,   "<pad>" )
             self.assertEqual(vocab_keys[-1]      ,   "<mask>" )
             self.assertEqual(len(__lowerCamelCase )      ,   101122 )





       def 		lowerCAmelCase  (     self							:    Union[str, Any] ):


             '''simple docstring'''



             self.assertEqual(self.get_tokenizer().vocab_size      ,   101122 )





       @require_torch
       def 		lowerCAmelCase  (     self							:    int ):


             '''simple docstring'''



             lowerCamelCase__	:       int					=						["A long paragraph for summarization.", "Another paragraph for summarization."]
             lowerCamelCase__	:       str					=						[0, 57, 3018, 70307, 91, 2]

             lowerCamelCase__	:       Tuple					=						self.tokenizer(
                 __lowerCamelCase      ,   max_length=len(__lowerCamelCase )      ,   padding=__lowerCamelCase      ,   truncation=__lowerCamelCase      ,   return_tensors="pt" )
             self.assertIsInstance(__lowerCamelCase      ,   __lowerCamelCase )

             self.assertEqual((2, 6)      ,   batch.input_ids.shape )
             self.assertEqual((2, 6)      ,   batch.attention_mask.shape )
             lowerCamelCase__	:       Any					=						batch.input_ids.tolist()[0]
             self.assertListEqual(__lowerCamelCase      ,   __lowerCamelCase )





       def 		lowerCAmelCase  (     self							:    Any ):


             '''simple docstring'''



             if not self.test_rust_tokenizer:
                   return

             lowerCamelCase__	:       Any					=						self.get_tokenizer()
             lowerCamelCase__	:       Tuple					=						self.get_rust_tokenizer()

             lowerCamelCase__	:       Union[str, Any]					=						"I was born in 92000, and this is falsé."

             lowerCamelCase__	:       Dict					=						tokenizer.tokenize(__lowerCamelCase )
             lowerCamelCase__	:       Optional[int]					=						rust_tokenizer.tokenize(__lowerCamelCase )
             self.assertListEqual(__lowerCamelCase      ,   __lowerCamelCase )

             lowerCamelCase__	:       Tuple					=						tokenizer.encode(__lowerCamelCase      ,   add_special_tokens=__lowerCamelCase )
             lowerCamelCase__	:       List[Any]					=						rust_tokenizer.encode(__lowerCamelCase      ,   add_special_tokens=__lowerCamelCase )
             self.assertListEqual(__lowerCamelCase      ,   __lowerCamelCase )

             lowerCamelCase__	:       List[str]					=						self.get_rust_tokenizer()
             lowerCamelCase__	:       Optional[Any]					=						tokenizer.encode(__lowerCamelCase )
             lowerCamelCase__	:       List[Any]					=						rust_tokenizer.encode(__lowerCamelCase )
             self.assertListEqual(__lowerCamelCase      ,   __lowerCamelCase )





       @slow
       def 		lowerCAmelCase  (     self							:    Dict ):


             '''simple docstring'''



             lowerCamelCase__	:       int					=						{"input_ids": [[0, 490, 14328, 4507, 354, 47, 43669, 95, 25, 78117, 20215, 19779, 190, 22, 400, 4, 35343, 80310, 603, 86, 24937, 105, 33438, 94762, 196, 39642, 7, 15, 15933, 173, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 10534, 87, 25, 66, 3358, 196, 55289, 8, 82961, 81, 2204, 75203, 7, 15, 763, 12956, 216, 178, 14328, 9595, 1377, 69693, 7, 448, 71021, 196, 18106, 1437, 13974, 108, 9083, 4, 49315, 7, 39, 86, 1326, 2793, 46333, 4, 448, 196, 74588, 7, 49315, 7, 39, 21, 822, 38470, 74, 21, 66723, 62480, 8, 22050, 5, 2]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
             # fmt: on

             # moussaKam/mbarthez is a french model. So we also use french texts.
             lowerCamelCase__	:       List[str]					=						[
                 "Le transformeur est un modèle d'apprentissage profond introduit en 2017, "
                 "utilisé principalement dans le domaine du traitement automatique des langues (TAL).",
                 "À l'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus "
                 "pour gérer des données séquentielles, telles que le langage naturel, pour des tâches "
                 "telles que la traduction et la synthèse de texte.",
             ]

             self.tokenizer_integration_test_util(
                 expected_encoding=__lowerCamelCase      ,   model_name="moussaKam/mbarthez"      ,   revision="c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6"      ,   sequences=__lowerCamelCase      ,   )


