






'''simple docstring'''
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							="block_records.npy"


__lowerCAmelCase							=logging.get_logger(__name__)

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase )				->				np.ndarray:






     """simple docstring"""







     import tensorflow.compat.va as tf

     a_									=   tf.data.TFRecordDataset(_UpperCAmelCase					,  buffer_size=5_1_2 * 1_0_2_4 * 1_0_2_4 )
     a_									=   blocks_dataset.batch(_UpperCAmelCase					,  drop_remainder=_UpperCAmelCase )
     a_									=   next(blocks_dataset.take(1 ).as_numpy_iterator() )

     return np_record


class    _snake_case   :

     """simple docstring"""





     def __init__(     self   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__=2   , UpperCAmelCase__=1000   , UpperCAmelCase__=100   , UpperCAmelCase__=10_0000   , )      ->	Dict:
          from scann.scann_ops.py.scann_ops_pybind import builder as Builder

          a_									=   Builder(db=UpperCAmelCase__   , num_neighbors=UpperCAmelCase__   , distance_measure='dot_product'     )
          a_									=   builder.tree(
              num_leaves=UpperCAmelCase__   , num_leaves_to_search=UpperCAmelCase__   , training_sample_size=UpperCAmelCase__     )
          a_									=   builder.score_ah(dimensions_per_block=UpperCAmelCase__     )

          a_									=   builder.build()




     def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__     )      ->	List[str]:
          a_							,					a_									=   self.searcher.search_batched(question_projection.detach().cpu()     )
          return retrieved_block_ids.astype('int64'     )



class    _snake_case   :

     """simple docstring"""





     def __init__(     self   , UpperCAmelCase__   , UpperCAmelCase__     )      ->	Any:
          super().__init__()
          a_									=   block_records
          a_									=   tokenizer




     def __call__(     self   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__=None   , UpperCAmelCase__="pt"     )      ->	Union[str, Any]:
          a_									=   np.take(self.block_records   , indices=UpperCAmelCase__   , axis=0     )

          a_									=   self.tokenizer.decode(question_input_ids[0]   , skip_special_tokens=UpperCAmelCase__     )

          a_									=   []
          a_									=   []
          for retrieved_block in retrieved_blocks:
               text.append(UpperCAmelCase__     )
               text_pair.append(retrieved_block.decode()     )

          a_									=   self.tokenizer(
              UpperCAmelCase__   , UpperCAmelCase__   , padding=UpperCAmelCase__   , truncation=UpperCAmelCase__   , return_special_tokens_mask=UpperCAmelCase__   , max_length=UpperCAmelCase__     )
          a_									=   concat_inputs.convert_to_tensors(UpperCAmelCase__     )

          if answer_ids is not None:
               return self.block_has_answer(UpperCAmelCase__   , UpperCAmelCase__     ) + (concat_inputs_tensors,)
          else:
               return (None, None, None, concat_inputs_tensors)




     @classmethod
     def  __SCREAMING_SNAKE_CASE (     cls   , UpperCAmelCase__   , *UpperCAmelCase__   , **UpperCAmelCase__     )      ->	str:
          if os.path.isdir(UpperCAmelCase__     ):
               a_									=   os.path.join(UpperCAmelCase__   , _REALM_BLOCK_RECORDS_FILENAME     )
          else:
               a_									=   hf_hub_download(
                   repo_id=UpperCAmelCase__   , filename=_REALM_BLOCK_RECORDS_FILENAME   , **UpperCAmelCase__     )
          a_									=   np.load(UpperCAmelCase__   , allow_pickle=UpperCAmelCase__     )

          a_									=   AutoTokenizer.from_pretrained(UpperCAmelCase__   , *UpperCAmelCase__   , **UpperCAmelCase__     )

          return cls(UpperCAmelCase__   , UpperCAmelCase__     )




     def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__     )      ->	List[str]:
          # save block records
          np.save(os.path.join(UpperCAmelCase__   , _REALM_BLOCK_RECORDS_FILENAME     )   , self.block_records     )
          # save tokenizer
          self.tokenizer.save_pretrained(UpperCAmelCase__     )




     def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__     )      ->	Optional[int]:
          a_									=   []
          a_									=   []
          a_									=   []
          a_									=   0

          for input_id in concat_inputs.input_ids:
               a_									=   input_id.tolist()
               # Check answers between two [SEP] tokens
               a_									=   input_id_list.index(self.tokenizer.sep_token_id     )
               a_									=   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   , UpperCAmelCase__     ):
                         if answer[0] == input_id_list[idx]:
                              if input_id_list[idx : idx + len(UpperCAmelCase__     )] == answer:
                                   start_pos[-1].append(UpperCAmelCase__     )
                                   end_pos[-1].append(idx + len(UpperCAmelCase__     ) - 1     )

               if len(start_pos[-1]     ) == 0:
                    has_answers.append(UpperCAmelCase__     )
               else:
                    has_answers.append(UpperCAmelCase__     )
                    if len(start_pos[-1]     ) > max_answers:
                         a_									=   len(start_pos[-1]     )

        # Pad -1 to max_answers
          for start_pos_, end_pos_ in zip(UpperCAmelCase__   , UpperCAmelCase__     ):
               if len(start_pos_     ) < max_answers:
                    a_									=   [-1] * (max_answers - len(start_pos_     ))
                    start_pos_ += padded
                    end_pos_ += padded
          return has_answers, start_pos, end_pos









