


'''simple docstring'''



import unittest

from transformers import (
    MODEL_FOR_OBJECT_DETECTION_MAPPING,
    AutoFeatureExtractor,
    AutoModelForObjectDetection,
    ObjectDetectionPipeline,
    is_vision_available,
    pipeline,
)
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_pytesseract,
    require_tf,
    require_timm,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
      from PIL import Image
else:





      class   UpperCAmelCase_    :





          """simple docstring"""






          @staticmethod
          def  lowercase_							(				*SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	Any:
               pass





@is_pipeline_test
@require_vision
@require_timm
@require_torch
class   UpperCAmelCase_    (unittest.TestCase    ):





    """simple docstring"""






    lowerCamelCase    :   Union[str, Any]					=     MODEL_FOR_OBJECT_DETECTION_MAPPING







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )							->	str:
         __lowerCamelCase  :      Dict														=						ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE_     ,	image_processor=SCREAMING_SNAKE_CASE_      )
         return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )							->	Tuple:
         __lowerCamelCase  :      Optional[int]														=						object_detector('./tests/fixtures/tests_samples/COCO/000000039769.png'     ,	threshold=0.0      )

         self.assertGreater(len(SCREAMING_SNAKE_CASE_      )     ,	0      )
         for detected_object in outputs:
              self.assertEqual(
                  SCREAMING_SNAKE_CASE_     ,	{
                      'score': ANY(SCREAMING_SNAKE_CASE_      ),
                      'label': ANY(SCREAMING_SNAKE_CASE_      ),
                      'box': {'xmin': ANY(SCREAMING_SNAKE_CASE_      ), 'ymin': ANY(SCREAMING_SNAKE_CASE_      ), 'xmax': ANY(SCREAMING_SNAKE_CASE_      ), 'ymax': ANY(SCREAMING_SNAKE_CASE_      )},
                  }     ,	)

         import datasets

         __lowerCamelCase  :      Any														=						datasets.load_dataset('hf-internal-testing/fixtures_image_utils'     ,	'image'     ,	split='test'      )

         __lowerCamelCase  :      str														=						[
             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'],
         ]
         __lowerCamelCase  :      Any														=						object_detector(SCREAMING_SNAKE_CASE_     ,	threshold=0.0      )

         self.assertEqual(len(SCREAMING_SNAKE_CASE_      )     ,	len(SCREAMING_SNAKE_CASE_      )      )
         for outputs in batch_outputs:
              self.assertGreater(len(SCREAMING_SNAKE_CASE_      )     ,	0      )
              for detected_object in outputs:
                   self.assertEqual(
                       SCREAMING_SNAKE_CASE_     ,	{
                           'score': ANY(SCREAMING_SNAKE_CASE_      ),
                           'label': ANY(SCREAMING_SNAKE_CASE_      ),
                           'box': {'xmin': ANY(SCREAMING_SNAKE_CASE_      ), 'ymin': ANY(SCREAMING_SNAKE_CASE_      ), 'xmax': ANY(SCREAMING_SNAKE_CASE_      ), 'ymax': ANY(SCREAMING_SNAKE_CASE_      )},
                       }     ,	)







    @require_tf
    @unittest.skip('Object detection not implemented in TF'      )
    def  lowercase_							(				self      )							->	Any:
         pass







    @require_torch
    def  lowercase_							(				self      )							->	str:
         __lowerCamelCase  :      Union[str, Any]														=						'hf-internal-testing/tiny-detr-mobilenetsv3'

         __lowerCamelCase  :      Optional[Any]														=						AutoModelForObjectDetection.from_pretrained(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Optional[int]														=						AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Any														=						ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE_     ,	feature_extractor=SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      Optional[int]														=						object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'     ,	threshold=0.0      )

         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                 {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
             ]     ,	)

         __lowerCamelCase  :      Tuple														=						object_detector(
             [
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
             ]     ,	threshold=0.0     ,	)

         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 [
                     {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                     {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                 ],
                 [
                     {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                     {'score': 0.3_3_7_6, 'label': 'LABEL_0', 'box': {'xmin': 1_59, 'ymin': 1_20, 'xmax': 4_80, 'ymax': 3_59}},
                 ],
             ]     ,	)







    @require_torch
    @slow
    def  lowercase_							(				self      )							->	Dict:
         __lowerCamelCase  :      int														=						'facebook/detr-resnet-50'

         __lowerCamelCase  :      Dict														=						AutoModelForObjectDetection.from_pretrained(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      List[Any]														=						AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Optional[int]														=						ObjectDetectionPipeline(model=SCREAMING_SNAKE_CASE_     ,	feature_extractor=SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      Tuple														=						object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                 {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                 {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                 {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                 {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
             ]     ,	)

         __lowerCamelCase  :      Dict														=						object_detector(
             [
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
             ]      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 [
                     {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                     {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                     {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                     {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                     {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
                 ],
                 [
                     {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                     {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                     {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                     {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                     {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
                 ],
             ]     ,	)







    @require_torch
    @slow
    def  lowercase_							(				self      )							->	List[Any]:
         __lowerCamelCase  :      Optional[int]														=						'facebook/detr-resnet-50'

         __lowerCamelCase  :      str														=						pipeline('object-detection'     ,	model=SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      int														=						object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                 {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                 {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                 {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                 {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
             ]     ,	)

         __lowerCamelCase  :      str														=						object_detector(
             [
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 'http://images.cocodataset.org/val2017/000000039769.jpg',
             ]      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 [
                     {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                     {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                     {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                     {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                     {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
                 ],
                 [
                     {'score': 0.9_9_8_2, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 70, 'xmax': 1_75, 'ymax': 1_17}},
                     {'score': 0.9_9_6_0, 'label': 'remote', 'box': {'xmin': 3_33, 'ymin': 72, 'xmax': 3_68, 'ymax': 1_87}},
                     {'score': 0.9_9_5_5, 'label': 'couch', 'box': {'xmin': 0, 'ymin': 1, 'xmax': 6_39, 'ymax': 4_73}},
                     {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                     {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
                 ],
             ]     ,	)







    @require_torch
    @slow
    def  lowercase_							(				self      )							->	List[str]:
         __lowerCamelCase  :      List[Any]														=						0.9_9_8_5
         __lowerCamelCase  :      Optional[int]														=						'facebook/detr-resnet-50'

         __lowerCamelCase  :      int														=						pipeline('object-detection'     ,	model=SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      List[str]														=						object_detector('http://images.cocodataset.org/val2017/000000039769.jpg'     ,	threshold=SCREAMING_SNAKE_CASE_      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.9_9_8_8, 'label': 'cat', 'box': {'xmin': 13, 'ymin': 52, 'xmax': 3_14, 'ymax': 4_70}},
                 {'score': 0.9_9_8_7, 'label': 'cat', 'box': {'xmin': 3_45, 'ymin': 23, 'xmax': 6_40, 'ymax': 3_68}},
             ]     ,	)







    @require_torch
    @require_pytesseract
    @slow
    def  lowercase_							(				self      )							->	Tuple:
         __lowerCamelCase  :      Optional[Any]														=						'Narsil/layoutlmv3-finetuned-funsd'
         __lowerCamelCase  :      Dict														=						0.9_9_9_3

         __lowerCamelCase  :      int														=						pipeline('object-detection'     ,	model=SCREAMING_SNAKE_CASE_     ,	threshold=SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      int														=						object_detector(
             'https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png'      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE_     ,	decimals=4      )     ,	[
                 {'score': 0.9_9_9_3, 'label': 'I-ANSWER', 'box': {'xmin': 2_94, 'ymin': 2_54, 'xmax': 3_43, 'ymax': 2_64}},
                 {'score': 0.9_9_9_3, 'label': 'I-ANSWER', 'box': {'xmin': 2_94, 'ymin': 2_54, 'xmax': 3_43, 'ymax': 2_64}},
             ]     ,	)










'''simple docstring'''



import json
from typing import List, Optional, Tuple

from tokenizers import normalizers
from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roformer import RoFormerTokenizer
from .tokenization_utils import JiebaPreTokenizer


A__						:   str				=    logging.get_logger(__name__)

A__						:   str				=    {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

A__						:   Tuple				=    {
    """vocab_file""": {
        """junnyu/roformer_chinese_small""": """https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt""",
        """junnyu/roformer_chinese_base""": """https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt""",
        """junnyu/roformer_chinese_char_small""": (
            """https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt"""
        ),
        """junnyu/roformer_chinese_char_base""": (
            """https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt"""
        ),
        """junnyu/roformer_small_discriminator""": (
            """https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt"""
        ),
        """junnyu/roformer_small_generator""": (
            """https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt"""
        ),
    }
}

A__						:   str				=    {
    """junnyu/roformer_chinese_small""": 1536,
    """junnyu/roformer_chinese_base""": 1536,
    """junnyu/roformer_chinese_char_small""": 512,
    """junnyu/roformer_chinese_char_base""": 512,
    """junnyu/roformer_small_discriminator""": 128,
    """junnyu/roformer_small_generator""": 128,
}


A__						:   Tuple				=    {
    """junnyu/roformer_chinese_small""": {"""do_lower_case""": True},
    """junnyu/roformer_chinese_base""": {"""do_lower_case""": True},
    """junnyu/roformer_chinese_char_small""": {"""do_lower_case""": True},
    """junnyu/roformer_chinese_char_base""": {"""do_lower_case""": True},
    """junnyu/roformer_small_discriminator""": {"""do_lower_case""": True},
    """junnyu/roformer_small_generator""": {"""do_lower_case""": True},
}





class   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    lowerCamelCase    :   Union[str, Any]					=     VOCAB_FILES_NAMES
    lowerCamelCase    :   int					=     PRETRAINED_VOCAB_FILES_MAP
    lowerCamelCase    :   Any					=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    lowerCamelCase    :   Optional[int]					=     PRETRAINED_INIT_CONFIGURATION
    lowerCamelCase    :   Dict					=     RoFormerTokenizer







    def __init__(				self     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=True     ,	SCREAMING_SNAKE_CASE_="[UNK]"     ,	SCREAMING_SNAKE_CASE_="[SEP]"     ,	SCREAMING_SNAKE_CASE_="[PAD]"     ,	SCREAMING_SNAKE_CASE_="[CLS]"     ,	SCREAMING_SNAKE_CASE_="[MASK]"     ,	SCREAMING_SNAKE_CASE_=True     ,	SCREAMING_SNAKE_CASE_=None     ,	**SCREAMING_SNAKE_CASE_     ,	)							->	Optional[Any]:
         super().__init__(
             SCREAMING_SNAKE_CASE_     ,	tokenizer_file=SCREAMING_SNAKE_CASE_     ,	do_lower_case=SCREAMING_SNAKE_CASE_     ,	unk_token=SCREAMING_SNAKE_CASE_     ,	sep_token=SCREAMING_SNAKE_CASE_     ,	pad_token=SCREAMING_SNAKE_CASE_     ,	cls_token=SCREAMING_SNAKE_CASE_     ,	mask_token=SCREAMING_SNAKE_CASE_     ,	tokenize_chinese_chars=SCREAMING_SNAKE_CASE_     ,	strip_accents=SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_     ,	)

         __lowerCamelCase  :      Optional[int]														=						json.loads(self.backend_tokenizer.normalizer.__getstate__()      )
         if (
             pre_tok_state.get('lowercase'     ,	SCREAMING_SNAKE_CASE_      ) != do_lower_case
             or pre_tok_state.get('strip_accents'     ,	SCREAMING_SNAKE_CASE_      ) != strip_accents
         ):
              __lowerCamelCase  :      Optional[int]														=						getattr(SCREAMING_SNAKE_CASE_     ,	pre_tok_state.pop('type'      )      )
              __lowerCamelCase  :      Union[str, Any]														=						do_lower_case
              __lowerCamelCase  :      str														=						strip_accents
              __lowerCamelCase  :      Optional[Any]														=						pre_tok_class(**SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      Tuple														=						do_lower_case







    def __getstate__(				self      )							->	List[str]:
         __lowerCamelCase  :      Union[str, Any]														=						self.__dict__.copy()
         __lowerCamelCase  :      Dict														=						BertPreTokenizer()
         return state







    def __setstate__(				self     ,	SCREAMING_SNAKE_CASE_      )							->	Optional[Any]:
         __lowerCamelCase  :      Optional[int]														=						d
         __lowerCamelCase  :      List[Any]														=						self.__dict__['_tokenizer'].get_vocab()
         __lowerCamelCase  :      Union[str, Any]														=						PreTokenizer.custom(JiebaPreTokenizer(SCREAMING_SNAKE_CASE_      )      )







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_=None      )							->	str:
         __lowerCamelCase  :      Union[str, Any]														=						[self.cls_token_id] + token_ids_a + [self.sep_token_id]

         if token_ids_a:
              output += token_ids_a + [self.sep_token_id]

         return output







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_ = None      )							->	List[int]:
         __lowerCamelCase  :      List[str]														=						[self.sep_token_id]
         __lowerCamelCase  :      Dict														=						[self.cls_token_id]
         if token_ids_a is None:
              return len(cls + token_ids_a + sep      ) * [0]
         return len(cls + token_ids_a + sep      ) * [0] + len(token_ids_a + sep      ) * [1]







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_ = None      )							->	Tuple[str]:
         __lowerCamelCase  :      Optional[Any]														=						self._tokenizer.model.save(SCREAMING_SNAKE_CASE_     ,	name=SCREAMING_SNAKE_CASE_      )
         return tuple(SCREAMING_SNAKE_CASE_      )







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=False     ,	**SCREAMING_SNAKE_CASE_     ,	)							->	Any:
         __lowerCamelCase  :      Tuple														=						BertPreTokenizer()
         return super().save_pretrained(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )






