




'''simple docstring'''


from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
  from PIL import Image

  from ..image_utils import load_image

if is_torch_available():
  from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
  from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
  from ..tf_utils import stable_softmax

UpperCAmelCase_ 		=      logging.get_logger(__name__)





@add_end_docstrings(__magic_name__ )
class    __lowercase						(  __magic_name__ ):







   def __init__(	self      ,							**UpperCamelCase      )				->    List[Any]:
     super().__init__(**UpperCamelCase      )

     requires_backends(self      ,							'vision'      )
     self.check_model_type(
         TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
         if self.framework == 'tf'
         else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING      )







   def __call__(	self      ,							UpperCamelCase      ,							**UpperCamelCase      )				->    List[str]:
     return super().__call__(UpperCamelCase      ,							**UpperCamelCase      )







   def 		UpperCamelCase__			(	self      ,							**UpperCamelCase      )				->    Optional[int]:
     __a			=					{}
     if "candidate_labels" in kwargs:
       __a			=					kwargs['candidate_labels']
     if "hypothesis_template" in kwargs:
       __a			=					kwargs['hypothesis_template']

     return preprocess_params, {}, {}







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase=None      ,							UpperCamelCase="This is a photo of {}."      )				->    List[Any]:
     __a			=					load_image(UpperCamelCase      )
     __a			=					self.image_processor(images=[image]      ,							return_tensors=self.framework      )
     __a			=					candidate_labels
     __a			=					[hypothesis_template.format(UpperCamelCase      ) for x in candidate_labels]
     __a			=					self.tokenizer(UpperCamelCase      ,							return_tensors=self.framework      ,							padding=UpperCamelCase      )
     __a			=					[text_inputs]
     return inputs







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      )				->    Union[str, Any]:
     __a			=					model_inputs.pop('candidate_labels'      )
     __a			=					model_inputs.pop('text_inputs'      )
     if isinstance(text_inputs[0]      ,							UpperCamelCase      ):
       __a			=					text_inputs[0]
     else:
       # Batching case.
       __a			=					text_inputs[0][0]

     __a			=					self.model(**UpperCamelCase      ,							**UpperCamelCase      )

     __a			=					{
         'candidate_labels': candidate_labels,
         'logits': outputs.logits_per_image,
     }
     return model_outputs







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      )				->    Union[str, Any]:
     __a			=					model_outputs.pop('candidate_labels'      )
     __a			=					model_outputs['logits'][0]
     if self.framework == "pt":
       __a			=					logits.softmax(dim=-1      ).squeeze(-1      )
       __a			=					probs.tolist()
       if not isinstance(UpperCamelCase      ,							UpperCamelCase      ):
         __a			=					[scores]
     elif self.framework == "tf":
       __a			=					stable_softmax(UpperCamelCase      ,							axis=-1      )
       __a			=					probs.numpy().tolist()
     else:
       raise ValueError(f"Unsupported framework: {self.framework}"      )

     __a			=					[
         {'score': score, 'label': candidate_label}
         for score, candidate_label in sorted(zip(UpperCamelCase      ,							UpperCamelCase      )      ,							key=lambda UpperCamelCase      : -x[0]      )
     ]
     return result






'''simple docstring'''


import torch

from diffusers import StableDiffusionPipeline


UpperCAmelCase_ 		=      "path-to-your-trained-model"
UpperCAmelCase_ 		=      StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.floataa).to("cuda")

UpperCAmelCase_ 		=      "A photo of sks dog in a bucket"
UpperCAmelCase_ 		=      pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0]

image.save("dog-bucket.png")



