code: 
  
  
  
  
  
  
  import multiprocessing
  import os
  from typing import BinaryIO, Optional, Union
  
  from .. import Dataset, Features, NamedSplit, config
  from ..formatting import query_table
  from ..packaged_modules.csv.csv import Csv
  from ..utils import logging
  from ..utils.typing import NestedDataStructureLike, PathLike
  from .abc import AbstractDatasetReader
  
  
  
  
  
  class     __a						(					A__ ):
  
  
  
  
  
  
  
   def __init__(	self      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     NestedDataStructureLike[PathLike]							,     SCREAMING_SNAKE_CASE      :     Optional[NamedSplit] = None							,     SCREAMING_SNAKE_CASE      :     Optional[Features] = None							,     SCREAMING_SNAKE_CASE      :     str = None							,     SCREAMING_SNAKE_CASE      :     bool = False							,     SCREAMING_SNAKE_CASE      :     bool = False							,     SCREAMING_SNAKE_CASE      :     Optional[int] = None							,     **SCREAMING_SNAKE_CASE      :     Union[str, Any]							,     ):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        super().__init__(
            SCREAMING_SNAKE_CASE							,     split=SCREAMING_SNAKE_CASE							,     features=SCREAMING_SNAKE_CASE							,     cache_dir=SCREAMING_SNAKE_CASE							,     keep_in_memory=SCREAMING_SNAKE_CASE							,     streaming=SCREAMING_SNAKE_CASE							,     num_proc=SCREAMING_SNAKE_CASE							,     **SCREAMING_SNAKE_CASE							,     )
        UpperCamelCase__    :     Union[str, Any]        =						path_or_paths if isinstance(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		) else {self.split: path_or_paths}
        UpperCamelCase__    :     List[Any]        =						Csv(
            cache_dir=SCREAMING_SNAKE_CASE							,     data_files=SCREAMING_SNAKE_CASE							,     features=SCREAMING_SNAKE_CASE							,     **SCREAMING_SNAKE_CASE							,     )
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Optional[int]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        if self.streaming:
             UpperCamelCase__    :     Optional[Any]        =						self.builder.as_streaming_dataset(split=self.split		)
        # Build regular (map-style) dataset
        else:
             UpperCamelCase__    :     List[Any]        =						None
             UpperCamelCase__    :     Tuple        =						None
             UpperCamelCase__    :     Tuple        =						None
             UpperCamelCase__    :     List[str]        =						None
  
             self.builder.download_and_prepare(
                 download_config=SCREAMING_SNAKE_CASE							,     download_mode=SCREAMING_SNAKE_CASE							,     verification_mode=SCREAMING_SNAKE_CASE							,     base_path=SCREAMING_SNAKE_CASE							,     num_proc=self.num_proc							,     )
             UpperCamelCase__    :     Optional[Any]        =						self.builder.as_dataset(
                 split=self.split							,     verification_mode=SCREAMING_SNAKE_CASE							,     in_memory=self.keep_in_memory		)
        return dataset
  
  
  
  
  
  
  
  class     __a						:
  
  
  
  
  
  
  
   def __init__(	self      :     List[str]							,     SCREAMING_SNAKE_CASE      :     Dataset							,     SCREAMING_SNAKE_CASE      :     Union[PathLike, BinaryIO]							,     SCREAMING_SNAKE_CASE      :     Optional[int] = None							,     SCREAMING_SNAKE_CASE      :     Optional[int] = None							,     **SCREAMING_SNAKE_CASE      :     Optional[Any]							,     ):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        if num_proc is not None and num_proc <= 0:
             raise ValueError(F'num_proc {num_proc} must be an integer > 0.'		)
  
        UpperCamelCase__    :     int        =						dataset
        UpperCamelCase__    :     str        =						path_or_buf
        UpperCamelCase__    :     Dict        =						batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
        UpperCamelCase__    :     Union[str, Any]        =						num_proc
        UpperCamelCase__    :     Optional[Any]        =						"utf-8"
        UpperCamelCase__    :     List[str]        =						to_csv_kwargs
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[str]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Dict        =						self.to_csv_kwargs.pop("path_or_buf"							,     SCREAMING_SNAKE_CASE		)
        UpperCamelCase__    :     Dict        =						self.to_csv_kwargs.pop("header"							,     SCREAMING_SNAKE_CASE		)
        UpperCamelCase__    :     Dict        =						self.to_csv_kwargs.pop("index"							,     SCREAMING_SNAKE_CASE		)
  
        if isinstance(self.path_or_buf							,     (str, bytes, os.PathLike)		):
             with open(self.path_or_buf							,     "wb+"		) as buffer:
                  UpperCamelCase__    :     List[Any]        =						self._write(file_obj=SCREAMING_SNAKE_CASE							,     header=SCREAMING_SNAKE_CASE							,     index=SCREAMING_SNAKE_CASE							,     **self.to_csv_kwargs		)
        else:
             UpperCamelCase__    :     int        =						self._write(file_obj=self.path_or_buf							,     header=SCREAMING_SNAKE_CASE							,     index=SCREAMING_SNAKE_CASE							,     **self.to_csv_kwargs		)
        return written
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Optional[Any]							,     SCREAMING_SNAKE_CASE      :     str		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__       ,  UpperCamelCase__       ,  UpperCamelCase__       ,  UpperCamelCase__    :     str        =						args
  
        UpperCamelCase__    :     Any        =						query_table(
            table=self.dataset.data							,     key=slice(SCREAMING_SNAKE_CASE							,     offset + self.batch_size		)							,     indices=self.dataset._indices							,     )
        UpperCamelCase__    :     Union[str, Any]        =						batch.to_pandas().to_csv(
            path_or_buf=SCREAMING_SNAKE_CASE							,     header=header if (offset == 0) else False							,     index=SCREAMING_SNAKE_CASE							,     **SCREAMING_SNAKE_CASE		)
        return csv_str.encode(self.encoding		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     str							,     SCREAMING_SNAKE_CASE      :     BinaryIO							,     SCREAMING_SNAKE_CASE      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]							,     **SCREAMING_SNAKE_CASE      :     List[str]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Any        =						0
  
        if self.num_proc is None or self.num_proc == 1:
             for offset in logging.tqdm(
                 range(0							,     len(self.dataset		)							,     self.batch_size		)							,     unit="ba"							,     disable=not logging.is_progress_bar_enabled()							,     desc="Creating CSV from Arrow format"							,     ):
                  UpperCamelCase__    :     List[Any]        =						self._batch_csv((offset, header, index, to_csv_kwargs)		)
                  written += file_obj.write(SCREAMING_SNAKE_CASE		)
  
        else:
             UpperCamelCase__       ,  UpperCamelCase__    :     str        =						len(self.dataset		), self.batch_size
             with multiprocessing.Pool(self.num_proc		) as pool:
                  for csv_str in logging.tqdm(
                      pool.imap(
                          self._batch_csv							,     [(offset, header, index, to_csv_kwargs) for offset in range(0							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)]							,     )							,     total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size							,     unit="ba"							,     disable=not logging.is_progress_bar_enabled()							,     desc="Creating CSV from Arrow format"							,     ):
                       written += file_obj.write(SCREAMING_SNAKE_CASE		)
  
        return written
