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 		=      logging.get_logger(__name__)
@add_end_docstrings(__lowerCAmelCase		)
class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):







   def __init__(     self						:						List[Any]   ,   *lowerCamelCase_						:						Union[str, Any]   ,   **lowerCamelCase_						:						Union[str, Any]				):





    """simple docstring"""


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







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





    """simple docstring"""


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







   def 						lowerCamelCase_	(     self						:						List[str]   ,   lowerCamelCase_						:						Optional[Any]   ,   lowerCamelCase_						:						Any=None   ,   lowerCamelCase_						:						Dict=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(lowerCamelCase_				).content				)

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

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

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

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







   def 						lowerCamelCase_	(     self						:						str   ,   lowerCamelCase_						:						Optional[Any]				):





    """simple docstring"""


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







   def 						lowerCamelCase_	(     self						:						Optional[Any]   ,   lowerCamelCase_						:						Any   ,   lowerCamelCase_						:						Optional[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(lowerCamelCase_				)
    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(lowerCamelCase_   ,   lowerCamelCase_				)]

from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE 		=      logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE 		=      {
    """sail/poolformer_s12""": """https://huggingface.co/sail/poolformer_s12/resolve/main/config.json""",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
}
class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):
   __lowerCAmelCase           =				"""poolformer"""







   def __init__(     self						:						str   ,   lowerCamelCase_						:						Any=3   ,   lowerCamelCase_						:						Any=16   ,   lowerCamelCase_						:						Any=16   ,   lowerCamelCase_						:						Optional[Any]=3   ,   lowerCamelCase_						:						List[str]=4.0   ,   lowerCamelCase_						:						Tuple=[2, 2, 6, 2]   ,   lowerCamelCase_						:						Optional[Any]=[64, 128, 320, 512]   ,   lowerCamelCase_						:						Tuple=[7, 3, 3, 3]   ,   lowerCamelCase_						:						Optional[Any]=[4, 2, 2, 2]   ,   lowerCamelCase_						:						Dict=[2, 1, 1, 1]   ,   lowerCamelCase_						:						Tuple=4   ,   lowerCamelCase_						:						Optional[int]=0.0   ,   lowerCamelCase_						:						Union[str, Any]="gelu"   ,   lowerCamelCase_						:						Union[str, Any]=True   ,   lowerCamelCase_						:						Union[str, Any]=1E-5   ,   lowerCamelCase_						:						int=0.0_2   ,   **lowerCamelCase_						:						str   ,   ):





    """simple docstring"""


    UpperCamelCase			  =      num_channels
    UpperCamelCase			  =      patch_size
    UpperCamelCase			  =      stride
    UpperCamelCase			  =      padding
    UpperCamelCase			  =      pool_size
    UpperCamelCase			  =      hidden_sizes
    UpperCamelCase			  =      mlp_ratio
    UpperCamelCase			  =      depths
    UpperCamelCase			  =      patch_sizes
    UpperCamelCase			  =      strides
    UpperCamelCase			  =      num_encoder_blocks
    UpperCamelCase			  =      drop_path_rate
    UpperCamelCase			  =      hidden_act
    UpperCamelCase			  =      use_layer_scale
    UpperCamelCase			  =      layer_scale_init_value
    UpperCamelCase			  =      initializer_range
    super().__init__(**lowerCamelCase_				)





class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):
   __lowerCAmelCase           =				version.parse("""1.11"""		)







   @property
   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    return OrderedDict(
        [
            ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
        ]				)







   @property
   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    return 2E-3
