'''simple docstring'''




import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class 			lowerCamelCase_			(snake_case__					):
      '''simple docstring'''

      __UpperCamelCase:						List[Any]      			=		"AutoFeatureExtractor"
      __UpperCamelCase:						Optional[Any]      			=		"Speech2Text2Tokenizer"




      def __init__( self		:       Optional[Any]		,      A		:       Optional[Any]		,      A		:       Dict ):
             super().__init__(A		,      A )
             _UpperCAmelCase  :						int							   =					self.feature_extractor
             _UpperCAmelCase  :						Optional[int]							   =					False




      def __call__( self		:       Optional[int]		,      *A		:       Any		,      **A		:       List[str] ):
             # For backward compatibility
             if self._in_target_context_manager:
                    return self.current_processor(*A		,      **A )

             if "raw_speech" in kwargs:
                    warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead." )
                    _UpperCAmelCase  :						str							   =					kwargs.pop("raw_speech" )
             else:
                    _UpperCAmelCase  :						List[Any]							   =					kwargs.pop("audio"		,      A )
             _UpperCAmelCase  :						Union[str, Any]							   =					kwargs.pop("sampling_rate"		,      A )
             _UpperCAmelCase  :						str							   =					kwargs.pop("text"		,      A )
             if len(A ) > 0:
                    _UpperCAmelCase  :						str							   =					args[0]
                    _UpperCAmelCase  :						int							   =					args[1:]

             if audio is None and text is None:
                    raise ValueError("You need to specify either an `audio` or `text` input to process." )

             if audio is not None:
                    _UpperCAmelCase  :						Tuple							   =					self.feature_extractor(A		,      *A		,      sampling_rate=A		,      **A )
             if text is not None:
                    _UpperCAmelCase  :						Optional[int]							   =					self.tokenizer(A		,      **A )

             if text is None:
                    return inputs
             elif audio is None:
                    return encodings
             else:
                    _UpperCAmelCase  :						Optional[Any]							   =					encodings["input_ids"]
                    return inputs




      def   _A			( self		:       List[str]		,      *A		:       Union[str, Any]		,      **A		:       int ):
             return self.tokenizer.batch_decode(*A		,      **A )




      def   _A			( self		:       Optional[int]		,      *A		:       Tuple		,      **A		:       Optional[int] ):
             return self.tokenizer.decode(*A		,      **A )





      @contextmanager
      def   _A			( self		:       List[Any] ):
             warnings.warn(
                 "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
                 "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
                 "your audio inputs, or in a separate call." )
             _UpperCAmelCase  :						int							   =					True
             _UpperCAmelCase  :						Dict							   =					self.tokenizer
             yield
             _UpperCAmelCase  :						int							   =					self.feature_extractor
             _UpperCAmelCase  :						int							   =					False


'''simple docstring'''




import unittest

import requests

from transformers import MODEL_FOR_VISION_2_SEQ_MAPPING, TF_MODEL_FOR_VISION_2_SEQ_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_torch_available():
  from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
  __SCREAMING_SNAKE_CASE :  Any  =						False


if is_vision_available():
  from PIL import Image
else:

  class 			lowerCamelCase_			:
        '''simple docstring'''

        @staticmethod
        def   _A			( *A		:       str		,      **A		:       Optional[Any] ):
               pass

