



'''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											=		logging.get_logger(__name__)






@add_end_docstrings(SCREAMING_SNAKE_CASE_	)
class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_	):
  """simple docstring"""




  def __init__(					self :      Dict     ,      *_a :      Tuple     ,      **_a :      str  )   ->   Optional[int]:
    super().__init__(*_a     ,      **_a  )
    requires_backends(self     ,      'decord'  )
    self.check_model_type(_a  )




  def 		_lowercase	(					self :      Optional[Any]     ,      _a :      Dict=None     ,      _a :      Dict=None     ,      _a :      List[Any]=None  )   ->   Any:
    __lowerCamelCase			:			Dict       			=					{}
    if frame_sampling_rate is not None:
      __lowerCamelCase			:			List[str]       			=					frame_sampling_rate
    if num_frames is not None:
      __lowerCamelCase			:			Any       			=					num_frames

    __lowerCamelCase			:			Union[str, Any]       			=					{}
    if top_k is not None:
      __lowerCamelCase			:			str       			=					top_k
    return preprocess_params, {}, postprocess_params




  def __call__(					self :      Any     ,      _a :      Union[str, List[str]]     ,      **_a :      Tuple  )   ->   str:
    return super().__call__(_a     ,      **_a  )




  def 		_lowercase	(					self :      Dict     ,      _a :      Optional[int]     ,      _a :      int=None     ,      _a :      Any=1  )   ->   str:
    if num_frames is None:
      __lowerCamelCase			:			Dict       			=					self.model.config.num_frames

    if video.startswith('http://'  ) or video.startswith('https://'  ):
      __lowerCamelCase			:			Any       			=					BytesIO(requests.get(_a  ).content  )

    __lowerCamelCase			:			Tuple       			=					VideoReader(_a  )
    videoreader.seek(0  )

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

    __lowerCamelCase			:			List[str]       			=					videoreader.get_batch(_a  ).asnumpy()
    __lowerCamelCase			:			Tuple       			=					list(_a  )

    __lowerCamelCase			:			str       			=					self.image_processor(_a     ,      return_tensors=self.framework  )
    return model_inputs




  def 		_lowercase	(					self :      Dict     ,      _a :      str  )   ->   str:
    __lowerCamelCase			:			Union[str, Any]       			=					self.model(**_a  )
    return model_outputs




  def 		_lowercase	(					self :      Optional[Any]     ,      _a :      Dict     ,      _a :      Any=5  )   ->   Union[str, Any]:
    if top_k > self.model.config.num_labels:
      __lowerCamelCase			:			Tuple       			=					self.model.config.num_labels

    if self.framework == "pt":
      __lowerCamelCase			:			str       			=					model_outputs.logits.softmax(-1  )[0]
      __lowerCamelCase			,__lowerCamelCase			:			List[str]       			=					probs.topk(_a  )
    else:
      raise ValueError(f'Unsupported framework: {self.framework}'  )

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











'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_UpperCamelCase											=		{
    'configuration_bridgetower': [
        'BRIDGETOWER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'BridgeTowerConfig',
        'BridgeTowerTextConfig',
        'BridgeTowerVisionConfig',
    ],
    'processing_bridgetower': ['BridgeTowerProcessor'],
}

try:
   if not is_vision_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _UpperCamelCase											=		['BridgeTowerImageProcessor']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _UpperCamelCase											=		[
       'BRIDGETOWER_PRETRAINED_MODEL_ARCHIVE_LIST',
       'BridgeTowerForContrastiveLearning',
       'BridgeTowerForImageAndTextRetrieval',
       'BridgeTowerForMaskedLM',
       'BridgeTowerModel',
       'BridgeTowerPreTrainedModel',
   ]


if TYPE_CHECKING:
   from .configuration_bridgetower import (
       BRIDGETOWER_PRETRAINED_CONFIG_ARCHIVE_MAP,
       BridgeTowerConfig,
       BridgeTowerTextConfig,
       BridgeTowerVisionConfig,
   )
   from .processing_bridgetower import BridgeTowerProcessor

   try:
      if not is_vision_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .image_processing_bridgetower import BridgeTowerImageProcessor

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_bridgetower import (
          BRIDGETOWER_PRETRAINED_MODEL_ARCHIVE_LIST,
          BridgeTowerForContrastiveLearning,
          BridgeTowerForImageAndTextRetrieval,
          BridgeTowerForMaskedLM,
          BridgeTowerModel,
          BridgeTowerPreTrainedModel,
      )


else:
   import sys

   _UpperCamelCase											=		_LazyModule(__name__, globals()['__file__'], _import_structure)






