


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

snake_case__			 =						logging.get_logger(__name__)


@add_end_docstrings(_a)
class 					lowerCAmelCase_ (       _a):




      def __init__(	self						:   int   ,     *__A						:   List[Any]   ,     **__A						:   int						)      ->List[Any]:






         """simple docstring"""






         super().__init__(*__A   ,     **__A						)
         requires_backends(self   ,     "decord"						)
         self.check_model_type(__A						)




      def  _snake_case					(	self						:   List[Any]   ,     __A						:   List[str]=None   ,     __A						:   Any=None   ,     __A						:   List[str]=None						)      ->str:






         """simple docstring"""






         a__ :str						  =    {}
         if frame_sampling_rate is not None:
            a__ :int						  =    frame_sampling_rate
         if num_frames is not None:
            a__ :Optional[Any]						  =    num_frames

         a__ :Dict						  =    {}
         if top_k is not None:
            a__ :Optional[Any]						  =    top_k
         return preprocess_params, {}, postprocess_params




      def __call__(	self						:   Optional[Any]   ,     __A						:   Union[str, List[str]]   ,     **__A						:   Any						)      ->List[Any]:






         """simple docstring"""






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




      def  _snake_case					(	self						:   Any   ,     __A						:   Dict   ,     __A						:   Optional[int]=None   ,     __A						:   List[str]=1						)      ->str:






         """simple docstring"""






         if num_frames is None:
            a__ :Optional[Any]						  =    self.model.config.num_frames

         if video.startswith("http://"						) or video.startswith("https://"						):
            a__ :Optional[int]						  =    BytesIO(requests.get(__A						).content						)

         a__ :Tuple						  =    VideoReader(__A						)
         videoreader.seek(0						)

         a__ :List[str]						  =    0
         a__ :Tuple						  =    num_frames * frame_sampling_rate - 1
         a__ :Optional[Any]						  =    np.linspace(__A   ,     __A   ,     num=__A   ,     dtype=np.intaa						)

         a__ :Tuple						  =    videoreader.get_batch(__A						).asnumpy()
         a__ :Union[str, Any]						  =    list(__A						)

         a__ :str						  =    self.image_processor(__A   ,     return_tensors=self.framework						)
         return model_inputs




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






         """simple docstring"""






         a__ :Tuple						  =    self.model(**__A						)
         return model_outputs






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






         """simple docstring"""






         if top_k > self.model.config.num_labels:
            a__ :Any						  =    self.model.config.num_labels

         if self.framework == "pt":
            a__ :str						  =    model_outputs.logits.softmax(-1						)[0]
            a__ :Dict						  =    probs.topk(__A						)
         else:
            raise ValueError(F'''Unsupported framework: {self.framework}'''						)

         a__ :int						  =    scores.tolist()
         a__ :List[str]						  =    ids.tolist()
         return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(__A   ,     __A						)]






from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case__			 =						logging.get_logger(__name__)

snake_case__			 =						{
    '''google/pegasus-large''': '''https://huggingface.co/google/pegasus-large/resolve/main/config.json''',
    # See all PEGASUS models at https://huggingface.co/models?filter=pegasus
}


class 					lowerCAmelCase_ (       _a):
						lowerCamelCase_         =     'pegasus'
						lowerCamelCase_         =     ['past_key_values']
						lowerCamelCase_         =     {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}




						def __init__(	self						:   Any   ,     __A						:   Dict=50265   ,     __A						:   List[Any]=1024   ,     __A						:   int=12   ,     __A						:   Optional[Any]=4096   ,     __A						:   Optional[int]=16   ,     __A						:   Dict=12   ,     __A						:   List[Any]=4096   ,     __A						:   List[str]=16   ,     __A						:   Optional[int]=0.0   ,     __A						:   List[Any]=0.0   ,     __A						:   List[str]=True   ,     __A						:   Optional[int]=True   ,     __A						:   str="gelu"   ,     __A						:   Tuple=1024   ,     __A						:   Any=0.1   ,     __A						:   List[Any]=0.0   ,     __A						:   List[str]=0.0   ,     __A						:   Tuple=0.02   ,     __A						:   Union[str, Any]=0   ,     __A						:   Union[str, Any]=False   ,     __A						:   Optional[Any]=0   ,     __A						:   Tuple=1   ,     __A						:   str=1   ,     **__A						:   Any   ,     )      ->Union[str, Any]:






									"""simple docstring"""






									a__ :Any						  =    vocab_size
									a__ :List[str]						  =    max_position_embeddings
									a__ :int						  =    d_model
									a__ :Union[str, Any]						  =    encoder_ffn_dim
									a__ :List[Any]						  =    encoder_layers
									a__ :Union[str, Any]						  =    encoder_attention_heads
									a__ :Tuple						  =    decoder_ffn_dim
									a__ :List[Any]						  =    decoder_layers
									a__ :Tuple						  =    decoder_attention_heads
									a__ :Optional[int]						  =    dropout
									a__ :str						  =    attention_dropout
									a__ :Optional[int]						  =    activation_dropout
									a__ :str						  =    activation_function
									a__ :Dict						  =    init_std
									a__ :Any						  =    encoder_layerdrop
									a__ :int						  =    decoder_layerdrop
									a__ :Union[str, Any]						  =    use_cache
									a__ :List[Any]						  =    encoder_layers
									a__ :Dict						  =    scale_embedding  # scale factor will be sqrt(d_model) if True
									super().__init__(
									    pad_token_id=__A   ,     eos_token_id=__A   ,     is_encoder_decoder=__A   ,     decoder_start_token_id=__A   ,     forced_eos_token_id=__A   ,     **__A   ,     )




						@property
						def  _snake_case					(	self						:   Optional[Any]						)      ->int:






									"""simple docstring"""






									return self.encoder_attention_heads






						@property
						def  _snake_case					(	self						:   Union[str, Any]						)      ->int:






									"""simple docstring"""






									return self.d_model


