





'''simple docstring'''







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 					__a      :
							__a					:   Optional[int]									=       XGLMConfig
							__a					:   Dict									=       {}
							__a					:   Dict									=       "gelu"

							def __init__(							self							:     str       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     Dict=14       ,     __magic_name__							:     str=7       ,     __magic_name__							:     Tuple=True       ,     __magic_name__							:     int=True       ,     __magic_name__							:     int=True       ,     __magic_name__							:     Optional[int]=99       ,     __magic_name__							:     Dict=32       ,     __magic_name__							:     List[Any]=2       ,     __magic_name__							:     Dict=4       ,     __magic_name__							:     Optional[int]=37       ,     __magic_name__							:     Optional[int]="gelu"       ,     __magic_name__							:     List[str]=0.1       ,     __magic_name__							:     Tuple=0.1       ,     __magic_name__							:     Union[str, Any]=5_12       ,     __magic_name__							:     Dict=0.0_2       ,     )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  parent
													UpperCAmelCase_			:      Dict     						=  batch_size
													UpperCAmelCase_			:      Any     						=  seq_length
													UpperCAmelCase_			:      List[str]     						=  is_training
													UpperCAmelCase_			:      Optional[int]     						=  use_input_mask
													UpperCAmelCase_			:      Optional[int]     						=  use_labels
													UpperCAmelCase_			:      List[Any]     						=  vocab_size
													UpperCAmelCase_			:      Dict     						=  d_model
													UpperCAmelCase_			:      Union[str, Any]     						=  num_hidden_layers
													UpperCAmelCase_			:      Tuple     						=  num_attention_heads
													UpperCAmelCase_			:      Tuple     						=  ffn_dim
													UpperCAmelCase_			:      Optional[int]     						=  activation_function
													UpperCAmelCase_			:      str     						=  activation_dropout
													UpperCAmelCase_			:      Union[str, Any]     						=  attention_dropout
													UpperCAmelCase_			:      Union[str, Any]     						=  max_position_embeddings
													UpperCAmelCase_			:      str     						=  initializer_range
													UpperCAmelCase_			:      Optional[int]     						=  None
													UpperCAmelCase_			:      Union[str, Any]     						=  0
													UpperCAmelCase_			:      int     						=  2
													UpperCAmelCase_			:      List[Any]     						=  1

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







													"""simple docstring"""
													return XGLMConfig.from_pretrained('''facebook/xglm-564M'''   )

							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  tf.clip_by_value(
													    ids_tensor([self.batch_size, self.seq_length]       ,     self.vocab_size   )       ,     clip_value_min=0       ,     clip_value_max=3   )

													UpperCAmelCase_			:      Union[str, Any]     						=  None
													if self.use_input_mask:
																			UpperCAmelCase_			:      List[str]     						=  random_attention_mask([self.batch_size, self.seq_length]   )

													UpperCAmelCase_			:      int     						=  self.get_config()

													UpperCAmelCase_			:      str     						=  floats_tensor([self.num_hidden_layers, self.num_attention_heads]       ,     2   )

													return (
													    config,
													    input_ids,
													    input_mask,
													    head_mask,
													)

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







													"""simple docstring"""
													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=__magic_name__       ,     bos_token_id=self.bos_token_id       ,     eos_token_id=self.eos_token_id       ,     pad_token_id=self.pad_token_id       ,     return_dict=__magic_name__       ,     )



							def     UpperCAmelCase__	(							self							:     List[str]   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  self.prepare_config_and_inputs()

													(
													    (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       
													)			:      Dict     						=  config_and_inputs

													UpperCAmelCase_			:      str     						=  {
													    '''input_ids''': input_ids,
													    '''head_mask''': head_mask,
													}

													return config, inputs_dict





@require_tf
class 					__a      (lowerCamelCase       ,		lowerCamelCase       ,		unittest.TestCase     ):
							__a					:   Dict									=       (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
							__a					:   Optional[int]									=       (TFXGLMForCausalLM,) if is_tf_available() else ()
							__a					:   Any									=       (
							    {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
							)
							__a					:   Any									=       False
							__a					:   Dict									=       False
							__a					:   Union[str, Any]									=       False

							def     UpperCAmelCase__	(							self							:     int   )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  TFXGLMModelTester(self   )
													UpperCAmelCase_			:      Optional[Any]     						=  ConfigTester(self       ,     config_class=__magic_name__       ,     n_embd=37   )

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       Optional[Any]:







													"""simple docstring"""
													self.config_tester.run_common_tests()

							@slow
							def     UpperCAmelCase__	(							self							:     Dict   )					->       Optional[Any]:







													"""simple docstring"""
													for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase_			:      Optional[int]     						=  TFXGLMModel.from_pretrained(__magic_name__   )
																			self.assertIsNotNone(__magic_name__   )



							@unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.'''   )
							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       Optional[int]:







													"""simple docstring"""
													super().test_resize_token_embeddings()





@require_tf
class 					__a      (unittest.TestCase     ):

							@slow
							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     Optional[Any]=True   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''   )
													UpperCAmelCase_			:      Optional[Any]     						=  tf.convert_to_tensor([[2, 2_68, 98_65]]       ,     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
													UpperCAmelCase_			:      Any     						=  [2, 2_68, 98_65, 67, 11, 19_88, 5_72_52, 98_65, 5, 9_84, 67, 19_88, 21_38_38, 16_58, 53, 7_04_46, 33, 66_57, 2_78, 15_81]
													# fmt: on
													UpperCAmelCase_			:      Union[str, Any]     						=  model.generate(__magic_name__       ,     do_sample=__magic_name__       ,     num_beams=1   )
													if verify_outputs:
																			self.assertListEqual(output_ids[0].numpy().tolist()       ,     __magic_name__   )

							@slow
							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''   )
													UpperCAmelCase_			:      Optional[int]     						=  TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''   )

													tf.random.set_seed(0   )
													UpperCAmelCase_			:      Optional[int]     						=  tokenizer('''Today is a nice day and'''       ,     return_tensors='''tf'''   )
													UpperCAmelCase_			:      str     						=  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'''   ):
																			UpperCAmelCase_			:      List[str]     						=  model.generate(__magic_name__       ,     do_sample=__magic_name__       ,     seed=[7, 0]   )
													UpperCAmelCase_			:      List[str]     						=  tokenizer.decode(output_ids[0]       ,     skip_special_tokens=__magic_name__   )

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



							@slow
							def     UpperCAmelCase__	(							self							:     str   )					->       str:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''   )
													UpperCAmelCase_			:      Union[str, Any]     						=  XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''   )

													UpperCAmelCase_			:      Any     						=  '''left'''

													# use different length sentences to test batching
													UpperCAmelCase_			:      int     						=  [
													    '''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''',
													]

													UpperCAmelCase_			:      Tuple     						=  tokenizer(__magic_name__       ,     return_tensors='''tf'''       ,     padding=__magic_name__   )
													UpperCAmelCase_			:      Union[str, Any]     						=  inputs['''input_ids''']

													UpperCAmelCase_			:      List[str]     						=  model.generate(input_ids=__magic_name__       ,     attention_mask=inputs['''attention_mask''']       ,     max_new_tokens=12   )

													UpperCAmelCase_			:      Union[str, Any]     						=  tokenizer(sentences[0]       ,     return_tensors='''tf'''   ).input_ids
													UpperCAmelCase_			:      Any     						=  model.generate(input_ids=__magic_name__       ,     max_new_tokens=12   )

													UpperCAmelCase_			:      Optional[int]     						=  tokenizer(sentences[1]       ,     return_tensors='''tf'''   ).input_ids
													UpperCAmelCase_			:      Dict     						=  model.generate(input_ids=__magic_name__       ,     max_new_tokens=12   )

													UpperCAmelCase_			:      Any     						=  tokenizer.batch_decode(__magic_name__       ,     skip_special_tokens=__magic_name__   )
													UpperCAmelCase_			:      List[str]     						=  tokenizer.decode(output_non_padded[0]       ,     skip_special_tokens=__magic_name__   )
													UpperCAmelCase_			:      Tuple     						=  tokenizer.decode(output_padded[0]       ,     skip_special_tokens=__magic_name__   )

													UpperCAmelCase_			:      Tuple     						=  [
													    '''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(__magic_name__       ,     __magic_name__   )
													self.assertListEqual(__magic_name__       ,     [non_padded_sentence, padded_sentence]   )












'''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.
from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor
from .base import PipelineTool


class 					__a      (lowerCamelCase     ):
							__a					:   List[Any]									=       "openai/whisper-base"
							__a					:   Optional[Any]									=       (
							    "This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the "
							    "transcribed text."
							)
							__a					:   Any									=       "transcriber"
							__a					:   str									=       WhisperProcessor
							__a					:   List[Any]									=       WhisperForConditionalGeneration

							__a					:   int									=       ["audio"]
							__a					:   Optional[Any]									=       ["text"]

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     List[str]   )					->       Optional[int]:







													"""simple docstring"""
													return self.pre_processor(__magic_name__       ,     return_tensors='''pt'''   ).input_features

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     Dict   )					->       Tuple:







													"""simple docstring"""
													return self.model.generate(inputs=__magic_name__   )



							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     Dict   )					->       str:







													"""simple docstring"""
													return self.pre_processor.batch_decode(__magic_name__       ,     skip_special_tokens=__magic_name__   )[0]





