


from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
   from PIL import Image

   from ..image_utils import load_image

if is_torch_available():
   import torch

   from transformers.modeling_outputs import BaseModelOutput

   from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

_A			=						logging.get_logger(__name__)






@add_end_docstrings(A_		)
class    UpperCAmelCase__	( A_		):



  """simple docstring"""






  def __init__(      self ,       **A_   )  ->	Optional[int]:
       super().__init__(**A_   )

       if self.framework == "tf":
            raise ValueError(f'The {self.__class__} is only available in PyTorch.'   )

       requires_backends(self ,       'vision'   )
       self.check_model_type(A_   )






  def __call__(      self ,       A_ ,       A_ = None ,       **A_ ,       )  ->	int:
       if "text_queries" in kwargs:
            __UpperCamelCase   							=kwargs.pop('text_queries'   )

       if isinstance(A_ ,       (str, Image.Image)   ):
            __UpperCamelCase   							={'image': image, 'candidate_labels': candidate_labels}
       else:
            __UpperCamelCase   							=image
       __UpperCamelCase   							=super().__call__(A_ ,       **A_   )
       return results






  def _a (      self ,       **A_   )  ->	int:
       __UpperCamelCase   							={}
       if "threshold" in kwargs:
            __UpperCamelCase   							=kwargs['threshold']
       if "top_k" in kwargs:
            __UpperCamelCase   							=kwargs['top_k']
       return {}, {}, postprocess_params






  def _a (      self ,       A_   )  ->	Tuple:
       __UpperCamelCase   							=load_image(inputs['image']   )
       __UpperCamelCase   							=inputs['candidate_labels']
       if isinstance(A_ ,       A_   ):
            __UpperCamelCase   							=candidate_labels.split(','   )

       __UpperCamelCase   							=torch.tensor([[image.height, image.width]] ,       dtype=torch.intaa   )
       for i, candidate_label in enumerate(A_   ):
            __UpperCamelCase   							=self.tokenizer(A_ ,       return_tensors=self.framework   )
            __UpperCamelCase   							=self.image_processor(A_ ,       return_tensors=self.framework   )
            yield {
                "is_last": i == len(A_   ) - 1,
                "target_size": target_size,
                "candidate_label": candidate_label,
                **text_inputs,
                **image_features,
            }






  def _a (      self ,       A_   )  ->	Optional[Any]:
       __UpperCamelCase   							=model_inputs.pop('target_size'   )
       __UpperCamelCase   							=model_inputs.pop('candidate_label'   )
       __UpperCamelCase   							=model_inputs.pop('is_last'   )

       __UpperCamelCase   							=self.model(**A_   )

       __UpperCamelCase   							={'target_size': target_size, 'candidate_label': candidate_label, 'is_last': is_last, **outputs}
       return model_outputs






  def _a (      self ,       A_ ,       A_=0.1 ,       A_=None   )  ->	Dict:
       __UpperCamelCase   							=[]
       for model_output in model_outputs:
            __UpperCamelCase   							=model_output['candidate_label']
            __UpperCamelCase   							=BaseModelOutput(A_   )
            __UpperCamelCase   							=self.image_processor.post_process_object_detection(
                outputs=A_ ,       threshold=A_ ,       target_sizes=model_output['target_size']   )[0]

            for index in outputs["scores"].nonzero():
                 __UpperCamelCase   							=outputs['scores'][index].item()
                 __UpperCamelCase   							=self._get_bounding_box(outputs['boxes'][index][0]   )

                 __UpperCamelCase   							={'score': score, 'label': label, 'box': box}
                 results.append(A_   )

       __UpperCamelCase   							=sorted(A_ ,       key=lambda A_   : x["score"] ,       reverse=A_   )
       if top_k:
            __UpperCamelCase   							=results[:top_k]

       return results






  def _a (      self ,       A_   )  ->	Dict[str, int]:
       if self.framework != "pt":
            raise ValueError('The ZeroShotObjectDetectionPipeline is only available in PyTorch.'   )
       __UpperCamelCase							,				__UpperCamelCase							,				__UpperCamelCase							,				__UpperCamelCase   							=box.int().tolist()
       __UpperCamelCase   							={
           'xmin': xmin,
           'ymin': ymin,
           'xmax': xmax,
           'ymax': ymax,
       }
       return bbox









import os
from typing import BinaryIO, Optional, Union

import numpy as np
import pyarrow.parquet as pq

from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config
from ..features.features import FeatureType, _visit
from ..formatting import query_table
from ..packaged_modules import _PACKAGED_DATASETS_MODULES
from ..packaged_modules.parquet.parquet import Parquet
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader

