



from collections import namedtuple

__lowerCamelCase								=      namedtuple('''from_to''', '''from_ to''')

__lowerCamelCase								=      {
    '''millimeter''': '''mm''',
    '''centimeter''': '''cm''',
    '''meter''': '''m''',
    '''kilometer''': '''km''',
    '''inch''': '''in''',
    '''inche''': '''in''',  # Trailing 's' has been stripped off
    '''feet''': '''ft''',
    '''foot''': '''ft''',
    '''yard''': '''yd''',
    '''mile''': '''mi''',
}

__lowerCamelCase								=      {
    '''mm''': from_to(0.0_0_1, 1000),
    '''cm''': from_to(0.0_1, 100),
    '''m''': from_to(1, 1),
    '''km''': from_to(1000, 0.0_0_1),
    '''in''': from_to(0.0_2_5_4, 3_9.3_7_0_1),
    '''ft''': from_to(0.3_0_4_8, 3.2_8_0_8_4),
    '''yd''': from_to(0.9_1_4_4, 1.0_9_3_6_1),
    '''mi''': from_to(1_6_0_9.3_4, 0.0_0_0_6_2_1_3_7_1),
}







def  _snake_case			(							__snake_case       ,	__snake_case       ,	__snake_case     )  ->  float:






      '''simple docstring'''







      UpperCAmelCase_	:						List[str]					   =		from_type.lower().rstrip("s"     )
      UpperCAmelCase_	:						Any					   =		TYPE_CONVERSION.get(__snake_case       ,	__snake_case     )
      UpperCAmelCase_	:						str					   =		to_type.lower().rstrip("s"     )
      UpperCAmelCase_	:						List[Any]					   =		TYPE_CONVERSION.get(__snake_case       ,	__snake_case     )
      if new_from not in METRIC_CONVERSION:
            UpperCAmelCase_	:						Optional[Any]					   =		(
                F"""Invalid 'from_type' value: {from_type!r}.\n"""
                F"""Conversion abbreviations are: {', '.join(__snake_case     )}"""
            )
            raise ValueError(__snake_case     )
      if new_to not in METRIC_CONVERSION:
            UpperCAmelCase_	:						Tuple					   =		(
                F"""Invalid 'to_type' value: {to_type!r}.\n"""
                F"""Conversion abbreviations are: {', '.join(__snake_case     )}"""
            )
            raise ValueError(__snake_case     )
      return value * METRIC_CONVERSION[new_from].from_ * METRIC_CONVERSION[new_to].to


if __name__ == "__main__":
   import doctest

   doctest.testmod()





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


if is_vision_available():
   from PIL import Image
