
'''simple docstring'''






import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class 			UpperCAmelCase__    (							A			):
    lowerCAmelCase_					     =		['image_processor', 'tokenizer']
    lowerCAmelCase_					     =		'AutoImageProcessor'
    lowerCAmelCase_					     =		'AutoTokenizer'



    def __init__(						self   :							str,__A   :							List[str]=None,__A   :							Optional[Any]=None,**__A   :							str  ):
          _lowerCamelCase					: Optional[Any]		    = None
          if "feature_extractor" in kwargs:
                warnings.warn(
                    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                    " instead.",__A,)
                _lowerCamelCase					: Optional[int]		    = kwargs.pop("feature_extractor"  )

          _lowerCamelCase					: List[Any]		    = image_processor if image_processor is not None else feature_extractor
          if image_processor is None:
                raise ValueError("You need to specify an `image_processor`."  )
          if tokenizer is None:
                raise ValueError("You need to specify a `tokenizer`."  )

          super().__init__(__A,__A  )
          _lowerCamelCase					: Any		    = self.image_processor
          _lowerCamelCase					: str		    = False



    def __call__(						self   :							List[Any],*__A   :							Tuple,**__A   :							Optional[int]  ):
          # For backward compatibility
          if self._in_target_context_manager:
                return self.current_processor(*__A,**__A  )

          _lowerCamelCase					: List[str]		    = kwargs.pop("images",__A  )
          _lowerCamelCase					: Any		    = kwargs.pop("text",__A  )
          if len(__A  ) > 0:
                _lowerCamelCase					: Dict		    = args[0]
                _lowerCamelCase					: Tuple		    = args[1:]

          if images is None and text is None:
                raise ValueError("You need to specify either an `images` or `text` input to process."  )

          if images is not None:
                _lowerCamelCase					: str		    = self.image_processor(__A,*__A,**__A  )
          if text is not None:
                _lowerCamelCase					: Optional[int]		    = self.tokenizer(__A,**__A  )

          if text is None:
                return inputs
          elif images is None:
                return encodings
          else:
                _lowerCamelCase					: Union[str, Any]		    = encodings["input_ids"]
                return inputs



    def 	lowerCamelCase_	(						self   :							int,*__A   :							List[str],**__A   :							Any  ):
          return self.tokenizer.batch_decode(*__A,**__A  )



    def 	lowerCamelCase_	(						self   :							Dict,*__A   :							List[Any],**__A   :							List[str]  ):
          return self.tokenizer.decode(*__A,**__A  )



    @contextmanager
    def 	lowerCamelCase_	(						self   :							Optional[Any]  ):
          warnings.warn(
              "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
              "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
              "your images inputs, or in a separate call."  )
          _lowerCamelCase					: Tuple		    = True
          _lowerCamelCase					: Optional[int]		    = self.tokenizer
          yield
          _lowerCamelCase					: Any		    = self.image_processor
          _lowerCamelCase					: List[str]		    = False



    def 	lowerCamelCase_	(						self   :							List[Any],__A   :							str,__A   :							int=False,__A   :							int=None  ):
          if added_vocab is None:
                _lowerCamelCase					: List[Any]		    = self.tokenizer.get_added_vocab()

          _lowerCamelCase					: str		    = {}

          while tokens:
                _lowerCamelCase					: Tuple		    = re.search(r"<s_(.*?)>",__A,re.IGNORECASE  )
                if start_token is None:
                      break
                _lowerCamelCase					: Any		    = start_token.group(1  )
                _lowerCamelCase					: Optional[Any]		    = re.search(rf'</s_{key}>',__A,re.IGNORECASE  )
                _lowerCamelCase					: int		    = start_token.group()
                if end_token is None:
                      _lowerCamelCase					: Any		    = tokens.replace(__A,""  )
                else:
                      _lowerCamelCase					: Dict		    = end_token.group()
                      _lowerCamelCase					: Union[str, Any]		    = re.escape(__A  )
                      _lowerCamelCase					: Union[str, Any]		    = re.escape(__A  )
                      _lowerCamelCase					: Union[str, Any]		    = re.search(f'{start_token_escaped}(.*?){end_token_escaped}',__A,re.IGNORECASE  )
                      if content is not None:
                            _lowerCamelCase					: str		    = content.group(1  ).strip()
                            if r"<s_" in content and r"</s_" in content:  # non-leaf node
                                  _lowerCamelCase					: Union[str, Any]		    = self.tokenajson(__A,is_inner_value=__A,added_vocab=__A  )
                                  if value:
                                        if len(__A  ) == 1:
                                              _lowerCamelCase					: Optional[Any]		    = value[0]
                                        _lowerCamelCase					: Dict		    = value
                            else:  # leaf nodes
                                  _lowerCamelCase					: Tuple		    = []
                                  for leaf in content.split(r"<sep/>"  ):
                                        _lowerCamelCase					: List[Any]		    = leaf.strip()
                                        if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
                                              _lowerCamelCase					: int		    = leaf[1:-2]  # for categorical special tokens
                                        output[key].append(__A  )
                                  if len(output[key]  ) == 1:
                                        _lowerCamelCase					: Any		    = output[key][0]

                      _lowerCamelCase					: List[Any]		    = tokens[tokens.find(__A  ) + len(__A  ) :].strip()
                      if tokens[:6] == r"<sep/>":  # non-leaf nodes
                            return [output] + self.tokenajson(tokens[6:],is_inner_value=__A,added_vocab=__A  )

          if len(__A  ):
                return [output] if is_inner_value else output
          else:
                return [] if is_inner_value else {"text_sequence": tokens}



    @property
    def 	lowerCamelCase_	(						self   :							Any  ):
          warnings.warn(
              "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.",__A,)
          return self.image_processor_class



    @property
    def 	lowerCamelCase_	(						self   :							Dict  ):
          warnings.warn(
              "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.",__A,)
          return self.image_processor





