


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







       def __init__(       self:      Optional[int],*A_:      Tuple,**A_:      Union[str, Any] ):






            '''simple docstring'''




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







       def 						snake_case_     (       self:      Union[str, Any],A_:      int=None,A_:      str=None,A_:      int=None ):






            '''simple docstring'''




            __UpperCamelCase      =       {}
            if frame_sampling_rate is not None:
                 __UpperCamelCase      =       frame_sampling_rate
            if num_frames is not None:
                 __UpperCamelCase      =       num_frames

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







       def __call__(       self:      str,A_:      Union[str, List[str]],**A_:      Dict ):






            '''simple docstring'''




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







       def 						snake_case_     (       self:      Any,A_:      List[Any],A_:      Union[str, Any]=None,A_:      Union[str, Any]=1 ):






            '''simple docstring'''




            if num_frames is None:
                 __UpperCamelCase      =       self.model.config.num_frames

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

            __UpperCamelCase      =       VideoReader(SCREAMING_SNAKE_CASE_ )
            videoreader.seek(0 )

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

            __UpperCamelCase      =       videoreader.get_batch(SCREAMING_SNAKE_CASE_ ).asnumpy()
            __UpperCamelCase      =       list(SCREAMING_SNAKE_CASE_ )

            __UpperCamelCase      =       self.image_processor(SCREAMING_SNAKE_CASE_,return_tensors=self.framework )
            return model_inputs







       def 						snake_case_     (       self:      Union[str, Any],A_:      Optional[int] ):






            '''simple docstring'''




            __UpperCamelCase      =       self.model(**SCREAMING_SNAKE_CASE_ )
            return model_outputs







       def 						snake_case_     (       self:      int,A_:      List[Any],A_:      List[Any]=5 ):






            '''simple docstring'''




            if top_k > self.model.config.num_labels:
                 __UpperCamelCase      =       self.model.config.num_labels

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

            __UpperCamelCase      =       scores.tolist()
            __UpperCamelCase      =       ids.tolist()
            return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ )]




import tempfile
import unittest

from make_student import create_student_by_copying_alternating_layers

from transformers import AutoConfig
from transformers.file_utils import cached_property
from transformers.testing_utils import require_torch


__a  			=					'sshleifer/bart-tiny-random'
__a  			=					'patrickvonplaten/t5-tiny-random'
@require_torch
class  lowercase__(				unittest.TestCase							):



	"""simple docstring"""




	@cached_property
	def 						_lowercase  (       self  :			Any       )       ->     Tuple:
					return AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			int       )       ->     List[Any]:
					lowercase_  ,	*lowercase_       =					create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_       ,							tempfile.mkdtemp()       ,							e=1       ,							d=1       )
					self.assertEqual(student.config.num_hidden_layers       ,							1       )
	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     Optional[int]:
					lowercase_  ,	*lowercase_       =					create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_       ,							tempfile.mkdtemp()       ,							e=1       ,							d=SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			List[str]       )       ->     List[Any]:
					lowercase_  ,	*lowercase_       =					create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_       ,							tempfile.mkdtemp()       ,							e=1       ,							d=SCREAMING_SNAKE_CASE_       )
					self.assertEqual(student.config.encoder_layers       ,							1       )
					self.assertEqual(student.config.decoder_layers       ,							self.teacher_config.encoder_layers       )
	def 						_lowercase  (       self  :			List[Any]       )       ->     List[Any]:
					lowercase_  ,	*lowercase_       =					create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_       ,							tempfile.mkdtemp()       ,							e=1       ,							d=1       )
					self.assertEqual(student.config.encoder_layers       ,							1       )
					self.assertEqual(student.config.decoder_layers       ,							1       )




	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     Optional[int]:
					with self.assertRaises(SCREAMING_SNAKE_CASE_       ):
									create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_       ,							tempfile.mkdtemp()       ,							e=SCREAMING_SNAKE_CASE_       ,							d=SCREAMING_SNAKE_CASE_       )




