|
| 1 | +from typing import Any, TypedDict |
| 2 | + |
| 3 | +import PIL |
| 4 | +import torch |
| 5 | +from diffusers import EulerDiscreteScheduler, StableDiffusionPipeline |
| 6 | +from pydantic import BaseModel, Field |
| 7 | +from ray import serve |
| 8 | + |
| 9 | +from aana.deployments.base_deployment import BaseDeployment |
| 10 | +from aana.models.core.dtype import Dtype |
| 11 | +from aana.models.pydantic.prompt import Prompt |
| 12 | + |
| 13 | + |
| 14 | +class StableDiffusion2Output(TypedDict): |
| 15 | + """Output class for the StableDiffusion2 deployment.""" |
| 16 | + |
| 17 | + image: PIL.Image.Image |
| 18 | + |
| 19 | + |
| 20 | +class StableDiffusion2Config(BaseModel): |
| 21 | + """The configuration for the Stable Diffusion 2 deployment. |
| 22 | +
|
| 23 | + Attributes: |
| 24 | + model (str): the model ID on HuggingFace |
| 25 | + dtype (str): the data type (optional, default: "auto"), one of "auto", "float32", "float16" |
| 26 | + """ |
| 27 | + |
| 28 | + model: str |
| 29 | + dtype: Dtype = Field(default=Dtype.AUTO) |
| 30 | + |
| 31 | + |
| 32 | +@serve.deployment |
| 33 | +class StableDiffusion2Deployment(BaseDeployment): |
| 34 | + """Stable Diffusion 2 deployment.""" |
| 35 | + |
| 36 | + async def apply_config(self, config: dict[str, Any]): |
| 37 | + """Apply the configuration. |
| 38 | +
|
| 39 | + The method is called when the deployment is created or updated. |
| 40 | +
|
| 41 | + It loads the model and scheduler from HuggingFace. |
| 42 | +
|
| 43 | + The configuration should conform to the StableDiffusion2Confgi schema. |
| 44 | + """ |
| 45 | + config_obj = StableDiffusion2Config(**config) |
| 46 | + |
| 47 | + # Load the model and processor from HuggingFace |
| 48 | + self.model_id = config_obj.model |
| 49 | + self.dtype = config_obj.dtype |
| 50 | + if self.dtype == Dtype.INT8: |
| 51 | + self.torch_dtype = Dtype.FLOAT16.to_torch() |
| 52 | + else: |
| 53 | + self.torch_dtype = self.dtype.to_torch() |
| 54 | + self.device = "cuda" if torch.cuda.is_available() else "cpu" |
| 55 | + self.model = StableDiffusionPipeline.from_pretrained( |
| 56 | + self.model_id, |
| 57 | + torch_dtype=self.torch_dtype, |
| 58 | + scheduler=EulerDiscreteScheduler.from_pretrained( |
| 59 | + self.model_id, subfolder="scheduler" |
| 60 | + ), |
| 61 | + device_map="auto", |
| 62 | + ) |
| 63 | + |
| 64 | + self.model.to(self.device) |
| 65 | + |
| 66 | + async def generate(self, prompt: Prompt) -> StableDiffusion2Output: |
| 67 | + """Runs the model on a given prompt and returns the first output. |
| 68 | +
|
| 69 | + Arguments: |
| 70 | + prompt (Prompt): the prompt to the model. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + StableDiffusion2Output: a dictionary with one key containing the result |
| 74 | + """ |
| 75 | + image = self.model(str(prompt)).images[0] |
| 76 | + return {"image": image} |
0 commit comments