code: 
  
  
  
  
  
  
  import os
  from typing import Optional, Union
  
  import numpy as np
  from huggingface_hub import hf_hub_download
  
  from ... import AutoTokenizer
  from ...utils import logging
  
  
  lowerCamelCase			:							List[Any]       ='''block_records.npy'''
  
  
  lowerCamelCase			:							Tuple       =logging.get_logger(__name__)
  
  
  
  
  
  
  
  def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			np.ndarray:
       import tensorflow.compat.va as tf
  
       UpperCamelCase__    :     Union[str, Any]        =						tf.data.TFRecordDataset(__lowerCAmelCase      ,					buffer_size=512 * 1024 * 1024    )
       UpperCamelCase__    :     List[str]        =						blocks_dataset.batch(__lowerCAmelCase      ,					drop_remainder=__lowerCAmelCase    )
       UpperCamelCase__    :     Any        =						next(blocks_dataset.take(1    ).as_numpy_iterator()    )
  
       return np_record
  
  
  
  
  
  class     __a						:
  
  
  
  
  
  
  
   def __init__(	self      :     str							,     SCREAMING_SNAKE_CASE      :     str							,     SCREAMING_SNAKE_CASE      :     Any							,     SCREAMING_SNAKE_CASE      :     Optional[int]=2							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=10_00							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=1_00							,     SCREAMING_SNAKE_CASE      :     int=10_00_00							,     ):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        from scann.scann_ops.py.scann_ops_pybind import builder as Builder
  
        UpperCamelCase__    :     Any        =						Builder(db=SCREAMING_SNAKE_CASE							,     num_neighbors=SCREAMING_SNAKE_CASE							,     distance_measure="dot_product"		)
        UpperCamelCase__    :     List[str]        =						builder.tree(
            num_leaves=SCREAMING_SNAKE_CASE							,     num_leaves_to_search=SCREAMING_SNAKE_CASE							,     training_sample_size=SCREAMING_SNAKE_CASE		)
        UpperCamelCase__    :     Tuple        =						builder.score_ah(dimensions_per_block=SCREAMING_SNAKE_CASE		)
  
        UpperCamelCase__    :     Union[str, Any]        =						builder.build()
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[str]							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__       ,  UpperCamelCase__    :     Tuple        =						self.searcher.search_batched(question_projection.detach().cpu()		)
        return retrieved_block_ids.astype("int64"		)
  
  
  
  
  
  
  
  class     __a						:
  
  
  
  
  
  
  
   def __init__(	self      :     Any							,     SCREAMING_SNAKE_CASE      :     Optional[int]							,     SCREAMING_SNAKE_CASE      :     List[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        super().__init__()
        UpperCamelCase__    :     str        =						block_records
        UpperCamelCase__    :     List[str]        =						tokenizer
  
  
  
  
  
  
  
   def __call__(	self      :     Optional[Any]							,     SCREAMING_SNAKE_CASE      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     Any							,     SCREAMING_SNAKE_CASE      :     Optional[int]							,     SCREAMING_SNAKE_CASE      :     int=None							,     SCREAMING_SNAKE_CASE      :     Dict="pt"		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Union[str, Any]        =						np.take(self.block_records							,     indices=SCREAMING_SNAKE_CASE							,     axis=0		)
  
        UpperCamelCase__    :     Dict        =						self.tokenizer.decode(question_input_ids[0]							,     skip_special_tokens=SCREAMING_SNAKE_CASE		)
  
        UpperCamelCase__    :     Dict        =						[]
        UpperCamelCase__    :     Tuple        =						[]
        for retrieved_block in retrieved_blocks:
             text.append(SCREAMING_SNAKE_CASE		)
             text_pair.append(retrieved_block.decode()		)
  
        UpperCamelCase__    :     List[str]        =						self.tokenizer(
            SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     padding=SCREAMING_SNAKE_CASE							,     truncation=SCREAMING_SNAKE_CASE							,     return_special_tokens_mask=SCREAMING_SNAKE_CASE							,     max_length=SCREAMING_SNAKE_CASE		)
        UpperCamelCase__    :     Dict        =						concat_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE		)
  
        if answer_ids is not None:
             return self.block_has_answer(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		) + (concat_inputs_tensors,)
        else:
             return (None, None, None, concat_inputs_tensors)
  
  
  
  
  
  
  
   @classmethod
   def       __lowercase      (	cls      :     int							,     SCREAMING_SNAKE_CASE      :     Optional[Union[str, os.PathLike]]							,     *SCREAMING_SNAKE_CASE      :     Tuple							,     **SCREAMING_SNAKE_CASE      :     Union[str, Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        if os.path.isdir(SCREAMING_SNAKE_CASE		):
             UpperCamelCase__    :     Optional[Any]        =						os.path.join(SCREAMING_SNAKE_CASE							,     _REALM_BLOCK_RECORDS_FILENAME		)
        else:
             UpperCamelCase__    :     List[str]        =						hf_hub_download(
                 repo_id=SCREAMING_SNAKE_CASE							,     filename=_REALM_BLOCK_RECORDS_FILENAME							,     **SCREAMING_SNAKE_CASE		)
        UpperCamelCase__    :     Union[str, Any]        =						np.load(SCREAMING_SNAKE_CASE							,     allow_pickle=SCREAMING_SNAKE_CASE		)
  
        UpperCamelCase__    :     List[Any]        =						AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE							,     *SCREAMING_SNAKE_CASE							,     **SCREAMING_SNAKE_CASE		)
  
        return cls(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     int							,     SCREAMING_SNAKE_CASE      :     Tuple		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        np.save(os.path.join(SCREAMING_SNAKE_CASE							,     _REALM_BLOCK_RECORDS_FILENAME		)							,     self.block_records		)
        # save tokenizer
        self.tokenizer.save_pretrained(SCREAMING_SNAKE_CASE		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     int							,     SCREAMING_SNAKE_CASE      :     List[str]							,     SCREAMING_SNAKE_CASE      :     Any		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Optional[int]        =						[]
        UpperCamelCase__    :     Optional[int]        =						[]
        UpperCamelCase__    :     Any        =						[]
        UpperCamelCase__    :     int        =						0
  
        for input_id in concat_inputs.input_ids:
             UpperCamelCase__    :     List[str]        =						input_id.tolist()
             # Check answers between two [SEP] tokens
             UpperCamelCase__    :     Optional[int]        =						input_id_list.index(self.tokenizer.sep_token_id		)
             UpperCamelCase__    :     str        =						first_sep_idx + 1 + input_id_list[first_sep_idx + 1 :].index(self.tokenizer.sep_token_id		)
  
             start_pos.append([]		)
             end_pos.append([]		)
             for answer in answer_ids:
                  for idx in range(first_sep_idx + 1							,     SCREAMING_SNAKE_CASE		):
                       if answer[0] == input_id_list[idx]:
                            if input_id_list[idx : idx + len(SCREAMING_SNAKE_CASE		)] == answer:
                                 start_pos[-1].append(SCREAMING_SNAKE_CASE		)
                                 end_pos[-1].append(idx + len(SCREAMING_SNAKE_CASE		) - 1		)
  
             if len(start_pos[-1]		) == 0:
                  has_answers.append(SCREAMING_SNAKE_CASE		)
             else:
                  has_answers.append(SCREAMING_SNAKE_CASE		)
                  if len(start_pos[-1]		) > max_answers:
                       UpperCamelCase__    :     str        =						len(start_pos[-1]		)
  
          # Pad -1 to max_answers
        for start_pos_, end_pos_ in zip(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		):
             if len(start_pos_		) < max_answers:
                  UpperCamelCase__    :     Dict        =						[-1] * (max_answers - len(start_pos_		))
                  start_pos_ += padded
                  end_pos_ += padded
        return has_answers, start_pos, end_pos
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"},
            ]							,     )