


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


SCREAMING_SNAKE_CASE       :		Optional[Any]         =				logging.get_logger(__name__)

SCREAMING_SNAKE_CASE       :		Tuple         =				{
    "facebook/s2t-small-librispeech-asr": (
        "https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text
}






class      UpperCamelCase					(		lowercase__     ):


   '''simple docstring'''







   lowercase					:	str									="""speech_to_text"""
   lowercase					:	Dict									=["""past_key_values"""]
   lowercase					:	Dict									={"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}
   def __init__(     self   ,							UpperCamelCase_=1_0000   ,							UpperCamelCase_=12   ,							UpperCamelCase_=2048   ,							UpperCamelCase_=4   ,							UpperCamelCase_=6   ,							UpperCamelCase_=2048   ,							UpperCamelCase_=4   ,							UpperCamelCase_=0.0   ,							UpperCamelCase_=0.0   ,							UpperCamelCase_=True   ,							UpperCamelCase_=True   ,							UpperCamelCase_="relu"   ,							UpperCamelCase_=256   ,							UpperCamelCase_=0.1   ,							UpperCamelCase_=0.0   ,							UpperCamelCase_=0.0   ,							UpperCamelCase_=0.02   ,							UpperCamelCase_=2   ,							UpperCamelCase_=True   ,							UpperCamelCase_=1   ,							UpperCamelCase_=0   ,							UpperCamelCase_=2   ,							UpperCamelCase_=6000   ,							UpperCamelCase_=1024   ,							UpperCamelCase_=2   ,							UpperCamelCase_=(5, 5)   ,							UpperCamelCase_=1024   ,							UpperCamelCase_=80   ,							UpperCamelCase_=1   ,							**UpperCamelCase_   ,							):
        lowercase_       :Union[str, Any]     =       vocab_size
        lowercase_       :Dict     =       d_model
        lowercase_       :str     =       encoder_ffn_dim
        lowercase_       :List[Any]     =       encoder_layers
        lowercase_       :str     =       encoder_attention_heads
        lowercase_       :List[str]     =       decoder_ffn_dim
        lowercase_       :Optional[Any]     =       decoder_layers
        lowercase_       :Optional[Any]     =       decoder_attention_heads
        lowercase_       :Optional[int]     =       dropout
        lowercase_       :str     =       attention_dropout
        lowercase_       :Dict     =       activation_dropout
        lowercase_       :Tuple     =       activation_function
        lowercase_       :Optional[Any]     =       init_std
        lowercase_       :str     =       encoder_layerdrop
        lowercase_       :str     =       decoder_layerdrop
        lowercase_       :str     =       use_cache
        lowercase_       :List[str]     =       encoder_layers
        lowercase_       :List[Any]     =       scale_embedding  # scale factor will be sqrt(d_model) if True
        lowercase_       :Union[str, Any]     =       max_source_positions
        lowercase_       :Optional[int]     =       max_target_positions
        lowercase_       :Any     =       num_conv_layers
        lowercase_       :Union[str, Any]     =       list(UpperCamelCase_		)
        lowercase_       :Union[str, Any]     =       conv_channels
        lowercase_       :int     =       input_feat_per_channel
        lowercase_       :str     =       input_channels

        if len(self.conv_kernel_sizes		) != self.num_conv_layers:
             raise ValueError(
                 '''Configuration for convolutional module is incorrect. '''
                 '''It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` '''
                 f"but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes		)}`, "
                 f"`config.num_conv_layers = {self.num_conv_layers}`."		)

        super().__init__(
            pad_token_id=UpperCamelCase_   ,							bos_token_id=UpperCamelCase_   ,							eos_token_id=UpperCamelCase_   ,							is_encoder_decoder=UpperCamelCase_   ,							decoder_start_token_id=UpperCamelCase_   ,							**UpperCamelCase_   ,							)





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

SCREAMING_SNAKE_CASE       :		List[str]         =				logging.get_logger(__name__)






@add_end_docstrings(lowercase__     )
class      UpperCamelCase					(		lowercase__     ):


   '''simple docstring'''







   def __init__(     self   ,							*UpperCamelCase_   ,							**UpperCamelCase_		):
        super().__init__(*UpperCamelCase_   ,							**UpperCamelCase_		)
        requires_backends(self   ,							'''decord'''		)
        self.check_model_type(UpperCamelCase_		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_=None   ,							UpperCamelCase_=None   ,							UpperCamelCase_=None		):
        lowercase_       :int     =       {}
        if frame_sampling_rate is not None:
             lowercase_       :int     =       frame_sampling_rate
        if num_frames is not None:
             lowercase_       :int     =       num_frames

        lowercase_       :str     =       {}
        if top_k is not None:
             lowercase_       :Optional[int]     =       top_k
        return preprocess_params, {}, postprocess_params
   def __call__(     self   ,							UpperCamelCase_   ,							**UpperCamelCase_		):
        return super().__call__(UpperCamelCase_   ,							**UpperCamelCase_		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_=None   ,							UpperCamelCase_=1		):
        if num_frames is None:
             lowercase_       :str     =       self.model.config.num_frames

        if video.startswith('''http://'''		) or video.startswith('''https://'''		):
             lowercase_       :str     =       BytesIO(requests.get(UpperCamelCase_		).content		)

        lowercase_       :Optional[int]     =       VideoReader(UpperCamelCase_		)
        videoreader.seek(0		)

        lowercase_       :Tuple     =       0
        lowercase_       :Optional[Any]     =       num_frames * frame_sampling_rate - 1
        lowercase_       :Any     =       np.linspace(UpperCamelCase_   ,							UpperCamelCase_   ,							num=UpperCamelCase_   ,							dtype=np.intaa		)

        lowercase_       :Dict     =       videoreader.get_batch(UpperCamelCase_		).asnumpy()
        lowercase_       :List[Any]     =       list(UpperCamelCase_		)

        lowercase_       :Any     =       self.image_processor(UpperCamelCase_   ,							return_tensors=self.framework		)
        return model_inputs
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_		):
        lowercase_       :List[str]     =       self.model(**UpperCamelCase_		)
        return model_outputs




   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_=5		):
        if top_k > self.model.config.num_labels:
             lowercase_       :List[str]     =       self.model.config.num_labels

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

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

