from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers.models.xglm.modeling_tf_xglm import (
		    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFXGLMForCausalLM,
		    TFXGLMModel,
		)





@require_tf
class     snake_case_			:


							"""simple docstring"""





							__UpperCAmelCase       =XGLMConfig
							__UpperCAmelCase       ={}
							__UpperCAmelCase       ="""gelu"""




							def __init__( self					,						_A					,						_A=1_4					,						_A=7					,						_A=True					,						_A=True					,						_A=True					,						_A=9_9					,						_A=3_2					,						_A=2					,						_A=4					,						_A=3_7					,						_A="gelu"					,						_A=0.1					,						_A=0.1					,						_A=5_1_2					,						_A=0.02					,						):
												__lowerCAmelCase					=							parent
												__lowerCAmelCase					=							batch_size
												__lowerCAmelCase					=							seq_length
												__lowerCAmelCase					=							is_training
												__lowerCAmelCase					=							use_input_mask
												__lowerCAmelCase					=							use_labels
												__lowerCAmelCase					=							vocab_size
												__lowerCAmelCase					=							d_model
												__lowerCAmelCase					=							num_hidden_layers
												__lowerCAmelCase					=							num_attention_heads
												__lowerCAmelCase					=							ffn_dim
												__lowerCAmelCase					=							activation_function
												__lowerCAmelCase					=							activation_dropout
												__lowerCAmelCase					=							attention_dropout
												__lowerCAmelCase					=							max_position_embeddings
												__lowerCAmelCase					=							initializer_range
												__lowerCAmelCase					=							None
												__lowerCAmelCase					=							0
												__lowerCAmelCase					=							2
												__lowerCAmelCase					=							1




							def        A__  ( self							):
												return XGLMConfig.from_pretrained('facebook/xglm-564M'							)




							def        A__  ( self							):
												__lowerCAmelCase					=							tf.clip_by_value(
												    ids_tensor([self.batch_size, self.seq_length]					,						self.vocab_size							)					,						clip_value_min=0					,						clip_value_max=3							)

												__lowerCAmelCase					=							None
												if self.use_input_mask:
																	__lowerCAmelCase					=							random_attention_mask([self.batch_size, self.seq_length]							)

												__lowerCAmelCase					=							self.get_config()

												__lowerCAmelCase					=							floats_tensor([self.num_hidden_layers, self.num_attention_heads]					,						2							)

												return (
												    config,
												    input_ids,
												    input_mask,
												    head_mask,
												)




							def        A__  ( self							):
												return XGLMConfig(
												    vocab_size=self.vocab_size					,						d_model=self.hidden_size					,						num_layers=self.num_hidden_layers					,						attention_heads=self.num_attention_heads					,						ffn_dim=self.ffn_dim					,						activation_function=self.activation_function					,						activation_dropout=self.activation_dropout					,						attention_dropout=self.attention_dropout					,						max_position_embeddings=self.max_position_embeddings					,						initializer_range=self.initializer_range					,						use_cache=_A					,						bos_token_id=self.bos_token_id					,						eos_token_id=self.eos_token_id					,						pad_token_id=self.pad_token_id					,						return_dict=_A					,						)




							def        A__  ( self							):
												__lowerCAmelCase					=							self.prepare_config_and_inputs()

												(
												    __lowerCAmelCase       
												)					=							config_and_inputs

												__lowerCAmelCase					=							{
												    'input_ids': input_ids,
												    'head_mask': head_mask,
												}

												return config, inputs_dict





