

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     snake_case_			(				_a							):


							"""simple docstring"""





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




							def        A__  ( self					,						_A=None					,						_A=None					,						_A=None							):
												__lowerCAmelCase					=							{}
												if frame_sampling_rate is not None:
																	__lowerCAmelCase					=							frame_sampling_rate
												if num_frames is not None:
																	__lowerCAmelCase					=							num_frames

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




							def __call__( self					,						_A					,						**_A							):
												return super().__call__(_A					,						**_A							)




							def        A__  ( self					,						_A					,						_A=None					,						_A=1							):
												if num_frames is None:
																	__lowerCAmelCase					=							self.model.config.num_frames

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

												__lowerCAmelCase					=							VideoReader(_A							)
												videoreader.seek(0							)

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

												__lowerCAmelCase					=							videoreader.get_batch(_A							).asnumpy()
												__lowerCAmelCase					=							list(_A							)

												__lowerCAmelCase					=							self.image_processor(_A					,						return_tensors=self.framework							)
												return model_inputs




							def        A__  ( self					,						_A							):
												__lowerCAmelCase					=							self.model(**_A							)
												return model_outputs




							def        A__  ( self					,						_A					,						_A=5							):
												if top_k > self.model.config.num_labels:
																	__lowerCAmelCase					=							self.model.config.num_labels

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

												__lowerCAmelCase					=							scores.tolist()
												__lowerCAmelCase					=							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


lowerCamelCase      				=     {
    '''configuration_lilt''': ['''LILT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LiltConfig'''],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowerCamelCase      				=     [
		    '''LILT_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''LiltForQuestionAnswering''',
		    '''LiltForSequenceClassification''',
		    '''LiltForTokenClassification''',
		    '''LiltModel''',
		    '''LiltPreTrainedModel''',
		]

if TYPE_CHECKING:
		from .configuration_lilt import LILT_PRETRAINED_CONFIG_ARCHIVE_MAP, LiltConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_lilt import (
				    LILT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    LiltForQuestionAnswering,
				    LiltForSequenceClassification,
				    LiltForTokenClassification,
				    LiltModel,
				    LiltPreTrainedModel,
				)

else:
		import sys

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






