






# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
   from PIL import Image


class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		Tuple								=						'''Salesforce/blip-image-captioning-base'''
     UpperCamelCase_				:		List[str]								=						(
         '''This is a tool that generates a description of an image. It takes an input named `image` which should be the '''
         '''image to caption, and returns a text that contains the description in English.'''
     )
     UpperCamelCase_				:		str								=						'''image_captioner'''
     UpperCamelCase_				:		Any								=						AutoModelForVisionaSeq

     UpperCamelCase_				:		List[Any]								=						['''image''']
     UpperCamelCase_				:		Optional[int]								=						['''text''']


     def __init__(					self							:						List[str]       ,			*UpperCAmelCase_							:						Any       ,			**UpperCAmelCase_							:						str   ):
       requires_backends(self       ,			["vision"]   )
       super().__init__(*UpperCAmelCase_       ,			**UpperCAmelCase_   )


     def 					_A							(					self							:						List[Any]       ,			UpperCAmelCase_							:						"Image"   ):
       return self.pre_processor(images=UpperCAmelCase_       ,			return_tensors="pt"   )


     def 					_A							(					self							:						List[str]       ,			UpperCAmelCase_							:						int   ):
       return self.model.generate(**UpperCAmelCase_   )






     def 					_A							(					self							:						Optional[int]       ,			UpperCAmelCase_							:						Dict   ):
       return self.pre_processor.batch_decode(UpperCAmelCase_       ,			skip_special_tokens=UpperCAmelCase_   )[0].strip()







'''simple docstring'''






import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


_UpperCAmelCase    :     List[Any]         =       False







class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   pass







@slow
@require_torch_gpu
class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			Dict:
        _A    		=  VersatileDiffusionImageVariationPipeline.from_pretrained('shi-labs/versatile-diffusion'     )
        pipe.to(UpperCamelCase__     )
        pipe.set_progress_bar_config(disable=UpperCamelCase__     )

        _A    		=  load_image(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'     )
        _A    		=  torch.manual_seed(0     )
        _A    		=  pipe(
            image=UpperCamelCase__, generator=UpperCamelCase__, guidance_scale=7.5, num_inference_steps=50, output_type='numpy', ).images

        _A    		=  image[0, 2_53:2_56, 2_53:2_56, -1]

        assert image.shape == (1, 5_12, 5_12, 3)
        _A    		=  np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945]     )

        assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1e-2



