
"""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

SCREAMING_SNAKE_CASE_            =    logging.get_logger(__name__)
@add_end_docstrings(SCREAMING_SNAKE_CASE_				)
class   _UpperCAmelCase				(		SCREAMING_SNAKE_CASE_				):




      def __init__(				self ,							*lowercase_ ,							**lowercase_				)    ->       Optional[Any]:
          super().__init__(*lowercase_ ,							**lowercase_				)
          requires_backends(self ,							'decord'				)
          self.check_model_type(lowercase_				)




      def      a_   (				self ,							lowercase_=None ,							lowercase_=None ,							lowercase_=None				)    ->       List[str]:
          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 ,							lowercase_ ,							**lowercase_				)    ->       Optional[int]:
          return super().__call__(lowercase_ ,							**lowercase_				)




      def      a_   (				self ,							lowercase_ ,							lowercase_=None ,							lowercase_=1				)    ->       List[str]:
          if num_frames is None:
              UpperCAmelCase          =       self.model.config.num_frames

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

          UpperCAmelCase          =       VideoReader(lowercase_				)
          videoreader.seek(0				)

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

          UpperCAmelCase          =       videoreader.get_batch(lowercase_				).asnumpy()
          UpperCAmelCase          =       list(lowercase_				)

          UpperCAmelCase          =       self.image_processor(lowercase_ ,							return_tensors=self.framework				)
          return model_inputs




      def      a_   (				self ,							lowercase_				)    ->       Tuple:
          UpperCAmelCase          =       self.model(**lowercase_				)
          return model_outputs




      def      a_   (				self ,							lowercase_ ,							lowercase_=5				)    ->       Optional[int]:
          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(lowercase_				)
          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(lowercase_ ,							lowercase_				)]





"""simple docstring"""

from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_            =    logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_            =    {
    '''kssteven/ibert-roberta-base''': '''https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json''',
    '''kssteven/ibert-roberta-large''': '''https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json''',
    '''kssteven/ibert-roberta-large-mnli''': (
        '''https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json'''
    ),
}
class   _UpperCAmelCase				(		SCREAMING_SNAKE_CASE_				):
      __SCREAMING_SNAKE_CASE :				Tuple				   =						"ibert"




      def __init__(				self ,							lowercase_=3_0_5_2_2 ,							lowercase_=7_6_8 ,							lowercase_=1_2 ,							lowercase_=1_2 ,							lowercase_=3_0_7_2 ,							lowercase_="gelu" ,							lowercase_=0.1 ,							lowercase_=0.1 ,							lowercase_=5_1_2 ,							lowercase_=2 ,							lowercase_=0.0_2 ,							lowercase_=1E-12 ,							lowercase_=1 ,							lowercase_=0 ,							lowercase_=2 ,							lowercase_="absolute" ,							lowercase_=False ,							lowercase_="none" ,							**lowercase_ ,							)    ->       Optional[int]:
          super().__init__(pad_token_id=lowercase_ ,							bos_token_id=lowercase_ ,							eos_token_id=lowercase_ ,							**lowercase_				)

          UpperCAmelCase          =       vocab_size
          UpperCAmelCase          =       hidden_size
          UpperCAmelCase          =       num_hidden_layers
          UpperCAmelCase          =       num_attention_heads
          UpperCAmelCase          =       hidden_act
          UpperCAmelCase          =       intermediate_size
          UpperCAmelCase          =       hidden_dropout_prob
          UpperCAmelCase          =       attention_probs_dropout_prob
          UpperCAmelCase          =       max_position_embeddings
          UpperCAmelCase          =       type_vocab_size
          UpperCAmelCase          =       initializer_range
          UpperCAmelCase          =       layer_norm_eps
          UpperCAmelCase          =       position_embedding_type
          UpperCAmelCase          =       quant_mode
          UpperCAmelCase          =       force_dequant

class   _UpperCAmelCase				(		SCREAMING_SNAKE_CASE_				):




      @property
      def      a_   (				self				)    ->       Mapping[str, Mapping[int, str]]:
          if self.task == "multiple-choice":
              UpperCAmelCase          =       {0: 'batch', 1: 'choice', 2: 'sequence'}
          else:
              UpperCAmelCase          =       {0: 'batch', 1: 'sequence'}
          return OrderedDict(
              [
                  ('input_ids', dynamic_axis),
                  ('attention_mask', dynamic_axis),
              ]				)



