





from __future__ import annotations

import unittest

from transformers import AutoTokenizer, MBartConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel

@require_tf
class    lowerCAmelCase							:
 UpperCAmelCase__													=		MBartConfig
 UpperCAmelCase__													=		{}
 UpperCAmelCase__													=		"""gelu"""
 def __init__(      self  :      List[str]			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      str=13			,     UpperCAmelCase  :      Union[str, Any]=7			,     UpperCAmelCase  :      Dict=True			,     UpperCAmelCase  :      Tuple=False			,     UpperCAmelCase  :      Dict=99			,     UpperCAmelCase  :      int=32			,     UpperCAmelCase  :      Tuple=2			,     UpperCAmelCase  :      str=4			,     UpperCAmelCase  :      Any=37			,     UpperCAmelCase  :      int=0.1			,     UpperCAmelCase  :      Union[str, Any]=0.1			,     UpperCAmelCase  :      List[str]=20			,     UpperCAmelCase  :      Dict=2			,     UpperCAmelCase  :      Optional[int]=1			,     UpperCAmelCase  :      Dict=0			,     )			->	Dict:
     lowerCamelCase__    :      Union[str, Any]					 =		parent
     lowerCamelCase__    :      List[str]					 =		batch_size
     lowerCamelCase__    :      str					 =		seq_length
     lowerCamelCase__    :      str					 =		is_training
     lowerCamelCase__    :      Dict					 =		use_labels
     lowerCamelCase__    :      List[Any]					 =		vocab_size
     lowerCamelCase__    :      int					 =		hidden_size
     lowerCamelCase__    :      List[Any]					 =		num_hidden_layers
     lowerCamelCase__    :      Dict					 =		num_attention_heads
     lowerCamelCase__    :      Optional[Any]					 =		intermediate_size
     lowerCamelCase__    :      Optional[int]					 =		hidden_dropout_prob
     lowerCamelCase__    :      Union[str, Any]					 =		attention_probs_dropout_prob
     lowerCamelCase__    :      Dict					 =		max_position_embeddings
     lowerCamelCase__    :      Dict					 =		eos_token_id
     lowerCamelCase__    :      Any					 =		pad_token_id
     lowerCamelCase__    :      int					 =		bos_token_id
 def      A_	(      self  :      Tuple	)			->	int:
     lowerCamelCase__    :      str					 =		ids_tensor([self.batch_size, self.seq_length - 1]			,     self.vocab_size	)
     lowerCamelCase__    :      Optional[int]					 =		tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size	)			,     1	)
     lowerCamelCase__    :      List[str]					 =		tf.concat([input_ids, eos_tensor]			,     axis=1	)

     lowerCamelCase__    :      List[Any]					 =		ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size	)

     lowerCamelCase__    :      List[Any]					 =		self.config_cls(
         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_ids=[2]			,     bos_token_id=self.bos_token_id			,     pad_token_id=self.pad_token_id			,     decoder_start_token_id=self.pad_token_id			,     **self.config_updates			,     )
     lowerCamelCase__    :      int					 =		prepare_mbart_inputs_dict(UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase	)
     return config, inputs_dict







 def      A_	(      self  :      Optional[Any]			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      Optional[int]	)			->	Union[str, Any]:
     lowerCamelCase__    :      Tuple					 =		TFMBartModel(config=UpperCAmelCase	).get_decoder()
     lowerCamelCase__    :      int					 =		inputs_dict['input_ids']

     lowerCamelCase__    :      Union[str, Any]					 =		input_ids[:1, :]
     lowerCamelCase__    :      str					 =		inputs_dict['attention_mask'][:1, :]
     lowerCamelCase__    :      Union[str, Any]					 =		inputs_dict['head_mask']
     lowerCamelCase__    :      Union[str, Any]					 =		1

     # first forward pass
     lowerCamelCase__    :      Union[str, Any]					 =		model(UpperCAmelCase			,     attention_mask=UpperCAmelCase			,     head_mask=UpperCAmelCase			,     use_cache=UpperCAmelCase	)

     lowerCamelCase__ ,							lowerCamelCase__    :      Any					 =		outputs.to_tuple()
     lowerCamelCase__    :      Optional[int]					 =		past_key_values[1]


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase=None						,	_UpperCAmelCase=None						,	_UpperCAmelCase=None						,	_UpperCAmelCase=None						,	_UpperCAmelCase=None						,	)			->     Union[str, Any]:
    if attention_mask is None:
        lowerCamelCase__    :      Union[str, Any]					 =		tf.cast(tf.math.not_equal(_UpperCAmelCase						,	config.pad_token_id )						,	tf.inta )
    if decoder_attention_mask is None:
        lowerCamelCase__    :      Any					 =		tf.concat(
            [
                tf.ones(decoder_input_ids[:, :1].shape						,	dtype=tf.inta ),
                tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]						,	config.pad_token_id )						,	tf.inta ),
            ]						,	axis=-1						,	)
    if head_mask is None:
        lowerCamelCase__    :      Dict					 =		tf.ones((config.encoder_layers, config.encoder_attention_heads) )
    if decoder_head_mask is None:
        lowerCamelCase__    :      Dict					 =		tf.ones((config.decoder_layers, config.decoder_attention_heads) )
    if cross_attn_head_mask is None:
        lowerCamelCase__    :      Dict					 =		tf.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": decoder_attention_mask,
        "head_mask": head_mask,
        "decoder_head_mask": decoder_head_mask,
        "cross_attn_head_mask": cross_attn_head_mask,
    }

