






"""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


_lowerCAmelCase         =							{
    "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	(		snake_case__   ,   snake_case__   ,   snake_case__   ,   snake_case__=None					):
	# Initialise PyTorch model
	A_		:						Union[str, Any]	    =   XLNetConfig.from_json_file(snake_case__					)

	A_		:						Any	    =   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_		:						int	    =   finetuning_task
		A_		:						Any	    =   GLUE_TASKS_NUM_LABELS[finetuning_task]
		A_		:						str	    =   XLNetForSequenceClassification(snake_case__					)
	elif "squad" in finetuning_task:
		A_		:						int	    =   finetuning_task
		A_		:						Union[str, Any]	    =   XLNetForQuestionAnswering(snake_case__					)
	else:
		A_		:						int	    =   XLNetLMHeadModel(snake_case__					)

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

	# Save pytorch-model
	A_		:						List[Any]	    =   os.path.join(snake_case__   ,   snake_case__					)
	A_		:						List[Any]	    =   os.path.join(snake_case__   ,   snake_case__					)
	print(F"""Save PyTorch model to {os.path.abspath(snake_case__					)}"""					)
	torch.save(model.state_dict()   ,   snake_case__					)
	print(F"""Save configuration file to {os.path.abspath(snake_case__					)}"""					)
	with open(snake_case__   ,   """w"""   ,   encoding="""utf-8"""					) as f:
		f.write(config.to_json_string()					)


if __name__ == "__main__":
							_lowerCAmelCase         =							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",
							)
							_lowerCAmelCase         =							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.auto import AutoModelForSequenceClassification, AutoTokenizer
from .base import PipelineTool



class      SCREAMING_SNAKE_CASE  (				_SCREAMING_SNAKE_CASE						):





	"""simple docstring"""




	_A       :				Optional[int]    =				"""facebook/bart-large-mnli"""
	_A       :				str    =				(
	    """This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which """
	    """should be the text to classify, and `labels`, which should be the list of labels to use for classification. """
	    """It returns the most likely label in the list of provided `labels` for the input text."""
	)
	_A       :				List[str]    =				"""text_classifier"""
	_A       :				Optional[int]    =				AutoTokenizer
	_A       :				Optional[Any]    =				AutoModelForSequenceClassification

	_A       :				List[str]    =				["""text""", ["""text"""]]
	_A       :				Dict    =				["""text"""]





	def    lowerCamelCase(self							):
		super().setup()
		A_		:						int	    =   self.model.config
		A_		:						List[str]	    =   -1
		for idx, label in config.idalabel.items():
			if label.lower().startswith("""entail"""							):
				A_		:						List[Any]	    =   int(lowerCAmelCase_							)
		if self.entailment_id == -1:
			raise ValueError("""Could not determine the entailment ID from the model config, please pass it at init."""							)





	def    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						List[Any]	    =   labels
		return self.pre_processor(
		    [text] * len(lowerCAmelCase_							)     ,     [f"""This example is {label}""" for label in labels]     ,     return_tensors="""pt"""     ,     padding="""max_length"""     ,     )





	def    lowerCamelCase(self     ,     lowerCAmelCase_							):
		A_		:						str	    =   outputs.logits
		A_		:						Optional[int]	    =   torch.argmax(logits[:, 2]							).item()
		return self._labels[label_id]

