



'''simple docstring'''





import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


class 				snake_case (unittest.TestCase		,      UpperCamelCase   ):



       def 							_a      (				self      )  ->							List[str]:
           lowercase__		       =						load_tool("text-classification"      )
           self.tool.setup()
           lowercase__		       =						load_tool("text-classification"     ,remote=UpperCAmelCase_      )



       def 							_a      (				self      )  ->							Any:
           lowercase__		       =						self.tool("That's quite cool"     ,["positive", "negative"]      )
           self.assertEqual(UpperCAmelCase_     ,"positive"      )



       def 							_a      (				self      )  ->							Optional[int]:
           lowercase__		       =						self.remote_tool("That's quite cool"     ,["positive", "negative"]      )
           self.assertEqual(UpperCAmelCase_     ,"positive"      )



       def 							_a      (				self      )  ->							List[Any]:
           lowercase__		       =						self.tool(text="That's quite cool"     ,labels=["positive", "negative"]      )
           self.assertEqual(UpperCAmelCase_     ,"positive"      )







       def 							_a      (				self      )  ->							List[Any]:
           lowercase__		       =						self.remote_tool(text="That's quite cool"     ,labels=["positive", "negative"]      )
           self.assertEqual(UpperCAmelCase_     ,"positive"      )









'''simple docstring'''





import copy
import random

from transformers import CLIPTokenizer


class 				snake_case (UpperCamelCase   ):



       def __init__(				self     ,*UpperCAmelCase_     ,**UpperCAmelCase_      )  ->							List[str]:
           super().__init__(*UpperCAmelCase_     ,**UpperCAmelCase_      )
           lowercase__		       =						{}



       def 							_a      (				self     ,UpperCAmelCase_     ,*UpperCAmelCase_     ,**UpperCAmelCase_      )  ->							str:
           lowercase__		       =						super().add_tokens(UpperCAmelCase_     ,*UpperCAmelCase_     ,**UpperCAmelCase_      )
           if num_added_tokens == 0:
               raise ValueError(
                   F'''The tokenizer already contains the token {placeholder_token}. Please pass a different'''
                   " `placeholder_token` that is not already in the tokenizer."      )



       def 							_a      (				self     ,UpperCAmelCase_     ,*UpperCAmelCase_     ,UpperCAmelCase_=1     ,**UpperCAmelCase_      )  ->							List[Any]:
           lowercase__		       =						[]
           if num_vec_per_token == 1:
               self.try_adding_tokens(UpperCAmelCase_     ,*UpperCAmelCase_     ,**UpperCAmelCase_      )
               output.append(UpperCAmelCase_      )
           else:
               lowercase__		       =						[]
               for i in range(UpperCAmelCase_      ):
                   lowercase__		       =						placeholder_token + F'''_{i}'''
                   self.try_adding_tokens(UpperCAmelCase_     ,*UpperCAmelCase_     ,**UpperCAmelCase_      )
                   output.append(UpperCAmelCase_      )
        # handle cases where there is a new placeholder token that contains the current placeholder token but is larger
           for token in self.token_map:
               if token in placeholder_token:
                   raise ValueError(
                       F'''The tokenizer already has placeholder token {token} that can get confused with'''
                       F''' {placeholder_token}keep placeholder tokens independent'''      )
           lowercase__		       =						output



       def 							_a      (				self     ,UpperCAmelCase_     ,UpperCAmelCase_=False     ,UpperCAmelCase_=1.0      )  ->							Optional[int]:
           if isinstance(UpperCAmelCase_     ,UpperCAmelCase_      ):
               lowercase__		       =						[]
               for i in range(len(UpperCAmelCase_      )      ):
                   output.append(self.replace_placeholder_tokens_in_text(text[i]     ,vector_shuffle=UpperCAmelCase_      )      )
               return output
           for placeholder_token in self.token_map:
               if placeholder_token in text:
                   lowercase__		       =						self.token_map[placeholder_token]
                   lowercase__		       =						tokens[: 1 + int(len(UpperCAmelCase_      ) * prop_tokens_to_load      )]
                   if vector_shuffle:
                       lowercase__		       =						copy.copy(UpperCAmelCase_      )
                       random.shuffle(UpperCAmelCase_      )
                   lowercase__		       =						text.replace(UpperCAmelCase_     ," ".join(UpperCAmelCase_      )      )
           return text



       def __call__(				self     ,UpperCAmelCase_     ,*UpperCAmelCase_     ,UpperCAmelCase_=False     ,UpperCAmelCase_=1.0     ,**UpperCAmelCase_      )  ->							int:
           return super().__call__(
               self.replace_placeholder_tokens_in_text(
                   UpperCAmelCase_     ,vector_shuffle=UpperCAmelCase_     ,prop_tokens_to_load=UpperCAmelCase_      )     ,*UpperCAmelCase_     ,**UpperCAmelCase_     ,)







       def 							_a      (				self     ,UpperCAmelCase_     ,*UpperCAmelCase_     ,UpperCAmelCase_=False     ,UpperCAmelCase_=1.0     ,**UpperCAmelCase_      )  ->							Optional[int]:
           return super().encode(
               self.replace_placeholder_tokens_in_text(
                   UpperCAmelCase_     ,vector_shuffle=UpperCAmelCase_     ,prop_tokens_to_load=UpperCAmelCase_      )     ,*UpperCAmelCase_     ,**UpperCAmelCase_     ,)




