

from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
	import tensorflow as tf

	from transformers import (
	    TFDebertaVaForMaskedLM,
	    TFDebertaVaForQuestionAnswering,
	    TFDebertaVaForSequenceClassification,
	    TFDebertaVaForTokenClassification,
	    TFDebertaVaModel,
	)



class     _a						:


					'''simple docstring'''






					def __init__( self    ,       A__    ,       A__=13    ,       A__=7    ,       A__=True    ,       A__=True    ,       A__=True    ,       A__=True    ,       A__=99    ,       A__=32    ,       A__=2    ,       A__=4    ,       A__=37    ,       A__="gelu"    ,       A__=0.1    ,       A__=0.1    ,       A__=512    ,       A__=16    ,       A__=2    ,       A__=0.0_2    ,       A__=False    ,       A__=True    ,       A__="None"    ,       A__=3    ,       A__=4    ,       A__=None    ,       ):
						A__							:    Tuple        =		parent
						A__							:    int        =		batch_size
						A__							:    Optional[int]        =		seq_length
						A__							:    Any        =		is_training
						A__							:    Union[str, Any]        =		use_input_mask
						A__							:    List[Any]        =		use_token_type_ids
						A__							:    Union[str, Any]        =		use_labels
						A__							:    Dict        =		vocab_size
						A__							:    Dict        =		hidden_size
						A__							:    int        =		num_hidden_layers
						A__							:    Any        =		num_attention_heads
						A__							:    Optional[Any]        =		intermediate_size
						A__							:    Tuple        =		hidden_act
						A__							:    Optional[Any]        =		hidden_dropout_prob
						A__							:    Tuple        =		attention_probs_dropout_prob
						A__							:    Optional[int]        =		max_position_embeddings
						A__							:    List[Any]        =		type_vocab_size
						A__							:    Dict        =		type_sequence_label_size
						A__							:    List[Any]        =		initializer_range
						A__							:    List[str]        =		num_labels
						A__							:    Dict        =		num_choices
						A__							:    Any        =		relative_attention
						A__							:    Tuple        =		position_biased_input
						A__							:    Tuple        =		pos_att_type
						A__							:    List[str]        =		scope






					def   __A			( self			):
						A__							:    Dict        =		ids_tensor([self.batch_size, self.seq_length]    ,       self.vocab_size			)

						A__							:    Optional[Any]        =		None
						if self.use_input_mask:
							A__							:    str        =		random_attention_mask([self.batch_size, self.seq_length]			)

						A__							:    str        =		None
						if self.use_token_type_ids:
							A__							:    List[str]        =		ids_tensor([self.batch_size, self.seq_length]    ,       self.type_vocab_size			)

						A__							:    List[Any]        =		None
						A__							:    Tuple        =		None
						A__							:    Any        =		None
						if self.use_labels:
							A__							:    Optional[Any]        =		ids_tensor([self.batch_size]    ,       self.type_sequence_label_size			)
							A__							:    Tuple        =		ids_tensor([self.batch_size, self.seq_length]    ,       self.num_labels			)

						A__							:    Optional[int]        =		DebertaVaConfig(
						    vocab_size=self.vocab_size    ,       hidden_size=self.hidden_size    ,       num_hidden_layers=self.num_hidden_layers    ,       num_attention_heads=self.num_attention_heads    ,       intermediate_size=self.intermediate_size    ,       hidden_act=self.hidden_act    ,       hidden_dropout_prob=self.hidden_dropout_prob    ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,       max_position_embeddings=self.max_position_embeddings    ,       type_vocab_size=self.type_vocab_size    ,       relative_attention=self.relative_attention    ,       position_biased_input=self.position_biased_input    ,       initializer_range=self.initializer_range    ,       return_dict=__SCREAMING_SNAKE_CASE    ,       )

						return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






					def   __A			( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__			):
						A__							:    Union[str, Any]        =		TFDebertaVaModel(config=__SCREAMING_SNAKE_CASE			)
						A__							:    str        =		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

						A__							:    Dict        =		[input_ids, input_mask]
						A__							:    Any        =		model(__SCREAMING_SNAKE_CASE			)

						A__							:    Tuple        =		model(__SCREAMING_SNAKE_CASE			)

						self.parent.assertEqual(result.last_hidden_state.shape    ,       (self.batch_size, self.seq_length, self.hidden_size)			)






					def   __A			( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__			):
						A__							:    Dict        =		TFDebertaVaForMaskedLM(config=__SCREAMING_SNAKE_CASE			)
						A__							:    Tuple        =		{
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						A__							:    Tuple        =		model(__SCREAMING_SNAKE_CASE			)
						self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.seq_length, self.vocab_size)			)






					def   __A			( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__			):
						A__							:    int        =		self.num_labels
						A__							:    List[str]        =		TFDebertaVaForSequenceClassification(config=__SCREAMING_SNAKE_CASE			)
						A__							:    Union[str, Any]        =		{
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}

						A__							:    Union[str, Any]        =		model(__SCREAMING_SNAKE_CASE			)
						self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.num_labels)			)






					def   __A			( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__			):
						A__							:    int        =		self.num_labels
						A__							:    Tuple        =		TFDebertaVaForTokenClassification(config=__SCREAMING_SNAKE_CASE			)
						A__							:    int        =		{
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						A__							:    Dict        =		model(__SCREAMING_SNAKE_CASE			)
						self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.seq_length, self.num_labels)			)






					def   __A			( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__			):
						A__							:    List[str]        =		TFDebertaVaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE			)
						A__							:    Any        =		{
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}

						A__							:    str        =		model(__SCREAMING_SNAKE_CASE			)
						self.parent.assertEqual(result.start_logits.shape    ,       (self.batch_size, self.seq_length)			)
						self.parent.assertEqual(result.end_logits.shape    ,       (self.batch_size, self.seq_length)			)






					def   __A			( self			):
						A__							:    int        =		self.prepare_config_and_inputs()
						(
						    (
						    A__	
						)   ,   (
						    A__	
						)   ,   (
						    A__	
						)   ,   (
						    A__	
						)   ,   (
						    A__	
						)   ,   (
						    A__	
						)   ,   (
						    A__	
						)   ,   	
						)							:    List[str]        =		config_and_inputs
						A__							:    str        =		{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
						return config, inputs_dict



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


					'''simple docstring'''






					UpperCAmelCase__:   str  							=       (
					    (
					        TFDebertaVaModel,
					        TFDebertaVaForMaskedLM,
					        TFDebertaVaForQuestionAnswering,
					        TFDebertaVaForSequenceClassification,
					        TFDebertaVaForTokenClassification,
					    )
					    if is_tf_available()
					    else ()
					)
					UpperCAmelCase__:   Optional[int]  							=       (
					    {
					        """feature-extraction""": TFDebertaVaModel,
					        """fill-mask""": TFDebertaVaForMaskedLM,
					        """question-answering""": TFDebertaVaForQuestionAnswering,
					        """text-classification""": TFDebertaVaForSequenceClassification,
					        """token-classification""": TFDebertaVaForTokenClassification,
					        """zero-shot""": TFDebertaVaForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)

					UpperCAmelCase__:   List[str]  							=       False
					UpperCAmelCase__:   Tuple  							=       False






					def   __A			( self			):
						A__							:    List[Any]        =		TFDebertaVaModelTester(self			)
						A__							:    str        =		ConfigTester(self    ,       config_class=__SCREAMING_SNAKE_CASE    ,       hidden_size=37			)






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






					def   __A			( self			):
						A__							:    Optional[Any]        =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE			)






					def   __A			( self			):
						A__							:    Optional[Any]        =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE			)






					def   __A			( self			):
						A__							:    Optional[Any]        =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE			)






					def   __A			( self			):
						A__							:    Union[str, Any]        =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE			)






					def   __A			( self			):
						A__							:    Optional[Any]        =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE			)






					@slow
					def   __A			( self			):
						A__							:    Dict        =		TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge"""			)
						self.assertIsNotNone(__SCREAMING_SNAKE_CASE			)



@require_tf
class     _a						(unittest.TestCase				):


					'''simple docstring'''






					@unittest.skip(reason="""Model not available yet"""			)
					def   __A			( self			):
						pass






					@slow
					def   __A			( self			):
						A__							:    Optional[Any]        =		TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge"""			)
						A__							:    str        =		tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]			)
						A__							:    List[str]        =		tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
						A__							:    Union[str, Any]        =		model(__SCREAMING_SNAKE_CASE    ,       attention_mask=__SCREAMING_SNAKE_CASE			)[0]

						A__							:    List[Any]        =		tf.constant(
						    [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]			)
						tf.debugging.assert_near(output[:, 1:4, 1:4]    ,       __SCREAMING_SNAKE_CASE    ,       atol=1e-4			)











