






import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import BlenderbotConfig, is_flax_available
from transformers.testing_utils import jax_device, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor


if is_flax_available():
   import os

   # The slow tests are often failing with OOM error on GPU
   # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
   # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
   SCREAMING_SNAKE_CASE 							=						"platform"

   import jax
   import jax.numpy as jnp

   from transformers import BlenderbotTokenizer
   from transformers.models.blenderbot.modeling_flax_blenderbot import (
       FlaxBlenderbotForConditionalGeneration,
       FlaxBlenderbotModel,
       shift_tokens_right,
   )

def      _lowerCamelCase	(						__A    :							List[str]		,		__A    :							Any		,		__A    :							Union[str, Any]=None		,		__A    :							Tuple=None		,		__A    :							Union[str, Any]=None		,		__A    :							List[str]=None		,		__A    :							str=None		,		__A    :							Dict=None		,		)      ->					Any:




    if attention_mask is None:
        _UpperCAmelCase				:      Optional[Any]			  =				np.where(input_ids != config.pad_token_id		,		1		,		0 )
    if decoder_attention_mask is None:
        _UpperCAmelCase				:      Union[str, Any]			  =				np.where(decoder_input_ids != config.pad_token_id		,		1		,		0 )
    if head_mask is None:
        _UpperCAmelCase				:      Dict			  =				np.ones((config.encoder_layers, config.encoder_attention_heads) )
    if decoder_head_mask is None:
        _UpperCAmelCase				:      str			  =				np.ones((config.decoder_layers, config.decoder_attention_heads) )
    if cross_attn_head_mask is None:
        _UpperCAmelCase				:      List[str]			  =				np.ones((config.decoder_layers, config.decoder_attention_heads) )
    return {
        "input_ids": input_ids,
        "decoder_input_ids": decoder_input_ids,
        "attention_mask": attention_mask,
        "decoder_attention_mask": attention_mask,
    }