'''simple docstring'''







import argparse
import json
import os
import sys
import tempfile
import unittest
from argparse import Namespace
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import List, Literal, Optional

import yaml

from transformers import HfArgumentParser, TrainingArguments
from transformers.hf_argparser import make_choice_type_function, string_to_bool


# Since Python 3.10, we can use the builtin `|` operator for Union types
# See PEP 604: https://peps.python.org/pep-0604
lowercase      :   Tuple      =       sys.version_info >= (3, 10)



def        lowerCamelCase__				(      __lowercase=None     ,   __lowercase=None				):
     return field(default_factory=lambda: default     ,   metadata=__lowercase				)







@dataclass
class 					_a :





     '''simple docstring'''



     lowerCAmelCase_  :     int
     lowerCAmelCase_  :     float
     lowerCAmelCase_  :     str
     lowerCAmelCase_  :     bool







@dataclass
class 					_a :





     '''simple docstring'''



     lowerCAmelCase_  :     int          =					42
     lowerCAmelCase_  :     str          =					field(default="""toto""",		metadata={"""help""": """help message"""}	)







@dataclass
class 					_a :





     '''simple docstring'''



     lowerCAmelCase_  :     bool          =					False
     lowerCAmelCase_  :     bool          =					True
     lowerCAmelCase_  :     Optional[bool]          =					None







class 					_a (a__	):





     '''simple docstring'''



     lowerCAmelCase_  :     int          =					"""titi"""
     lowerCAmelCase_  :     Optional[int]          =					"""toto"""







class 					_a (a__	):





     '''simple docstring'''



     lowerCAmelCase_  :     Any          =					"""titi"""
     lowerCAmelCase_  :     Dict          =					"""toto"""
     lowerCAmelCase_  :     int          =					42







@dataclass
class 					_a :





     '''simple docstring'''



     lowerCAmelCase_  :     BasicEnum          =					"toto"







     def 			snake_case_      (				self							)      ->				str:
          snake_case			:      Union[str, Any]        =			BasicEnum(self.foo							)







@dataclass
class 					_a :





     '''simple docstring'''



     lowerCAmelCase_  :     MixedTypeEnum          =					"toto"







     def 			snake_case_      (				self							)      ->				Tuple:
          snake_case			:      List[str]        =			MixedTypeEnum(self.foo							)







