






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(_snake_case			)
class   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		def __init__(							self				,		*_UpperCamelCase				,		**_UpperCamelCase   )	->							List[Any]:
							super().__init__(*_UpperCamelCase				,		**_UpperCamelCase   )
							requires_backends(self				,		'decord'   )
							self.check_model_type(_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase=None				,		_UpperCamelCase=None				,		_UpperCamelCase=None   )	->							Dict:
							UpperCAmelCase_    :      Optional[Any]            =						{}
							if frame_sampling_rate is not None:
												UpperCAmelCase_    :      Optional[Any]            =						frame_sampling_rate
							if num_frames is not None:
												UpperCAmelCase_    :      Optional[Any]            =						num_frames

							UpperCAmelCase_    :      Optional[Any]            =						{}
							if top_k is not None:
												UpperCAmelCase_    :      List[Any]            =						top_k
							return preprocess_params, {}, postprocess_params



		def __call__(							self				,		_UpperCamelCase				,		**_UpperCamelCase   )	->							Union[str, Any]:
							return super().__call__(_UpperCamelCase				,		**_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=None				,		_UpperCamelCase=1   )	->							Optional[int]:
							if num_frames is None:
												UpperCAmelCase_    :      Optional[Any]            =						self.model.config.num_frames

							if video.startswith('http://'   ) or video.startswith('https://'   ):
												UpperCAmelCase_    :      List[str]            =						BytesIO(requests.get(_UpperCamelCase   ).content   )

							UpperCAmelCase_    :      Optional[Any]            =						VideoReader(_UpperCamelCase   )
							videoreader.seek(0   )

							UpperCAmelCase_    :      List[Any]            =						0
							UpperCAmelCase_    :      Union[str, Any]            =						num_frames * frame_sampling_rate - 1
							UpperCAmelCase_    :      Optional[int]            =						np.linspace(_UpperCamelCase				,		_UpperCamelCase				,		num=_UpperCamelCase				,		dtype=np.intaa   )

							UpperCAmelCase_    :      List[Any]            =						videoreader.get_batch(_UpperCamelCase   ).asnumpy()
							UpperCAmelCase_    :      Union[str, Any]            =						list(_UpperCamelCase   )

							UpperCAmelCase_    :      List[Any]            =						self.image_processor(_UpperCamelCase				,		return_tensors=self.framework   )
							return model_inputs



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase   )	->							List[Any]:
							UpperCAmelCase_    :      Dict            =						self.model(**_UpperCamelCase   )
							return model_outputs



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=5   )	->							List[str]:
							if top_k > self.model.config.num_labels:
												UpperCAmelCase_    :      List[Any]            =						self.model.config.num_labels

							if self.framework == "pt":
												UpperCAmelCase_    :      Dict            =						model_outputs.logits.softmax(-1   )[0]
												UpperCAmelCase_       ,  UpperCAmelCase_    :      Tuple            =						probs.topk(_UpperCamelCase   )
							else:
												raise ValueError(f"Unsupported framework: {self.framework}"   )

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









import os




def 			UpperCAmelCase__				(			_A      : Any							):





  '''simple docstring'''







  a__  =len(grid[0]							)
  a__  =len(_A							)

  a__  =0
  a__  =0
  a__  =0

  # Check vertically, horizontally, diagonally at the same time (only works
  # for nxn grid)
  for i in range(_A							):
    for j in range(n_rows - 3							):
      a__  =grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]
      a__  =grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]

      # Left-to-right diagonal (\) product
      if i < n_columns - 3:
        a__  =(
            grid[i][j]
            * grid[i + 1][j + 1]
            * grid[i + 2][j + 2]
            * grid[i + 3][j + 3]
        )

      # Right-to-left diagonal(/) product
      if i > 2:
        a__  =(
            grid[i][j]
            * grid[i - 1][j + 1]
            * grid[i - 2][j + 2]
            * grid[i - 3][j + 3]
        )

      a__  =max(
          _A	,      _A	,      _A	,      _A							)
      if max_product > largest:
        a__  =max_product

  return largest




def 			UpperCAmelCase__				(			):





  '''simple docstring'''







  a__  =[]
  with open(os.path.dirname(_A							) + '''/grid.txt'''							) as file:
    for line in file:
      grid.append(line.strip('''\n'''							).split(''' '''							)							)

  a__  =[[int(_A							) for i in grid[j]] for j in range(len(_A							)							)]

  return largest_product(_A							)


if __name__ == "__main__":
       print(solution())
