






'''simple docstring'''







from collections import UserDict
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_torch_available():
							from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
							from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
							from ..tf_utils import stable_softmax

UpperCamelCase    :      Dict  			=		logging.get_logger(__name__)




@add_end_docstrings(a			)
class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		def __init__(      self   ,**_lowerCAmelCase       ):
								super().__init__(**_lowerCAmelCase       )

								requires_backends(self   ,"""vision"""       )
								self.check_model_type(
								    TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
								    if self.framework == """tf"""
								    else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING       )






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






		def 			UpperCamelCase_					(      self   ,**_lowerCAmelCase       ):
								lowerCamelCase__         =  {}
								if "candidate_labels" in kwargs:
														lowerCamelCase__         =  kwargs["""candidate_labels"""]
								if "hypothesis_template" in kwargs:
														lowerCamelCase__         =  kwargs["""hypothesis_template"""]

								return preprocess_params, {}, {}






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase=None   ,_lowerCAmelCase="This is a photo of {}."       ):
								lowerCamelCase__         =  load_image(_lowerCAmelCase       )
								lowerCamelCase__         =  self.image_processor(images=[image]   ,return_tensors=self.framework       )
								lowerCamelCase__         =  candidate_labels
								lowerCamelCase__         =  [hypothesis_template.format(_lowerCAmelCase       ) for x in candidate_labels]
								lowerCamelCase__         =  self.tokenizer(_lowerCAmelCase   ,return_tensors=self.framework   ,padding=_lowerCAmelCase       )
								lowerCamelCase__         =  [text_inputs]
								return inputs






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  model_inputs.pop("""candidate_labels"""       )
								lowerCamelCase__         =  model_inputs.pop("""text_inputs"""       )
								if isinstance(text_inputs[0]   ,_lowerCAmelCase       ):
														lowerCamelCase__         =  text_inputs[0]
								else:
														# Batching case.
														lowerCamelCase__         =  text_inputs[0][0]

								lowerCamelCase__         =  self.model(**_lowerCAmelCase   ,**_lowerCAmelCase       )

								lowerCamelCase__         =  {
								    """candidate_labels""": candidate_labels,
								    """logits""": outputs.logits_per_image,
								}
								return model_outputs






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  model_outputs.pop("""candidate_labels"""       )
								lowerCamelCase__         =  model_outputs["""logits"""][0]
								if self.framework == "pt":
														lowerCamelCase__         =  logits.softmax(dim=-1       ).squeeze(-1       )
														lowerCamelCase__         =  probs.tolist()
														if not isinstance(_lowerCAmelCase   ,_lowerCAmelCase       ):
																				lowerCamelCase__         =  [scores]
								elif self.framework == "tf":
														lowerCamelCase__         =  stable_softmax(_lowerCAmelCase   ,axis=-1       )
														lowerCamelCase__         =  probs.numpy().tolist()
								else:
														raise ValueError(F'''Unsupported framework: {self.framework}'''       )

								lowerCamelCase__         =  [
								    {"""score""": score, """label""": candidate_label}
								    for score, candidate_label in sorted(zip(_lowerCAmelCase   ,_lowerCAmelCase       )   ,key=lambda _lowerCAmelCase       : -x[0]       )
								]
								return result









'''simple docstring'''







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 		UpperCamelCase__    (a			):




		'''simple docstring'''





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

								lowerCamelCase__         =  [None] * len(self.special_tokens       )
								for token_dict in self.special_tokens.values():
														lowerCamelCase__         =  token_dict["""token"""]

								lowerCamelCase__         =  Tokenizer(Unigram()       )

								lowerCamelCase__         =  normalizers.Sequence(
								    [
								        normalizers.Nmt(),
								        normalizers.NFKC(),
								        normalizers.Replace(Regex(""" {2,}"""       )   ,""" """       ),
								        normalizers.Lowercase(),
								    ]       )
								lowerCamelCase__         =  pre_tokenizers.Sequence(
								    [
								        pre_tokenizers.Metaspace(replacement=_lowerCAmelCase   ,add_prefix_space=_lowerCAmelCase       ),
								        pre_tokenizers.Digits(individual_digits=_lowerCAmelCase       ),
								        pre_tokenizers.Punctuation(),
								    ]       )
								lowerCamelCase__         =  decoders.Metaspace(replacement=_lowerCAmelCase   ,add_prefix_space=_lowerCAmelCase       )

								lowerCamelCase__         =  TemplateProcessing(
								    single=F'''$A {self.special_tokens["eos"]["token"]}'''   ,special_tokens=[(self.special_tokens["""eos"""]["""token"""], self.special_tokens["""eos"""]["""id"""])]   ,)

								lowerCamelCase__         =  {
								    """model""": """SentencePieceUnigram""",
								    """replacement""": replacement,
								    """add_prefix_space""": add_prefix_space,
								}

								super().__init__(_lowerCAmelCase   ,_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = 80_00   ,_lowerCAmelCase = True   ,):
								lowerCamelCase__         =  trainers.UnigramTrainer(
								    vocab_size=_lowerCAmelCase   ,special_tokens=self.special_tokens_list   ,show_progress=_lowerCAmelCase   ,)

								if isinstance(_lowerCAmelCase   ,_lowerCAmelCase       ):
														lowerCamelCase__         =  [files]
								self._tokenizer.train(_lowerCAmelCase   ,trainer=_lowerCAmelCase       )

								self.add_unk_id()






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = 80_00   ,_lowerCAmelCase = True   ,):
								lowerCamelCase__         =  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 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  json.loads(self._tokenizer.to_str()       )

								lowerCamelCase__         =  self.special_tokens["""unk"""]["""id"""]

								lowerCamelCase__         =  Tokenizer.from_str(json.dumps(_lowerCAmelCase       )       )

