





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

_SCREAMING_SNAKE_CASE 		=						logging.get_logger(__name__)
@add_end_docstrings(snake_case_   )
class SCREAMING_SNAKE_CASE_  (   snake_case_   ):






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







							"""simple docstring"""


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






						def UpperCAmelCase_			( self : List[str]						,				_A : Optional[int]=None						,				_A : Tuple=None						,				_A : Any=None		)							->      Any:







							"""simple docstring"""


							snake_case_						:     Tuple     =		{}
							if frame_sampling_rate is not None:
								snake_case_						:     Optional[Any]     =		frame_sampling_rate
							if num_frames is not None:
								snake_case_						:     int     =		num_frames

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






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







							"""simple docstring"""


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






						def UpperCAmelCase_			( self : int						,				_A : List[Any]						,				_A : Any=None						,				_A : Any=1		)							->      Dict:







							"""simple docstring"""


							if num_frames is None:
								snake_case_						:     int     =		self.model.config.num_frames

							if video.startswith('http://'		) or video.startswith('https://'		):
								snake_case_						:     Tuple     =		BytesIO(requests.get(_A		).content		)

							snake_case_						:     Optional[Any]     =		VideoReader(_A		)
							videoreader.seek(0		)

							snake_case_						:     int     =		0
							snake_case_						:     Tuple     =		num_frames * frame_sampling_rate - 1
							snake_case_						:     Any     =		np.linspace(_A						,				_A						,				num=_A						,				dtype=np.intaa		)

							snake_case_						:     str     =		videoreader.get_batch(_A		).asnumpy()
							snake_case_						:     List[Any]     =		list(_A		)

							snake_case_						:     Any     =		self.image_processor(_A						,				return_tensors=self.framework		)
							return model_inputs






						def UpperCAmelCase_			( self : Any						,				_A : List[str]		)							->      Any:







							"""simple docstring"""


							snake_case_						:     str     =		self.model(**_A		)
							return model_outputs






						def UpperCAmelCase_			( self : int						,				_A : Union[str, Any]						,				_A : Any=5		)							->      str:







							"""simple docstring"""


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

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

							snake_case_						:     List[str]     =		scores.tolist()
							snake_case_						:     Dict     =		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


_SCREAMING_SNAKE_CASE 		=						{
    """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:
						_SCREAMING_SNAKE_CASE 		=						[
						    """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

						_SCREAMING_SNAKE_CASE 		=						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

