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:
       __A         =				False


if is_vision_available():
       from PIL import Image
else:


       class 					UpperCAmelCase		:



           """simple docstring"""

           @staticmethod
           def _snake_case    (     *_UpperCAmelCase					,    **_UpperCAmelCase							):
              pass


@is_pipeline_test
@require_vision
class 					UpperCAmelCase		(unittest.TestCase						):



    """simple docstring"""

    _UpperCAmelCase			:List[str]							=				MODEL_FOR_VISION_2_SEQ_MAPPING
    _UpperCAmelCase			:Optional[int]							=				TF_MODEL_FOR_VISION_2_SEQ_MAPPING






    def _snake_case    (     self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase							):
       lowercase__:     Dict      =   pipeline('''image-to-text'''					,    model=_UpperCAmelCase					,    tokenizer=_UpperCAmelCase					,    image_processor=_UpperCAmelCase							)
       lowercase__:     Dict      =   [
           Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							),
           '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
       ]
       return pipe, examples






    def _snake_case    (     self					,    _UpperCAmelCase					,    _UpperCAmelCase							):
       lowercase__:     List[str]      =   pipe(_UpperCAmelCase							)
       self.assertEqual(
           _UpperCAmelCase					,    [
               [{'''generated_text''': ANY(_UpperCAmelCase							)}],
               [{'''generated_text''': ANY(_UpperCAmelCase							)}],
           ]					,    )






    @require_tf
    def _snake_case    (     self							):
       lowercase__:     int      =   pipeline('''image-to-text'''					,    model='''hf-internal-testing/tiny-random-vit-gpt2'''					,    framework='''tf'''							)
       lowercase__:     List[str]      =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

       lowercase__:     Dict      =   pipe(_UpperCAmelCase							)
       self.assertEqual(
           _UpperCAmelCase					,    [
               {
                   '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
               },
           ]					,    )

       lowercase__:     List[str]      =   pipe([image, image]							)
       self.assertEqual(
           _UpperCAmelCase					,    [
               [
                   {
                       '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                   }
               ],
               [
                   {
                       '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                   }
               ],
           ]					,    )

       lowercase__:     Union[str, Any]      =   pipe(_UpperCAmelCase					,    max_new_tokens=1							)
       self.assertEqual(
           _UpperCAmelCase					,    [{'''generated_text''': '''growth'''}]					,    )






    @require_torch
    def _snake_case    (     self							):
       lowercase__:     Any      =   pipeline('''image-to-text'''					,    model='''hf-internal-testing/tiny-random-vit-gpt2'''							)
       lowercase__:     Optional[int]      =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

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

       lowercase__:     Any      =   pipe([image, image]							)
       self.assertEqual(
           _UpperCAmelCase					,    [
               [
                   {
                       '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                   }
               ],
               [
                   {
                       '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                   }
               ],
           ]					,    )






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

       lowercase__:     Optional[int]      =   pipe(_UpperCAmelCase					,    prompt=_UpperCAmelCase							)
       self.assertTrue(outputs[0]['''generated_text'''].startswith(_UpperCAmelCase							)							)






    @slow
    @require_torch
    def _snake_case    (     self							):
       lowercase__:     Tuple      =   pipeline('''image-to-text'''					,    model='''ydshieh/vit-gpt2-coco-en'''							)
       lowercase__:     Tuple      =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

       lowercase__:     Dict      =   pipe(_UpperCAmelCase							)
       self.assertEqual(_UpperCAmelCase					,    [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]							)

       lowercase__:     Dict      =   pipe([image, image]							)
       self.assertEqual(
           _UpperCAmelCase					,    [
               [{'''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 _snake_case    (     self							):
       lowercase__:     int      =   pipeline('''image-to-text'''					,    model='''Salesforce/blip-image-captioning-base'''							)
       lowercase__:     Optional[Any]      =   '''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
       lowercase__:     str      =   Image.open(requests.get(_UpperCAmelCase					,    stream=_UpperCAmelCase							).raw							)

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






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

       lowercase__:     Tuple      =   pipe(_UpperCAmelCase							)
       self.assertEqual(_UpperCAmelCase					,    [{'''generated_text''': '''a cartoon of a purple character.'''}]							)






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

       lowercase__:     Any      =   '''a photography of'''

       lowercase__:     str      =   pipe(_UpperCAmelCase					,    prompt=_UpperCAmelCase							)
       self.assertEqual(_UpperCAmelCase					,    [{'''generated_text''': '''a photography of a volcano'''}]							)

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






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

       lowercase__:     int      =   '''a photo of a'''

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

       with self.assertRaises(_UpperCAmelCase							):
          lowercase__:     List[Any]      =   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 _snake_case    (     self							):
       lowercase__:     List[Any]      =   pipeline('''image-to-text'''					,    model='''google/pix2struct-ai2d-base'''							)
       lowercase__:     Optional[Any]      =   '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
       lowercase__:     int      =   Image.open(requests.get(_UpperCAmelCase					,    stream=_UpperCAmelCase							).raw							)

       lowercase__:     List[Any]      =   '''What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'''

       lowercase__:     int      =   pipe(_UpperCAmelCase					,    prompt=_UpperCAmelCase							)
       self.assertEqual(_UpperCAmelCase					,    [{'''generated_text''': '''ash cloud'''}]							)

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






    @slow
    @require_tf
    def _snake_case    (     self							):
       lowercase__:     Optional[int]      =   pipeline('''image-to-text'''					,    model='''ydshieh/vit-gpt2-coco-en'''					,    framework='''tf'''							)
       lowercase__:     Union[str, Any]      =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

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

       lowercase__:     Optional[Any]      =   pipe([image, image]							)
       self.assertEqual(
           _UpperCAmelCase					,    [
               [{'''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 '''}],
           ]					,    )

"""simple docstring"""






import shutil
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import SpeechaTextTokenizer, is_speech_available
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


if is_speech_available():
       from transformers import SpeechaTextFeatureExtractor, SpeechaTextProcessor


__A         =				get_tests_dir("fixtures/test_sentencepiece.model")


@require_torch
@require_torchaudio
@require_sentencepiece
class 					UpperCAmelCase		(unittest.TestCase						):



    """simple docstring"""

    def _snake_case    (     self							):
       lowercase__:     Dict      =   tempfile.mkdtemp()

       lowercase__:     str      =   ['''<s>''', '''<pad>''', '''</s>''', '''<unk>''', '''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']
       lowercase__:     Optional[Any]      =   dict(zip(_UpperCAmelCase					,    range(len(_UpperCAmelCase							)							)							)							)
       lowercase__:     List[Any]      =   Path(self.tmpdirname							)
       save_json(_UpperCAmelCase					,    save_dir / VOCAB_FILES_NAMES['''vocab_file''']							)
       if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists():
          copyfile(_UpperCAmelCase					,    save_dir / VOCAB_FILES_NAMES['''spm_file''']							)

       lowercase__:     Tuple      =   SpeechaTextTokenizer.from_pretrained(self.tmpdirname							)
       tokenizer.save_pretrained(self.tmpdirname							)

       lowercase__:     Optional[int]      =   {
           '''feature_size''': 24,
           '''num_mel_bins''': 24,
           '''padding_value''': 0.0,
           '''sampling_rate''': 16000,
           '''return_attention_mask''': False,
           '''do_normalize''': True,
       }
       save_json(_UpperCAmelCase					,    save_dir / FEATURE_EXTRACTOR_NAME							)






    def _snake_case    (     self					,    **_UpperCAmelCase							):
       return SpeechaTextTokenizer.from_pretrained(self.tmpdirname					,    **_UpperCAmelCase							)






    def _snake_case    (     self					,    **_UpperCAmelCase							):
       return SpeechaTextFeatureExtractor.from_pretrained(self.tmpdirname					,    **_UpperCAmelCase							)






    def _snake_case    (     self							):
       shutil.rmtree(self.tmpdirname							)






    def _snake_case    (     self							):
       lowercase__:     Tuple      =   self.get_tokenizer()
       lowercase__:     Optional[int]      =   self.get_feature_extractor()

       lowercase__:     int      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       processor.save_pretrained(self.tmpdirname							)
       lowercase__:     Union[str, Any]      =   SpeechaTextProcessor.from_pretrained(self.tmpdirname							)

       self.assertEqual(processor.tokenizer.get_vocab()					,    tokenizer.get_vocab()							)
       self.assertIsInstance(processor.tokenizer					,    _UpperCAmelCase							)

       self.assertEqual(processor.feature_extractor.to_json_string()					,    feature_extractor.to_json_string()							)
       self.assertIsInstance(processor.feature_extractor					,    _UpperCAmelCase							)






    def _snake_case    (     self							):
       lowercase__:     Tuple      =   SpeechaTextProcessor(
           tokenizer=self.get_tokenizer()					,    feature_extractor=self.get_feature_extractor()							)
       processor.save_pretrained(self.tmpdirname							)

       lowercase__:     Any      =   self.get_tokenizer(bos_token='''(BOS)'''					,    eos_token='''(EOS)'''							)
       lowercase__:     Tuple      =   self.get_feature_extractor(do_normalize=_UpperCAmelCase					,    padding_value=1.0							)

       lowercase__:     List[str]      =   SpeechaTextProcessor.from_pretrained(
           self.tmpdirname					,    bos_token='''(BOS)'''					,    eos_token='''(EOS)'''					,    do_normalize=_UpperCAmelCase					,    padding_value=1.0							)

       self.assertEqual(processor.tokenizer.get_vocab()					,    tokenizer_add_kwargs.get_vocab()							)
       self.assertIsInstance(processor.tokenizer					,    _UpperCAmelCase							)

       self.assertEqual(processor.feature_extractor.to_json_string()					,    feature_extractor_add_kwargs.to_json_string()							)
       self.assertIsInstance(processor.feature_extractor					,    _UpperCAmelCase							)






    def _snake_case    (     self							):
       lowercase__:     Tuple      =   self.get_feature_extractor()
       lowercase__:     List[Any]      =   self.get_tokenizer()

       lowercase__:     Any      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       lowercase__:     int      =   floats_list((3, 1000)							)

       lowercase__:     Optional[Any]      =   feature_extractor(_UpperCAmelCase					,    return_tensors='''np'''							)
       lowercase__:     Dict      =   processor(_UpperCAmelCase					,    return_tensors='''np'''							)

       for key in input_feat_extract.keys():
          self.assertAlmostEqual(input_feat_extract[key].sum()					,    input_processor[key].sum()					,    delta=1e-2							)






    def _snake_case    (     self							):
       lowercase__:     Tuple      =   self.get_feature_extractor()
       lowercase__:     Dict      =   self.get_tokenizer()

       lowercase__:     Union[str, Any]      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       lowercase__:     Dict      =   '''This is a test string'''

       lowercase__:     int      =   processor(text=_UpperCAmelCase							)

       lowercase__:     Union[str, Any]      =   tokenizer(_UpperCAmelCase							)

       for key in encoded_tok.keys():
          self.assertListEqual(encoded_tok[key]					,    encoded_processor[key]							)






    def _snake_case    (     self							):
       lowercase__:     List[Any]      =   self.get_feature_extractor()
       lowercase__:     Union[str, Any]      =   self.get_tokenizer()

       lowercase__:     Tuple      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       lowercase__:     List[str]      =   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       lowercase__:     List[Any]      =   processor.batch_decode(_UpperCAmelCase							)
       lowercase__:     Dict      =   tokenizer.batch_decode(_UpperCAmelCase							)

       self.assertListEqual(_UpperCAmelCase					,    _UpperCAmelCase							)






    def _snake_case    (     self							):
       lowercase__:     Optional[Any]      =   self.get_feature_extractor()
       lowercase__:     List[str]      =   self.get_tokenizer()

       lowercase__:     Union[str, Any]      =   SpeechaTextProcessor(tokenizer=_UpperCAmelCase					,    feature_extractor=_UpperCAmelCase							)

       self.assertListEqual(
           processor.model_input_names					,    feature_extractor.model_input_names					,    msg='''`processor` and `feature_extractor` model input names do not match'''					,    )
