



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

UpperCamelCase__ =						logging.get_logger(__name__)
@add_end_docstrings(_UpperCAmelCase			)
class 			_lowerCAmelCase   ( _UpperCAmelCase			):






			"""simple docstring"""





			def __init__(				self	:       Dict			,     *lowercase	:       List[Any]			,     **lowercase	:       Union[str, Any]   )						->					List[str]:
									"""simple docstring"""







									super().__init__(*lowercase			,     **lowercase   )
									requires_backends(self			,     """decord"""   )
									self.check_model_type(lowercase   )





			def 		snake_case__			(				self	:       Optional[int]			,     lowercase	:       Optional[int]=None			,     lowercase	:       List[str]=None			,     lowercase	:       str=None   )						->					Union[str, 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	:       Tuple			,     lowercase	:       Union[str, List[str]]			,     **lowercase	:       Union[str, Any]   )						->					str:
									"""simple docstring"""







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





			def 		snake_case__			(				self	:       Tuple			,     lowercase	:       Union[str, Any]			,     lowercase	:       List[str]=None			,     lowercase	:       Dict=1   )						->					Any:
									"""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(lowercase   ).content   )

									__lowercase					=				VideoReader(lowercase   )
									videoreader.seek(0   )

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

									__lowercase					=				videoreader.get_batch(lowercase   ).asnumpy()
									__lowercase					=				list(lowercase   )

									__lowercase					=				self.image_processor(lowercase			,     return_tensors=self.framework   )
									return model_inputs





			def 		snake_case__			(				self	:       Tuple			,     lowercase	:       Dict   )						->					List[str]:
									"""simple docstring"""







									__lowercase					=				self.model(**lowercase   )
									return model_outputs







			def 		snake_case__			(				self	:       Optional[int]			,     lowercase	:       Optional[Any]			,     lowercase	:       Tuple=5   )						->					int:
									"""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(lowercase   )
									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(lowercase			,     lowercase   )]










import os
import unittest

from transformers import LxmertTokenizer, LxmertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin
@require_tokenizers
class 			_lowerCAmelCase   ( _UpperCAmelCase    ,					unittest.TestCase			):






			"""simple docstring"""





			lowercase__      :  Tuple      =			LxmertTokenizer
			lowercase__      :  List[str]      =			LxmertTokenizerFast
			lowercase__      :  Optional[Any]      =			True
			lowercase__      :  List[Any]      =			True





			def 		snake_case__			(				self	:       Tuple   )						->					Tuple:
									"""simple docstring"""







									super().setUp()

									__lowercase					=				[
									    """[UNK]""",
									    """[CLS]""",
									    """[SEP]""",
									    """want""",
									    """##want""",
									    """##ed""",
									    """wa""",
									    """un""",
									    """runn""",
									    """##ing""",
									    """,""",
									    """low""",
									    """lowest""",
									]
									__lowercase					=				os.path.join(self.tmpdirname			,     VOCAB_FILES_NAMES["""vocab_file"""]   )
									with open(self.vocab_file			,     """w"""			,     encoding="""utf-8"""   ) as vocab_writer:
															vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]   )   )





			def 		snake_case__			(				self	:       Optional[int]			,     lowercase	:       int   )						->					List[Any]:
									"""simple docstring"""







									__lowercase					=				"""UNwant\u00E9d,running"""
									__lowercase					=				"""unwanted, running"""
									return input_text, output_text





			def 		snake_case__			(				self	:       str   )						->					Any:
									"""simple docstring"""







									__lowercase					=				self.tokenizer_class(self.vocab_file   )

									__lowercase					=				tokenizer.tokenize("""UNwant\u00E9d,running"""   )
									self.assertListEqual(lowercase			,     ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""]   )
									self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase   )			,     [7, 4, 5, 10, 8, 9]   )







			def 		snake_case__			(				self	:       Union[str, Any]   )						->					int:
									"""simple docstring"""







									if not self.test_rust_tokenizer:
															return

									__lowercase					=				self.get_tokenizer()
									__lowercase					=				self.get_rust_tokenizer()

									__lowercase					=				"""I was born in 92000, and this is falsé."""

									__lowercase					=				tokenizer.tokenize(lowercase   )
									__lowercase					=				rust_tokenizer.tokenize(lowercase   )
									self.assertListEqual(lowercase			,     lowercase   )

									__lowercase					=				tokenizer.encode(lowercase			,     add_special_tokens=lowercase   )
									__lowercase					=				rust_tokenizer.encode(lowercase			,     add_special_tokens=lowercase   )
									self.assertListEqual(lowercase			,     lowercase   )

									__lowercase					=				self.get_rust_tokenizer()
									__lowercase					=				tokenizer.encode(lowercase   )
									__lowercase					=				rust_tokenizer.encode(lowercase   )
									self.assertListEqual(lowercase			,     lowercase   )





