





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

__UpperCamelCase     :						Tuple						       =  logging.get_logger(__name__)







@add_end_docstrings(a__  )
class       a							(   a__  ):



 def __init__(     self				,       *_snake_case				,       **_snake_case     ):







  """simple docstring"""






  super().__init__(*_snake_case				,       **_snake_case     )
  requires_backends(self				,       'decord'     )
  self.check_model_type(_snake_case     )



 def    UpperCamelCase__  (     self				,       _snake_case=None				,       _snake_case=None				,       _snake_case=None     ):







  """simple docstring"""






  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				,       _snake_case				,       **_snake_case     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case=None				,       _snake_case=1     ):







  """simple docstring"""






  if num_frames is None:
   lowerCAmelCase    				= self.model.config.num_frames

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

  lowerCAmelCase    				= VideoReader(_snake_case     )
  videoreader.seek(0     )

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

  lowerCAmelCase    				= videoreader.get_batch(_snake_case     ).asnumpy()
  lowerCAmelCase    				= list(_snake_case     )

  lowerCAmelCase    				= self.image_processor(_snake_case				,       return_tensors=self.framework     )
  return model_inputs



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= self.model(**_snake_case     )
  return model_outputs



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case=5     ):







  """simple docstring"""






  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(_snake_case     )
  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(_snake_case				,       _snake_case     )]












"""simple docstring"""
from typing import TYPE_CHECKING

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


__UpperCamelCase     :						Dict						       =  {'''processing_layoutxlm''': ['''LayoutXLMProcessor''']}

try:
       if not is_sentencepiece_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __UpperCamelCase     :						int						       =  ['''LayoutXLMTokenizer''']

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __UpperCamelCase     :						Any						       =  ['''LayoutXLMTokenizerFast''']

if TYPE_CHECKING:
       from .processing_layoutxlm import LayoutXLMProcessor

       try:
              if not is_sentencepiece_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_layoutxlm import LayoutXLMTokenizer

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast

else:
       import sys

       __UpperCamelCase     :						Dict						       =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





