

from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


snake_case_        =			logging.get_logger(__name__)


class 				SCREAMING_SNAKE_CASE__ (__snake_case					):





    def 				snake_case_    (				self						,       a):
         if isinstance(a						,       a):
              lowercase__   :       Dict				=	[label.strip() for label in labels.split(',') if label.strip()]
         return labels





    def __call__(				self						,       a						,       a						,       a):
         if len(a) == 0 or len(a) == 0:
              raise ValueError('You must include at least one label and at least one sequence.')
         if hypothesis_template.format(labels[0]) == hypothesis_template:
              raise ValueError(
                  (
                      'The provided hypothesis_template "{}" was not able to be formatted with the target labels. '
                      'Make sure the passed template includes formatting syntax such as {{}} where the label should go.'
                  ).format(a))

         if isinstance(a						,       a):
              lowercase__   :       Tuple				=	[sequences]

         lowercase__   :       Optional[int]				=	[]
         for sequence in sequences:
              sequence_pairs.extend([[sequence, hypothesis_template.format(a)] for label in labels])

         return sequence_pairs, sequences



@add_end_docstrings(__snake_case					)
class 				SCREAMING_SNAKE_CASE__ (__snake_case					):





    def __init__(				self						,       a=ZeroShotClassificationArgumentHandler()						,       *a						,       **a):
         lowercase__   :       List[Any]				=	args_parser
         super().__init__(*a						,       **a)
         if self.entailment_id == -1:
              logger.warning(
                  'Failed to determine \'entailment\' label id from the label2id mapping in the model config. Setting to '
                  '-1. Define a descriptive label2id mapping in the model config to ensure correct outputs.')





    @property
    def 				snake_case_    (				self):
         for label, ind in self.model.config.labelaid.items():
              if label.lower().startswith('entail'):
                   return ind
         return -1





    def 				snake_case_    (				self						,       a						,       a=True						,       a=True						,       a=TruncationStrategy.ONLY_FIRST						,       **a):
         lowercase__   :       List[Any]				=	self.framework
         if self.tokenizer.pad_token is None:
              # Override for tokenizers not supporting padding
              logger.error(
                  'Tokenizer was not supporting padding necessary for zero-shot, attempting to use '
                  ' `pad_token=eos_token`')
              lowercase__   :       Tuple				=	self.tokenizer.eos_token
         try:
              lowercase__   :       Tuple				=	self.tokenizer(
                  a						,       add_special_tokens=a						,       return_tensors=a						,       padding=a						,       truncation=a						,       )
         except Exception as e:
              if "too short" in str(a):
                   # tokenizers might yell that we want to truncate
                   # to a value that is not even reached by the input.
                   # In that case we don't want to truncate.
                   # It seems there's not a really better way to catch that
                   # exception.

                   lowercase__   :       Union[str, Any]				=	self.tokenizer(
                       a						,       add_special_tokens=a						,       return_tensors=a						,       padding=a						,       truncation=TruncationStrategy.DO_NOT_TRUNCATE						,       )
              else:
                   raise e

         return inputs





    def 				snake_case_    (				self						,       **a):
         if kwargs.get('multi_class'						,       a) is not None:
              lowercase__   :       Any				=	kwargs['multi_class']
              logger.warning(
                  'The `multi_class` argument has been deprecated and renamed to `multi_label`. '
                  '`multi_class` will be removed in a future version of Transformers.')
         lowercase__   :       Optional[Any]				=	{}
         if "candidate_labels" in kwargs:
              lowercase__   :       Optional[int]				=	self._args_parser._parse_labels(kwargs['candidate_labels'])
         if "hypothesis_template" in kwargs:
              lowercase__   :       Union[str, Any]				=	kwargs['hypothesis_template']

         lowercase__   :       Any				=	{}
         if "multi_label" in kwargs:
              lowercase__   :       Tuple				=	kwargs['multi_label']
         return preprocess_params, {}, postprocess_params





    def __call__(				self						,       a						,       *a						,       **a						,       ):
         if len(a) == 0:
              pass
         elif len(a) == 1 and "candidate_labels" not in kwargs:
              lowercase__   :       Dict				=	args[0]
         else:
              raise ValueError(f"""Unable to understand extra arguments {args}""")

         return super().__call__(a						,       **a)





    def 				snake_case_    (				self						,       a						,       a=None						,       a="This example is {}."):
         lowercase__				,			lowercase__   :       Union[str, Any]				=	self._args_parser(a						,       a						,       a)

         for i, (candidate_label, sequence_pair) in enumerate(zip(a						,       a)):
              lowercase__   :       List[str]				=	self._parse_and_tokenize([sequence_pair])

              yield {
                  "candidate_label": candidate_label,
                  "sequence": sequences[0],
                  "is_last": i == len(a) - 1,
                  **model_input,
              }





    def 				snake_case_    (				self						,       a):
         lowercase__   :       Dict				=	inputs['candidate_label']
         lowercase__   :       List[Any]				=	inputs['sequence']
         lowercase__   :       Optional[int]				=	{k: inputs[k] for k in self.tokenizer.model_input_names}
         lowercase__   :       List[Any]				=	self.model(**a)

         lowercase__   :       List[Any]				=	{
             'candidate_label': candidate_label,
             'sequence': sequence,
             'is_last': inputs['is_last'],
             **outputs,
         }
         return model_outputs





    def 				snake_case_    (				self						,       a						,       a=False):
         lowercase__   :       List[str]				=	[outputs['candidate_label'] for outputs in model_outputs]
         lowercase__   :       str				=	[outputs['sequence'] for outputs in model_outputs]
         lowercase__   :       str				=	np.concatenate([output['logits'].numpy() for output in model_outputs])
         lowercase__   :       List[Any]				=	logits.shape[0]
         lowercase__   :       List[Any]				=	len(a)
         lowercase__   :       Optional[int]				=	N // n
         lowercase__   :       Optional[int]				=	logits.reshape((num_sequences, n, -1))

         if multi_label or len(a) == 1:
              # softmax over the entailment vs. contradiction dim for each label independently
              lowercase__   :       Any				=	self.entailment_id
              lowercase__   :       Optional[int]				=	-1 if entailment_id == 0 else 0
              lowercase__   :       Optional[Any]				=	reshaped_outputs[..., [contradiction_id, entailment_id]]
              lowercase__   :       Union[str, Any]				=	np.exp(a) / np.exp(a).sum(-1						,       keepdims=a)
              lowercase__   :       List[Any]				=	scores[..., 1]
         else:
              # softmax the "entailment" logits over all candidate labels
              lowercase__   :       Union[str, Any]				=	reshaped_outputs[..., self.entailment_id]
              lowercase__   :       List[str]				=	np.exp(a) / np.exp(a).sum(-1						,       keepdims=a)

         lowercase__   :       List[Any]				=	list(reversed(scores[0].argsort()))
         return {
             "sequence": sequences[0],
             "labels": [candidate_labels[i] for i in top_inds],
             "scores": scores[0, top_inds].tolist(),
         }




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