@dataclass
class 					_a :





     '''simple docstring'''



     lowerCAmelCase_  :     Optional[int]          =					None
     lowerCAmelCase_  :     Optional[float]          =					field(default=a__,		metadata={"""help""": """help message"""}	)
     lowerCAmelCase_  :     Optional[str]          =					None
     lowerCAmelCase_  :     Optional[List[str]]          =					list_field(default=[]	)
     lowerCAmelCase_  :     Optional[List[int]]          =					list_field(default=[]	)







@dataclass
class 					_a :





     '''simple docstring'''



     lowerCAmelCase_  :     List[int]          =					list_field(default=[]	)
     lowerCAmelCase_  :     List[int]          =					list_field(default=[1, 2, 3]	)
     lowerCAmelCase_  :     List[str]          =					list_field(default=["""Hallo""", """Bonjour""", """Hello"""]	)
     lowerCAmelCase_  :     List[float]          =					list_field(default=[0.1, 0.2, 0.3]	)







@dataclass
class 					_a :





     '''simple docstring'''



     lowerCAmelCase_  :     List[int]          =					field()
     lowerCAmelCase_  :     str          =					field()
     lowerCAmelCase_  :     BasicEnum          =					field()







     def 			snake_case_      (				self							)      ->				int:
          snake_case			:      Optional[int]        =			BasicEnum(self.required_enum							)







@dataclass
class 					_a :





     '''simple docstring'''



     lowerCAmelCase_  :     int
     lowerCAmelCase_  :     "BasicEnum"          =					field()
     lowerCAmelCase_  :     "Optional[bool]"          =					None
     lowerCAmelCase_  :     "str"          =					field(default="""toto""",		metadata={"""help""": """help message"""}	)
     lowerCAmelCase_  :     "List[str]"          =					list_field(default=["""Hallo""", """Bonjour""", """Hello"""]	)


if is_python_no_less_than_3_10:







      @dataclass
      class 					_a :





           '''simple docstring'''



           lowerCAmelCase_  :     bool          =					False
           lowerCAmelCase_  :     bool          =					True
           lowerCAmelCase_  :     bool | None          =					None







      @dataclass
      class 					_a :





           '''simple docstring'''



           lowerCAmelCase_  :     int | None          =					None
           lowerCAmelCase_  :     float | None          =					field(default=a__,		metadata={"""help""": """help message"""}	)
           lowerCAmelCase_  :     str | None          =					None
           lowerCAmelCase_  :     list[str] | None          =					list_field(default=[]	)
           lowerCAmelCase_  :     list[int] | None          =					list_field(default=[]	)







