






'''simple docstring'''



import argparse
import os

import torch

from transformers import (
    XLNetConfig,
    XLNetForQuestionAnswering,
    XLNetForSequenceClassification,
    XLNetLMHeadModel,
    load_tf_weights_in_xlnet,
)
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


__UpperCamelCase      :       Any      			=      {
    """cola""": 2,
    """mnli""": 3,
    """mrpc""": 2,
    """sst-2""": 2,
    """sts-b""": 1,
    """qqp""": 2,
    """qnli""": 2,
    """rte""": 2,
    """wnli""": 2,
}


logging.set_verbosity_info()



def    __UpperCAmelCase	(				SCREAMING_SNAKE_CASE__:	List[Any],					SCREAMING_SNAKE_CASE__:	Dict,					SCREAMING_SNAKE_CASE__:	Tuple,					SCREAMING_SNAKE_CASE__:	Dict=None    )  ->			Any:

   """simple docstring"""
   # Initialise PyTorch model
   __a				     =    XLNetConfig.from_json_file(SCREAMING_SNAKE_CASE__    )

   __a				     =    finetuning_task.lower() if finetuning_task is not None else ''
   if finetuning_task in GLUE_TASKS_NUM_LABELS:
      print(f"""Building PyTorch XLNetForSequenceClassification model from configuration: {config}"""    )
      __a				     =    finetuning_task
      __a				     =    GLUE_TASKS_NUM_LABELS[finetuning_task]
      __a				     =    XLNetForSequenceClassification(SCREAMING_SNAKE_CASE__    )
   elif "squad" in finetuning_task:
      __a				     =    finetuning_task
      __a				     =    XLNetForQuestionAnswering(SCREAMING_SNAKE_CASE__    )
   else:
      __a				     =    XLNetLMHeadModel(SCREAMING_SNAKE_CASE__    )

   # Load weights from tf checkpoint
   load_tf_weights_in_xlnet(SCREAMING_SNAKE_CASE__,					SCREAMING_SNAKE_CASE__,					SCREAMING_SNAKE_CASE__    )

   # Save pytorch-model
   __a				     =    os.path.join(SCREAMING_SNAKE_CASE__,					SCREAMING_SNAKE_CASE__    )
   __a				     =    os.path.join(SCREAMING_SNAKE_CASE__,					SCREAMING_SNAKE_CASE__    )
   print(f"""Save PyTorch model to {os.path.abspath(SCREAMING_SNAKE_CASE__    )}"""    )
   torch.save(model.state_dict(),					SCREAMING_SNAKE_CASE__    )
   print(f"""Save configuration file to {os.path.abspath(SCREAMING_SNAKE_CASE__    )}"""    )
   with open(SCREAMING_SNAKE_CASE__,					'w',					encoding='utf-8'    ) as f:
      f.write(config.to_json_string()    )


if __name__ == "__main__":
 __UpperCamelCase      :       Dict      			=      argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
 )
 parser.add_argument(
     """--xlnet_config_file""",
     default=None,
     type=str,
     required=True,
     help=(
         """The config json file corresponding to the pre-trained XLNet model. \n"""
         """This specifies the model architecture."""
     ),
 )
 parser.add_argument(
     """--pytorch_dump_folder_path""",
     default=None,
     type=str,
     required=True,
     help="""Path to the folder to store the PyTorch model or dataset/vocab.""",
 )
 parser.add_argument(
     """--finetuning_task""",
     default=None,
     type=str,
     help="""Name of a task on which the XLNet TensorFlow model was fine-tuned""",
 )
 __UpperCamelCase      :       Optional[int]      			=      parser.parse_args()
 print(args)

 convert_xlnet_checkpoint_to_pytorch(
     args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task
 )







'''simple docstring'''



# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch

from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor
from ..utils import is_datasets_available
from .base import PipelineTool


if is_datasets_available():
 from datasets import load_dataset
class 							__SCREAMING_SNAKE_CASE       (							_lowerCAmelCase     ):
      __a  					="microsoft/speecht5_tts"
      __a  					=(
          "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the "
          "text to read (in English) and returns a waveform object containing the sound."
      )
      __a  					="text_reader"
      __a  					=SpeechTaProcessor
      __a  					=SpeechTaForTextToSpeech
      __a  					=SpeechTaHifiGan

      __a  					=["text"]
      __a  					=["audio"]




      def  __UpperCamelCase							(  self      )							->str:



         '''simple docstring'''
         if self.post_processor is None:
            __a				     =    'microsoft/speecht5_hifigan'
         super().setup()




      def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase=None      )							->str:



         '''simple docstring'''
         __a				     =    self.pre_processor(text=lowerCamelCase ,   return_tensors='pt' ,   truncation=lowerCamelCase      )

         if speaker_embeddings is None:
            if not is_datasets_available():
               raise ImportError('Datasets needs to be installed if not passing speaker embeddings.'      )

            __a				     =    load_dataset('Matthijs/cmu-arctic-xvectors' ,   split='validation'      )
            __a				     =    torch.tensor(embeddings_dataset[7305]['xvector']      ).unsqueeze(0      )

         return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings}




      def  __UpperCamelCase							(  self ,   lowerCamelCase      )							->List[str]:



         '''simple docstring'''
         with torch.no_grad():
            return self.model.generate_speech(**lowerCamelCase      )






      def  __UpperCamelCase							(  self ,   lowerCamelCase      )							->List[str]:



         '''simple docstring'''
         with torch.no_grad():
            return self.post_processor(lowerCamelCase      ).cpu().detach()