class 							A_    :






 '''simple docstring'''




 def __init__(	self							,  _A							,  _A=13							,  _A=7							,  _A=True							,  _A=False							,  _A=99							,  _A=16							,  _A=2							,  _A=4							,  _A=4							,  _A="gelu"							,  _A=0.1							,  _A=0.1							,  _A=32							,  _A=2							,  _A=1							,  _A=0							,  _A=0.02							,  ) ->		str:

     """simple docstring"""



     _UpperCAmelCase				:      List[str]			  =				parent
     _UpperCAmelCase				:      int			  =				batch_size
     _UpperCAmelCase				:      Tuple			  =				seq_length
     _UpperCAmelCase				:      Tuple			  =				is_training
     _UpperCAmelCase				:      Dict			  =				use_labels
     _UpperCAmelCase				:      Dict			  =				vocab_size
     _UpperCAmelCase				:      Any			  =				hidden_size
     _UpperCAmelCase				:      Tuple			  =				num_hidden_layers
     _UpperCAmelCase				:      Tuple			  =				num_attention_heads
     _UpperCAmelCase				:      Optional[Any]			  =				intermediate_size
     _UpperCAmelCase				:      str			  =				hidden_act
     _UpperCAmelCase				:      Any			  =				hidden_dropout_prob
     _UpperCAmelCase				:      Dict			  =				attention_probs_dropout_prob
     _UpperCAmelCase				:      Optional[int]			  =				max_position_embeddings
     _UpperCAmelCase				:      int			  =				eos_token_id
     _UpperCAmelCase				:      Tuple			  =				pad_token_id
     _UpperCAmelCase				:      Union[str, Any]			  =				bos_token_id
     _UpperCAmelCase				:      List[str]			  =				initializer_range


 def  snake_case__				(	self) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				np.clip(ids_tensor([self.batch_size, self.seq_length - 1]							,  self.vocab_size)							,  3							,  self.vocab_size)
     _UpperCAmelCase				:      Tuple			  =				np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)							,  dtype=np.intaa))							,  -1)

     _UpperCAmelCase				:      int			  =				shift_tokens_right(__lowerCamelCase							,  1							,  2)

     _UpperCAmelCase				:      Tuple			  =				BlenderbotConfig(
         vocab_size=self.vocab_size							,  d_model=self.hidden_size							,  encoder_layers=self.num_hidden_layers							,  decoder_layers=self.num_hidden_layers							,  encoder_attention_heads=self.num_attention_heads							,  decoder_attention_heads=self.num_attention_heads							,  encoder_ffn_dim=self.intermediate_size							,  decoder_ffn_dim=self.intermediate_size							,  dropout=self.hidden_dropout_prob							,  attention_dropout=self.attention_probs_dropout_prob							,  max_position_embeddings=self.max_position_embeddings							,  eos_token_id=self.eos_token_id							,  bos_token_id=self.bos_token_id							,  pad_token_id=self.pad_token_id							,  initializer_range=self.initializer_range							,  use_cache=__lowerCamelCase							,  )
     _UpperCAmelCase				:      Tuple			  =				prepare_blenderbot_inputs_dict(__lowerCamelCase							,  __lowerCamelCase							,  __lowerCamelCase)
     return config, inputs_dict


 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				self.prepare_config_and_inputs()
     return config, inputs_dict


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

     """simple docstring"""



     _UpperCAmelCase				:      Any			  =				20
     _UpperCAmelCase				:      Any			  =				model_class_name(__lowerCamelCase)

     _UpperCAmelCase				:      Tuple			  =				model.encode(inputs_dict['''input_ids'''])

     _UpperCAmelCase				:      int			  =				(
         inputs_dict["decoder_input_ids"],
         inputs_dict["decoder_attention_mask"],
     )

     _UpperCAmelCase				:      str			  =				model.init_cache(decoder_input_ids.shape[0]							,  __lowerCamelCase							,  __lowerCamelCase)
     _UpperCAmelCase				:      List[Any]			  =				jnp.ones((decoder_input_ids.shape[0], max_decoder_length)							,  dtype='''i4''')

     _UpperCAmelCase				:      Dict			  =				jnp.broadcast_to(
         jnp.arange(decoder_input_ids.shape[-1] - 1)[None, :]							,  (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1)							,  )
     _UpperCAmelCase				:      Dict			  =				model.decode(
         decoder_input_ids[:, :-1]							,  __lowerCamelCase							,  decoder_attention_mask=__lowerCamelCase							,  past_key_values=__lowerCamelCase							,  decoder_position_ids=__lowerCamelCase							,  )

     _UpperCAmelCase				:      str			  =				jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]]							,  dtype='''i4''')
     _UpperCAmelCase				:      Union[str, Any]			  =				model.decode(
         decoder_input_ids[:, -1:]							,  __lowerCamelCase							,  decoder_attention_mask=__lowerCamelCase							,  past_key_values=outputs_cache.past_key_values							,  decoder_position_ids=__lowerCamelCase							,  )

     _UpperCAmelCase				:      Dict			  =				model.decode(__lowerCamelCase							,  __lowerCamelCase)

     _UpperCAmelCase				:      str			  =				np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
     self.parent.assertTrue(diff < 1e-3							,  msg=f'''Max diff is {diff}''')


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

     """simple docstring"""



     _UpperCAmelCase				:      List[Any]			  =				20
     _UpperCAmelCase				:      Optional[int]			  =				model_class_name(__lowerCamelCase)

     _UpperCAmelCase				:      List[str]			  =				model.encode(inputs_dict['''input_ids'''])

     _UpperCAmelCase				:      int			  =				(
         inputs_dict["decoder_input_ids"],
         inputs_dict["decoder_attention_mask"],
     )

     _UpperCAmelCase				:      Optional[Any]			  =				jnp.concatenate(
         [
             decoder_attention_mask,
             jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1])),
         ]							,  axis=-1							,  )

     _UpperCAmelCase				:      Tuple			  =				model.init_cache(decoder_input_ids.shape[0]							,  __lowerCamelCase							,  __lowerCamelCase)
     _UpperCAmelCase				:      Dict			  =				jnp.broadcast_to(
         jnp.arange(decoder_input_ids.shape[-1] - 1)[None, :]							,  (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1)							,  )

     _UpperCAmelCase				:      Optional[int]			  =				model.decode(
         decoder_input_ids[:, :-1]							,  __lowerCamelCase							,  decoder_attention_mask=__lowerCamelCase							,  past_key_values=__lowerCamelCase							,  decoder_position_ids=__lowerCamelCase							,  )
     _UpperCAmelCase				:      Dict			  =				jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]]							,  dtype='''i4''')
     _UpperCAmelCase				:      Dict			  =				model.decode(
         decoder_input_ids[:, -1:]							,  __lowerCamelCase							,  past_key_values=outputs_cache.past_key_values							,  decoder_attention_mask=__lowerCamelCase							,  decoder_position_ids=__lowerCamelCase							,  )

     _UpperCAmelCase				:      Dict			  =				model.decode(__lowerCamelCase							,  __lowerCamelCase							,  decoder_attention_mask=__lowerCamelCase)

     _UpperCAmelCase				:      Tuple			  =				np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
     self.parent.assertTrue(diff < 1e-3							,  msg=f'''Max diff is {diff}''')



