'''simple docstring'''

import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
							from PIL import Image
else:
							class   SCREAMING_SNAKE_CASE			:







									"""simple docstring"""






									@staticmethod
									def 		UpperCamelCase__       (   *__A		,						**__A				) ->     Union[str, Any]:
															pass
@is_pipeline_test
@require_vision
class   SCREAMING_SNAKE_CASE			(						unittest.TestCase):







		"""simple docstring"""






		@require_torch
		def 		UpperCamelCase__       (   self				) ->     int:
								_lowerCAmelCase										=pipeline(
								    model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'		,						)
								_lowerCAmelCase										=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
								_lowerCAmelCase										=image_classifier(__A		,						candidate_labels=['a', 'b', 'c']				)

								# The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
								# python and torch versions.
								self.assertIn(
								    nested_simplify(__A				)		,						[
								        [{'score': 0.333, 'label': 'a'}, {'score': 0.333, 'label': 'b'}, {'score': 0.333, 'label': 'c'}],
								        [{'score': 0.333, 'label': 'a'}, {'score': 0.333, 'label': 'c'}, {'score': 0.333, 'label': 'b'}],
								    ]		,						)

								_lowerCAmelCase										=image_classifier([image] * 5		,						candidate_labels=['A', 'B', 'C']		,						batch_size=2				)
								self.assertEqual(
								    nested_simplify(__A				)		,						[
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								    ]		,						)






		@require_tf
		def 		UpperCamelCase__       (   self				) ->     str:
								_lowerCAmelCase										=pipeline(
								    model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'		,						framework='tf'				)
								_lowerCAmelCase										=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
								_lowerCAmelCase										=image_classifier(__A		,						candidate_labels=['a', 'b', 'c']				)

								self.assertEqual(
								    nested_simplify(__A				)		,						[{'score': 0.333, 'label': 'a'}, {'score': 0.333, 'label': 'b'}, {'score': 0.333, 'label': 'c'}]		,						)

								_lowerCAmelCase										=image_classifier([image] * 5		,						candidate_labels=['A', 'B', 'C']		,						batch_size=2				)
								self.assertEqual(
								    nested_simplify(__A				)		,						[
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								            {'score': 0.333, 'label': ANY(__A				)},
								        ],
								    ]		,						)






		@slow
		@require_torch
		def 		UpperCamelCase__       (   self				) ->     Tuple:
								_lowerCAmelCase										=pipeline(
								    task='zero-shot-image-classification'		,						model='openai/clip-vit-base-patch32'		,						)
								# This is an image of 2 cats with remotes and no planes
								_lowerCAmelCase										=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
								_lowerCAmelCase										=image_classifier(__A		,						candidate_labels=['cat', 'plane', 'remote']				)

								self.assertEqual(
								    nested_simplify(__A				)		,						[
								        {'score': 0.511, 'label': 'remote'},
								        {'score': 0.485, 'label': 'cat'},
								        {'score': 0.004, 'label': 'plane'},
								    ]		,						)

								_lowerCAmelCase										=image_classifier([image] * 5		,						candidate_labels=['cat', 'plane', 'remote']		,						batch_size=2				)
								self.assertEqual(
								    nested_simplify(__A				)		,						[
								        [
								            {'score': 0.511, 'label': 'remote'},
								            {'score': 0.485, 'label': 'cat'},
								            {'score': 0.004, 'label': 'plane'},
								        ],
								    ]
								    * 5		,						)






		@slow
		@require_tf
		def 		UpperCamelCase__       (   self				) ->     int:
								_lowerCAmelCase										=pipeline(
								    task='zero-shot-image-classification'		,						model='openai/clip-vit-base-patch32'		,						framework='tf'				)
								# This is an image of 2 cats with remotes and no planes
								_lowerCAmelCase										=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
								_lowerCAmelCase										=image_classifier(__A		,						candidate_labels=['cat', 'plane', 'remote']				)
								self.assertEqual(
								    nested_simplify(__A				)		,						[
								        {'score': 0.511, 'label': 'remote'},
								        {'score': 0.485, 'label': 'cat'},
								        {'score': 0.004, 'label': 'plane'},
								    ]		,						)

								_lowerCAmelCase										=image_classifier([image] * 5		,						candidate_labels=['cat', 'plane', 'remote']		,						batch_size=2				)
								self.assertEqual(
								    nested_simplify(__A				)		,						[
								        [
								            {'score': 0.511, 'label': 'remote'},
								            {'score': 0.485, 'label': 'cat'},
								            {'score': 0.004, 'label': 'plane'},
								        ],
								    ]
								    * 5		,						)






'''simple docstring'''

import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_convbert import ConvBertTokenizer


lowercase_     		= logging.get_logger(__name__)

lowercase_     		= {'''vocab_file''': '''vocab.txt'''}

lowercase_     		= {
    '''vocab_file''': {
        '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt''',
        '''YituTech/conv-bert-medium-small''': (
            '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt'''
        ),
        '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt''',
    }
}

lowercase_     		= {
    '''YituTech/conv-bert-base''': 512,
    '''YituTech/conv-bert-medium-small''': 512,
    '''YituTech/conv-bert-small''': 512,
}


lowercase_     		= {
    '''YituTech/conv-bert-base''': {'''do_lower_case''': True},
    '''YituTech/conv-bert-medium-small''': {'''do_lower_case''': True},
    '''YituTech/conv-bert-small''': {'''do_lower_case''': True},
}
class   SCREAMING_SNAKE_CASE			(						__lowercase):







		"""simple docstring"""
		lowercase   :      Union[str, Any]       =							VOCAB_FILES_NAMES
		lowercase   :      Tuple       =							PRETRAINED_VOCAB_FILES_MAP
		lowercase   :      Optional[int]       =							PRETRAINED_INIT_CONFIGURATION
		lowercase   :      int       =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		lowercase   :      List[str]       =							ConvBertTokenizer






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

								_lowerCAmelCase										=json.loads(self.backend_tokenizer.normalizer.__getstate__()				)
								if (
								    normalizer_state.get('lowercase'		,						__A				) != do_lower_case
								    or normalizer_state.get('strip_accents'		,						__A				) != strip_accents
								    or normalizer_state.get('handle_chinese_chars'		,						__A				) != tokenize_chinese_chars
								):
														_lowerCAmelCase										=getattr(__A		,						normalizer_state.pop('type'				)				)
														_lowerCAmelCase										=do_lower_case
														_lowerCAmelCase										=strip_accents
														_lowerCAmelCase										=tokenize_chinese_chars
														_lowerCAmelCase										=normalizer_class(**__A				)

								_lowerCAmelCase										=do_lower_case






		def 		UpperCamelCase__       (   self		,						__A		,						__A=None				) ->     int:
								_lowerCAmelCase										=[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 		UpperCamelCase__       (   self		,						__A		,						__A = None				) ->     List[int]:
								_lowerCAmelCase										=[self.sep_token_id]
								_lowerCAmelCase										=[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 		UpperCamelCase__       (   self		,						__A		,						__A = None				) ->     Tuple[str]:
								_lowerCAmelCase										=self._tokenizer.model.save(__A		,						name=__A				)
								return tuple(__A				)