@is_pipeline_test
@require_vision
class 			lowerCamelCase_			(unittest.TestCase					):
      '''simple docstring'''

      __UpperCamelCase:						Union[str, Any]      			=		MODEL_FOR_VISION_2_SEQ_MAPPING
      __UpperCamelCase:						int      			=		TF_MODEL_FOR_VISION_2_SEQ_MAPPING




      def   _A			( self		:       Any		,      A		:       Any		,      A		:       str		,      A		:       Optional[int] ):
             _UpperCAmelCase  :						Dict							   =					pipeline("image-to-text"		,      model=A		,      tokenizer=A		,      image_processor=A )
             _UpperCAmelCase  :						Union[str, Any]							   =					[
                 Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ),
                 "./tests/fixtures/tests_samples/COCO/000000039769.png",
             ]
             return pipe, examples




      def   _A			( self		:       Tuple		,      A		:       Union[str, Any]		,      A		:       Optional[Any] ):
             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(
                 A		,      [
                     [{"generated_text": ANY(A )}],
                     [{"generated_text": ANY(A )}],
                 ]		,      )




      @require_tf
      def   _A			( self		:       Any ):
             _UpperCAmelCase  :						Any							   =					pipeline("image-to-text"		,      model="hf-internal-testing/tiny-random-vit-gpt2"		,      framework="tf" )
             _UpperCAmelCase  :						int							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(
                 A		,      [
                     {
                         "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                     },
                 ]		,      )

             _UpperCAmelCase  :						Tuple							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]		,      )

             _UpperCAmelCase  :						Dict							   =					pipe(A		,      max_new_tokens=1 )
             self.assertEqual(
                 A		,      [{"generated_text": "growth"}]		,      )




      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						List[str]							   =					pipeline("image-to-text"		,      model="hf-internal-testing/tiny-random-vit-gpt2" )
             _UpperCAmelCase  :						Optional[int]							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

             _UpperCAmelCase  :						Optional[Any]							   =					pipe(A )
             self.assertEqual(
                 A		,      [
                     {
                         "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                     },
                 ]		,      )

             _UpperCAmelCase  :						str							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]		,      )




      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						Any							   =					pipeline("image-to-text"		,      model="hf-internal-testing/tiny-random-BlipForConditionalGeneration" )
             _UpperCAmelCase  :						Any							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"
             _UpperCAmelCase  :						int							   =					"a photo of"

             _UpperCAmelCase  :						str							   =					pipe(A		,      prompt=A )
             self.assertTrue(outputs[0]["generated_text"].startswith(A ) )




      @slow
      @require_torch
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						Dict							   =					pipeline("image-to-text"		,      model="ydshieh/vit-gpt2-coco-en" )
             _UpperCAmelCase  :						int							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(A		,      [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}] )

             _UpperCAmelCase  :						int							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}],
                     [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}],
                 ]		,      )




      @slow
      @require_torch
      def   _A			( self		:       Optional[Any] ):
             _UpperCAmelCase  :						Tuple							   =					pipeline("image-to-text"		,      model="Salesforce/blip-image-captioning-base" )
             _UpperCAmelCase  :						int							   =					"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             _UpperCAmelCase  :						Optional[int]							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						Any							   =					pipe(A )
             self.assertEqual(A		,      [{"generated_text": "a pink pokemon pokemon with a blue shirt and a blue shirt"}] )




      @slow
      @require_torch
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						List[str]							   =					pipeline("image-to-text"		,      model="microsoft/git-base-coco" )
             _UpperCAmelCase  :						Tuple							   =					"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             _UpperCAmelCase  :						Dict							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(A		,      [{"generated_text": "a cartoon of a purple character."}] )




      @slow
      @require_torch
      def   _A			( self		:       int ):
             _UpperCAmelCase  :						Any							   =					pipeline("image-to-text"		,      model="Salesforce/blip-image-captioning-base" )
             _UpperCAmelCase  :						List[str]							   =					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             _UpperCAmelCase  :						int							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						Dict							   =					"a photography of"

             _UpperCAmelCase  :						Optional[int]							   =					pipe(A		,      prompt=A )
             self.assertEqual(A		,      [{"generated_text": "a photography of a volcano"}] )

             with self.assertRaises(A ):
                    _UpperCAmelCase  :						List[Any]							   =					pipe([image, image]		,      prompt=[prompt, prompt] )




      @slow
      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						Optional[int]							   =					pipeline("image-to-text"		,      model="microsoft/git-base-coco" )
             _UpperCAmelCase  :						Optional[Any]							   =					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             _UpperCAmelCase  :						List[str]							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						List[Any]							   =					"a photo of a"

             _UpperCAmelCase  :						Tuple							   =					pipe(A		,      prompt=A )
             self.assertEqual(A		,      [{"generated_text": "a photo of a tent with a tent and a tent in the background."}] )

             with self.assertRaises(A ):
                    _UpperCAmelCase  :						List[str]							   =					pipe([image, image]		,      prompt=[prompt, prompt] )




      @unittest.skipIf(
          not is_torch_greater_or_equal_than_1_11		,      reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." )
      @slow
      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						str							   =					pipeline("image-to-text"		,      model="google/pix2struct-ai2d-base" )
             _UpperCAmelCase  :						List[Any]							   =					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             _UpperCAmelCase  :						List[Any]							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						Optional[int]							   =					"What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"

             _UpperCAmelCase  :						str							   =					pipe(A		,      prompt=A )
             self.assertEqual(A		,      [{"generated_text": "ash cloud"}] )

             with self.assertRaises(A ):
                    _UpperCAmelCase  :						List[str]							   =					pipe([image, image]		,      prompt=[prompt, prompt] )





      @slow
      @require_tf
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						str							   =					pipeline("image-to-text"		,      model="ydshieh/vit-gpt2-coco-en"		,      framework="tf" )
             _UpperCAmelCase  :						str							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

             _UpperCAmelCase  :						Any							   =					pipe(A )
             self.assertEqual(A		,      [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}] )

             _UpperCAmelCase  :						Optional[Any]							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}],
                     [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}],
                 ]		,      )