@require_tf
class    lowerCAmelCase							(					__UpperCamelCase,  __UpperCamelCase,  unittest.TestCase			):
 UpperCAmelCase__													=		(TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
 UpperCAmelCase__													=		(TFMBartForConditionalGeneration,) if is_tf_available() else ()
 UpperCAmelCase__													=		(
     {
         """conversational""": TFMBartForConditionalGeneration,
         """feature-extraction""": TFMBartModel,
         """summarization""": TFMBartForConditionalGeneration,
         """text2text-generation""": TFMBartForConditionalGeneration,
         """translation""": TFMBartForConditionalGeneration,
     }
     if is_tf_available()
     else {}
 )
 UpperCAmelCase__													=		True
 UpperCAmelCase__													=		False
 UpperCAmelCase__													=		False
 def      A_	(      self  :      Tuple			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      str	)			->	Tuple:
     if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
         # Exception encountered when calling layer '...'
         return True

     return False
 def      A_	(      self  :      Dict	)			->	int:
     lowerCamelCase__    :      Dict					 =		TFMBartModelTester(self	)
     lowerCamelCase__    :      List[str]					 =		ConfigTester(self			,     config_class=UpperCAmelCase	)
 def      A_	(      self  :      List[Any]	)			->	str:
     self.config_tester.run_common_tests()







 def      A_	(      self  :      Tuple	)			->	int:
     lowerCamelCase__    :      int					 =		self.model_tester.prepare_config_and_inputs_for_common()
     self.model_tester.check_decoder_model_past_large_inputs(*UpperCAmelCase	)


@require_sentencepiece
@require_tokenizers
@require_tf
class    lowerCAmelCase							(					unittest.TestCase			):
 UpperCAmelCase__													=		[
     """ UN Chief Says There Is No Military Solution in Syria""",
 ]
 UpperCAmelCase__													=		[
     """Şeful ONU declară că nu există o soluţie militară în Siria""",
 ]
 UpperCAmelCase__													=		"""facebook/mbart-large-en-ro"""
 @cached_property
 def      A_	(      self  :      Union[str, Any]	)			->	Optional[Any]:
     return AutoTokenizer.from_pretrained(self.model_name	)
 @cached_property
 def      A_	(      self  :      Optional[Any]	)			->	Any:
     lowerCamelCase__    :      str					 =		TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name	)
     return model
 def      A_	(      self  :      List[Any]			,     **UpperCAmelCase  :      Optional[Any]	)			->	Optional[Any]:
     lowerCamelCase__    :      List[str]					 =		self.translate_src_text(**UpperCAmelCase	)
     self.assertListEqual(self.expected_text			,     UpperCAmelCase	)
 def      A_	(      self  :      str			,     **UpperCAmelCase  :      int	)			->	List[str]:
     lowerCamelCase__    :      Union[str, Any]					 =		self.tokenizer(self.src_text			,     **UpperCAmelCase			,     return_tensors='tf'	)
     lowerCamelCase__    :      Optional[int]					 =		self.model.generate(
         model_inputs.input_ids			,     attention_mask=model_inputs.attention_mask			,     num_beams=2	)
     lowerCamelCase__    :      str					 =		self.tokenizer.batch_decode(UpperCAmelCase			,     skip_special_tokens=UpperCAmelCase	)
     return generated_words







 @slow
 def      A_	(      self  :      str	)			->	Union[str, Any]:
     self._assert_generated_batch_equal_expected()