style_context: 
  
  
  
  
  
  
  import unittest
  
  from transformers import (
      MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
      TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
      PreTrainedTokenizer,
      is_vision_available,
  )
  from transformers.pipelines import ImageClassificationPipeline, pipeline
  from transformers.testing_utils import (
      is_pipeline_test,
      nested_simplify,
      require_tf,
      require_torch,
      require_torch_or_tf,
      require_vision,
      slow,
  )
  
  from .test_pipelines_common import ANY
  
  
  if is_vision_available():
   from PIL import Image
  else:
  
  
  
  
  
   class     __a						:
  
  
  
  
  
  
  
    @staticmethod
    def       __lowercase      (	*SCREAMING_SNAKE_CASE      :     Optional[Any]							,     **SCREAMING_SNAKE_CASE      :     Union[str, Any]		):
  
  
         '''simple docstring'''
  
  
  
  
  
  
         pass
  
  
  
  
  
  @is_pipeline_test
  @require_torch_or_tf
  @require_vision
  class     __a						(					unittest.TestCase ):
   _lowerCAmelCase : Tuple 				=				MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
   _lowerCAmelCase : str 				=				TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Union[str, Any]							,     SCREAMING_SNAKE_CASE      :     str							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]							,     SCREAMING_SNAKE_CASE      :     Optional[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Dict        =						ImageClassificationPipeline(model=SCREAMING_SNAKE_CASE							,     image_processor=SCREAMING_SNAKE_CASE							,     top_k=2		)
        UpperCamelCase__    :     Union[str, Any]        =						[
            Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		),
            "http://images.cocodataset.org/val2017/000000039769.jpg",
        ]
        return image_classifier, examples
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Tuple							,     SCREAMING_SNAKE_CASE      :     Tuple							,     SCREAMING_SNAKE_CASE      :     Tuple		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Optional[Any]        =						image_classifier("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
  
        self.assertEqual(
            SCREAMING_SNAKE_CASE							,     [
                {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
            ]							,     )
  
        import datasets
  
        UpperCamelCase__    :     int        =						datasets.load_dataset("hf-internal-testing/fixtures_image_utils"							,     "image"							,     split="test"		)
  
        # Accepts URL + PIL.Image + lists
        UpperCamelCase__    :     Tuple        =						image_classifier(
            [
                Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		),
                "http://images.cocodataset.org/val2017/000000039769.jpg",
                # RGBA
                dataset[0]["file"],
                # LA
                dataset[1]["file"],
                # L
                dataset[2]["file"],
            ]		)
        self.assertEqual(
            SCREAMING_SNAKE_CASE							,     [
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
            ]							,     )
  
  
  
  
  
  
  
   @require_torch
   def       __lowercase      (	self      :     List[str]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     List[Any]        =						"hf-internal-testing/tiny-random-vit"
        UpperCamelCase__    :     List[str]        =						pipeline("image-classification"							,     model=SCREAMING_SNAKE_CASE		)
  
        UpperCamelCase__    :     List[str]        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]							,     )
  
        UpperCamelCase__    :     Optional[Any]        =						image_classifier(
            [
                "http://images.cocodataset.org/val2017/000000039769.jpg",
                "http://images.cocodataset.org/val2017/000000039769.jpg",
            ]							,     top_k=2							,     )
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
                [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
            ]							,     )
  
  
  
  
  
  
  
   @require_tf
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     str        =						"hf-internal-testing/tiny-random-vit"
        UpperCamelCase__    :     int        =						pipeline("image-classification"							,     model=SCREAMING_SNAKE_CASE							,     framework="tf"		)
  
        UpperCamelCase__    :     List[Any]        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]							,     )
  
        UpperCamelCase__    :     int        =						image_classifier(
            [
                "http://images.cocodataset.org/val2017/000000039769.jpg",
                "http://images.cocodataset.org/val2017/000000039769.jpg",
            ]							,     top_k=2							,     )
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
                [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
            ]							,     )
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Optional[int]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Any        =						PreTrainedTokenizer()
  
        # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
        UpperCamelCase__    :     str        =						pipeline(
            "image-classification"							,     model="hf-internal-testing/tiny-random-vit"							,     tokenizer=SCREAMING_SNAKE_CASE		)
  
        self.assertIs(image_classifier.tokenizer							,     SCREAMING_SNAKE_CASE		)
  
  
  
  
  
  
  
   @slow
   @require_torch
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Optional[Any]        =						pipeline("image-classification"							,     model="deepmind/vision-perceiver-conv"		)
        UpperCamelCase__    :     Any        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                {"score": 0.4_3_8_5, "label": "tabby, tabby cat"},
                {"score": 0.3_2_1, "label": "tiger cat"},
                {"score": 0.0_5_0_2, "label": "Egyptian cat"},
                {"score": 0.0_1_3_7, "label": "crib, cot"},
                {"score": 0.0_0_7, "label": "radiator"},
            ]							,     )
  
        UpperCamelCase__    :     Optional[Any]        =						pipeline("image-classification"							,     model="deepmind/vision-perceiver-fourier"		)
        UpperCamelCase__    :     List[str]        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                {"score": 0.5_6_5_8, "label": "tabby, tabby cat"},
                {"score": 0.1_3_0_9, "label": "tiger cat"},
                {"score": 0.0_7_2_2, "label": "Egyptian cat"},
                {"score": 0.0_7_0_7, "label": "remote control, remote"},
                {"score": 0.0_0_8_2, "label": "computer keyboard, keypad"},
            ]							,     )
  
        UpperCamelCase__    :     List[str]        =						pipeline("image-classification"							,     model="deepmind/vision-perceiver-learned"		)
        UpperCamelCase__    :     Optional[int]        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                {"score": 0.3_0_2_2, "label": "tabby, tabby cat"},
                {"score": 0.2_3_6_2, "label": "Egyptian cat"},
                {"score": 0.1_8_5_6, "label": "tiger cat"},
                {"score": 0.0_3_2_4, "label": "remote control, remote"},
                {"score": 0.0_0_9_6, "label": "quilt, comforter, comfort, puff"},
            ]							,     )