

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

SCREAMING_SNAKE_CASE_    :		List[Any]							     =  logging.get_logger(__name__)



@add_end_docstrings(__a )
class     a  (				__a ):



    """simple docstring"""



    def __init__(   self:       List[str]			,  *UpperCamelCase:       Optional[int]			,  **UpperCamelCase:       Tuple				):






         """simple docstring"""


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

    def  UpperCamelCase		(   self:       List[str]			,  UpperCamelCase:       Union[str, Any]=None			,  UpperCamelCase:       List[Any]=None			,  UpperCamelCase:       Tuple=None				):






         """simple docstring"""


         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:       List[Any]			,  UpperCamelCase:       List[Any]			,  **UpperCamelCase:       List[Any]				):






         """simple docstring"""


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

    def  UpperCamelCase		(   self:       int			,  UpperCamelCase:       Any			,  UpperCamelCase:       List[Any]=None			,  UpperCamelCase:       Union[str, Any]=1				):






         """simple docstring"""


         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  UpperCamelCase		(   self:       Any			,  UpperCamelCase:       List[str]				):






         """simple docstring"""


         A__								=     self.model(**_UpperCamelCase				)
         return model_outputs





    def  UpperCamelCase		(   self:       List[Any]			,  UpperCamelCase:       Optional[int]			,  UpperCamelCase:       List[Any]=5				):






         """simple docstring"""


         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				)]








import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin





@require_tokenizers
class 	_lowerCAmelCase					(	__a					,      unittest.TestCase       ):
				_lowercase					=None
				_lowercase					=BloomTokenizerFast
				_lowercase					=BloomTokenizerFast
				_lowercase					=True
				_lowercase					=False
				_lowercase					='''tokenizer_file'''
				_lowercase					={'''bos_token''': '''<s>''', '''eos_token''': '''</s>''', '''unk_token''': '''<unk>''', '''pad_token''': '''<pad>'''}




				def      __a						(					self     )				->      Dict:
							super().setUp()
							lowerCAmelCase_						=	BloomTokenizerFast.from_pretrained("bigscience/tokenizer"     )
							tokenizer.save_pretrained(self.tmpdirname     )




				def      __a						(					self				,				**_UpperCamelCase     )				->      Tuple:
							kwargs.update(self.special_tokens_map     )
							return BloomTokenizerFast.from_pretrained(self.tmpdirname				,				**_UpperCamelCase     )




				def      __a						(					self     )				->      List[str]:
							lowerCAmelCase_						=	self.get_rust_tokenizer()

							lowerCAmelCase_						=	["The quick brown fox</s>", "jumps over the lazy dog</s>"]
							lowerCAmelCase_						=	[[2_175, 23_714, 73_173, 144_252, 2], [77, 132_619, 3_478, 368, 109_586, 35_433, 2]]

							lowerCAmelCase_						=	tokenizer.batch_encode_plus(_UpperCamelCase     )["input_ids"]
							self.assertListEqual(_UpperCamelCase				,				_UpperCamelCase     )

							lowerCAmelCase_						=	tokenizer.batch_decode(_UpperCamelCase     )
							self.assertListEqual(_UpperCamelCase				,				_UpperCamelCase     )




				def      __a						(					self				,				_UpperCamelCase=6     )				->      Tuple:
							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
										with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""     ):
													lowerCAmelCase_						=	self.rust_tokenizer_class.from_pretrained(_UpperCamelCase				,				**_UpperCamelCase     )
													# tokenizer_r.pad_token = None # Hotfixing padding = None
													# Simple input
													lowerCAmelCase_						=	"This is a simple input"
													lowerCAmelCase_						=	["This is a simple input 1", "This is a simple input 2"]
													lowerCAmelCase_						=	("This is a simple input", "This is a pair")
													lowerCAmelCase_						=	[
													    ("This is a simple input 1", "This is a simple input 2"),
													    ("This is a simple pair 1", "This is a simple pair 2"),
													]

													# Simple input tests
													try:
																tokenizer_r.encode(_UpperCamelCase				,				max_length=_UpperCamelCase     )
																tokenizer_r.encode_plus(_UpperCamelCase				,				max_length=_UpperCamelCase     )

																tokenizer_r.batch_encode_plus(_UpperCamelCase				,				max_length=_UpperCamelCase     )
																tokenizer_r.encode(_UpperCamelCase				,				max_length=_UpperCamelCase     )
																tokenizer_r.batch_encode_plus(_UpperCamelCase				,				max_length=_UpperCamelCase     )
													except ValueError:
																self.fail("Bloom Tokenizer should be able to deal with padding"     )

													lowerCAmelCase_						=	None  # Hotfixing padding = None
													self.assertRaises(_UpperCamelCase				,				tokenizer_r.encode				,				_UpperCamelCase				,				max_length=_UpperCamelCase				,				padding="max_length"     )

													# Simple input
													self.assertRaises(_UpperCamelCase				,				tokenizer_r.encode_plus				,				_UpperCamelCase				,				max_length=_UpperCamelCase				,				padding="max_length"     )

													# Simple input
													self.assertRaises(
													    _UpperCamelCase				,				tokenizer_r.batch_encode_plus				,				_UpperCamelCase				,				max_length=_UpperCamelCase				,				padding="max_length"				,				)

													# Pair input
													self.assertRaises(_UpperCamelCase				,				tokenizer_r.encode				,				_UpperCamelCase				,				max_length=_UpperCamelCase				,				padding="max_length"     )

													# Pair input
													self.assertRaises(_UpperCamelCase				,				tokenizer_r.encode_plus				,				_UpperCamelCase				,				max_length=_UpperCamelCase				,				padding="max_length"     )

													# Pair input
													self.assertRaises(
													    _UpperCamelCase				,				tokenizer_r.batch_encode_plus				,				_UpperCamelCase				,				max_length=_UpperCamelCase				,				padding="max_length"				,				)




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	self.get_rust_tokenizer()
							lowerCAmelCase_						=	load_dataset("xnli"				,				"all_languages"				,				split="test"				,				streaming=_UpperCamelCase     )

							lowerCAmelCase_						=	next(iter(_UpperCamelCase     )     )["premise"]  # pick up one data
							lowerCAmelCase_						=	list(sample_data.values()     )

							lowerCAmelCase_						=	list(map(tokenizer.encode				,				_UpperCamelCase     )     )
							lowerCAmelCase_						=	[tokenizer.decode(_UpperCamelCase				,				clean_up_tokenization_spaces=_UpperCamelCase     ) for x in output_tokens]
							self.assertListEqual(_UpperCamelCase				,				_UpperCamelCase     )




				def      __a						(					self     )				->      List[Any]:
							# The test has to be overriden because BLOOM uses ALiBi positional embeddings that does not have
							# any sequence length constraints. This test of the parent class will fail since it relies on the
							# maximum sequence length of the positoonal embeddings.
							self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map     )				,				1     )
							self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values()     )[0]     )				,				1     )



