


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


@add_end_docstrings(UpperCamelCase__     )
class 			_a				(     UpperCamelCase__     ):

							def __init__(				self:      Optional[Any]       ,		*UpperCamelCase_:      Tuple       ,		**UpperCamelCase_:      Any				)     ->  int:





									"""simple docstring"""







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

							def  lowerCamelCase_					(				self:      Tuple       ,		UpperCamelCase_:      int=None       ,		UpperCamelCase_:      List[Any]=None       ,		UpperCamelCase_:      List[Any]=None				)     ->  Optional[Any]:





									"""simple docstring"""







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

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

							def __call__(				self:      List[Any]       ,		UpperCamelCase_:      Union[str, List[str]]       ,		**UpperCamelCase_:      List[Any]				)     ->  Dict:





									"""simple docstring"""







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

							def  lowerCamelCase_					(				self:      List[Any]       ,		UpperCamelCase_:      Any       ,		UpperCamelCase_:      List[Any]=None       ,		UpperCamelCase_:      Union[str, Any]=1				)     ->  int:





									"""simple docstring"""







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

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

									lowercase__				= VideoReader(UpperCamelCase_				)
									videoreader.seek(0				)

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

									lowercase__				= videoreader.get_batch(UpperCamelCase_				).asnumpy()
									lowercase__				= list(UpperCamelCase_				)

									lowercase__				= self.image_processor(UpperCamelCase_       ,		return_tensors=self.framework				)
									return model_inputs

							def  lowerCamelCase_					(				self:      Tuple       ,		UpperCamelCase_:      List[Any]				)     ->  Any:





									"""simple docstring"""







									lowercase__				= self.model(**UpperCamelCase_				)
									return model_outputs


							def  lowerCamelCase_					(				self:      Tuple       ,		UpperCamelCase_:      int       ,		UpperCamelCase_:      List[str]=5				)     ->  str:





									"""simple docstring"""







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

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

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




"""simple docstring"""


from typing import TYPE_CHECKING

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


_lowercase				 =					{
    '''configuration_mctct''': ['''MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MCTCTConfig'''],
    '''feature_extraction_mctct''': ['''MCTCTFeatureExtractor'''],
    '''processing_mctct''': ['''MCTCTProcessor'''],
}


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase				 =					[
		    '''MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''MCTCTForCTC''',
		    '''MCTCTModel''',
		    '''MCTCTPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig
		from .feature_extraction_mctct import MCTCTFeatureExtractor
		from .processing_mctct import MCTCTProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel

else:
		import sys

		_lowercase				 =					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)