@require_flax
class 							A_    (						unittest.TestCase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Union[str, Any] =					9_9


 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[Any]			  =				np.array(
         [
             [71, 82, 18, 33, 46, 91, 2],
             [68, 34, 26, 58, 30, 82, 2],
             [5, 97, 17, 39, 94, 40, 2],
             [76, 83, 94, 25, 70, 78, 2],
             [87, 59, 41, 35, 48, 66, 2],
             [55, 13, 16, 58, 5, 2, 1],  # note padding
             [64, 27, 31, 51, 12, 75, 2],
             [52, 64, 86, 17, 83, 39, 2],
             [48, 61, 9, 24, 71, 82, 2],
             [26, 1, 60, 48, 22, 13, 2],
             [21, 5, 62, 28, 14, 76, 2],
             [45, 98, 37, 86, 59, 48, 2],
             [70, 70, 50, 9, 28, 0, 2],
         ]							,  dtype=np.intaa							,  )

     _UpperCAmelCase				:      Optional[Any]			  =				input_ids.shape[0]
     _UpperCAmelCase				:      str			  =				BlenderbotConfig(
         vocab_size=self.vocab_size							,  d_model=24							,  encoder_layers=2							,  decoder_layers=2							,  encoder_attention_heads=2							,  decoder_attention_heads=2							,  encoder_ffn_dim=32							,  decoder_ffn_dim=32							,  max_position_embeddings=48							,  eos_token_id=2							,  pad_token_id=1							,  bos_token_id=0							,  )
     return config, input_ids, batch_size


 def  snake_case__				(	self) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      List[str]			  =				self._get_config_and_data()
     _UpperCAmelCase				:      Optional[Any]			  =				FlaxBlenderbotForConditionalGeneration(__lowerCamelCase)
     _UpperCAmelCase				:      Any			  =				lm_model(input_ids=__lowerCamelCase)
     _UpperCAmelCase				:      str			  =				(batch_size, input_ids.shape[1], config.vocab_size)
     self.assertEqual(outputs['''logits'''].shape							,  __lowerCamelCase)


 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				BlenderbotConfig(
         vocab_size=self.vocab_size							,  d_model=14							,  encoder_layers=2							,  decoder_layers=2							,  encoder_attention_heads=2							,  decoder_attention_heads=2							,  encoder_ffn_dim=8							,  decoder_ffn_dim=8							,  max_position_embeddings=48							,  )
     _UpperCAmelCase				:      Optional[int]			  =				FlaxBlenderbotForConditionalGeneration(__lowerCamelCase)
     _UpperCAmelCase				:      str			  =				np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]]							,  dtype=np.intaa)
     _UpperCAmelCase				:      List[str]			  =				np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]]							,  dtype=np.intaa)
     _UpperCAmelCase				:      int			  =				lm_model(input_ids=__lowerCamelCase							,  decoder_input_ids=__lowerCamelCase)
     _UpperCAmelCase				:      List[Any]			  =				(*summary.shape, config.vocab_size)
     self.assertEqual(outputs['''logits'''].shape							,  __lowerCamelCase)


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]]							,  dtype=np.intaa)
     _UpperCAmelCase				:      int			  =				shift_tokens_right(__lowerCamelCase							,  1							,  2)
     _UpperCAmelCase				:      List[Any]			  =				np.equal(__lowerCamelCase							,  1).astype(np.floataa).sum()
     _UpperCAmelCase				:      Dict			  =				np.equal(__lowerCamelCase							,  1).astype(np.floataa).sum()
     self.assertEqual(shifted.shape							,  input_ids.shape)
     self.assertEqual(__lowerCamelCase							,  n_pad_before - 1)
     self.assertTrue(np.equal(shifted[:, 0]							,  2).all())



