




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	:	List[Any]				=   logging.get_logger(__name__)




@add_end_docstrings(UpperCamelCase__							)
class 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





      def __init__(				self    ,				*A    ,				**A     )		->    int:
         super().__init__(*_lowerCAmelCase    ,				**_lowerCAmelCase     )
         requires_backends(self    ,				"""decord"""     )
         self.check_model_type(_lowerCAmelCase     )

      def      UpperCAmelCase				(				self    ,				A=None    ,				A=None    ,				A=None     )		->    Dict:
         snake_case				:    Dict           =	{}
         if frame_sampling_rate is not None:
            snake_case				:    str           =	frame_sampling_rate
         if num_frames is not None:
            snake_case				:    str           =	num_frames

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

      def __call__(				self    ,				A    ,				**A     )		->    int:
         return super().__call__(_lowerCAmelCase    ,				**_lowerCAmelCase     )

      def      UpperCAmelCase				(				self    ,				A    ,				A=None    ,				A=1     )		->    Dict:

         if num_frames is None:
            snake_case				:    Any           =	self.model.config.num_frames

         if video.startswith("""http://"""     ) or video.startswith("""https://"""     ):
            snake_case				:    List[Any]           =	BytesIO(requests.get(_lowerCAmelCase     ).content     )

         snake_case				:    List[str]           =	VideoReader(_lowerCAmelCase     )
         videoreader.seek(0     )

         snake_case				:    Any           =	0
         snake_case				:    int           =	num_frames * frame_sampling_rate - 1
         snake_case				:    Optional[int]           =	np.linspace(_lowerCAmelCase    ,				_lowerCAmelCase    ,				num=_lowerCAmelCase    ,				dtype=np.intaa     )

         snake_case				:    str           =	videoreader.get_batch(_lowerCAmelCase     ).asnumpy()
         snake_case				:    List[str]           =	list(_lowerCAmelCase     )

         snake_case				:    Union[str, Any]           =	self.image_processor(_lowerCAmelCase    ,				return_tensors=self.framework     )
         return model_inputs

      def      UpperCAmelCase				(				self    ,				A     )		->    Dict:
         snake_case				:    Dict           =	self.model(**_lowerCAmelCase     )
         return model_outputs

      def      UpperCAmelCase				(				self    ,				A    ,				A=5     )		->    Optional[Any]:

         if top_k > self.model.config.num_labels:
            snake_case				:    Optional[int]           =	self.model.config.num_labels

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

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











from typing import TYPE_CHECKING

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


lowerCamelCase	:	str				=   {
    'configuration_roformer': ['ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RoFormerConfig', 'RoFormerOnnxConfig'],
    'tokenization_roformer': ['RoFormerTokenizer'],
}

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowerCamelCase	:	List[Any]				=   ['RoFormerTokenizerFast']

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowerCamelCase	:	Dict				=   [
           'ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
           'RoFormerForCausalLM',
           'RoFormerForMaskedLM',
           'RoFormerForMultipleChoice',
           'RoFormerForQuestionAnswering',
           'RoFormerForSequenceClassification',
           'RoFormerForTokenClassification',
           'RoFormerLayer',
           'RoFormerModel',
           'RoFormerPreTrainedModel',
           'load_tf_weights_in_roformer',
       ]


try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowerCamelCase	:	Any				=   [
           'TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
           'TFRoFormerForCausalLM',
           'TFRoFormerForMaskedLM',
           'TFRoFormerForMultipleChoice',
           'TFRoFormerForQuestionAnswering',
           'TFRoFormerForSequenceClassification',
           'TFRoFormerForTokenClassification',
           'TFRoFormerLayer',
           'TFRoFormerModel',
           'TFRoFormerPreTrainedModel',
       ]


try:
       if not is_flax_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowerCamelCase	:	Union[str, Any]				=   [
           'FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
           'FlaxRoFormerForMaskedLM',
           'FlaxRoFormerForMultipleChoice',
           'FlaxRoFormerForQuestionAnswering',
           'FlaxRoFormerForSequenceClassification',
           'FlaxRoFormerForTokenClassification',
           'FlaxRoFormerModel',
           'FlaxRoFormerPreTrainedModel',
       ]


if TYPE_CHECKING:
       from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig
       from .tokenization_roformer import RoFormerTokenizer

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_roformer_fast import RoFormerTokenizerFast

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_roformer import (
                  ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
                  RoFormerForCausalLM,
                  RoFormerForMaskedLM,
                  RoFormerForMultipleChoice,
                  RoFormerForQuestionAnswering,
                  RoFormerForSequenceClassification,
                  RoFormerForTokenClassification,
                  RoFormerLayer,
                  RoFormerModel,
                  RoFormerPreTrainedModel,
                  load_tf_weights_in_roformer,
              )

       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_roformer import (
                  TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TFRoFormerForCausalLM,
                  TFRoFormerForMaskedLM,
                  TFRoFormerForMultipleChoice,
                  TFRoFormerForQuestionAnswering,
                  TFRoFormerForSequenceClassification,
                  TFRoFormerForTokenClassification,
                  TFRoFormerLayer,
                  TFRoFormerModel,
                  TFRoFormerPreTrainedModel,
              )

       try:
              if not is_flax_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_flax_roformer import (
                  FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
                  FlaxRoFormerForMaskedLM,
                  FlaxRoFormerForMultipleChoice,
                  FlaxRoFormerForQuestionAnswering,
                  FlaxRoFormerForSequenceClassification,
                  FlaxRoFormerForTokenClassification,
                  FlaxRoFormerModel,
                  FlaxRoFormerPreTrainedModel,
              )


else:
       import sys

       lowerCamelCase	:	Optional[int]				=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