'''simple docstring'''
def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				list[int]:






					"""simple docstring"""







					if hue < 0 or hue > 3_6_0:
										raise Exception('hue should be between 0 and 360' )

					if saturation < 0 or saturation > 1:
										raise Exception('saturation should be between 0 and 1' )

					if value < 0 or value > 1:
										raise Exception('value should be between 0 and 1' )

					a_									=   value * saturation
					a_									=   hue / 6_0
					a_									=   chroma * (1 - abs(hue_section % 2 - 1 ))
					a_									=   value - chroma

					if hue_section >= 0 and hue_section <= 1:
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (match_value) )
					elif hue_section > 1 and hue_section <= 2:
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (match_value) )
					elif hue_section > 2 and hue_section <= 3:
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
					elif hue_section > 3 and hue_section <= 4:
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
					elif hue_section > 4 and hue_section <= 5:
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
					else:
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )

					return [red, green, blue]

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				list[float]:






					"""simple docstring"""







					if red < 0 or red > 2_5_5:
										raise Exception('red should be between 0 and 255' )

					if green < 0 or green > 2_5_5:
										raise Exception('green should be between 0 and 255' )

					if blue < 0 or blue > 2_5_5:
										raise Exception('blue should be between 0 and 255' )

					a_									=   red / 2_5_5
					a_									=   green / 2_5_5
					a_									=   blue / 2_5_5
					a_									=   max(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
					a_									=   value - min(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
					a_									=   0 if value == 0 else chroma / value

					if chroma == 0:
										a_									=   0.0
					elif value == float_red:
										a_									=   6_0 * (0 + (float_green - float_blue) / chroma)
					elif value == float_green:
										a_									=   6_0 * (2 + (float_blue - float_red) / chroma)
					else:
										a_									=   6_0 * (4 + (float_red - float_green) / chroma)

					a_									=   (hue + 3_6_0) % 3_6_0

					return [hue, saturation, value]





def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase )				->				bool:






					"""simple docstring"""







					a_									=   abs(hsv_a[0] - hsv_a[0] ) < 0.2
					a_									=   abs(hsv_a[1] - hsv_a[1] ) < 0.0_0_2
					a_									=   abs(hsv_a[2] - hsv_a[2] ) < 0.0_0_2

					return check_hue and check_saturation and check_value

