




import itertools
from dataclasses import dataclass
from typing import List, Optional

import pyarrow as pa
import pyarrow.parquet as pq

import datasets
from datasets.table import table_cast


lowerCamelCase	:	str				=   datasets.utils.logging.get_logger(__name__)




@dataclass
class 						__lowercase		(datasets.BuilderConfig							):




      """simple docstring"""





      _snake_case												=						10_000
      _snake_case												=						None
      _snake_case												=						None




class 						__lowercase		(datasets.ArrowBasedBuilder							):




      """simple docstring"""





      _snake_case												=						ParquetConfig

      def      UpperCAmelCase				(				self     )		->    str:
         return datasets.DatasetInfo(features=self.config.features     )

      def      UpperCAmelCase				(				self    ,				A     )		->    Union[str, Any]:
         if not self.config.data_files:
            raise ValueError(f"""At least one data file must be specified, but got data_files={self.config.data_files}"""     )
         snake_case				:    Any           =	dl_manager.download_and_extract(self.config.data_files     )
         if isinstance(A    ,				(str, list, tuple)     ):
            snake_case				:    Optional[Any]           =	data_files
            if isinstance(A    ,				A     ):
               snake_case				:    str           =	[files]
            # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
            snake_case				:    List[str]           =	[dl_manager.iter_files(A     ) for file in files]
            return [datasets.SplitGenerator(name=datasets.Split.TRAIN    ,				gen_kwargs={"""files""": files}     )]
         snake_case				:    Optional[int]           =	[]
         for split_name, files in data_files.items():
            if isinstance(A    ,				A     ):
               snake_case				:    str           =	[files]
            # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
            snake_case				:    Optional[Any]           =	[dl_manager.iter_files(A     ) for file in files]
            # Infer features is they are stoed in the arrow schema
            if self.info.features is None:
               for file in itertools.chain.from_iterable(A     ):
                  with open(A    ,				"""rb"""     ) as f:
                     snake_case				:    Any           =	datasets.Features.from_arrow_schema(pq.read_schema(A     )     )
                  break
            splits.append(datasets.SplitGenerator(name=A    ,				gen_kwargs={"""files""": files}     )     )
         return splits

      def      UpperCAmelCase				(				self    ,				A     )		->    pa.Table:
         if self.info.features is not None:
            # more expensive cast to support nested features with keys in a different order
            # allows str <-> int/float or str to Audio for example
            snake_case				:    Union[str, Any]           =	table_cast(A    ,				self.info.features.arrow_schema     )
         return pa_table

      def      UpperCAmelCase				(				self    ,				A     )		->    Optional[int]:
         snake_case				:    str           =	self.info.features.arrow_schema if self.info.features is not None else None
         if self.info.features is not None and self.config.columns is not None:
            if sorted(field.name for field in schema     ) != sorted(self.config.columns     ):
               raise ValueError(
                   f"""Tried to load parquet data with columns '{self.config.columns}' with mismatching features '{self.info.features}'"""     )
         for file_idx, file in enumerate(itertools.chain.from_iterable(A     )     ):
            with open(A    ,				"""rb"""     ) as f:
               snake_case				:    Union[str, Any]           =	pq.ParquetFile(A     )
               try:
                  for batch_idx, record_batch in enumerate(
                      parquet_file.iter_batches(batch_size=self.config.batch_size    ,				columns=self.config.columns     )     ):
                     snake_case				:    Dict           =	pa.Table.from_batches([record_batch]     )
                     # Uncomment for debugging (will print the Arrow table size and elements)
                     # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
                     # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
                     yield f"""{file_idx}_{batch_idx}""", self._cast_table(A     )
               except ValueError as e:
                  logger.error(f"""Failed to read file '{file}' with error {type(A     )}: {e}"""     )
                  raise











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	:	int				=   logging.get_logger(__name__)




@add_end_docstrings(UpperCamelCase__							)
class 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





      def __init__(				self    ,				*A    ,				**A     )		->    Optional[int]:
         super().__init__(*A    ,				**A     )
         requires_backends(self    ,				"""decord"""     )
         self.check_model_type(A     )

      def      UpperCAmelCase				(				self    ,				A=None    ,				A=None    ,				A=None     )		->    int:
         snake_case				:    Any           =	{}
         if frame_sampling_rate is not None:
            snake_case				:    int           =	frame_sampling_rate
         if num_frames is not None:
            snake_case				:    Union[str, Any]           =	num_frames

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

      def __call__(				self    ,				A    ,				**A     )		->    Dict:
         return super().__call__(A    ,				**A     )

      def      UpperCAmelCase				(				self    ,				A    ,				A=None    ,				A=1     )		->    Tuple:
         if num_frames is None:
            snake_case				:    Tuple           =	self.model.config.num_frames

         if video.startswith("""http://"""     ) or video.startswith("""https://"""     ):
            snake_case				:    Optional[int]           =	BytesIO(requests.get(A     ).content     )

         snake_case				:    Optional[Any]           =	VideoReader(A     )
         videoreader.seek(0     )

         snake_case				:    Optional[Any]           =	0
         snake_case				:    Optional[Any]           =	num_frames * frame_sampling_rate - 1
         snake_case				:    Any           =	np.linspace(A    ,				A    ,				num=A    ,				dtype=np.intaa     )

         snake_case				:    int           =	videoreader.get_batch(A     ).asnumpy()
         snake_case				:    List[Any]           =	list(A     )

         snake_case				:    Optional[Any]           =	self.image_processor(A    ,				return_tensors=self.framework     )
         return model_inputs

      def      UpperCAmelCase				(				self    ,				A     )		->    List[str]:
         snake_case				:    Dict           =	self.model(**A     )
         return model_outputs

      def      UpperCAmelCase				(				self    ,				A    ,				A=5     )		->    int:
         if top_k > self.model.config.num_labels:
            snake_case				:    str           =	self.model.config.num_labels

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

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