class 					_a (unittest.TestCase	):





     '''simple docstring'''



     def 			snake_case_      (				self							,__a							,__a							)      ->				Optional[int]:
          self.assertEqual(len(a._actions							)							,len(b._actions							)							)
          for x, y in zip(a._actions							,b._actions							):
               snake_case			:      Optional[int]        =			{k: v for k, v in vars(__a							).items() if k != """container"""}
               snake_case			:      Tuple        =			{k: v for k, v in vars(__a							).items() if k != """container"""}

               # Choices with mixed type have custom function as "type"
               # So we need to compare results directly for equality
               if xx.get("""choices"""							,__a							) and yy.get("""choices"""							,__a							):
                    for expected_choice in yy["choices"] + xx["choices"]:
                         self.assertEqual(xx["""type"""](__a							)							,yy["""type"""](__a							)							)
                    del xx["type"], yy["type"]

               self.assertEqual(__a							,__a							)







     def 			snake_case_      (				self							)      ->				int:
          snake_case			:      str        =			HfArgumentParser(__a							)

          snake_case			:      Dict        =			argparse.ArgumentParser()
          expected.add_argument("""--foo"""							,type=__a							,required=__a							)
          expected.add_argument("""--bar"""							,type=__a							,required=__a							)
          expected.add_argument("""--baz"""							,type=__a							,required=__a							)
          expected.add_argument("""--flag"""							,type=__a							,default=__a							,const=__a							,nargs="""?"""							)
          self.argparsersEqual(__a							,__a							)

          snake_case			:      Optional[int]        =			["""--foo""", """1""", """--baz""", """quux""", """--bar""", """0.5"""]
          ((snake_case) ,		)			:      Dict        =			parser.parse_args_into_dataclasses(__a							,look_for_args_file=__a							)
          self.assertFalse(example.flag							)







     def 			snake_case_      (				self							)      ->				int:
          snake_case			:      Optional[int]        =			HfArgumentParser(__a							)

          snake_case			:      str        =			argparse.ArgumentParser()
          expected.add_argument("""--foo"""							,default=42							,type=__a							)
          expected.add_argument("""--baz"""							,default="""toto"""							,type=__a							,help="""help message"""							)
          self.argparsersEqual(__a							,__a							)







     def 			snake_case_      (				self							)      ->				Optional[Any]:
          snake_case			:      Optional[int]        =			argparse.ArgumentParser()
          expected.add_argument("""--foo"""							,type=__a							,default=__a							,const=__a							,nargs="""?"""							)
          expected.add_argument("""--baz"""							,type=__a							,default=__a							,const=__a							,nargs="""?"""							)
          # A boolean no_* argument always has to come after its "default: True" regular counter-part
          # and its default must be set to False
          expected.add_argument("""--no_baz"""							,action="""store_false"""							,default=__a							,dest="""baz"""							)
          expected.add_argument("""--opt"""							,type=__a							,default=__a							)

          snake_case			:      str        =			[WithDefaultBoolExample]
          if is_python_no_less_than_3_10:
               dataclass_types.append(__a							)

          for dataclass_type in dataclass_types:
               snake_case			:      Optional[int]        =			HfArgumentParser(__a							)
               self.argparsersEqual(__a							,__a							)

               snake_case			:      List[str]        =			parser.parse_args([]							)
               self.assertEqual(__a							,Namespace(foo=__a							,baz=__a							,opt=__a							)							)

               snake_case			:      str        =			parser.parse_args(["""--foo""", """--no_baz"""]							)
               self.assertEqual(__a							,Namespace(foo=__a							,baz=__a							,opt=__a							)							)

               snake_case			:      Union[str, Any]        =			parser.parse_args(["""--foo""", """--baz"""]							)
               self.assertEqual(__a							,Namespace(foo=__a							,baz=__a							,opt=__a							)							)

               snake_case			:      Union[str, Any]        =			parser.parse_args(["""--foo""", """True""", """--baz""", """True""", """--opt""", """True"""]							)
               self.assertEqual(__a							,Namespace(foo=__a							,baz=__a							,opt=__a							)							)

               snake_case			:      List[str]        =			parser.parse_args(["""--foo""", """False""", """--baz""", """False""", """--opt""", """False"""]							)
               self.assertEqual(__a							,Namespace(foo=__a							,baz=__a							,opt=__a							)							)







     def 			snake_case_      (				self							)      ->				str:
          snake_case			:      Any        =			HfArgumentParser(__a							)

          snake_case			:      Union[str, Any]        =			argparse.ArgumentParser()
          expected.add_argument(
              """--foo"""							,default="""toto"""							,choices=["""titi""", """toto""", 42]							,type=make_choice_type_function(["""titi""", """toto""", 42]							)							,)
          self.argparsersEqual(__a							,__a							)

          snake_case			:      Any        =			parser.parse_args([]							)
          self.assertEqual(args.foo							,"""toto"""							)
          snake_case			:      str        =			parser.parse_args_into_dataclasses([]							)[0]
          self.assertEqual(enum_ex.foo							,MixedTypeEnum.toto							)

          snake_case			:      Optional[Any]        =			parser.parse_args(["""--foo""", """titi"""]							)
          self.assertEqual(args.foo							,"""titi"""							)
          snake_case			:      Optional[Any]        =			parser.parse_args_into_dataclasses(["""--foo""", """titi"""]							)[0]
          self.assertEqual(enum_ex.foo							,MixedTypeEnum.titi							)

          snake_case			:      Any        =			parser.parse_args(["""--foo""", """42"""]							)
          self.assertEqual(args.foo							,42							)
          snake_case			:      Any        =			parser.parse_args_into_dataclasses(["""--foo""", """42"""]							)[0]
          self.assertEqual(enum_ex.foo							,MixedTypeEnum.fourtytwo							)







     def 			snake_case_      (				self							)      ->				Union[str, Any]:







          @dataclass
          class 					_a :





               '''simple docstring'''



               lowerCAmelCase_  :     Literal["titi", "toto", 42]          =					"toto"
          snake_case			:      Tuple        =			HfArgumentParser(__a							)

          snake_case			:      List[Any]        =			argparse.ArgumentParser()
          expected.add_argument(
              """--foo"""							,default="""toto"""							,choices=("""titi""", """toto""", 42)							,type=make_choice_type_function(["""titi""", """toto""", 42]							)							,)
          self.argparsersEqual(__a							,__a							)

          snake_case			:      Optional[int]        =			parser.parse_args([]							)
          self.assertEqual(args.foo							,"""toto"""							)

          snake_case			:      int        =			parser.parse_args(["""--foo""", """titi"""]							)
          self.assertEqual(args.foo							,"""titi"""							)

          snake_case			:      Optional[int]        =			parser.parse_args(["""--foo""", """42"""]							)
          self.assertEqual(args.foo							,42							)







     def 			snake_case_      (				self							)      ->				List[Any]:
          snake_case			:      Any        =			HfArgumentParser(__a							)

          snake_case			:      Dict        =			argparse.ArgumentParser()
          expected.add_argument("""--foo_int"""							,nargs="""+"""							,default=[]							,type=__a							)
          expected.add_argument("""--bar_int"""							,nargs="""+"""							,default=[1, 2, 3]							,type=__a							)
          expected.add_argument("""--foo_str"""							,nargs="""+"""							,default=["""Hallo""", """Bonjour""", """Hello"""]							,type=__a							)
          expected.add_argument("""--foo_float"""							,nargs="""+"""							,default=[0.1, 0.2, 0.3]							,type=__a							)

          self.argparsersEqual(__a							,__a							)

          snake_case			:      int        =			parser.parse_args([]							)
          self.assertEqual(
              __a							,Namespace(foo_int=[]							,bar_int=[1, 2, 3]							,foo_str=["""Hallo""", """Bonjour""", """Hello"""]							,foo_float=[0.1, 0.2, 0.3]							)							,)

          snake_case			:      Optional[int]        =			parser.parse_args("""--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7""".split()							)
          self.assertEqual(__a							,Namespace(foo_int=[1]							,bar_int=[2, 3]							,foo_str=["""a""", """b""", """c"""]							,foo_float=[0.1, 0.7]							)							)







     def 			snake_case_      (				self							)      ->				Optional[int]:
          snake_case			:      List[Any]        =			argparse.ArgumentParser()
          expected.add_argument("""--foo"""							,default=__a							,type=__a							)
          expected.add_argument("""--bar"""							,default=__a							,type=__a							,help="""help message"""							)
          expected.add_argument("""--baz"""							,default=__a							,type=__a							)
          expected.add_argument("""--ces"""							,nargs="""+"""							,default=[]							,type=__a							)
          expected.add_argument("""--des"""							,nargs="""+"""							,default=[]							,type=__a							)

          snake_case			:      Optional[Any]        =			[OptionalExample]
          if is_python_no_less_than_3_10:
               dataclass_types.append(__a							)

          for dataclass_type in dataclass_types:
               snake_case			:      Any        =			HfArgumentParser(__a							)

               self.argparsersEqual(__a							,__a							)

               snake_case			:      Any        =			parser.parse_args([]							)
               self.assertEqual(__a							,Namespace(foo=__a							,bar=__a							,baz=__a							,ces=[]							,des=[]							)							)

               snake_case			:      Any        =			parser.parse_args("""--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3""".split()							)
               self.assertEqual(__a							,Namespace(foo=12							,bar=3.14							,baz="""42"""							,ces=["""a""", """b""", """c"""]							,des=[1, 2, 3]							)							)







     def 			snake_case_      (				self							)      ->				Any:
          snake_case			:      Optional[Any]        =			HfArgumentParser(__a							)

          snake_case			:      List[Any]        =			argparse.ArgumentParser()
          expected.add_argument("""--required_list"""							,nargs="""+"""							,type=__a							,required=__a							)
          expected.add_argument("""--required_str"""							,type=__a							,required=__a							)
          expected.add_argument(
              """--required_enum"""							,type=make_choice_type_function(["""titi""", """toto"""]							)							,choices=["""titi""", """toto"""]							,required=__a							,)
          self.argparsersEqual(__a							,__a							)







     def 			snake_case_      (				self							)      ->				List[Any]:
          snake_case			:      Optional[int]        =			HfArgumentParser(__a							)

          snake_case			:      Any        =			argparse.ArgumentParser()
          expected.add_argument("""--foo"""							,type=__a							,required=__a							)
          expected.add_argument(
              """--required_enum"""							,type=make_choice_type_function(["""titi""", """toto"""]							)							,choices=["""titi""", """toto"""]							,required=__a							,)
          expected.add_argument("""--opt"""							,type=__a							,default=__a							)
          expected.add_argument("""--baz"""							,default="""toto"""							,type=__a							,help="""help message"""							)
          expected.add_argument("""--foo_str"""							,nargs="""+"""							,default=["""Hallo""", """Bonjour""", """Hello"""]							,type=__a							)
          self.argparsersEqual(__a							,__a							)







     def 			snake_case_      (				self							)      ->				Tuple:
          snake_case			:      str        =			HfArgumentParser(__a							)

          snake_case			:      Any        =			{
              """foo""": 12,
              """bar""": 3.14,
              """baz""": """42""",
              """flag""": True,
          }

          snake_case			:      List[str]        =			parser.parse_dict(__a							)[0]
          snake_case			:      str        =			BasicExample(**__a							)
          self.assertEqual(__a							,__a							)







     def 			snake_case_      (				self							)      ->				Any:
          snake_case			:      Tuple        =			HfArgumentParser(__a							)

          snake_case			:      int        =			{
              """foo""": 12,
              """bar""": 3.14,
              """baz""": """42""",
              """flag""": True,
              """extra""": 42,
          }

          self.assertRaises(__a							,parser.parse_dict							,__a							,allow_extra_keys=__a							)







     def 			snake_case_      (				self							)      ->				Optional[int]:
          snake_case			:      List[str]        =			HfArgumentParser(__a							)

          snake_case			:      Any        =			{
              """foo""": 12,
              """bar""": 3.14,
              """baz""": """42""",
              """flag""": True,
          }
          with tempfile.TemporaryDirectory() as tmp_dir:
               snake_case			:      Dict        =			os.path.join(__a							,"""temp_json"""							)
               os.mkdir(__a							)
               with open(temp_local_path + """.json"""							,"""w+"""							) as f:
                    json.dump(__a							,__a							)
               snake_case			:      Any        =			parser.parse_yaml_file(Path(temp_local_path + """.json"""							)							)[0]

          snake_case			:      List[str]        =			BasicExample(**__a							)
          self.assertEqual(__a							,__a							)







     def 			snake_case_      (				self							)      ->				str:
          snake_case			:      Union[str, Any]        =			HfArgumentParser(__a							)

          snake_case			:      Any        =			{
              """foo""": 12,
              """bar""": 3.14,
              """baz""": """42""",
              """flag""": True,
          }
          with tempfile.TemporaryDirectory() as tmp_dir:
               snake_case			:      str        =			os.path.join(__a							,"""temp_yaml"""							)
               os.mkdir(__a							)
               with open(temp_local_path + """.yaml"""							,"""w+"""							) as f:
                    yaml.dump(__a							,__a							)
               snake_case			:      Union[str, Any]        =			parser.parse_yaml_file(Path(temp_local_path + """.yaml"""							)							)[0]
          snake_case			:      Tuple        =			BasicExample(**__a							)
          self.assertEqual(__a							,__a							)







     def 			snake_case_      (				self							)      ->				List[str]:
          snake_case			:      Union[str, Any]        =			HfArgumentParser(__a							)
          self.assertIsNotNone(__a							)



