





"""simple docstring"""




from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__lowerCamelCase											=  logging.get_logger(__name__)

__lowerCamelCase											=  {
    """facebook/levit-128S""": """https://huggingface.co/facebook/levit-128S/resolve/main/config.json""",
    # See all LeViT models at https://huggingface.co/models?filter=levit
}
class     UpperCamelCase__(			lowercase_ ):
						lowerCAmelCase__  :						int													=		'''levit'''





						def __init__(       self		,__UpperCAmelCase=2_24		,__UpperCAmelCase=3		,__UpperCAmelCase=3		,__UpperCAmelCase=2		,__UpperCAmelCase=1		,__UpperCAmelCase=16		,__UpperCAmelCase=[1_28, 2_56, 3_84]		,__UpperCAmelCase=[4, 8, 12]		,__UpperCAmelCase=[4, 4, 4]		,__UpperCAmelCase=[16, 16, 16]		,__UpperCAmelCase=0		,__UpperCAmelCase=[2, 2, 2]		,__UpperCAmelCase=[2, 2, 2]		,__UpperCAmelCase=0.0_2		,**__UpperCAmelCase		,)					->				str:
													super().__init__(**__UpperCAmelCase     )
													A__	     =							image_size
													A__	     =							num_channels
													A__	     =							kernel_size
													A__	     =							stride
													A__	     =							padding
													A__	     =							hidden_sizes
													A__	     =							num_attention_heads
													A__	     =							depths
													A__	     =							key_dim
													A__	     =							drop_path_rate
													A__	     =							patch_size
													A__	     =							attention_ratio
													A__	     =							mlp_ratio
													A__	     =							initializer_range
													A__	     =							[
													    ['Subsample', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
													    ['Subsample', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
													]






class     UpperCamelCase__(			lowercase_ ):
						lowerCAmelCase__  :						Dict													=		version.parse('1.11' )





						@property
						def 				snake_case__	(       self     )					->				Dict:
													return OrderedDict(
													    [
													        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
													    ]     )





						@property
						def 				snake_case__	(       self     )					->				str:
													return 1e-4






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

__lowerCamelCase											=  logging.get_logger(__name__)
@add_end_docstrings(__A )
class     UpperCamelCase__(			__A ):





						def __init__(       self		,*__UpperCAmelCase		,**__UpperCAmelCase     )					->				List[Any]:
													super().__init__(*__UpperCAmelCase		,**__UpperCAmelCase     )
													requires_backends(self		,'decord'     )
													self.check_model_type(__UpperCAmelCase     )





						def 				snake_case__	(       self		,__UpperCAmelCase=None		,__UpperCAmelCase=None		,__UpperCAmelCase=None     )					->				Any:
													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		,__UpperCAmelCase		,**__UpperCAmelCase     )					->				Optional[int]:
													return super().__call__(__UpperCAmelCase		,**__UpperCAmelCase     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase=None		,__UpperCAmelCase=1     )					->				List[Any]:
													if num_frames is None:
																				A__	     =							self.model.config.num_frames

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

													A__	     =							VideoReader(__UpperCAmelCase     )
													videoreader.seek(0     )

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

													A__	     =							videoreader.get_batch(__UpperCAmelCase     ).asnumpy()
													A__	     =							list(__UpperCAmelCase     )

													A__	     =							self.image_processor(__UpperCAmelCase		,return_tensors=self.framework     )
													return model_inputs





						def 				snake_case__	(       self		,__UpperCAmelCase     )					->				Optional[int]:
													A__	     =							self.model(**__UpperCAmelCase     )
													return model_outputs





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase=5     )					->				int:
													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(__UpperCAmelCase     )
													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(__UpperCAmelCase		,__UpperCAmelCase     )]





