


import json
from typing import Iterator, List, Union

from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers
from tokenizers.implementations.base_tokenizer import BaseTokenizer
from tokenizers.models import Unigram
from tokenizers.processors import TemplateProcessing







class 		_snake_case  (				_snake_case							):




   def __init__(		self ,  _lowerCamelCase = "▁" ,  _lowerCamelCase = True ,  _lowerCamelCase = "<unk>" ,  _lowerCamelCase = "</s>" ,  _lowerCamelCase = "<pad>" ,  ):
         a						:Dict										=	{
             '''pad''': {'''id''': 0, '''token''': pad_token},
             '''eos''': {'''id''': 1, '''token''': eos_token},
             '''unk''': {'''id''': 2, '''token''': unk_token},
         }

         a						:Any										=	[None] * len(self.special_tokens   )
         for token_dict in self.special_tokens.values():
               a						:Optional[int]										=	token_dict['''token''']

         a						:Union[str, Any]										=	Tokenizer(Unigram()   )

         a						:str										=	normalizers.Sequence(
             [
                 normalizers.Nmt(),
                 normalizers.NFKC(),
                 normalizers.Replace(Regex(''' {2,}'''   ) ,  ''' '''   ),
                 normalizers.Lowercase(),
             ]   )
         a						:Any										=	pre_tokenizers.Sequence(
             [
                 pre_tokenizers.Metaspace(replacement=_lowerCamelCase ,  add_prefix_space=_lowerCamelCase   ),
                 pre_tokenizers.Digits(individual_digits=_lowerCamelCase   ),
                 pre_tokenizers.Punctuation(),
             ]   )
         a						:Dict										=	decoders.Metaspace(replacement=_lowerCamelCase ,  add_prefix_space=_lowerCamelCase   )

         a						:Optional[Any]										=	TemplateProcessing(
             single=F'''$A {self.special_tokens['eos']['token']}''' ,  special_tokens=[(self.special_tokens['''eos''']['''token'''], self.special_tokens['''eos''']['''id'''])] ,  )

         a						:List[Any]										=	{
             '''model''': '''SentencePieceUnigram''',
             '''replacement''': replacement,
             '''add_prefix_space''': add_prefix_space,
         }

         super().__init__(_lowerCamelCase ,  _lowerCamelCase   )




   def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase = 8000 ,  _lowerCamelCase = True ,  ):
         a						:Union[str, Any]										=	trainers.UnigramTrainer(
             vocab_size=_lowerCamelCase ,  special_tokens=self.special_tokens_list ,  show_progress=_lowerCamelCase ,  )

         if isinstance(_lowerCamelCase ,  _lowerCamelCase   ):
               a						:List[str]										=	[files]
         self._tokenizer.train(_lowerCamelCase ,  trainer=_lowerCamelCase   )

         self.add_unk_id()




   def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase = 8000 ,  _lowerCamelCase = True ,  ):
         a						:Optional[Any]										=	trainers.UnigramTrainer(
             vocab_size=_lowerCamelCase ,  special_tokens=self.special_tokens_list ,  show_progress=_lowerCamelCase ,  )

         self._tokenizer.train_from_iterator(_lowerCamelCase ,  trainer=_lowerCamelCase   )

         self.add_unk_id()




   def 	SCREAMING_SNAKE_CASE__    (		self   ):
         a						:Any										=	json.loads(self._tokenizer.to_str()   )

         a						:List[Any]										=	self.special_tokens['''unk''']['''id''']

         a						:Any										=	Tokenizer.from_str(json.dumps(_lowerCamelCase   )   )









from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
     from PIL import Image

     from ..image_utils import load_image

if is_tf_available():
     import tensorflow as tf

     from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
     from ..tf_utils import stable_softmax

if is_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

snake_case    :   str	    =					logging.get_logger(__name__)







@add_end_docstrings(_snake_case							)
class 		_snake_case  (				_snake_case							):




 def __init__(		self ,  *_lowerCamelCase ,  **_lowerCamelCase   ):
       super().__init__(*_lowerCamelCase ,  **_lowerCamelCase   )
       requires_backends(self ,  '''vision'''   )
       self.check_model_type(
           TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
           if self.framework == '''tf'''
           else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING   )




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase=None   ):
       a						:Tuple										=	{}
       if top_k is not None:
             a						:int										=	top_k
       return {}, {}, postprocess_params




 def __call__(		self ,  _lowerCamelCase ,  **_lowerCamelCase   ):
       return super().__call__(_lowerCamelCase ,  **_lowerCamelCase   )




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase   ):
       a						:str										=	load_image(_lowerCamelCase   )
       a						:Any										=	self.image_processor(images=_lowerCamelCase ,  return_tensors=self.framework   )
       return model_inputs




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase   ):
       a						:List[str]										=	self.model(**_lowerCamelCase   )
       return model_outputs




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase=5   ):
       if top_k > self.model.config.num_labels:
             a						:List[Any]										=	self.model.config.num_labels

       if self.framework == "pt":
             a						:int										=	model_outputs.logits.softmax(-1   )[0]
             a							,    a						:Union[str, Any]										=	probs.topk(_lowerCamelCase   )
       elif self.framework == "tf":
             a						:Optional[Any]										=	stable_softmax(model_outputs.logits ,  axis=-1   )[0]
             a						:Union[str, Any]										=	tf.math.top_k(_lowerCamelCase ,  k=_lowerCamelCase   )
             a							,    a						:Optional[int]										=	topk.values.numpy(), topk.indices.numpy()
       else:
             raise ValueError(F'''Unsupported framework: {self.framework}'''   )

       a						:Optional[int]										=	scores.tolist()
       a						:str										=	ids.tolist()
       return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_lowerCamelCase ,  _lowerCamelCase   )]





