

"""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       _lowerCAmelCase						:







    """simple docstring"""







    @staticmethod
    def       _lowercase				(    *UpperCAmelCase__							:    Tuple,      **UpperCAmelCase__							:    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			:      Dict     	=			TF_MODEL_FOR_VISION_2_SEQ_MAPPING


 def       _lowercase				(    self							:    Any,      UpperCAmelCase__							:    int,      UpperCAmelCase__							:    Optional[int],      UpperCAmelCase__							:    List[str]			):
    __lowercase			=		pipeline("image-to-text",      model=UpperCAmelCase__,      tokenizer=UpperCAmelCase__,      image_processor=UpperCAmelCase__			)
    __lowercase			=		[
        Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			),
        "./tests/fixtures/tests_samples/COCO/000000039769.png",
    ]
    return pipe, examples


 def       _lowercase				(    self							:    List[Any],      UpperCAmelCase__							:    Optional[int],      UpperCAmelCase__							:    Tuple			):
    __lowercase			=		pipe(UpperCAmelCase__			)
    self.assertEqual(
        UpperCAmelCase__,      [
            [{"generated_text": ANY(UpperCAmelCase__			)}],
            [{"generated_text": ANY(UpperCAmelCase__			)}],
        ],      )


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

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

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

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


 @require_torch
 def       _lowercase				(    self							:    Any			):
    __lowercase			=		pipeline("image-to-text",      model="hf-internal-testing/tiny-random-vit-gpt2"			)
    __lowercase			=		"./tests/fixtures/tests_samples/COCO/000000039769.png"

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

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


 @require_torch
 def       _lowercase				(    self							:    int			):
    __lowercase			=		pipeline("image-to-text",      model="hf-internal-testing/tiny-random-BlipForConditionalGeneration"			)
    __lowercase			=		"./tests/fixtures/tests_samples/COCO/000000039769.png"
    __lowercase			=		"a photo of"

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


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

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

    __lowercase			=		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       _lowercase				(    self							:    Tuple			):
    __lowercase			=		pipeline("image-to-text",      model="Salesforce/blip-image-captioning-base"			)
    __lowercase			=		"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
    __lowercase			=		Image.open(requests.get(UpperCAmelCase__,      stream=UpperCAmelCase__			).raw			)

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


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

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


 @slow
 @require_torch
 def       _lowercase				(    self							:    Dict			):
    __lowercase			=		pipeline("image-to-text",      model="Salesforce/blip-image-captioning-base"			)
    __lowercase			=		"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
    __lowercase			=		Image.open(requests.get(UpperCAmelCase__,      stream=UpperCAmelCase__			).raw			)

    __lowercase			=		"a photography of"

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

    with self.assertRaises(UpperCAmelCase__			):
       __lowercase			=		pipe([image, image],      prompt=[prompt, prompt]			)


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

    __lowercase			=		"a photo of a"

    __lowercase			=		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			=		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       _lowercase				(    self							:    Any			):
    __lowercase			=		pipeline("image-to-text",      model="google/pix2struct-ai2d-base"			)
    __lowercase			=		"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
    __lowercase			=		Image.open(requests.get(UpperCAmelCase__,      stream=UpperCAmelCase__			).raw			)

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

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

    with self.assertRaises(UpperCAmelCase__			):
       __lowercase			=		pipe([image, image],      prompt=[prompt, prompt]			)



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

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

    __lowercase			=		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 requests




def 						_A						(						UpperCamelCase_  : str,    UpperCamelCase_  : bool = False,    UpperCamelCase_  : str = ".")			->		dict:
   '''simple docstring'''


   __lowercase			=		"https://api.nasa.gov/planetary/apod"
   return requests.get(UpperCamelCase_,    params={"api_key": api_key}).json()




def 						_A						(						UpperCamelCase_  : str,    UpperCamelCase_  : str = ".")			->		dict:
   '''simple docstring'''


   __lowercase			=		get_apod_data(UpperCamelCase_)
   __lowercase			=		apod_data["url"]
   __lowercase			=		img_url.split("/")[-1]
   __lowercase			=		requests.get(UpperCamelCase_,    stream=UpperCamelCase_)

   with open(F"""{path}/{img_name}""",    "wb+") as img_file:
      shutil.copyfileobj(response.raw,    UpperCamelCase_)
   del response
   return apod_data




def 						_A						(						UpperCamelCase_  : str)			->		dict:
   '''simple docstring'''


   __lowercase			=		"https://images-api.nasa.gov/search"
   return requests.get(UpperCamelCase_,    params={"q": query}).json()


if __name__ == "__main__":
   print(save_apod('YOUR API KEY'))
   _a				   =							get_archive_data('apollo 2011')['collection']['items']
   print(apollo_aaaa_items[0]['data'][0]['description'])