def 						_UpperCAmelCase    (      SCREAMING_SNAKE_CASE__      : Features    ):
     __UpperCamelCase   							=np.inf

     def set_batch_size(SCREAMING_SNAKE_CASE__      : FeatureType    ) -> None:
          nonlocal batch_size
          if isinstance(SCREAMING_SNAKE_CASE__      ,					SCREAMING_SNAKE_CASE__    ):
               __UpperCamelCase   							=min(SCREAMING_SNAKE_CASE__      ,					config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS    )
          elif isinstance(SCREAMING_SNAKE_CASE__      ,					SCREAMING_SNAKE_CASE__    ):
               __UpperCamelCase   							=min(SCREAMING_SNAKE_CASE__      ,					config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS    )
          elif isinstance(SCREAMING_SNAKE_CASE__      ,					SCREAMING_SNAKE_CASE__    ) and feature.dtype == "binary":
               __UpperCamelCase   							=min(SCREAMING_SNAKE_CASE__      ,					config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS    )

     _visit(SCREAMING_SNAKE_CASE__      ,					SCREAMING_SNAKE_CASE__    )

     return None if batch_size is np.inf else batch_size






class    UpperCAmelCase__	( A_		):



    """simple docstring"""






    def __init__(      self ,       A_ ,       A_ = None ,       A_ = None ,       A_ = None ,       A_ = False ,       A_ = False ,       A_ = None ,       **A_ ,       )  ->	Dict:
         super().__init__(
             A_ ,       split=A_ ,       features=A_ ,       cache_dir=A_ ,       keep_in_memory=A_ ,       streaming=A_ ,       num_proc=A_ ,       **A_ ,       )
         __UpperCamelCase   							=path_or_paths if isinstance(A_ ,       A_   ) else {self.split: path_or_paths}
         __UpperCamelCase   							=_PACKAGED_DATASETS_MODULES['parquet'][1]
         __UpperCamelCase   							=Parquet(
             cache_dir=A_ ,       data_files=A_ ,       features=A_ ,       hash=A_ ,       **A_ ,       )






    def _a (      self   )  ->	List[Any]:
         # Build iterable dataset
         if self.streaming:
              __UpperCamelCase   							=self.builder.as_streaming_dataset(split=self.split   )
         # Build regular (map-style) dataset
         else:
              __UpperCamelCase   							=None
              __UpperCamelCase   							=None
              __UpperCamelCase   							=None
              __UpperCamelCase   							=None

              self.builder.download_and_prepare(
                  download_config=A_ ,       download_mode=A_ ,       verification_mode=A_ ,       base_path=A_ ,       num_proc=self.num_proc ,       )
              __UpperCamelCase   							=self.builder.as_dataset(
                  split=self.split ,       verification_mode=A_ ,       in_memory=self.keep_in_memory   )
         return dataset






class    UpperCAmelCase__	:



    """simple docstring"""






    def __init__(      self ,       A_ ,       A_ ,       A_ = None ,       **A_ ,       )  ->	List[Any]:
         __UpperCamelCase   							=dataset
         __UpperCamelCase   							=path_or_buf
         __UpperCamelCase   							=batch_size or get_writer_batch_size(dataset.features   )
         __UpperCamelCase   							=parquet_writer_kwargs






    def _a (      self   )  ->	int:
         __UpperCamelCase   							=self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE

         if isinstance(self.path_or_buf ,       (str, bytes, os.PathLike)   ):
              with open(self.path_or_buf ,       'wb+'   ) as buffer:
                   __UpperCamelCase   							=self._write(file_obj=A_ ,       batch_size=A_ ,       **self.parquet_writer_kwargs   )
         else:
              __UpperCamelCase   							=self._write(file_obj=self.path_or_buf ,       batch_size=A_ ,       **self.parquet_writer_kwargs   )
         return written






    def _a (      self ,       A_ ,       A_ ,       **A_   )  ->	int:
         __UpperCamelCase   							=0
         __UpperCamelCase   							=parquet_writer_kwargs.pop('path_or_buf' ,       A_   )
         __UpperCamelCase   							=self.dataset.features.arrow_schema

         __UpperCamelCase   							=pq.ParquetWriter(A_ ,       schema=A_ ,       **A_   )

         for offset in logging.tqdm(
             range(0 ,       len(self.dataset   ) ,       A_   ) ,       unit='ba' ,       disable=not logging.is_progress_bar_enabled() ,       desc='Creating parquet from Arrow format' ,       ):
              __UpperCamelCase   							=query_table(
                  table=self.dataset._data ,       key=slice(A_ ,       offset + batch_size   ) ,       indices=self.dataset._indices if self.dataset._indices is not None else None ,       )
              writer.write_table(A_   )
              written += batch.nbytes
         writer.close()
         return written





