




'''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:
		a__         =     False


if is_vision_available():
		from PIL import Image
else:





		class __magic_name__:






								@staticmethod
								def       __lowerCAmelCase(   *__UpperCamelCase						:						List[Any]					,   **__UpperCamelCase						:						List[Any]       ):



											'''simple docstring'''






											pass





@is_pipeline_test
@require_vision
class __magic_name__(						unittest.TestCase      ):
						UpperCAmelCase_		:					int    =  MODEL_FOR_VISION_2_SEQ_MAPPING
						UpperCAmelCase_		:					Tuple    =  TF_MODEL_FOR_VISION_2_SEQ_MAPPING






						def       __lowerCAmelCase(   self						:						Union[str, Any]					,   __UpperCamelCase						:						Tuple					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						List[Any]       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model=__UpperCamelCase					,   tokenizer=__UpperCamelCase					,   image_processor=__UpperCamelCase       )
									snake_case__          =						[
									    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       ),
									    """./tests/fixtures/tests_samples/COCO/000000039769.png""",
									]
									return pipe, examples






						def       __lowerCAmelCase(   self						:						List[Any]					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						int       ):



									'''simple docstring'''






									snake_case__          =						pipe(__UpperCamelCase       )
									self.assertEqual(
									    __UpperCamelCase					,   [
									        [{"""generated_text""": ANY(__UpperCamelCase       )}],
									        [{"""generated_text""": ANY(__UpperCamelCase       )}],
									    ]					,   )






						@require_tf
						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""hf-internal-testing/tiny-random-vit-gpt2"""					,   framework="""tf"""       )
									snake_case__          =						"""./tests/fixtures/tests_samples/COCO/000000039769.png"""

									snake_case__          =						pipe(__UpperCamelCase       )
									self.assertEqual(
									    __UpperCamelCase					,   [
									        {
									            """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									        },
									    ]					,   )

									snake_case__          =						pipe([image, image]       )
									self.assertEqual(
									    __UpperCamelCase					,   [
									        [
									            {
									                """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									            }
									        ],
									        [
									            {
									                """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									            }
									        ],
									    ]					,   )

									snake_case__          =						pipe(__UpperCamelCase					,   max_new_tokens=1       )
									self.assertEqual(
									    __UpperCamelCase					,   [{"""generated_text""": """growth"""}]					,   )






						@require_torch
						def       __lowerCAmelCase(   self						:						str       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""hf-internal-testing/tiny-random-vit-gpt2"""       )
									snake_case__          =						"""./tests/fixtures/tests_samples/COCO/000000039769.png"""

									snake_case__          =						pipe(__UpperCamelCase       )
									self.assertEqual(
									    __UpperCamelCase					,   [
									        {
									            """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									        },
									    ]					,   )

									snake_case__          =						pipe([image, image]       )
									self.assertEqual(
									    __UpperCamelCase					,   [
									        [
									            {
									                """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									            }
									        ],
									        [
									            {
									                """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									            }
									        ],
									    ]					,   )






						@require_torch
						def       __lowerCAmelCase(   self						:						Any       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""hf-internal-testing/tiny-random-BlipForConditionalGeneration"""       )
									snake_case__          =						"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
									snake_case__          =						"""a photo of"""

									snake_case__          =						pipe(__UpperCamelCase					,   prompt=__UpperCamelCase       )
									self.assertTrue(outputs[0]["""generated_text"""].startswith(__UpperCamelCase       )       )






						@slow
						@require_torch
						def       __lowerCAmelCase(   self						:						List[str]       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""ydshieh/vit-gpt2-coco-en"""       )
									snake_case__          =						"""./tests/fixtures/tests_samples/COCO/000000039769.png"""

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

									snake_case__          =						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       __lowerCAmelCase(   self						:						List[str]       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""Salesforce/blip-image-captioning-base"""       )
									snake_case__          =						"""https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"""
									snake_case__          =						Image.open(requests.get(__UpperCamelCase					,   stream=__UpperCamelCase       ).raw       )

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






						@slow
						@require_torch
						def       __lowerCAmelCase(   self						:						int       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""microsoft/git-base-coco"""       )
									snake_case__          =						"""https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"""
									snake_case__          =						Image.open(requests.get(__UpperCamelCase					,   stream=__UpperCamelCase       ).raw       )

									snake_case__          =						pipe(__UpperCamelCase       )
									self.assertEqual(__UpperCamelCase					,   [{"""generated_text""": """a cartoon of a purple character."""}]       )






						@slow
						@require_torch
						def       __lowerCAmelCase(   self						:						Optional[Any]       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""Salesforce/blip-image-captioning-base"""       )
									snake_case__          =						"""https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
									snake_case__          =						Image.open(requests.get(__UpperCamelCase					,   stream=__UpperCamelCase       ).raw       )

									snake_case__          =						"""a photography of"""

									snake_case__          =						pipe(__UpperCamelCase					,   prompt=__UpperCamelCase       )
									self.assertEqual(__UpperCamelCase					,   [{"""generated_text""": """a photography of a volcano"""}]       )

									with self.assertRaises(__UpperCamelCase       ):
												snake_case__          =						pipe([image, image]					,   prompt=[prompt, prompt]       )






						@slow
						@require_torch
						def       __lowerCAmelCase(   self						:						Any       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""microsoft/git-base-coco"""       )
									snake_case__          =						"""https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
									snake_case__          =						Image.open(requests.get(__UpperCamelCase					,   stream=__UpperCamelCase       ).raw       )

									snake_case__          =						"""a photo of a"""

									snake_case__          =						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       ):
												snake_case__          =						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       __lowerCAmelCase(   self						:						str       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""google/pix2struct-ai2d-base"""       )
									snake_case__          =						"""https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
									snake_case__          =						Image.open(requests.get(__UpperCamelCase					,   stream=__UpperCamelCase       ).raw       )

									snake_case__          =						"""What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"""

									snake_case__          =						pipe(__UpperCamelCase					,   prompt=__UpperCamelCase       )
									self.assertEqual(__UpperCamelCase					,   [{"""generated_text""": """ash cloud"""}]       )

									with self.assertRaises(__UpperCamelCase       ):
												snake_case__          =						pipe([image, image]					,   prompt=[prompt, prompt]       )







						@slow
						@require_tf
						def       __lowerCAmelCase(   self						:						int       ):



									'''simple docstring'''






									snake_case__          =						pipeline("""image-to-text"""					,   model="""ydshieh/vit-gpt2-coco-en"""					,   framework="""tf"""       )
									snake_case__          =						"""./tests/fixtures/tests_samples/COCO/000000039769.png"""

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

									snake_case__          =						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 """}],
									    ]					,   )




import json
import os
from collections.abc import Mapping
from typing import Dict, List, Optional, Union

import numpy as np
import torch

from .imports import is_safetensors_available







def  _snake_case			(							__snake_case       ,	__snake_case       ,	__snake_case       ,	__snake_case=None     )  ->  Any:






      '''simple docstring'''







      UpperCAmelCase_	:						Dict					   =		None
      # Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
      if str(weight.dtype     ) == "torch.bfloat16":
            # Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
            UpperCAmelCase_	:						Optional[Any]					   =		weight.view(torch.intaa     )
            UpperCAmelCase_	:						Dict					   =		"bfloat16"
      UpperCAmelCase_	:						Optional[int]					   =		weight.cpu().numpy()
      UpperCAmelCase_	:						Tuple					   =		os.path.join(__snake_case       ,	F"""{weight_name}.dat"""     )
      if index is not None:
            if dtype is None:
                  UpperCAmelCase_	:						int					   =		str(array.dtype     )
            UpperCAmelCase_	:						Any					   =		{"dtype": dtype, "shape": list(array.shape     )}
      if array.ndim == 0:
            UpperCAmelCase_	:						List[str]					   =		array[None]
      UpperCAmelCase_	:						List[Any]					   =		np.memmap(__snake_case       ,	dtype=array.dtype       ,	mode="w+"       ,	shape=array.shape     )
      UpperCAmelCase_	:						List[str]					   =		array[:]
      file_array.flush()
      return index







def  _snake_case			(							__snake_case       ,	__snake_case     )  ->  Any:






      '''simple docstring'''







      UpperCAmelCase_	:						Union[str, Any]					   =		tuple(weight_info["shape"]     )
      if shape == ():
            # NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
            UpperCAmelCase_	:						Dict					   =		(1,)

      UpperCAmelCase_	:						Tuple					   =		weight_info["dtype"]
      if dtype == "bfloat16":
            # NumPy does not support bfloat16 so this was saved as a int16
            UpperCAmelCase_	:						Tuple					   =		"int16"

      UpperCAmelCase_	:						Union[str, Any]					   =		np.memmap(__snake_case       ,	dtype=__snake_case       ,	shape=__snake_case       ,	mode="r"     )

      if len(weight_info["shape"]     ) == 0:
            UpperCAmelCase_	:						Optional[Any]					   =		weight[0]
      UpperCAmelCase_	:						List[str]					   =		torch.tensor(__snake_case     )
      if weight_info["dtype"] == "bfloat16":
            UpperCAmelCase_	:						Union[str, Any]					   =		weight.view(torch.bfloataa     )

      return weight







def  _snake_case			(							__snake_case       ,	__snake_case     )  ->  List[Any]:






      '''simple docstring'''







      if index is None or len(__snake_case     ) == 0:
            # Nothing to save
            return

      UpperCAmelCase_	:						str					   =		os.path.join(__snake_case       ,	"index.json"     )
      if os.path.isfile(__snake_case     ):
            with open(__snake_case       ,	"r"       ,	encoding="utf-8"     ) as f:
                  UpperCAmelCase_	:						str					   =		json.load(__snake_case     )
      else:
            UpperCAmelCase_	:						Dict					   =		{}
      current_index.update(__snake_case     )

      with open(__snake_case       ,	"w"       ,	encoding="utf-8"     ) as f:
            json.dump(__snake_case       ,	__snake_case       ,	indent=2     )







def  _snake_case			(							__snake_case       ,	__snake_case     )  ->  Union[str, Any]:






      '''simple docstring'''







      os.makedirs(__snake_case       ,	exist_ok=__snake_case     )
      UpperCAmelCase_	:						Any					   =		{}
      for name, parameter in state_dict.items():
            UpperCAmelCase_	:						Tuple					   =		offload_weight(__snake_case       ,	__snake_case       ,	__snake_case       ,	index=__snake_case     )

      # Update index
      save_offload_index(__snake_case       ,	__snake_case     )


class 	snake_case_			(lowercase__	):




       """simple docstring"""






       def __init__(  self  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[Any]					   =		dataset
             UpperCAmelCase_	:						Any					   =		prefix



       def __getitem__(  self  ,lowercase):

             """simple docstring"""





             return self.dataset[F"""{self.prefix}{key}"""]



       def __iter__(  self):

             """simple docstring"""





             return iter([key for key in self.dataset if key.startswith(self.prefix)])



       def __len__(  self):

             """simple docstring"""





             return len(self.dataset)


class 	snake_case_			(lowercase__	):




       """simple docstring"""






       def __init__(  self  ,lowercase = None  ,lowercase = None  ,lowercase = None  ,lowercase=None  ,):

             """simple docstring"""





             if state_dict is None and save_folder is None:
                   raise ValueError("Need either a `state_dict` or a `save_folder` containing offloaded weights.")

             UpperCAmelCase_	:						Tuple					   =		{} if state_dict is None else state_dict
             UpperCAmelCase_	:						List[Any]					   =		save_folder
             if index is None and save_folder is not None:
                   with open(os.path.join(lowercase  ,"index.json")) as f:
                         UpperCAmelCase_	:						Dict					   =		json.load(lowercase)
             UpperCAmelCase_	:						List[str]					   =		{} if index is None else index
             UpperCAmelCase_	:						int					   =		list(self.state_dict.keys())
             self.all_keys.extend([key for key in self.index if key not in self.all_keys])
             UpperCAmelCase_	:						Optional[Any]					   =		device



       def __getitem__(  self  ,lowercase):

             """simple docstring"""





             if key in self.state_dict:
                   return self.state_dict[key]
             UpperCAmelCase_	:						str					   =		self.index[key]
             if weight_info.get("safetensors_file") is not None:
                   if not is_safetensors_available():
                         raise ImportError("These offloaded weights require the use of safetensors: `pip install safetensors`.")

                   from safetensors import safe_open

                   UpperCAmelCase_	:						Any					   =		"cpu" if self.device is None else self.device
                   with safe_open(weight_info["safetensors_file"]  ,framework="pt"  ,device=lowercase) as f:
                         UpperCAmelCase_	:						int					   =		f.get_tensor(weight_info.get("weight_name"  ,lowercase))

                   if "dtype" in weight_info:
                         return tensor.to(getattr(lowercase  ,weight_info["dtype"]))
                   else:
                         return tensor

             UpperCAmelCase_	:						Optional[int]					   =		os.path.join(self.save_folder  ,F"""{key}.dat""")
             return load_offloaded_weight(lowercase  ,lowercase)



       def __iter__(  self):

             """simple docstring"""





             return iter(self.all_keys)



       def __len__(  self):

             """simple docstring"""





             return len(self.all_keys)







def  _snake_case			(							__snake_case       ,	__snake_case     )  ->  Optional[Any]:






      '''simple docstring'''







      UpperCAmelCase_	:						str					   =		{}
      for module_name in submodule_names:
            # We want to catch module_name parameter (module_name.xxx) or potentially module_name, but not any of the
            # submodules that could being like module_name (transformers.h.1 and transformers.h.10 for instance)
            result.update(
                {
                    key: param
                    for key, param in state_dict.items()
                    if key == module_name or key.startswith(module_name + "."     )
                }     )
      return result
