
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

lowercase   =	logging.get_logger(__name__)





@add_end_docstrings(UpperCAmelCase							)
class    __A(					UpperCAmelCase							):

					def __init__(						self       :  List[Any] ,       *__UpperCamelCase       :  Optional[int] ,       **__UpperCamelCase       :  Optional[Any] ):
												super().__init__(*_lowercase ,       **_lowercase )
												requires_backends(self ,       """decord""" )
												self.check_model_type(_lowercase )

					def  lowercase__   (						self       :  List[str] ,       __UpperCamelCase       :  List[str]=None ,       __UpperCamelCase       :  str=None ,       __UpperCamelCase       :  Any=None ):
												lowerCamelCase_					=       {}
												if frame_sampling_rate is not None:
																			lowerCamelCase_					=       frame_sampling_rate
												if num_frames is not None:
																			lowerCamelCase_					=       num_frames

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

					def __call__(						self       :  List[Any] ,       __UpperCamelCase       :  Union[str, Any] ,       **__UpperCamelCase       :  int ):
												return super().__call__(_lowercase ,       **_lowercase )

					def  lowercase__   (						self       :  List[str] ,       __UpperCamelCase       :  Tuple ,       __UpperCamelCase       :  Any=None ,       __UpperCamelCase       :  int=1 ):
												if num_frames is None:
																			lowerCamelCase_					=       self.model.config.num_frames

												if video.startswith("""http://""" ) or video.startswith("""https://""" ):
																			lowerCamelCase_					=       BytesIO(requests.get(_lowercase ).content )

												lowerCamelCase_					=       VideoReader(_lowercase )
												videoreader.seek(0 )

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

												lowerCamelCase_					=       videoreader.get_batch(_lowercase ).asnumpy()
												lowerCamelCase_					=       list(_lowercase )

												lowerCamelCase_					=       self.image_processor(_lowercase ,       return_tensors=self.framework )
												return model_inputs

					def  lowercase__   (						self       :  Dict ,       __UpperCamelCase       :  Union[str, Any] ):
												lowerCamelCase_					=       self.model(**_lowercase )
												return model_outputs



					def  lowercase__   (						self       :  List[str] ,       __UpperCamelCase       :  Tuple ,       __UpperCamelCase       :  Optional[int]=5 ):
												if top_k > self.model.config.num_labels:
																			lowerCamelCase_					=       self.model.config.num_labels

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

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








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE		:Optional[int]						=     {
    """configuration_rembert""": ["""REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RemBertConfig""", """RemBertOnnxConfig"""]
}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE		:str						=     ["""RemBertTokenizer"""]

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE		:Optional[Any]						=     ["""RemBertTokenizerFast"""]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE		:Dict						=     [
     """REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """RemBertForCausalLM""",
     """RemBertForMaskedLM""",
     """RemBertForMultipleChoice""",
     """RemBertForQuestionAnswering""",
     """RemBertForSequenceClassification""",
     """RemBertForTokenClassification""",
     """RemBertLayer""",
     """RemBertModel""",
     """RemBertPreTrainedModel""",
     """load_tf_weights_in_rembert""",
 ]


try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE		:Optional[int]						=     [
     """TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """TFRemBertForCausalLM""",
     """TFRemBertForMaskedLM""",
     """TFRemBertForMultipleChoice""",
     """TFRemBertForQuestionAnswering""",
     """TFRemBertForSequenceClassification""",
     """TFRemBertForTokenClassification""",
     """TFRemBertLayer""",
     """TFRemBertModel""",
     """TFRemBertPreTrainedModel""",
 ]


if TYPE_CHECKING:
 from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_rembert import RemBertTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_rembert_fast import RemBertTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_rembert import (
      REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      RemBertForCausalLM,
      RemBertForMaskedLM,
      RemBertForMultipleChoice,
      RemBertForQuestionAnswering,
      RemBertForSequenceClassification,
      RemBertForTokenClassification,
      RemBertLayer,
      RemBertModel,
      RemBertPreTrainedModel,
      load_tf_weights_in_rembert,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_rembert import (
      TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFRemBertForCausalLM,
      TFRemBertForMaskedLM,
      TFRemBertForMultipleChoice,
      TFRemBertForQuestionAnswering,
      TFRemBertForSequenceClassification,
      TFRemBertForTokenClassification,
      TFRemBertLayer,
      TFRemBertModel,
      TFRemBertPreTrainedModel,
  )


else:
 import sys

 SCREAMING_SNAKE_CASE		:List[Any]						=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