@require_tf
class     snake_case_			(				_a			, _a			, unittest.TestCase							):


							"""simple docstring"""





							__UpperCAmelCase       =(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
							__UpperCAmelCase       =(TFXGLMForCausalLM,) if is_tf_available() else ()
							__UpperCAmelCase       =(
							    {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {}
							)
							__UpperCAmelCase       =False
							__UpperCAmelCase       =False
							__UpperCAmelCase       =False




							def        A__  ( self							):
												__lowerCAmelCase					=							TFXGLMModelTester(self							)
												__lowerCAmelCase					=							ConfigTester(self					,						config_class=_A					,						n_embd=3_7							)




							def        A__  ( self							):
												self.config_tester.run_common_tests()




							@slow
							def        A__  ( self							):







												for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	__lowerCAmelCase					=							TFXGLMModel.from_pretrained(_A							)
																	self.assertIsNotNone(_A							)




							@unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.'							)
							def        A__  ( self							):
												super().test_resize_token_embeddings()





@require_tf
class     snake_case_			(				unittest.TestCase							):


							"""simple docstring"""





							@slow
							def        A__  ( self					,						_A=True							):
												__lowerCAmelCase					=							TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M'							)
												__lowerCAmelCase					=							tf.convert_to_tensor([[2, 2_6_8, 9_8_6_5]]					,						dtype=tf.intaa							)  # The dog
												# </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
												# fmt: off
												__lowerCAmelCase					=							[2, 2_6_8, 9_8_6_5, 6_7, 1_1, 1_9_8_8, 5_7_2_5_2, 9_8_6_5, 5, 9_8_4, 6_7, 1_9_8_8, 2_1_3_8_3_8, 1_6_5_8, 5_3, 7_0_4_4_6, 3_3, 6_6_5_7, 2_7_8, 1_5_8_1]
												# fmt: on
												__lowerCAmelCase					=							model.generate(_A					,						do_sample=_A					,						num_beams=1							)
												if verify_outputs:
																	self.assertListEqual(output_ids[0].numpy().tolist()					,						_A							)




							@slow
							def        A__  ( self							):
												__lowerCAmelCase					=							XGLMTokenizer.from_pretrained('facebook/xglm-564M'							)
												__lowerCAmelCase					=							TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M'							)

												tf.random.set_seed(0							)
												__lowerCAmelCase					=							tokenizer('Today is a nice day and'					,						return_tensors='tf'							)
												__lowerCAmelCase					=							tokenized.input_ids
												# forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
												with tf.device(':/CPU:0'							):
																	__lowerCAmelCase					=							model.generate(_A					,						do_sample=_A					,						seed=[7, 0]							)
												__lowerCAmelCase					=							tokenizer.decode(output_ids[0]					,						skip_special_tokens=_A							)

												__lowerCAmelCase					=							(
												    'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'
												)
												self.assertEqual(_A					,						_A							)




							@slow
							def        A__  ( self							):
												__lowerCAmelCase					=							TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M'							)
												__lowerCAmelCase					=							XGLMTokenizer.from_pretrained('facebook/xglm-564M'							)

												__lowerCAmelCase					=							'left'

												# use different length sentences to test batching
												__lowerCAmelCase					=							[
												    'This is an extremelly long sentence that only exists to test the ability of the model to cope with '
												    'left-padding, such as in batched generation. The output for the sequence below should be the same '
												    'regardless of whether left padding is applied or not. When',
												    'Hello, my dog is a little',
												]

												__lowerCAmelCase					=							tokenizer(_A					,						return_tensors='tf'					,						padding=_A							)
												__lowerCAmelCase					=							inputs['input_ids']

												__lowerCAmelCase					=							model.generate(input_ids=_A					,						attention_mask=inputs['attention_mask']					,						max_new_tokens=1_2							)

												__lowerCAmelCase					=							tokenizer(sentences[0]					,						return_tensors='tf'							).input_ids
												__lowerCAmelCase					=							model.generate(input_ids=_A					,						max_new_tokens=1_2							)

												__lowerCAmelCase					=							tokenizer(sentences[1]					,						return_tensors='tf'							).input_ids
												__lowerCAmelCase					=							model.generate(input_ids=_A					,						max_new_tokens=1_2							)

												__lowerCAmelCase					=							tokenizer.batch_decode(_A					,						skip_special_tokens=_A							)
												__lowerCAmelCase					=							tokenizer.decode(output_non_padded[0]					,						skip_special_tokens=_A							)
												__lowerCAmelCase					=							tokenizer.decode(output_padded[0]					,						skip_special_tokens=_A							)

												__lowerCAmelCase					=							[
												    'This is an extremelly long sentence that only exists to test the ability of the model to cope with '
												    'left-padding, such as in batched generation. The output for the sequence below should be the same '
												    'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be '
												    'a single',
												    'Hello, my dog is a little bit of a shy one, but he is very friendly',
												]
												self.assertListEqual(_A					,						_A							)
												self.assertListEqual(_A					,						[non_padded_sentence, padded_sentence]							)









# 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     snake_case_			(				_a							):


							"""simple docstring"""





							__UpperCAmelCase       ="""facebook/bart-large-mnli"""
							__UpperCAmelCase       =(
							    """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."""
							)
							__UpperCAmelCase       ="""text_classifier"""
							__UpperCAmelCase       =AutoTokenizer
							__UpperCAmelCase       =AutoModelForSequenceClassification

							__UpperCAmelCase       =["""text""", ["""text"""]]
							__UpperCAmelCase       =["""text"""]




							def        A__  ( self							):
												super().setup()
												__lowerCAmelCase					=							self.model.config
												__lowerCAmelCase					=							-1
												for idx, label in config.idalabel.items():
																	if label.lower().startswith('entail'							):
																						__lowerCAmelCase					=							int(_A							)
												if self.entailment_id == -1:
																	raise ValueError('Could not determine the entailment ID from the model config, please pass it at init.'							)




							def        A__  ( self					,						_A					,						_A							):
												__lowerCAmelCase					=							labels
												return self.pre_processor(
												    [text] * len(_A							)					,						[F"""This example is {label}""" for label in labels]					,						return_tensors='pt'					,						padding='max_length'					,						)




							def        A__  ( self					,						_A							):
												__lowerCAmelCase					=							outputs.logits
												__lowerCAmelCase					=							torch.argmax(logits[:, 2]							).item()
												return self._labels[label_id]