@require_flax
class 							A_    (						lowercase__ ,   unittest.TestCase ,   lowercase__			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Tuple =					True
 _SCREAMING_SNAKE_CASE							:			str =					(
     (
         FlaxBlenderbotModel,
         FlaxBlenderbotForConditionalGeneration,
     )
     if is_flax_available()
     else ()
 )
 _SCREAMING_SNAKE_CASE							:			List[Any] =					(FlaxBlenderbotForConditionalGeneration,) if is_flax_available() else ()


 def  snake_case__				(	self) ->		Tuple:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				FlaxBlenderbotModelTester(self)


 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				self.model_tester.prepare_config_and_inputs()
     for model_class in self.all_model_classes:
         self.model_tester.check_use_cache_forward(__lowerCamelCase							,  __lowerCamelCase							,  __lowerCamelCase)


 def  snake_case__				(	self) ->		Union[str, Any]:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				self.model_tester.prepare_config_and_inputs()
     for model_class in self.all_model_classes:
         self.model_tester.check_use_cache_forward_with_attn_mask(__lowerCamelCase							,  __lowerCamelCase							,  __lowerCamelCase)


 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         with self.subTest(model_class.__name__):
             _UpperCAmelCase				:      Any			  =				self._prepare_for_class(__lowerCamelCase							,  __lowerCamelCase)
             _UpperCAmelCase				:      Union[str, Any]			  =				model_class(__lowerCamelCase)

             @jax.jit
             def encode_jitted(_A							,  _A=None							,  **_A):
                 return model.encode(input_ids=__lowerCamelCase							,  attention_mask=__lowerCamelCase)

             with self.subTest('''JIT Enabled'''):
                 _UpperCAmelCase				:      Optional[Any]			  =				encode_jitted(**__lowerCamelCase).to_tuple()

             with self.subTest('''JIT Disabled'''):
                 with jax.disable_jit():
                     _UpperCAmelCase				:      List[Any]			  =				encode_jitted(**__lowerCamelCase).to_tuple()

             self.assertEqual(len(__lowerCamelCase)							,  len(__lowerCamelCase))
             for jitted_output, output in zip(__lowerCamelCase							,  __lowerCamelCase):
                 self.assertEqual(jitted_output.shape							,  output.shape)


 def  snake_case__				(	self) ->		Tuple:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         with self.subTest(model_class.__name__):
             _UpperCAmelCase				:      Union[str, Any]			  =				model_class(__lowerCamelCase)
             _UpperCAmelCase				:      str			  =				model.encode(inputs_dict['''input_ids''']							,  inputs_dict['''attention_mask'''])

             _UpperCAmelCase				:      List[Any]			  =				{
                 "decoder_input_ids": inputs_dict["decoder_input_ids"],
                 "decoder_attention_mask": inputs_dict["decoder_attention_mask"],
                 "encoder_outputs": encoder_outputs,
             }

             @jax.jit
             def decode_jitted(_A							,  _A							,  _A):
                 return model.decode(
                     decoder_input_ids=__lowerCamelCase							,  decoder_attention_mask=__lowerCamelCase							,  encoder_outputs=__lowerCamelCase							,  )

             with self.subTest('''JIT Enabled'''):
                 _UpperCAmelCase				:      Optional[int]			  =				decode_jitted(**__lowerCamelCase).to_tuple()

             with self.subTest('''JIT Disabled'''):
                 with jax.disable_jit():
                     _UpperCAmelCase				:      Any			  =				decode_jitted(**__lowerCamelCase).to_tuple()

             self.assertEqual(len(__lowerCamelCase)							,  len(__lowerCamelCase))
             for jitted_output, output in zip(__lowerCamelCase							,  __lowerCamelCase):
                 self.assertEqual(jitted_output.shape							,  output.shape)


 @slow
 def  snake_case__				(	self) ->		Tuple:

     """simple docstring"""



     for model_class_name in self.all_model_classes:
         _UpperCAmelCase				:      str			  =				model_class_name.from_pretrained('''facebook/blenderbot-400M-distill''')
         # FlaxBlenderbotForSequenceClassification expects eos token in input_ids
         _UpperCAmelCase				:      int			  =				np.ones((1, 1)) * model.config.eos_token_id
         _UpperCAmelCase				:      Optional[int]			  =				model(__lowerCamelCase)
         self.assertIsNotNone(__lowerCamelCase)


 @unittest.skipUnless(jax_device != '''cpu'''							,  '''3B test too slow on CPU.''')
 @slow
 def  snake_case__				(	self) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      List[Any]			  =				{"num_beams": 1, "early_stopping": True, "min_length": 15, "max_length": 25}
     _UpperCAmelCase				:      Any			  =				{"skip_special_tokens": True, "clean_up_tokenization_spaces": True}

     _UpperCAmelCase				:      Dict			  =				FlaxBlenderbotForConditionalGeneration.from_pretrained('''facebook/blenderbot-3B'''							,  from_pt=__lowerCamelCase)
     _UpperCAmelCase				:      Dict			  =				BlenderbotTokenizer.from_pretrained('''facebook/blenderbot-3B''')

     _UpperCAmelCase				:      Dict			  =				["Sam"]
     _UpperCAmelCase				:      List[Any]			  =				tokenizer(__lowerCamelCase							,  return_tensors='''jax''')

     _UpperCAmelCase				:      Any			  =				model.generate(**__lowerCamelCase							,  **__lowerCamelCase)
     _UpperCAmelCase				:      List[Any]			  =				"Sam is a great name. It means \"sun\" in Gaelic."

     _UpperCAmelCase				:      int			  =				tokenizer.batch_decode(__lowerCamelCase							,  **__lowerCamelCase)
     assert generated_txt[0].strip() == tgt_text












import collections
import json
import math
import os
import re
import time
from fnmatch import fnmatch
from typing import Dict

import requests
from slack_sdk import WebClient


SCREAMING_SNAKE_CASE 							=						WebClient(token=os.environ['CI_SLACK_BOT_TOKEN'])

def      _lowerCamelCase	(						__A    :							Optional[Any] )      ->					int:
    _UpperCAmelCase				:      Optional[int]			  =				test_results.split(''' ''' )

    _UpperCAmelCase				:      Optional[int]			  =				0
    _UpperCAmelCase				:      Union[str, Any]			  =				0

    # When the output is short enough, the output is surrounded by = signs: "== OUTPUT =="
    # When it is too long, those signs are not present.
    _UpperCAmelCase				:      Tuple			  =				expressions[-2] if '''=''' in expressions[-1] else expressions[-1]

    for i, expression in enumerate(__A ):
        if "failed" in expression:
            failed += int(expressions[i - 1] )
        if "passed" in expression:
            success += int(expressions[i - 1] )

    return failed, success, time_spent

def      _lowerCamelCase	(						__A    :							List[str] )      ->					int:
    _UpperCAmelCase				:      Optional[Any]			  =				{}
    _UpperCAmelCase				:      int			  =				None
    _UpperCAmelCase				:      Any			  =				False
    for line in failures_short_lines.split('''\n''' ):
        if re.search(r'''_ \[doctest\]'''		,		__A ):
            _UpperCAmelCase				:      Tuple			  =				True
            _UpperCAmelCase				:      Optional[Any]			  =				line.split(''' ''' )[2]
        elif in_error and not line.split(''' ''' )[0].isdigit():
            _UpperCAmelCase				:      int			  =				line
            _UpperCAmelCase				:      Optional[Any]			  =				False

    return failures
class 							A_    :






 '''simple docstring'''




 def __init__(	self							,  _A							,  _A) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				title

     _UpperCAmelCase				:      List[str]			  =				doc_test_results['''time_spent'''].split(''',''')[0]
     _UpperCAmelCase				:      List[Any]			  =				doc_test_results['''success''']
     _UpperCAmelCase				:      Optional[Any]			  =				doc_test_results['''failures''']
     _UpperCAmelCase				:      Tuple			  =				self.n_success + self.n_failures

     # Failures and success of the modeling tests
     _UpperCAmelCase				:      Optional[int]			  =				doc_test_results


 @property
 def  snake_case__				(	self) ->		str:

     """simple docstring"""



     _UpperCAmelCase				:      List[str]			  =				[self._time_spent]
     _UpperCAmelCase				:      Dict			  =				0

     for time in time_spent:
         _UpperCAmelCase				:      Optional[int]			  =				time.split(''':''')

         # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute.
         if len(_A) == 1:
             _UpperCAmelCase				:      List[Any]			  =				[0, 0, time_parts[0]]

         _UpperCAmelCase  , _UpperCAmelCase  , _UpperCAmelCase				:      Optional[int]			  =				int(time_parts[0]), int(time_parts[1]), float(time_parts[2])
         total_secs += hours * 3600 + minutes * 60 + seconds

     _UpperCAmelCase  , _UpperCAmelCase  , _UpperCAmelCase				:      Dict			  =				total_secs // 3600, (total_secs % 3600) // 60, total_secs % 60
     return f'''{int(_A)}h{int(_A)}m{int(_A)}s'''


 @property
 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     return {"type": "header", "text": {"type": "plain_text", "text": self.title}}


 @property
 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     return {
         "type": "section",
         "text": {
             "type": "plain_text",
             "text": f'''🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.''',
             "emoji": True,
         },
         "accessory": {
             "type": "button",
             "text": {"type": "plain_text", "text": "Check Action results", "emoji": True},
             "url": f'''https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}''',
         },
     }


 @property
 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     return {
         "type": "section",
         "text": {
             "type": "plain_text",
             "text": (
                 f'''There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in'''
                 f''' {self.time}.'''
             ),
             "emoji": True,
         },
         "accessory": {
             "type": "button",
             "text": {"type": "plain_text", "text": "Check Action results", "emoji": True},
             "url": f'''https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}''',
         },
     }


 @property
 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      List[str]			  =				40
     _UpperCAmelCase				:      List[str]			  =				{k: v['''failed'''] for k, v in doc_test_results.items() if isinstance(_A							,  _A)}

     _UpperCAmelCase				:      List[Any]			  =				''''''
     for category, failures in category_failures.items():
         if len(_A) == 0:
             continue

         if report != "":
             report += "\n\n"

         report += f'''*{category} failures*:'''.ljust(line_length // 2).rjust(line_length // 2) + "\n"
         report += "`"
         report += "`\n`".join(_A)
         report += "`"

     return {
         "type": "section",
         "text": {
             "type": "mrkdwn",
             "text": f'''The following examples had failures:\n\n\n{report}\n''',
         },
     }


 @property
 def  snake_case__				(	self) ->		str:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				[self.header]

     if self.n_failures > 0:
         blocks.append(self.failures)

     if self.n_failures > 0:
         blocks.extend([self.category_failures])

     if self.n_failures == 0:
         blocks.append(self.no_failures)

     return json.dumps(_A)


 @staticmethod
 def  snake_case__				(	) ->		List[str]:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				[
         {
             '''type''': '''section''',
             '''text''': {
                 '''type''': '''plain_text''',
                 '''text''': '''There was an issue running the tests.''',
             },
             '''accessory''': {
                 '''type''': '''button''',
                 '''text''': {'''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True},
                 '''url''': f'''https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}''',
             },
         }
     ]

     print('''Sending the following payload''')
     print(json.dumps({'''blocks''': json.loads(_A)}))

     client.chat_postMessage(
         channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY''']							,  text='''There was an issue running the tests.'''							,  blocks=_A							,  )


 def  snake_case__				(	self) ->		str:

     """simple docstring"""



     print('''Sending the following payload''')
     print(json.dumps({'''blocks''': json.loads(self.payload)}))

     _UpperCAmelCase				:      str			  =				f'''{self.n_failures} failures out of {self.n_tests} tests,''' if self.n_failures else '''All tests passed.'''

     _UpperCAmelCase				:      Optional[Any]			  =				client.chat_postMessage(
         channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY''']							,  blocks=self.payload							,  text=_A							,  )


 def  snake_case__				(	self							,  _A							,  _A							,  _A							,  _A) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				''''''
     for key, value in failures.items():
         _UpperCAmelCase				:      str			  =				value[:200] + ''' [Truncated]''' if len(_A) > 250 else value
         failures_text += f'''*{key}*\n_{value}_\n\n'''

     _UpperCAmelCase				:      Optional[Any]			  =				job_name
     _UpperCAmelCase				:      Any			  =				{'''type''': '''section''', '''text''': {'''type''': '''mrkdwn''', '''text''': text}}

     if job_link is not None:
         _UpperCAmelCase				:      List[Any]			  =				{
             '''type''': '''button''',
             '''text''': {'''type''': '''plain_text''', '''text''': '''GitHub Action job''', '''emoji''': True},
             '''url''': job_link,
         }

     return [
         {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}},
         content,
         {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}},
     ]


 def  snake_case__				(	self) ->		Union[str, Any]:

     """simple docstring"""



     if self.thread_ts is None:
         raise ValueError('''Can only post reply if a post has been made.''')

     _UpperCAmelCase				:      Tuple			  =				self.doc_test_results.pop('''job_link''')
     self.doc_test_results.pop('''failures''')
     self.doc_test_results.pop('''success''')
     self.doc_test_results.pop('''time_spent''')

     _UpperCAmelCase				:      Any			  =				sorted(self.doc_test_results.items()							,  key=lambda _A: t[0])
     for job, job_result in sorted_dict:
         if len(job_result['''failures''']):
             _UpperCAmelCase				:      Dict			  =				f'''*Num failures* :{len(job_result['failed'])} \n'''
             _UpperCAmelCase				:      Tuple			  =				job_result['''failures''']
             _UpperCAmelCase				:      Dict			  =				self.get_reply_blocks(_A							,  _A							,  _A							,  text=_A)

             print('''Sending the following reply''')
             print(json.dumps({'''blocks''': blocks}))

             client.chat_postMessage(
                 channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY''']							,  text=f'''Results for {job}'''							,  blocks=_A							,  thread_ts=self.thread_ts['''ts''']							,  )

             time.sleep(1)



def      _lowerCamelCase	(						)      ->					str:
    _UpperCAmelCase				:      Any			  =				os.environ['''GITHUB_RUN_ID''']
    _UpperCAmelCase				:      Any			  =				f'''https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100'''
    _UpperCAmelCase				:      Optional[int]			  =				requests.get(__A ).json()
    _UpperCAmelCase				:      Optional[int]			  =				{}

    try:
        jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} )
        _UpperCAmelCase				:      int			  =				math.ceil((result['''total_count'''] - 100) / 100 )

        for i in range(__A ):
            _UpperCAmelCase				:      int			  =				requests.get(url + f'''&page={i + 2}''' ).json()
            jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} )

        return jobs
    except Exception as e:
        print('''Unknown error, could not fetch links.'''		,		__A )

    return {}