'''simple docstring'''


from io import BytesIO
from typing import List, Union

import requests

from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_decord_available():
   import numpy as np
   from decord import VideoReader


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

_lowerCamelCase	:       Any      	=	logging.get_logger(__name__)

@add_end_docstrings(_a		)
class  SCREAMING_SNAKE_CASE (  _a		):






    """simple docstring"""





    def __init__(  self      :					Any  ,						*UpperCamelCase__      :					Dict  ,						**UpperCamelCase__      :					Union[str, Any] ):


       """simple docstring"""







       super().__init__(*UpperCamelCase__  ,						**UpperCamelCase__ )
       requires_backends(self  ,						'decord' )
       self.check_model_type(UpperCamelCase__ )






    def  A       (  self      :					Optional[int]  ,						UpperCamelCase__      :					Optional[int]=None  ,						UpperCamelCase__      :					Optional[Any]=None  ,						UpperCamelCase__      :					Optional[Any]=None ):


       """simple docstring"""







       UpperCamelCase					=  {}
       if frame_sampling_rate is not None:
          UpperCamelCase					=  frame_sampling_rate
       if num_frames is not None:
          UpperCamelCase					=  num_frames

       UpperCamelCase					=  {}
       if top_k is not None:
          UpperCamelCase					=  top_k
       return preprocess_params, {}, postprocess_params






    def __call__(  self      :					List[str]  ,						UpperCamelCase__      :					Union[str, List[str]]  ,						**UpperCamelCase__      :					Dict ):


       """simple docstring"""







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






    def  A       (  self      :					Tuple  ,						UpperCamelCase__      :					Union[str, Any]  ,						UpperCamelCase__      :					Tuple=None  ,						UpperCamelCase__      :					Tuple=1 ):


       """simple docstring"""







       if num_frames is None:
          UpperCamelCase					=  self.model.config.num_frames

       if video.startswith('http://' ) or video.startswith('https://' ):
          UpperCamelCase					=  BytesIO(requests.get(UpperCamelCase__ ).content )

       UpperCamelCase					=  VideoReader(UpperCamelCase__ )
       videoreader.seek(0 )

       UpperCamelCase					=  0
       UpperCamelCase					=  num_frames * frame_sampling_rate - 1
       UpperCamelCase					=  np.linspace(UpperCamelCase__  ,						UpperCamelCase__  ,						num=UpperCamelCase__  ,						dtype=np.intaa )

       UpperCamelCase					=  videoreader.get_batch(UpperCamelCase__ ).asnumpy()
       UpperCamelCase					=  list(UpperCamelCase__ )

       UpperCamelCase					=  self.image_processor(UpperCamelCase__  ,						return_tensors=self.framework )
       return model_inputs






    def  A       (  self      :					Union[str, Any]  ,						UpperCamelCase__      :					List[str] ):


       """simple docstring"""







       UpperCamelCase					=  self.model(**UpperCamelCase__ )
       return model_outputs






    def  A       (  self      :					int  ,						UpperCamelCase__      :					str  ,						UpperCamelCase__      :					List[Any]=5 ):


       """simple docstring"""







       if top_k > self.model.config.num_labels:
          UpperCamelCase					=  self.model.config.num_labels

       if self.framework == "pt":
          UpperCamelCase					=  model_outputs.logits.softmax(-1 )[0]
          UpperCamelCase				,      UpperCamelCase					=  probs.topk(UpperCamelCase__ )
       else:
          raise ValueError(f"""Unsupported framework: {self.framework}""" )

       UpperCamelCase					=  scores.tolist()
       UpperCamelCase					=  ids.tolist()
       return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(UpperCamelCase__  ,						UpperCamelCase__ )]

