






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 __lowerCamelCase			:





			"""simple docstring"""






			@staticmethod
			def 	UpperCamelCase   (							*UpperCAmelCase	,   **UpperCAmelCase   ):
									"""simple docstring"""







									pass





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





	"""simple docstring"""






	UpperCamelCase__								=				MODEL_FOR_VISION_2_SEQ_MAPPING
	UpperCamelCase__								=				TF_MODEL_FOR_VISION_2_SEQ_MAPPING

	def 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase   ):
							"""simple docstring"""







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

	def 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase   ):
							"""simple docstring"""







							_UpperCAmelCase     =      pipe(UpperCAmelCase   )
							self.assertEqual(
							    UpperCAmelCase	,   [
							        [{'generated_text': ANY(UpperCAmelCase   )}],
							        [{'generated_text': ANY(UpperCAmelCase   )}],
							    ]	,   )

	@require_tf
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

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

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

							_UpperCAmelCase     =      pipe(UpperCAmelCase	,   max_new_tokens=1   )
							self.assertEqual(
							    UpperCAmelCase	,   [{'generated_text': 'growth'}]	,   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

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

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

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

							_UpperCAmelCase     =      pipe(UpperCAmelCase	,   prompt=UpperCAmelCase   )
							self.assertTrue(outputs[0]['generated_text'].startswith(UpperCAmelCase   )   )

	@slow
	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

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

							_UpperCAmelCase     =      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 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

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

	@slow
	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

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

	@slow
	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

							_UpperCAmelCase     =      'a photography of'

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

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

	@slow
	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

							_UpperCAmelCase     =      'a photo of a'

							_UpperCAmelCase     =      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   ):
													_UpperCAmelCase     =      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 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

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

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

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

	@slow
	@require_tf
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







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

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

							_UpperCAmelCase     =      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 '}],
							    ]	,   )








from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a				     =  logging.get_logger(__name__)

_a				     =  {
    '''facebook/regnet-y-040''': '''https://huggingface.co/facebook/regnet-y-040/blob/main/config.json''',
}





class __lowerCamelCase			(				snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				"regnet"
	UpperCamelCase__								=				["x", "y"]

	def __init__(							self	,   UpperCAmelCase=3	,   UpperCAmelCase=32	,   UpperCAmelCase=[128, 192, 512, 1088]	,   UpperCAmelCase=[2, 6, 12, 2]	,   UpperCAmelCase=64	,   UpperCAmelCase="y"	,   UpperCAmelCase="relu"	,   **UpperCAmelCase	,   ):
							"""simple docstring"""







							super().__init__(**UpperCAmelCase   )
							if layer_type not in self.layer_types:
													raise ValueError(F"""layer_type={layer_type} is not one of {",".join(self.layer_types   )}"""   )
							_UpperCAmelCase     =      num_channels
							_UpperCAmelCase     =      embedding_size
							_UpperCAmelCase     =      hidden_sizes
							_UpperCAmelCase     =      depths
							_UpperCAmelCase     =      groups_width
							_UpperCAmelCase     =      layer_type
							_UpperCAmelCase     =      hidden_act
							# always downsample in the first stage
							_UpperCAmelCase     =      True
