




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 ):
  _UpperCamelCase  :     Optional[Any]					=     MODEL_FOR_MASKED_LM_MAPPING
  _UpperCamelCase  :     Union[str, Any]					=     TF_MODEL_FOR_MASKED_LM_MAPPING






  def    __a     (    self						:      int )						->       Optional[Any]:






       """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    __a     (    self						:      str )						->       Any:






       """simple docstring"""







       lowercase   :  List[Any]        =					pipeline(task='''fill-mask''' ,		model='''sshleifer/tiny-distilroberta-base''' ,		top_k=2 ,		framework='''tf''' )
       lowercase   :  Union[str, Any]        =					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   :  Union[str, Any]        =					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   :  List[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    __a     (    self						:      Dict )						->       Union[str, Any]:






       """simple docstring"""







       lowercase   :  Tuple        =					pipeline(task='''fill-mask''' ,		model='''sshleifer/tiny-distilroberta-base''' ,		top_k=2 ,		framework='''pt''' )

       lowercase   :  Dict        =					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   :  Optional[Any]        =					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   :  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    __a     (    self						:      Dict )						->       Dict:






       """simple docstring"""







       lowercase   :  int        =					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    __a     (    self						:      str )						->       Tuple:






       """simple docstring"""







       lowercase   :  Optional[int]        =					pipeline(task='''fill-mask''' ,		model='''distilroberta-base''' ,		top_k=2 ,		framework='''pt''' )
       self.run_large_test(snake_case__ )






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






       """simple docstring"""







       lowercase   :  Optional[Any]        =					pipeline(task='''fill-mask''' ,		model='''distilroberta-base''' ,		top_k=2 ,		framework='''tf''' )
       self.run_large_test(snake_case__ )






  def    __a     (    self						:      List[str] ,		_A						:      Any )						->       List[Any]:






       """simple docstring"""







       lowercase   :  Any        =					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   :  List[str]        =					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   :  List[Any]        =					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    __a     (    self						:      Union[str, Any] )						->       Dict:






       """simple docstring"""







       lowercase   :  List[Any]        =					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    __a     (    self						:      List[str] )						->       Dict:






       """simple docstring"""







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






  def    __a     (    self						:      int ,		_A						:      Dict ,		_A						:      int ,		_A						:      Tuple )						->       str:






       """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   :  Union[str, Any]        =					FillMaskPipeline(model=snake_case__ ,		tokenizer=snake_case__ )
       lowercase   :  List[str]        =					[
           f"""This is another {tokenizer.mask_token} test""",
       ]
       return fill_masker, examples






  def    __a     (    self						:      Union[str, Any] ,		_A						:      List[Any] ,		_A						:      List[str] )						->       Any:






       """simple docstring"""







       lowercase   :  Union[str, Any]        =					fill_masker.tokenizer
       lowercase   :  Any        =					fill_masker.model

       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__ )},
               {'''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   :  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   :  Union[str, 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    __a     (    self						:      List[Any] ,		_A						:      Any ,		_A						:      Optional[Any] )						->       List[str]:






       """simple docstring"""







       lowercase   :  Tuple        =					tokenizer.get_vocab()
       lowercase   :  Tuple        =					sorted(vocab.keys() )[:2]
       # Pipeline argument
       lowercase   :  List[str]        =					FillMaskPipeline(model=snake_case__ ,		tokenizer=snake_case__ ,		targets=snake_case__ )
       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__ )},
           ] ,		)
       lowercase   :  Dict        =					{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__ ) )

       # Call argument
       lowercase   :  Tuple        =					FillMaskPipeline(model=snake_case__ ,		tokenizer=snake_case__ )
       lowercase   :  List[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   :  Tuple        =					{vocab[el] for el in targets}
       self.assertEqual({el['''token'''] for el in outputs} ,		snake_case__ )
       lowercase   :  Union[str, Any]        =					[tokenizer.decode([x] ) for x in target_ids]
       self.assertEqual({el['''token_str'''] for el in outputs} ,		set(snake_case__ ) )

       # Score equivalence
       lowercase   :  Any        =					fill_masker(f"""This is a {tokenizer.mask_token}""" ,		targets=snake_case__ )
       lowercase   :  Optional[Any]        =					[top_mask["token_str"] for top_mask in outputs]
       lowercase   :  Optional[Any]        =					[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   :  str        =					fill_masker(f"""This is a {tokenizer.mask_token}""" ,		targets=snake_case__ )
            lowercase   :  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   :  Any        =					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   :  List[Any]        =					fill_masker(f"""This is a {tokenizer.mask_token}""" ,		targets='''''' )






  def    __a     (    self						:      Optional[Any] ,		_A						:      Any ,		_A						:      Union[str, Any] )						->       str:






       """simple docstring"""







       lowercase   :  List[str]        =					FillMaskPipeline(model=snake_case__ ,		tokenizer=snake_case__ ,		top_k=2 )
       lowercase   :  Union[str, 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   :  List[str]        =					FillMaskPipeline(model=snake_case__ ,		tokenizer=snake_case__ )
       lowercase   :  int        =					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    __a     (    self						:      List[str] ,		_A						:      List[Any] ,		_A						:      int )						->       Optional[Any]:






       """simple docstring"""







       lowercase   :  List[str]        =					tokenizer.get_vocab()
       lowercase   :  Optional[int]        =					FillMaskPipeline(model=snake_case__ ,		tokenizer=snake_case__ )

       # top_k=2, ntargets=3
       lowercase   :  Any        =					sorted(vocab.keys() )[:3]
       lowercase   :  Tuple        =					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   :  Union[str, Any]        =					[el["token_str"] for el in sorted(snake_case__ ,		key=lambda _A : 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   :  Tuple        =					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    __a     (    self						:      Any ,		_A						:      Any ,		_A						:      List[Any] )						->       Optional[int]:






       """simple docstring"""







       lowercase   :  Union[str, Any]        =					FillMaskPipeline(model=snake_case__ ,		tokenizer=snake_case__ )
       lowercase   :  str        =					tokenizer.get_vocab()
       # String duplicates + id duplicates
       lowercase   :  Dict        =					sorted(vocab.keys() )[:3]
       lowercase   :  List[Any]        =					[targets[0], targets[1], targets[0], targets[2], targets[1]]
       lowercase   :  Dict        =					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    __a     (    self						:      Optional[Any] ,		_A						:      Union[str, Any] ,		_A						:      str )						->       Optional[Any]:






       """simple docstring"""







       lowercase   :  str        =					FillMaskPipeline(model=snake_case__ ,		tokenizer=snake_case__ )

       lowercase   :  List[Any]        =					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__ )},
               ],
           ] ,		)

from math import factorial


def      UpperCamelCase   (       __lowerCamelCase	:					int							,   __lowerCamelCase	:					int							,   __lowerCamelCase	:					float       ):
       if successes > trials:
              raise ValueError("successes must be lower or equal to trials"       )
       if trials < 0 or successes < 0:
              raise ValueError("the function is defined for non-negative integers"       )
       if not isinstance(__lowerCamelCase							,   __lowerCamelCase       ) or not isinstance(__lowerCamelCase							,   __lowerCamelCase       ):
              raise ValueError("the function is defined for non-negative integers"       )
       if not 0 < prob < 1:
              raise ValueError("prob has to be in range of 1 - 0"       )
       snake_case			:  Tuple     							= (prob**successes) * ((1 - prob) ** (trials - successes))
       # Calculate the binomial coefficient: n! / k!(n-k)!
       snake_case			:  str     							= float(factorial(__lowerCamelCase       )       )
       coefficient /= factorial(__lowerCamelCase       ) * factorial(trials - successes       )
       return probability * coefficient


if __name__ == "__main__":
   from doctest import testmod

   testmod()
   print("""Probability of 2 successes out of 4 trails""")
   print("""with probability of 0.75 is:""", end=""" """)
   print(binomial_distribution(2, 4, 0.75))





