





'''simple docstring'''




import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


_a			:	List[Any]								=  logging.getLogger(__name__)



@dataclass
class     lowercase_		:

  '''simple docstring'''





  __lowerCAmelCase   :	str      					=				field(
      metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}	)
  __lowerCAmelCase   :	Optional[str]      					=				field(
      default=a	,    metadata={"help": "Pretrained config name or path if not the same as model_name"}	)
  __lowerCAmelCase   :	Optional[str]      					=				field(
      default=a	,    metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}	)
  __lowerCAmelCase   :	bool      					=				field(default=a	,    metadata={"help": "Set this flag to use fast tokenization."}	)
  # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
  # or just modify its tokenizer_config.json.
  __lowerCAmelCase   :	Optional[str]      					=				field(
      default=a	,    metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}	,    )




def  lowerCamelCase__						(  ):
  UpperCAmelCase  							=		HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)  )

  if len(sys.argv  ) == 2 and sys.argv[1].endswith('.json'  ):
    # If we pass only one argument to the script and it's the path to a json file,
    # let's parse it to get our arguments.
    UpperCAmelCase							, UpperCAmelCase							, UpperCAmelCase  							=		parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]  )  )
  else:
    UpperCAmelCase							, UpperCAmelCase							, UpperCAmelCase  							=		parser.parse_args_into_dataclasses()

  if (
      os.path.exists(training_args.output_dir  )
      and os.listdir(training_args.output_dir  )
      and training_args.do_train
      and not training_args.overwrite_output_dir
  ):
    raise ValueError(
        f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use'''
        ' --overwrite_output_dir to overcome.'  )

  # Setup logging
  logging.basicConfig(
      format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'    ,				datefmt='%m/%d/%Y %H:%M:%S'    ,				level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN    ,				)
  logger.warning(
      'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s'    ,				training_args.local_rank    ,				training_args.device    ,				training_args.n_gpu    ,				bool(training_args.local_rank != -1  )    ,				training_args.fpaa    ,				)
  # Set the verbosity to info of the Transformers logger (on main process only):
  if is_main_process(training_args.local_rank  ):
    transformers.utils.logging.set_verbosity_info()
    transformers.utils.logging.enable_default_handler()
    transformers.utils.logging.enable_explicit_format()
  logger.info('Training/evaluation parameters %s'    ,				__A  )

  # Prepare Question-Answering task
  # Load pretrained model and tokenizer
  #
  # Distributed training:
  # The .from_pretrained methods guarantee that only one local process can concurrently
  # download model & vocab.

  UpperCAmelCase  							=		AutoConfig.from_pretrained(
      model_args.config_name if model_args.config_name else model_args.model_name_or_path    ,				cache_dir=model_args.cache_dir    ,				)
  UpperCAmelCase  							=		AutoTokenizer.from_pretrained(
      model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path    ,				cache_dir=model_args.cache_dir    ,				use_fast=__A    ,				)
  UpperCAmelCase  							=		AutoModelForQuestionAnswering.from_pretrained(
      model_args.model_name_or_path    ,				from_tf=bool('.ckpt' in model_args.model_name_or_path  )    ,				config=__A    ,				cache_dir=model_args.cache_dir    ,				)

  # Get datasets
  UpperCAmelCase  							=		hasattr(model.config    ,				'lang2id'  )
  UpperCAmelCase  							=		(
      SquadDataset(
          __A    ,				tokenizer=__A    ,				is_language_sensitive=__A    ,				cache_dir=model_args.cache_dir  )
      if training_args.do_train
      else None
  )
  UpperCAmelCase  							=		(
      SquadDataset(
          __A    ,				tokenizer=__A    ,				mode='dev'    ,				is_language_sensitive=__A    ,				cache_dir=model_args.cache_dir    ,				)
      if training_args.do_eval
      else None
  )

  # Data collator
  UpperCAmelCase  							=		DataCollatorWithPadding(__A    ,				pad_to_multiple_of=8  ) if training_args.fpaa else None

  # Initialize our Trainer
  UpperCAmelCase  							=		Trainer(
      model=__A    ,				args=__A    ,				train_dataset=__A    ,				eval_dataset=__A    ,				data_collator=__A    ,				)

  # Training
  if training_args.do_train:
    trainer.train(
        model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path  ) else None  )
    trainer.save_model()
    # For convenience, we also re-save the tokenizer to the same directory,
    # so that you can share your model easily on huggingface.co/models =)
    if trainer.is_world_master():
      tokenizer.save_pretrained(training_args.output_dir  )


def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				Tuple  ):
  main()


if __name__ == "__main__":
  main()





'''simple docstring'''




import os
import pathlib
import tempfile
import uuid

import numpy as np

from ..utils import is_soundfile_availble, is_torch_available, is_vision_available, logging


_a			:	int								=  logging.get_logger(__name__)

if is_vision_available():
  import PIL.Image
  from PIL import Image
  from PIL.Image import Image as ImageType
else:
  _a			:	List[str]								=  object

if is_torch_available():
  import torch

if is_soundfile_availble():
  import soundfile as sf



class     lowercase_		:

  '''simple docstring'''





  def __init__(       self				,	a_       )							->			Dict:


    """simple docstring"""
    UpperCAmelCase  							=		value
  def __str__(       self       )							->			Any:


    """simple docstring"""
    return self.to_string()
  def   snake_case_     (       self       )							->			Union[str, Any]:


    """simple docstring"""
    logger.error(
        'This is a raw AgentType of unknown type. Display in notebooks and string conversion will be unreliable'       )
    return self._value





  def   snake_case_     (       self       )							->			str:


    """simple docstring"""
    logger.error(
        'This is a raw AgentType of unknown type. Display in notebooks and string conversion will be unreliable'       )
    return str(self._value       )




class     lowercase_		(						a	,    a	):

  '''simple docstring'''





  def   snake_case_     (       self       )							->			Dict:


    """simple docstring"""
    return self._value





  def   snake_case_     (       self       )							->			List[Any]:


    """simple docstring"""
    return self._value




class     lowercase_		(						a	,    a	):

  '''simple docstring'''





  def __init__(       self				,	a_       )							->			int:


    """simple docstring"""
    super().__init__(a_       )

    if not is_vision_available():
      raise ImportError('PIL must be installed in order to handle images.'       )

    UpperCAmelCase  							=		None
    UpperCAmelCase  							=		None
    UpperCAmelCase  							=		None

    if isinstance(a_				,	a_       ):
      UpperCAmelCase  							=		value
    elif isinstance(a_				,	(str, pathlib.Path)       ):
      UpperCAmelCase  							=		value
    elif isinstance(a_				,	torch.Tensor       ):
      UpperCAmelCase  							=		value
    else:
      raise ValueError(F'''Unsupported type for {self.__class__.__name__}: {type(a_       )}'''       )
  def   snake_case_     (       self				,	a_=None				,	a_=None       )							->			Dict:


    """simple docstring"""
    from IPython.display import Image, display

    display(Image(self.to_string()       )       )
  def   snake_case_     (       self       )							->			List[str]:


    """simple docstring"""
    if self._raw is not None:
      return self._raw

    if self._path is not None:
      UpperCAmelCase  							=		Image.open(self._path       )
      return self._raw





  def   snake_case_     (       self       )							->			List[Any]:


    """simple docstring"""
    if self._path is not None:
      return self._path

    if self._raw is not None:
      UpperCAmelCase  							=		tempfile.mkdtemp()
      UpperCAmelCase  							=		os.path.join(a_				,	str(uuid.uuida()       ) + '.png'       )
      self._raw.save(self._path       )

      return self._path

    if self._tensor is not None:
      UpperCAmelCase  							=		self._tensor.cpu().detach().numpy()

      # There is likely simpler than load into image into save
      UpperCAmelCase  							=		Image.fromarray((array * 2_5_5).astype(np.uinta       )       )

      UpperCAmelCase  							=		tempfile.mkdtemp()
      UpperCAmelCase  							=		os.path.join(a_				,	str(uuid.uuida()       ) + '.png'       )

      img.save(self._path       )

      return self._path




class     lowercase_		(						a	):

  '''simple docstring'''





  def __init__(       self				,	a_				,	a_=1_6_0_0_0       )							->			str:


    """simple docstring"""
    super().__init__(a_       )

    if not is_soundfile_availble():
      raise ImportError('soundfile must be installed in order to handle audio.'       )

    UpperCAmelCase  							=		None
    UpperCAmelCase  							=		None

    UpperCAmelCase  							=		samplerate

    if isinstance(a_				,	(str, pathlib.Path)       ):
      UpperCAmelCase  							=		value
    elif isinstance(a_				,	torch.Tensor       ):
      UpperCAmelCase  							=		value
    else:
      raise ValueError(F'''Unsupported audio type: {type(a_       )}'''       )
  def   snake_case_     (       self				,	a_=None				,	a_=None       )							->			Dict:


    """simple docstring"""
    from IPython.display import Audio, display

    display(Audio(self.to_string()				,	rate=self.samplerate       )       )
  def   snake_case_     (       self       )							->			List[str]:


    """simple docstring"""
    if self._tensor is not None:
      return self._tensor

    if self._path is not None:
      UpperCAmelCase							, UpperCAmelCase  							=		sf.read(self._path       )
      UpperCAmelCase  							=		torch.tensor(a_       )
      return self._tensor





  def   snake_case_     (       self       )							->			int:


    """simple docstring"""
    if self._path is not None:
      return self._path

    if self._tensor is not None:
      UpperCAmelCase  							=		tempfile.mkdtemp()
      UpperCAmelCase  							=		os.path.join(a_				,	str(uuid.uuida()       ) + '.wav'       )
      sf.write(self._path				,	self._tensor				,	samplerate=self.samplerate       )
      return self._path




_a			:	Optional[int]								=  {'text': AgentText, 'image': AgentImage, 'audio': AgentAudio}
_a			:	Tuple								=  {str: AgentText}

if is_vision_available():
  _a			:	List[str]								=  AgentImage


def  lowerCamelCase__						(  *SCREAMING_SNAKE_CASE :				List[str]    ,				**SCREAMING_SNAKE_CASE :				Tuple  ):
  UpperCAmelCase  							=		[(arg.to_raw() if isinstance(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE  ) else arg) for arg in args]
  UpperCAmelCase  							=		{k: (v.to_raw() if isinstance(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE  ) else v) for k, v in kwargs.items()}
  return args, kwargs



def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				Tuple    ,				SCREAMING_SNAKE_CASE :				Optional[Any]=None  ):
  if isinstance(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE  ):
    UpperCAmelCase  							=		{}
    for i, (k, v) in enumerate(outputs.items()  ):
      if output_types is not None:
        # If the class has defined outputs, we can map directly according to the class definition
        if output_types[i] in AGENT_TYPE_MAPPING:
          UpperCAmelCase  							=		AGENT_TYPE_MAPPING[output_types[i]](SCREAMING_SNAKE_CASE  )
        else:
          UpperCAmelCase  							=		AgentType(SCREAMING_SNAKE_CASE  )

      else:
        # If the class does not have defined output, then we map according to the type
        for _k, _v in INSTANCE_TYPE_MAPPING.items():
          if isinstance(SCREAMING_SNAKE_CASE    ,				_k  ):
            UpperCAmelCase  							=		_v(SCREAMING_SNAKE_CASE  )
        if k not in decoded_outputs:
          UpperCAmelCase  							=		AgentType[v]

  elif isinstance(SCREAMING_SNAKE_CASE    ,				(list, tuple)  ):
    UpperCAmelCase  							=		type(SCREAMING_SNAKE_CASE  )()
    for i, v in enumerate(SCREAMING_SNAKE_CASE  ):
      if output_types is not None:
        # If the class has defined outputs, we can map directly according to the class definition
        if output_types[i] in AGENT_TYPE_MAPPING:
          decoded_outputs.append(AGENT_TYPE_MAPPING[output_types[i]](SCREAMING_SNAKE_CASE  )  )
        else:
          decoded_outputs.append(AgentType(SCREAMING_SNAKE_CASE  )  )
      else:
        # If the class does not have defined output, then we map according to the type
        UpperCAmelCase  							=		False
        for _k, _v in INSTANCE_TYPE_MAPPING.items():
          if isinstance(SCREAMING_SNAKE_CASE    ,				_k  ):
            decoded_outputs.append(_v(SCREAMING_SNAKE_CASE  )  )
            UpperCAmelCase  							=		True

        if not found:
          decoded_outputs.append(AgentType(SCREAMING_SNAKE_CASE  )  )

  else:
    if output_types[0] in AGENT_TYPE_MAPPING:
      # If the class has defined outputs, we can map directly according to the class definition
      UpperCAmelCase  							=		AGENT_TYPE_MAPPING[output_types[0]](SCREAMING_SNAKE_CASE  )

    else:
      # If the class does not have defined output, then we map according to the type
      for _k, _v in INSTANCE_TYPE_MAPPING.items():
        if isinstance(SCREAMING_SNAKE_CASE    ,				_k  ):
          return _v(SCREAMING_SNAKE_CASE  )
      return AgentType(SCREAMING_SNAKE_CASE  )

  return decoded_outputs