def      _lowerCamelCase	(						__A    :							str )      ->					Tuple:
    _UpperCAmelCase				:      Optional[Any]			  =				{}

    if os.path.exists(__A ):
        _UpperCAmelCase				:      Optional[int]			  =				os.listdir(__A )
        for file in files:
            try:
                with open(os.path.join(__A		,		__A )		,		encoding='''utf-8''' ) as f:
                    _UpperCAmelCase				:      Tuple			  =				f.read()
            except UnicodeDecodeError as e:
                raise ValueError(f'''Could not open {os.path.join(__A		,		__A )}.''' ) from e

    return _artifact

def      _lowerCamelCase	(						)      ->					List[str]:
    class 							A_    :






     '''simple docstring'''




     def __init__(	self							,  _A) ->		int:

         """simple docstring"""



         _UpperCAmelCase				:      List[Any]			  =				name
         _UpperCAmelCase				:      Union[str, Any]			  =				[]


     def __str__(	self) ->		int:

         """simple docstring"""



         return self.name


     def  snake_case__				(	self							,  _A) ->		int:

         """simple docstring"""



         self.paths.append({'''name''': self.name, '''path''': path})



    _UpperCAmelCase				:      Dict[str, Artifact]			  =				{}

    _UpperCAmelCase				:      Dict			  =				filter(os.path.isdir		,		os.listdir() )
    for directory in directories:
        _UpperCAmelCase				:      List[str]			  =				directory
        if artifact_name not in _available_artifacts:
            _UpperCAmelCase				:      Optional[int]			  =				Artifact(__A )

            _available_artifacts[artifact_name].add_path(__A )

    return _available_artifacts