else:


   class 	snake_case_			:




          """simple docstring"""






          @staticmethod
          def 			A_ (  *lowercase  ,**lowercase):

                """simple docstring"""





                pass


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




       """simple docstring"""






       _lowerCamelCase           =					MODEL_FOR_VISION_2_SEQ_MAPPING
       _lowerCamelCase           =					TF_MODEL_FOR_VISION_2_SEQ_MAPPING



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase):

             """simple docstring"""





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



       def 			A_ (  self  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						str					   =		pipe(lowercase)
             self.assertEqual(
                 lowercase  ,[
                     [{"generated_text": ANY(lowercase)}],
                     [{"generated_text": ANY(lowercase)}],
                 ]  ,)



       @require_tf
       def 			A_ (  self):

             """simple docstring"""





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

             UpperCAmelCase_	:						Tuple					   =		pipe(lowercase)
             self.assertEqual(
                 lowercase  ,[
                     {
                         "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                     },
                 ]  ,)

             UpperCAmelCase_	:						Optional[int]					   =		pipe([image, image])
             self.assertEqual(
                 lowercase  ,[
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]  ,)

             UpperCAmelCase_	:						Any					   =		pipe(lowercase  ,max_new_tokens=1)
             self.assertEqual(
                 lowercase  ,[{"generated_text": "growth"}]  ,)



       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						List[Any]					   =		pipeline("image-to-text"  ,model="hf-internal-testing/tiny-random-vit-gpt2")
             UpperCAmelCase_	:						str					   =		"./tests/fixtures/tests_samples/COCO/000000039769.png"

             UpperCAmelCase_	:						str					   =		pipe(lowercase)
             self.assertEqual(
                 lowercase  ,[
                     {
                         "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                     },
                 ]  ,)

             UpperCAmelCase_	:						Optional[int]					   =		pipe([image, image])
             self.assertEqual(
                 lowercase  ,[
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]  ,)



       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						List[Any]					   =		pipeline("image-to-text"  ,model="hf-internal-testing/tiny-random-BlipForConditionalGeneration")
             UpperCAmelCase_	:						Union[str, Any]					   =		"./tests/fixtures/tests_samples/COCO/000000039769.png"
             UpperCAmelCase_	:						Optional[Any]					   =		"a photo of"

             UpperCAmelCase_	:						Any					   =		pipe(lowercase  ,prompt=lowercase)
             self.assertTrue(outputs[0]["generated_text"].startswith(lowercase))



       @slow
       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						List[Any]					   =		pipeline("image-to-text"  ,model="ydshieh/vit-gpt2-coco-en")
             UpperCAmelCase_	:						List[str]					   =		"./tests/fixtures/tests_samples/COCO/000000039769.png"

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

             UpperCAmelCase_	:						Optional[Any]					   =		pipe([image, image])
             self.assertEqual(
                 lowercase  ,[
                     [{"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):

             """simple docstring"""





             UpperCAmelCase_	:						Any					   =		pipeline("image-to-text"  ,model="Salesforce/blip-image-captioning-base")
             UpperCAmelCase_	:						str					   =		"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             UpperCAmelCase_	:						Optional[Any]					   =		Image.open(requests.get(lowercase  ,stream=lowercase).raw)

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



       @slow
       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						str					   =		pipeline("image-to-text"  ,model="microsoft/git-base-coco")
             UpperCAmelCase_	:						Union[str, Any]					   =		"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             UpperCAmelCase_	:						int					   =		Image.open(requests.get(lowercase  ,stream=lowercase).raw)

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



       @slow
       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Any					   =		pipeline("image-to-text"  ,model="Salesforce/blip-image-captioning-base")
             UpperCAmelCase_	:						Union[str, Any]					   =		"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             UpperCAmelCase_	:						int					   =		Image.open(requests.get(lowercase  ,stream=lowercase).raw)

             UpperCAmelCase_	:						Any					   =		"a photography of"

             UpperCAmelCase_	:						Optional[Any]					   =		pipe(lowercase  ,prompt=lowercase)
             self.assertEqual(lowercase  ,[{"generated_text": "a photography of a volcano"}])

             with self.assertRaises(lowercase):
                   UpperCAmelCase_	:						Tuple					   =		pipe([image, image]  ,prompt=[prompt, prompt])



       @slow
       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[int]					   =		pipeline("image-to-text"  ,model="microsoft/git-base-coco")
             UpperCAmelCase_	:						List[str]					   =		"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             UpperCAmelCase_	:						List[str]					   =		Image.open(requests.get(lowercase  ,stream=lowercase).raw)

             UpperCAmelCase_	:						Optional[Any]					   =		"a photo of a"

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

             with self.assertRaises(lowercase):
                   UpperCAmelCase_	:						Tuple					   =		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):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[int]					   =		pipeline("image-to-text"  ,model="google/pix2struct-ai2d-base")
             UpperCAmelCase_	:						Any					   =		"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             UpperCAmelCase_	:						Union[str, Any]					   =		Image.open(requests.get(lowercase  ,stream=lowercase).raw)

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

             UpperCAmelCase_	:						Dict					   =		pipe(lowercase  ,prompt=lowercase)
             self.assertEqual(lowercase  ,[{"generated_text": "ash cloud"}])

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



       @slow
       @require_tf
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Union[str, Any]					   =		pipeline("image-to-text"  ,model="ydshieh/vit-gpt2-coco-en"  ,framework="tf")
             UpperCAmelCase_	:						str					   =		"./tests/fixtures/tests_samples/COCO/000000039769.png"

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

             UpperCAmelCase_	:						int					   =		pipe([image, image])
             self.assertEqual(
                 lowercase  ,[
                     [{"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 "}],
                 ]  ,)
