





'''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       		=      logging.get_logger(__name__)
@add_end_docstrings(_A       )
class 		lowerCamelCase  (      _A       ):



	def __init__(   self		,      *a_		,      **a_	):
			super().__init__(*a_		,      **a_	)
			requires_backends(self		,      "decord"	)
			self.check_model_type(a_	)



	def   _lowerCamelCase    (   self		,      a_=None		,      a_=None		,      a_=None	):
			lowerCAmelCase     :    List[Any]						=   {}
			if frame_sampling_rate is not None:
					lowerCAmelCase     :    List[Any]						=   frame_sampling_rate
			if num_frames is not None:
					lowerCAmelCase     :    Union[str, Any]						=   num_frames

			lowerCAmelCase     :    Optional[int]						=   {}
			if top_k is not None:
					lowerCAmelCase     :    Optional[int]						=   top_k
			return preprocess_params, {}, postprocess_params



	def __call__(   self		,      a_		,      **a_	):
			return super().__call__(a_		,      **a_	)



	def   _lowerCamelCase    (   self		,      a_		,      a_=None		,      a_=1	):
			if num_frames is None:
					lowerCAmelCase     :    List[Any]						=   self.model.config.num_frames

			if video.startswith("http://"	) or video.startswith("https://"	):
					lowerCAmelCase     :    List[str]						=   BytesIO(requests.get(a_	).content	)

			lowerCAmelCase     :    Any						=   VideoReader(a_	)
			videoreader.seek(0	)

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

			lowerCAmelCase     :    Dict						=   videoreader.get_batch(a_	).asnumpy()
			lowerCAmelCase     :    str						=   list(a_	)

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



	def   _lowerCamelCase    (   self		,      a_	):
			lowerCAmelCase     :    int						=   self.model(**a_	)
			return model_outputs





	def   _lowerCamelCase    (   self		,      a_		,      a_=5	):
			if top_k > self.model.config.num_labels:
					lowerCAmelCase     :    Any						=   self.model.config.num_labels

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

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










'''simple docstring'''






from collections import namedtuple

lowerCAmelCase       		=      namedtuple("""from_to""", """from_ to""")

lowerCAmelCase       		=      {
    """cubicmeter""": from_to(1, 1),
    """litre""": from_to(0.001, 10_00),
    """kilolitre""": from_to(1, 1),
    """gallon""": from_to(0.00454, 264.172),
    """cubicyard""": from_to(0.76455, 1.30795),
    """cubicfoot""": from_to(0.028, 35.3147),
    """cup""": from_to(0.000236588, 4226.75),
}






def      __A						(							a_						:  float      ,a_						:  str      ,a_						:  str	):
		if from_type not in METRIC_CONVERSION:
				raise ValueError(
				    f'''Invalid \'from_type\' value: {from_type!r}  Supported values are:\n'''
				    + ", ".join(a_	)	)
		if to_type not in METRIC_CONVERSION:
				raise ValueError(
				    f'''Invalid \'to_type\' value: {to_type!r}.  Supported values are:\n'''
				    + ", ".join(a_	)	)
		return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to


if __name__ == "__main__":
					import doctest

					doctest.testmod()



