



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

__snake_case						:int						=					logging.get_logger(__name__)

@add_end_docstrings(SCREAMING_SNAKE_CASE__			)
class    _A	(     SCREAMING_SNAKE_CASE__			):



       def __init__(		self							:					Any					,		*__SCREAMING_SNAKE_CASE							:					Tuple					,		**__SCREAMING_SNAKE_CASE							:					List[str]):


           '''simple docstring'''

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



       def 		_lowerCamelCase      (		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					str=None					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=None					,		__SCREAMING_SNAKE_CASE							:					Dict=None):


           '''simple docstring'''

           __a							=    {}
           if frame_sampling_rate is not None:
               __a							=    frame_sampling_rate
           if num_frames is not None:
               __a							=    num_frames

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



       def __call__(		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					Union[str, List[str]]					,		**__SCREAMING_SNAKE_CASE							:					str):


           '''simple docstring'''

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



       def 		_lowerCamelCase      (		self							:					List[Any]					,		__SCREAMING_SNAKE_CASE							:					List[str]					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]=None					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=1):


           '''simple docstring'''

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

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

           __a							=    VideoReader(a_)
           videoreader.seek(0)

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

           __a							=    videoreader.get_batch(a_).asnumpy()
           __a							=    list(a_)

           __a							=    self.image_processor(a_					,		return_tensors=self.framework)
           return model_inputs



       def 		_lowerCamelCase      (		self							:					str					,		__SCREAMING_SNAKE_CASE							:					Optional[int]):


           '''simple docstring'''

           __a							=    self.model(**a_)
           return model_outputs





       def 		_lowerCamelCase      (		self							:					Tuple					,		__SCREAMING_SNAKE_CASE							:					Any					,		__SCREAMING_SNAKE_CASE							:					Dict=5):


           '''simple docstring'''

           if top_k > self.model.config.num_labels:
               __a							=    self.model.config.num_labels

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

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










from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__snake_case						:Tuple						=					{
    '''configuration_luke''': ['''LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LukeConfig'''],
    '''tokenization_luke''': ['''LukeTokenizer'''],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __snake_case						:Optional[int]						=					[
     '''LUKE_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''LukeForEntityClassification''',
     '''LukeForEntityPairClassification''',
     '''LukeForEntitySpanClassification''',
     '''LukeForMultipleChoice''',
     '''LukeForQuestionAnswering''',
     '''LukeForSequenceClassification''',
     '''LukeForTokenClassification''',
     '''LukeForMaskedLM''',
     '''LukeModel''',
     '''LukePreTrainedModel''',
 ]


if TYPE_CHECKING:
 from .configuration_luke import LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP, LukeConfig
 from .tokenization_luke import LukeTokenizer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_luke import (
      LUKE_PRETRAINED_MODEL_ARCHIVE_LIST,
      LukeForEntityClassification,
      LukeForEntityPairClassification,
      LukeForEntitySpanClassification,
      LukeForMaskedLM,
      LukeForMultipleChoice,
      LukeForQuestionAnswering,
      LukeForSequenceClassification,
      LukeForTokenClassification,
      LukeModel,
      LukePreTrainedModel,
  )

else:
 import sys

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





