# 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 			lowerCamelCase_  (     UpperCAmelCase_				):






    '''simple docstring'''






    a__			:		int      			=   """Salesforce/blip-image-captioning-base"""
    a__			:		str      			=   (
        """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."""
    )
    a__			:		Tuple      			=   """image_captioner"""
    a__			:		Dict      			=   AutoModelForVisionaSeq

    a__			:		int      			=   ["""image"""]
    a__			:		List[str]      			=   ["""text"""]







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







    def     UpperCamelCase__     (			self				,				__lowercase) ->			Dict:
        return self.pre_processor(images=__lowercase				,				return_tensors='''pt''')







    def     UpperCamelCase__     (			self				,				__lowercase) ->			List[str]:
        return self.model.generate(**__lowercase)







    def     UpperCamelCase__     (			self				,				__lowercase) ->			Optional[int]:
        return self.pre_processor.batch_decode(__lowercase				,				skip_special_tokens=__lowercase)[0].strip()
import unittest

from transformers import is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow


if is_flax_available():
    import optax
    from flax.training.common_utils import onehot

    from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration
    from transformers.models.ta.modeling_flax_ta import shift_tokens_right


@require_torch
@require_sentencepiece
@require_tokenizers
@require_flax
class 			lowerCamelCase_  (     unittest.TestCase				):






    '''simple docstring'''






    @slow
    def     UpperCamelCase__     (			self) ->			List[str]:
        __UpperCamelCase  :List[str]    						=	FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small''')
        __UpperCamelCase  :Dict    						=	AutoTokenizer.from_pretrained('''google/mt5-small''')

        __UpperCamelCase  :Optional[Any]    						=	tokenizer('''Hello there'''				,				return_tensors='''np''').input_ids
        __UpperCamelCase  :List[str]    						=	tokenizer('''Hi I am'''				,				return_tensors='''np''').input_ids

        __UpperCamelCase  :Optional[int]    						=	shift_tokens_right(__lowercase				,				model.config.pad_token_id				,				model.config.decoder_start_token_id)

        __UpperCamelCase  :Tuple    						=	model(__lowercase				,				decoder_input_ids=__lowercase).logits
        __UpperCamelCase  :Any    						=	optax.softmax_cross_entropy(__lowercase				,				onehot(__lowercase				,				logits.shape[-1])).mean()

        __UpperCamelCase  :str    						=	-(labels.shape[-1] * loss.item())

        __UpperCamelCase  :Optional[Any]    						=	-84.91_27
        self.assertTrue(abs(mtf_score - EXPECTED_SCORE) < 1E-4)





