



'''simple docstring'''
from __future__ import annotations

from decimal import Decimal

from numpy import array






def A__    (     UpperCAmelCase_							):
						_UpperCamelCase					:		List[str] 				=   Decimal

						# Check if the provided matrix has 2 rows and 2 columns
						# since this implementation only works for 2x2 matrices
						if len(A__							) == 2 and len(matrix[0]							) == 2 and len(matrix[1]							) == 2:
												# Calculate the determinant of the matrix
												_UpperCamelCase					:		Optional[Any] 				=   float(
												    d(matrix[0][0]							) * d(matrix[1][1]							) - d(matrix[1][0]							) * d(matrix[0][1]							)							)
												if determinant == 0:
																		raise ValueError('This matrix has no inverse.'							)

												# Creates a copy of the matrix with swapped positions of the elements
												_UpperCamelCase					:		str 				=   [[0.0, 0.0], [0.0, 0.0]]
												_UpperCamelCase				,			_UpperCamelCase					:		Dict 				=   matrix[1][1], matrix[0][0]
												_UpperCamelCase				,			_UpperCamelCase					:		Optional[Any] 				=   -matrix[1][0], -matrix[0][1]

												# Calculate the inverse of the matrix
												return [
												    [(float(d(A__							)							) / determinant) or 0.0 for n in row] for row in swapped_matrix
												]
						elif (
						    len(A__							) == 3
						    and len(matrix[0]							) == 3
						    and len(matrix[1]							) == 3
						    and len(matrix[2]							) == 3
						):
												# Calculate the determinant of the matrix using Sarrus rule
												_UpperCamelCase					:		int 				=   float(
												    (
												        (d(matrix[0][0]							) * d(matrix[1][1]							) * d(matrix[2][2]							))
												        + (d(matrix[0][1]							) * d(matrix[1][2]							) * d(matrix[2][0]							))
												        + (d(matrix[0][2]							) * d(matrix[1][0]							) * d(matrix[2][1]							))
												    )
												    - (
												        (d(matrix[0][2]							) * d(matrix[1][1]							) * d(matrix[2][0]							))
												        + (d(matrix[0][1]							) * d(matrix[1][0]							) * d(matrix[2][2]							))
												        + (d(matrix[0][0]							) * d(matrix[1][2]							) * d(matrix[2][1]							))
												    )							)
												if determinant == 0:
																		raise ValueError('This matrix has no inverse.'							)

												# Creating cofactor matrix
												_UpperCamelCase					:		Tuple 				=   [
												    [d(0.0							), d(0.0							), d(0.0							)],
												    [d(0.0							), d(0.0							), d(0.0							)],
												    [d(0.0							), d(0.0							), d(0.0							)],
												]
												_UpperCamelCase					:		List[str] 				=   (d(matrix[1][1]							) * d(matrix[2][2]							)) - (
												    d(matrix[1][2]							) * d(matrix[2][1]							)
												)
												_UpperCamelCase					:		Tuple 				=   -(
												    (d(matrix[1][0]							) * d(matrix[2][2]							)) - (d(matrix[1][2]							) * d(matrix[2][0]							))
												)
												_UpperCamelCase					:		Union[str, Any] 				=   (d(matrix[1][0]							) * d(matrix[2][1]							)) - (
												    d(matrix[1][1]							) * d(matrix[2][0]							)
												)
												_UpperCamelCase					:		Optional[Any] 				=   -(
												    (d(matrix[0][1]							) * d(matrix[2][2]							)) - (d(matrix[0][2]							) * d(matrix[2][1]							))
												)
												_UpperCamelCase					:		Any 				=   (d(matrix[0][0]							) * d(matrix[2][2]							)) - (
												    d(matrix[0][2]							) * d(matrix[2][0]							)
												)
												_UpperCamelCase					:		List[Any] 				=   -(
												    (d(matrix[0][0]							) * d(matrix[2][1]							)) - (d(matrix[0][1]							) * d(matrix[2][0]							))
												)
												_UpperCamelCase					:		Dict 				=   (d(matrix[0][1]							) * d(matrix[1][2]							)) - (
												    d(matrix[0][2]							) * d(matrix[1][1]							)
												)
												_UpperCamelCase					:		Dict 				=   -(
												    (d(matrix[0][0]							) * d(matrix[1][2]							)) - (d(matrix[0][2]							) * d(matrix[1][0]							))
												)
												_UpperCamelCase					:		Dict 				=   (d(matrix[0][0]							) * d(matrix[1][1]							)) - (
												    d(matrix[0][1]							) * d(matrix[1][0]							)
												)

												# Transpose the cofactor matrix (Adjoint matrix)
												_UpperCamelCase					:		Dict 				=   array(A__							)
												for i in range(3							):
																		for j in range(3							):
																								_UpperCamelCase					:		Dict 				=   cofactor_matrix[j][i]

        # Inverse of the matrix using the formula (1/determinant) * adjoint matrix
												_UpperCamelCase					:		Dict 				=   array(A__							)
												for i in range(3							):
																		for j in range(3							):
																								inverse_matrix[i][j] /= d(A__							)

        # Calculate the inverse of the matrix
												return [[float(d(A__							)							) or 0.0 for n in row] for row in inverse_matrix]
						raise ValueError('Please provide a matrix of size 2x2 or 3x3.'							)







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

_lowerCamelCase	:       Any      	=	logging.get_logger(__name__)

@add_end_docstrings(_a		)
class  SCREAMING_SNAKE_CASE (  _a		):






    """simple docstring"""





    def __init__(  self      :					Any  ,						*UpperCamelCase__      :					Dict  ,						**UpperCamelCase__      :					Union[str, Any] ):


       """simple docstring"""







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






    def  A       (  self      :					Optional[int]  ,						UpperCamelCase__      :					Optional[int]=None  ,						UpperCamelCase__      :					Optional[Any]=None  ,						UpperCamelCase__      :					Optional[Any]=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      :					List[str]  ,						UpperCamelCase__      :					Union[str, List[str]]  ,						**UpperCamelCase__      :					Dict ):


       """simple docstring"""







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






    def  A       (  self      :					Tuple  ,						UpperCamelCase__      :					Union[str, Any]  ,						UpperCamelCase__      :					Tuple=None  ,						UpperCamelCase__      :					Tuple=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(UpperCamelCase__ ).content )

       UpperCamelCase					=  VideoReader(UpperCamelCase__ )
       videoreader.seek(0 )

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

       UpperCamelCase					=  videoreader.get_batch(UpperCamelCase__ ).asnumpy()
       UpperCamelCase					=  list(UpperCamelCase__ )

       UpperCamelCase					=  self.image_processor(UpperCamelCase__  ,						return_tensors=self.framework )
       return model_inputs






    def  A       (  self      :					Union[str, Any]  ,						UpperCamelCase__      :					List[str] ):


       """simple docstring"""







       UpperCamelCase					=  self.model(**UpperCamelCase__ )
       return model_outputs






    def  A       (  self      :					int  ,						UpperCamelCase__      :					str  ,						UpperCamelCase__      :					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(UpperCamelCase__ )
       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(UpperCamelCase__  ,						UpperCamelCase__ )]

