


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

__snake_case							   =  logging.get_logger(__name__)
@add_end_docstrings(UpperCamelCase_      )
class      lowercase			(							UpperCamelCase_      ):

		"""simple docstring"""

		def __init__(	self					,     *UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


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




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_=None					,     UpperCamelCase_=None					,     UpperCamelCase_=None							):

							'''simple docstring'''


							UpperCamelCase__     :Tuple							      =				{}
							if frame_sampling_rate is not None:
												UpperCamelCase__     :Optional[int]							      =				frame_sampling_rate
							if num_frames is not None:
												UpperCamelCase__     :Dict							      =				num_frames

							UpperCamelCase__     :List[str]							      =				{}
							if top_k is not None:
												UpperCamelCase__     :Optional[int]							      =				top_k
							return preprocess_params, {}, postprocess_params




		def __call__(	self					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


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




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_=None					,     UpperCamelCase_=1							):

							'''simple docstring'''


							if num_frames is None:
												UpperCamelCase__     :Tuple							      =				self.model.config.num_frames

							if video.startswith('''http://'''							) or video.startswith('''https://'''							):
												UpperCamelCase__     :Optional[Any]							      =				BytesIO(requests.get(__a							).content							)

							UpperCamelCase__     :Dict							      =				VideoReader(__a							)
							videoreader.seek(0							)

							UpperCamelCase__     :Dict							      =				0
							UpperCamelCase__     :Dict							      =				num_frames * frame_sampling_rate - 1
							UpperCamelCase__     :List[str]							      =				np.linspace(__a					,     __a					,     num=__a					,     dtype=np.intaa							)

							UpperCamelCase__     :str							      =				videoreader.get_batch(__a							).asnumpy()
							UpperCamelCase__     :List[str]							      =				list(__a							)

							UpperCamelCase__     :Tuple							      =				self.image_processor(__a					,     return_tensors=self.framework							)
							return model_inputs




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__     :Union[str, Any]							      =				self.model(**__a							)
							return model_outputs




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_=5							):

							'''simple docstring'''


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

							if self.framework == "pt":
												UpperCamelCase__     :Dict							      =				model_outputs.logits.softmax(-1							)[0]
												UpperCamelCase__     :Union[str, Any]							      =				probs.topk(__a							)
							else:
												raise ValueError(F'''Unsupported framework: {self.framework}'''							)

							UpperCamelCase__     :Tuple							      =				scores.tolist()
							UpperCamelCase__     :int							      =				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_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


__snake_case							   =  {
    '''configuration_layoutlmv3''': [
        '''LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''LayoutLMv3Config''',
        '''LayoutLMv3OnnxConfig''',
    ],
    '''processing_layoutlmv3''': ['''LayoutLMv3Processor'''],
    '''tokenization_layoutlmv3''': ['''LayoutLMv3Tokenizer'''],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case							   =  ['''LayoutLMv3TokenizerFast''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case							   =  [
		    '''LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''LayoutLMv3ForQuestionAnswering''',
		    '''LayoutLMv3ForSequenceClassification''',
		    '''LayoutLMv3ForTokenClassification''',
		    '''LayoutLMv3Model''',
		    '''LayoutLMv3PreTrainedModel''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case							   =  [
		    '''TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFLayoutLMv3ForQuestionAnswering''',
		    '''TFLayoutLMv3ForSequenceClassification''',
		    '''TFLayoutLMv3ForTokenClassification''',
		    '''TFLayoutLMv3Model''',
		    '''TFLayoutLMv3PreTrainedModel''',
		]

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case							   =  ['''LayoutLMv3FeatureExtractor''']
		__snake_case							   =  ['''LayoutLMv3ImageProcessor''']


if TYPE_CHECKING:
		from .configuration_layoutlmva import (
		    LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    LayoutLMvaConfig,
		    LayoutLMvaOnnxConfig,
		)
		from .processing_layoutlmva import LayoutLMvaProcessor
		from .tokenization_layoutlmva import LayoutLMvaTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_layoutlmva import (
				    LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
				    LayoutLMvaForQuestionAnswering,
				    LayoutLMvaForSequenceClassification,
				    LayoutLMvaForTokenClassification,
				    LayoutLMvaModel,
				    LayoutLMvaPreTrainedModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_layoutlmva import (
				    TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFLayoutLMvaForQuestionAnswering,
				    TFLayoutLMvaForSequenceClassification,
				    TFLayoutLMvaForTokenClassification,
				    TFLayoutLMvaModel,
				    TFLayoutLMvaPreTrainedModel,
				)

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
				from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
		import sys

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