



"""simple docstring"""







import gc
import unittest

from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline
from transformers.pipelines import PipelineException
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_gpu,
    slow,
)

from .test_pipelines_common import ANY
@is_pipeline_test
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		lowerCAmelCase			:					Tuple   	=				MODEL_FOR_MASKED_LM_MAPPING
		lowerCAmelCase			:					Any   	=				TF_MODEL_FOR_MASKED_LM_MAPPING







		def 					UpperCAmelCase    (	self						:  List[Any]						)					->		int:






				"""simple docstring"""



				super().tearDown()
				# clean-up as much as possible GPU memory occupied by PyTorch
				gc.collect()
				if is_torch_available():
						import torch

						torch.cuda.empty_cache()







		@require_tf
		def 					UpperCAmelCase    (	self						:  List[str]						)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    Dict				=			pipeline(task='''fill-mask'''			,model='''sshleifer/tiny-distilroberta-base'''			,top_k=2			,framework='''tf'''						)
				lowercase__   :    List[str]				=			unmasker('''My name is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case			,decimals=6						)			,[
				        {'''sequence''': '''My name is grouped''', '''score''': 2.1e-05, '''token''': 38_015, '''token_str''': ''' grouped'''},
				        {'''sequence''': '''My name is accuser''', '''score''': 2.1e-05, '''token''': 25_506, '''token_str''': ''' accuser'''},
				    ]			,)

				lowercase__   :    List[str]				=			unmasker('''The largest city in France is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case			,decimals=6						)			,[
				        {
				            '''sequence''': '''The largest city in France is grouped''',
				            '''score''': 2.1e-05,
				            '''token''': 38_015,
				            '''token_str''': ''' grouped''',
				        },
				        {
				            '''sequence''': '''The largest city in France is accuser''',
				            '''score''': 2.1e-05,
				            '''token''': 25_506,
				            '''token_str''': ''' accuser''',
				        },
				    ]			,)

				lowercase__   :    Any				=			unmasker('''My name is <mask>'''			,targets=[''' Patrick''', ''' Clara''', ''' Teven''']			,top_k=3						)
				self.assertEqual(
				    nested_simplify(_snake_case			,decimals=6						)			,[
				        {'''sequence''': '''My name is Clara''', '''score''': 2e-05, '''token''': 13_606, '''token_str''': ''' Clara'''},
				        {'''sequence''': '''My name is Patrick''', '''score''': 2e-05, '''token''': 3_499, '''token_str''': ''' Patrick'''},
				        {'''sequence''': '''My name is Te''', '''score''': 1.9e-05, '''token''': 2_941, '''token_str''': ''' Te'''},
				    ]			,)







		@require_torch
		def 					UpperCAmelCase    (	self						:  Dict						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			pipeline(task='''fill-mask'''			,model='''sshleifer/tiny-distilroberta-base'''			,top_k=2			,framework='''pt'''						)

				lowercase__   :    Any				=			unmasker('''My name is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case			,decimals=6						)			,[
				        {'''sequence''': '''My name is Maul''', '''score''': 2.2e-05, '''token''': 35_676, '''token_str''': ''' Maul'''},
				        {'''sequence''': '''My name isELS''', '''score''': 2.2e-05, '''token''': 16_416, '''token_str''': '''ELS'''},
				    ]			,)

				lowercase__   :    int				=			unmasker('''The largest city in France is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case			,decimals=6						)			,[
				        {
				            '''sequence''': '''The largest city in France is Maul''',
				            '''score''': 2.2e-05,
				            '''token''': 35_676,
				            '''token_str''': ''' Maul''',
				        },
				        {'''sequence''': '''The largest city in France isELS''', '''score''': 2.2e-05, '''token''': 16_416, '''token_str''': '''ELS'''},
				    ]			,)

				lowercase__   :    Optional[int]				=			unmasker('''My name is <mask>'''			,targets=[''' Patrick''', ''' Clara''', ''' Teven''']			,top_k=3						)
				self.assertEqual(
				    nested_simplify(_snake_case			,decimals=6						)			,[
				        {'''sequence''': '''My name is Patrick''', '''score''': 2.1e-05, '''token''': 3_499, '''token_str''': ''' Patrick'''},
				        {'''sequence''': '''My name is Te''', '''score''': 2e-05, '''token''': 2_941, '''token_str''': ''' Te'''},
				        {'''sequence''': '''My name is Clara''', '''score''': 2e-05, '''token''': 13_606, '''token_str''': ''' Clara'''},
				    ]			,)

				lowercase__   :    Any				=			unmasker('''My name is <mask> <mask>'''			,top_k=2						)

				self.assertEqual(
				    nested_simplify(_snake_case			,decimals=6						)			,[
				        [
				            {
				                '''score''': 2.2e-05,
				                '''token''': 35_676,
				                '''token_str''': ''' Maul''',
				                '''sequence''': '''<s>My name is Maul<mask></s>''',
				            },
				            {'''score''': 2.2e-05, '''token''': 16_416, '''token_str''': '''ELS''', '''sequence''': '''<s>My name isELS<mask></s>'''},
				        ],
				        [
				            {
				                '''score''': 2.2e-05,
				                '''token''': 35_676,
				                '''token_str''': ''' Maul''',
				                '''sequence''': '''<s>My name is<mask> Maul</s>''',
				            },
				            {'''score''': 2.2e-05, '''token''': 16_416, '''token_str''': '''ELS''', '''sequence''': '''<s>My name is<mask>ELS</s>'''},
				        ],
				    ]			,)







		@require_torch_gpu
		def 					UpperCAmelCase    (	self						:  str						)					->		int:






				"""simple docstring"""



				lowercase__   :    List[str]				=			pipeline('''fill-mask'''			,model='''hf-internal-testing/tiny-random-distilbert'''			,device=0			,framework='''pt'''						)

				# convert model to fp16
				pipe.model.half()

				lowercase__   :    Union[str, Any]				=			pipe('''Paris is the [MASK] of France.'''						)
				# We actually don't care about the result, we just want to make sure
				# it works, meaning the float16 tensor got casted back to float32
				# for postprocessing.
				self.assertIsInstance(_snake_case			,_snake_case						)







		@slow
		@require_torch
		def 					UpperCAmelCase    (	self						:  List[Any]						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Dict				=			pipeline(task='''fill-mask'''			,model='''distilroberta-base'''			,top_k=2			,framework='''pt'''						)
				self.run_large_test(_snake_case						)







		@slow
		@require_tf
		def 					UpperCAmelCase    (	self						:  Tuple						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Dict				=			pipeline(task='''fill-mask'''			,model='''distilroberta-base'''			,top_k=2			,framework='''tf'''						)
				self.run_large_test(_snake_case						)







		def 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  Dict						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Tuple				=			unmasker('''My name is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case						)			,[
				        {'''sequence''': '''My name is John''', '''score''': 0.008, '''token''': 610, '''token_str''': ''' John'''},
				        {'''sequence''': '''My name is Chris''', '''score''': 0.007, '''token''': 1_573, '''token_str''': ''' Chris'''},
				    ]			,)
				lowercase__   :    int				=			unmasker('''The largest city in France is <mask>'''						)
				self.assertEqual(
				    nested_simplify(_snake_case						)			,[
				        {
				            '''sequence''': '''The largest city in France is Paris''',
				            '''score''': 0.251,
				            '''token''': 2_201,
				            '''token_str''': ''' Paris''',
				        },
				        {
				            '''sequence''': '''The largest city in France is Lyon''',
				            '''score''': 0.214,
				            '''token''': 12_790,
				            '''token_str''': ''' Lyon''',
				        },
				    ]			,)

				lowercase__   :    Dict				=			unmasker('''My name is <mask>'''			,targets=[''' Patrick''', ''' Clara''', ''' Teven''']			,top_k=3						)
				self.assertEqual(
				    nested_simplify(_snake_case						)			,[
				        {'''sequence''': '''My name is Patrick''', '''score''': 0.005, '''token''': 3_499, '''token_str''': ''' Patrick'''},
				        {'''sequence''': '''My name is Clara''', '''score''': 0.000, '''token''': 13_606, '''token_str''': ''' Clara'''},
				        {'''sequence''': '''My name is Te''', '''score''': 0.000, '''token''': 2_941, '''token_str''': ''' Te'''},
				    ]			,)







		@require_torch
		def 					UpperCAmelCase    (	self						:  Dict						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    str				=			pipeline(task='''fill-mask'''			,model='''sshleifer/tiny-distilroberta-base'''			,framework='''pt'''						)
				lowercase__   :    Tuple				=			None
				lowercase__   :    str				=			None
				self.run_pipeline_test(_snake_case			,[]						)







		@require_tf
		def 					UpperCAmelCase    (	self						:  int						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			pipeline(task='''fill-mask'''			,model='''sshleifer/tiny-distilroberta-base'''			,framework='''tf'''						)
				lowercase__   :    str				=			None
				lowercase__   :    List[Any]				=			None
				self.run_pipeline_test(_snake_case			,[]						)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  Optional[int]			,_snake_case						:  Any			,_snake_case						:  Union[str, Any]						)					->		List[Any]:






				"""simple docstring"""



				if tokenizer is None or tokenizer.mask_token_id is None:
						self.skipTest('''The provided tokenizer has no mask token, (probably reformer or wav2vec2)'''						)

				lowercase__   :    List[str]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)
				lowercase__   :    List[str]				=			[
				    f"""This is another {tokenizer.mask_token} test""",
				]
				return fill_masker, examples







		def 					UpperCAmelCase    (	self						:  List[Any]			,_snake_case						:  Union[str, Any]			,_snake_case						:  str						)					->		Optional[Any]:






				"""simple docstring"""



				lowercase__   :    List[str]				=			fill_masker.tokenizer
				lowercase__   :    List[Any]				=			fill_masker.model

				lowercase__   :    str				=			fill_masker(
				    f"""This is a {tokenizer.mask_token}"""			,)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)

				lowercase__   :    int				=			fill_masker([f"""This is a {tokenizer.mask_token}"""]						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)

				lowercase__   :    Optional[Any]				=			fill_masker([f"""This is a {tokenizer.mask_token}""", f"""Another {tokenizer.mask_token} great test."""]						)
				self.assertEqual(
				    _snake_case			,[
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				    ]			,)

				with self.assertRaises(_snake_case						):
						fill_masker([None]						)
				# No mask_token is not supported
				with self.assertRaises(_snake_case						):
						fill_masker('''This is'''						)

				self.run_test_top_k(_snake_case			,_snake_case						)
				self.run_test_targets(_snake_case			,_snake_case						)
				self.run_test_top_k_targets(_snake_case			,_snake_case						)
				self.fill_mask_with_duplicate_targets_and_top_k(_snake_case			,_snake_case						)
				self.fill_mask_with_multiple_masks(_snake_case			,_snake_case						)







		def 					UpperCAmelCase    (	self						:  List[Any]			,_snake_case						:  Any			,_snake_case						:  Dict						)					->		Tuple:






				"""simple docstring"""



				lowercase__   :    Tuple				=			tokenizer.get_vocab()
				lowercase__   :    List[Any]				=			sorted(vocab.keys()						)[:2]
				# Pipeline argument
				lowercase__   :    Dict				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case			,targets=_snake_case						)
				lowercase__   :    Optional[Any]				=			fill_masker(f"""This is a {tokenizer.mask_token}"""						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)
				lowercase__   :    Tuple				=			{vocab[el] for el in targets}
				self.assertEqual({el['''token'''] for el in outputs}			,_snake_case						)
				lowercase__   :    Tuple				=			[tokenizer.decode([x]						) for x in target_ids]
				self.assertEqual({el['''token_str'''] for el in outputs}			,set(_snake_case						)						)

				# Call argument
				lowercase__   :    str				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)
				lowercase__   :    Union[str, Any]				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,targets=_snake_case						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)
				lowercase__   :    int				=			{vocab[el] for el in targets}
				self.assertEqual({el['''token'''] for el in outputs}			,_snake_case						)
				lowercase__   :    Dict				=			[tokenizer.decode([x]						) for x in target_ids]
				self.assertEqual({el['''token_str'''] for el in outputs}			,set(_snake_case						)						)

				# Score equivalence
				lowercase__   :    Union[str, Any]				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,targets=_snake_case						)
				lowercase__   :    Tuple				=			[top_mask['''token_str'''] for top_mask in outputs]
				lowercase__   :    str				=			[top_mask['''score'''] for top_mask in outputs]

				# For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
				if set(_snake_case						) == set(_snake_case						):
						lowercase__   :    Union[str, Any]				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,targets=_snake_case						)
						lowercase__   :    Optional[int]				=			[top_mask['''score'''] for top_mask in unmasked_targets]
						self.assertEqual(nested_simplify(_snake_case						)			,nested_simplify(_snake_case						)						)

				# Raises with invalid
				with self.assertRaises(_snake_case						):
						lowercase__   :    str				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,targets=[]						)
				# For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised
				if "" not in tokenizer.get_vocab():
						with self.assertRaises(_snake_case						):
								lowercase__   :    str				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,targets=['''''']						)
						with self.assertRaises(_snake_case						):
								lowercase__   :    Tuple				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,targets=''''''						)







		def 					UpperCAmelCase    (	self						:  Tuple			,_snake_case						:  Any			,_snake_case						:  Optional[int]						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case			,top_k=2						)
				lowercase__   :    List[str]				=			fill_masker(f"""This is a {tokenizer.mask_token}"""						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)

				lowercase__   :    Optional[Any]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)
				lowercase__   :    Dict				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,top_k=2						)
				self.assertEqual(
				    _snake_case			,[
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				    ]			,)
				self.assertEqual(nested_simplify(_snake_case						)			,nested_simplify(_snake_case						)						)







		def 					UpperCAmelCase    (	self						:  Any			,_snake_case						:  Optional[int]			,_snake_case						:  List[str]						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    int				=			tokenizer.get_vocab()
				lowercase__   :    Optional[int]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)

				# top_k=2, ntargets=3
				lowercase__   :    Tuple				=			sorted(vocab.keys()						)[:3]
				lowercase__   :    int				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,top_k=2			,targets=_snake_case						)

				# If we use the most probably targets, and filter differently, we should still
				# have the same results
				lowercase__   :    str				=			[el['''token_str'''] for el in sorted(_snake_case			,key=lambda _snake_case						: x["score"]			,reverse=_snake_case						)]
				# For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
				if set(_snake_case						).issubset(_snake_case						):
						lowercase__   :    Dict				=			fill_masker(f"""This is a {tokenizer.mask_token}"""			,top_k=3			,targets=_snake_case						)
						# They should yield exactly the same result
						self.assertEqual(nested_simplify(_snake_case						)			,nested_simplify(_snake_case						)						)







		def 					UpperCAmelCase    (	self						:  List[str]			,_snake_case						:  List[Any]			,_snake_case						:  Any						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)
				lowercase__   :    Tuple				=			tokenizer.get_vocab()
				# String duplicates + id duplicates
				lowercase__   :    Optional[int]				=			sorted(vocab.keys()						)[:3]
				lowercase__   :    List[str]				=			[targets[0], targets[1], targets[0], targets[2], targets[1]]
				lowercase__   :    Optional[int]				=			fill_masker(f"""My name is {tokenizer.mask_token}"""			,targets=_snake_case			,top_k=10						)

				# The target list contains duplicates, so we can't output more
				# than them
				self.assertEqual(len(_snake_case						)			,3						)







		def 					UpperCAmelCase    (	self						:  Optional[Any]			,_snake_case						:  Dict			,_snake_case						:  Union[str, Any]						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    Optional[int]				=			FillMaskPipeline(model=_snake_case			,tokenizer=_snake_case						)

				lowercase__   :    int				=			fill_masker(
				    f"""This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}"""			,top_k=2						)
				self.assertEqual(
				    _snake_case			,[
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				        [
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				            {'''sequence''': ANY(_snake_case						), '''score''': ANY(_snake_case						), '''token''': ANY(_snake_case						), '''token_str''': ANY(_snake_case						)},
				        ],
				    ]			,)





"""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

lowerCAmelCase_   						=   logging.get_logger(__name__)
@add_end_docstrings(A_    )
class        __A       (	A_    ):







		'''simple docstring'''





		def __init__(	self						:  List[str]			,**_snake_case						:  Dict						)					->		List[Any]:






				"""simple docstring"""



				super().__init__(**_snake_case						)

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






				"""simple docstring"""



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







		def 					UpperCAmelCase    (	self						:  Dict			,**_snake_case						:  Optional[int]						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    List[str]				=			{}
				if "candidate_labels" in kwargs:
						lowercase__   :    Any				=			kwargs['''candidate_labels''']
				if "hypothesis_template" in kwargs:
						lowercase__   :    Optional[Any]				=			kwargs['''hypothesis_template''']

				return preprocess_params, {}, {}







		def 					UpperCAmelCase    (	self						:  Optional[Any]			,_snake_case						:  Optional[int]			,_snake_case						:  Dict=None			,_snake_case						:  Union[str, Any]="This is a photo of {}."						)					->		List[str]:






				"""simple docstring"""



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







		def 					UpperCAmelCase    (	self						:  Optional[Any]			,_snake_case						:  Optional[int]						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    Optional[int]				=			model_inputs.pop('''candidate_labels'''						)
				lowercase__   :    Union[str, Any]				=			model_inputs.pop('''text_inputs'''						)
				if isinstance(text_inputs[0]			,_snake_case						):
						lowercase__   :    List[str]				=			text_inputs[0]
				else:
						# Batching case.
						lowercase__   :    int				=			text_inputs[0][0]

				lowercase__   :    Tuple				=			self.model(**_snake_case			,**_snake_case						)

				lowercase__   :    Union[str, Any]				=			{
				    '''candidate_labels''': candidate_labels,
				    '''logits''': outputs.logits_per_image,
				}
				return model_outputs







		def 					UpperCAmelCase    (	self						:  Any			,_snake_case						:  Tuple						)					->		Any:






				"""simple docstring"""



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

				lowercase__   :    Union[str, Any]				=			[
				    {'''score''': score, '''label''': candidate_label}
				    for score, candidate_label in sorted(zip(_snake_case			,_snake_case						)			,key=lambda _snake_case						: -x[0]						)
				]
				return result