if __name__ == "__main__":
  SCREAMING_SNAKE_CASE 							=						get_job_links()
  SCREAMING_SNAKE_CASE 							=						retrieve_available_artifacts()

  SCREAMING_SNAKE_CASE 							=						collections.OrderedDict(
      [
          ('*.py', 'API Examples'),
          ('*.md', 'MD Examples'),
      ]
  )

  # This dict will contain all the information relative to each doc test category:
  # - failed: list of failed tests
  # - failures: dict in the format 'test': 'error_message'
  SCREAMING_SNAKE_CASE 							=						{
      v: {
          'failed': [],
          'failures': {},
      }
      for v in docs.values()
  }

  # Link to the GitHub Action job
  SCREAMING_SNAKE_CASE 							=						github_actions_job_links.get('run_doctests')

  SCREAMING_SNAKE_CASE 							=						available_artifacts['doc_tests_gpu_test_reports'].paths[0]
  SCREAMING_SNAKE_CASE 							=						retrieve_artifact(artifact_path['name'])
  if "stats" in artifact:
    SCREAMING_SNAKE_CASE							,			SCREAMING_SNAKE_CASE							,			SCREAMING_SNAKE_CASE 							=						handle_test_results(artifact['stats'])
    SCREAMING_SNAKE_CASE 							=						failed
    SCREAMING_SNAKE_CASE 							=						success
    SCREAMING_SNAKE_CASE 							=						time_spent[1:-1] + ', '

    SCREAMING_SNAKE_CASE 							=						extract_first_line_failure(artifact['failures_short'])
    for line in artifact["summary_short"].split('\n'):
      if re.search('FAILED', line):
        SCREAMING_SNAKE_CASE 							=						line.replace('FAILED ', '')
        SCREAMING_SNAKE_CASE 							=						line.split()[0].replace('\n', '')

        if "::" in line:
          SCREAMING_SNAKE_CASE							,			SCREAMING_SNAKE_CASE 							=						line.split('::')
        else:
          SCREAMING_SNAKE_CASE							,			SCREAMING_SNAKE_CASE 							=						line, line

        for file_regex in docs.keys():
          if fnmatch(file_path, file_regex):
            SCREAMING_SNAKE_CASE 							=						docs[file_regex]
            doc_test_results[category]["failed"].append(test)

            SCREAMING_SNAKE_CASE 							=						all_failures[test] if test in all_failures else 'N/A'
            SCREAMING_SNAKE_CASE 							=						failure
            break

  SCREAMING_SNAKE_CASE 							=						Message('🤗 Results of the doc tests.', doc_test_results)
  message.post()
  message.post_reply()