snake_case_        =			logging.get_logger(__name__)

snake_case_        =			{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

snake_case_        =			{
    '''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'''
        ),
    }
}

snake_case_        =			{
    '''junnyu/roformer_chinese_small''': 1_536,
    '''junnyu/roformer_chinese_base''': 1_536,
    '''junnyu/roformer_chinese_char_small''': 512,
    '''junnyu/roformer_chinese_char_base''': 512,
    '''junnyu/roformer_small_discriminator''': 128,
    '''junnyu/roformer_small_generator''': 128,
}


snake_case_        =			{
    '''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 				SCREAMING_SNAKE_CASE__ (__snake_case					):
    __lowerCamelCase						:	Union[str, Any]														=					VOCAB_FILES_NAMES
    __lowerCamelCase						:	int														=					PRETRAINED_VOCAB_FILES_MAP
    __lowerCamelCase						:	Any														=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    __lowerCamelCase						:	int														=					PRETRAINED_INIT_CONFIGURATION
    __lowerCamelCase						:	Tuple														=					RoFormerTokenizer





    def __init__(				self						,       a=None						,       a=None						,       a=True						,       a="[UNK]"						,       a="[SEP]"						,       a="[PAD]"						,       a="[CLS]"						,       a="[MASK]"						,       a=True						,       a=None						,       **a						,       ):
         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						,       )

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

         lowercase__   :       Optional[int]				=	do_lower_case





    def __getstate__(				self):
         lowercase__   :       str				=	self.__dict__.copy()
         lowercase__   :       Any				=	BertPreTokenizer()
         return state





    def __setstate__(				self						,       a):
         lowercase__   :       Union[str, Any]				=	d
         lowercase__   :       int				=	self.__dict__['_tokenizer'].get_vocab()
         lowercase__   :       List[str]				=	PreTokenizer.custom(JiebaPreTokenizer(a))





    def 				snake_case_    (				self						,       a						,       a=None):
         lowercase__   :       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 				snake_case_    (				self						,       a						,       a = None):
         lowercase__   :       Tuple				=	[self.sep_token_id]
         lowercase__   :       Optional[int]				=	[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 				snake_case_    (				self						,       a						,       a = None):
         lowercase__   :       Optional[Any]				=	self._tokenizer.model.save(a						,       name=a)
         return tuple(a)





    def 				snake_case_    (				self						,       a						,       a=None						,       a=None						,       a=False						,       **a						,       ):
         lowercase__   :       List[str]				=	BertPreTokenizer()
         return super().save_pretrained(a						,       a						,       a						,       a						,       **a)