"""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 typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
  from PIL import Image






class      a						(					__snake_case		):
    SCREAMING_SNAKE_CASE						:     Dict						= """Salesforce/blip-image-captioning-base"""
    SCREAMING_SNAKE_CASE						:     int						= (
        """This is a tool that generates a description of an image. It takes an input named `image` which should be the """
        """image to caption, and returns a text that contains the description in English."""
    )
    SCREAMING_SNAKE_CASE						:     Optional[int]						= """image_captioner"""
    SCREAMING_SNAKE_CASE						:     Optional[Any]						= AutoModelForVisionaSeq

    SCREAMING_SNAKE_CASE						:     int						= ["""image"""]
    SCREAMING_SNAKE_CASE						:     Optional[Any]						= ["""text"""]






    def __init__(       self     :							Optional[Any]       ,						*__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						**__SCREAMING_SNAKE_CASE     :							List[Any]					)     ->		Optional[Any]:
      requires_backends(self       ,						['vision']					)
      super().__init__(*__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							"Image"					)     ->		Dict:
      return self.pre_processor(images=__SCREAMING_SNAKE_CASE       ,						return_tensors='pt'					)






    def    UpperCamelCase       (       self     :							List[str]       ,						__SCREAMING_SNAKE_CASE     :							Tuple					)     ->		str:
      return self.model.generate(**__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Union[str, Any]       ,						__SCREAMING_SNAKE_CASE     :							List[str]					)     ->		Optional[Any]:
      return self.pre_processor.batch_decode(__SCREAMING_SNAKE_CASE       ,						skip_special_tokens=__SCREAMING_SNAKE_CASE					)[0].strip()




