Instructions to use Elvenson/diffuser_inference with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use Elvenson/diffuser_inference with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("Elvenson/diffuser_inference", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
| import base64 | |
| from io import BytesIO | |
| from typing import Dict, List, Any | |
| import torch | |
| from PIL import Image | |
| from diffusers import StableDiffusionPipeline | |
| REPO_ID = "runwayml/stable-diffusion-v1-5" | |
| # helper decoder | |
| def decode_base64_image(image_string): | |
| base64_image = base64.b64decode(image_string) | |
| buffer = BytesIO(base64_image) | |
| return Image.open(buffer) | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| self.pipe = StableDiffusionPipeline.from_pretrained("./stable-diffusion-v1-5") | |
| # self.pipe = self.pipe.to("cuda") | |
| def __call__(self, data: Any) -> List[List[Dict[str, float]]]: | |
| """ | |
| Args: | |
| data (:obj:): | |
| includes the input data and the parameters for the inference. | |
| Return: | |
| A :obj:`dict`:. base64 encoded image | |
| """ | |
| prompts = data.pop("inputs", None) | |
| encoded_image = data.pop("image", None) | |
| init_image = None | |
| if encoded_image: | |
| init_image = decode_base64_image(encoded_image) | |
| init_image.thumbnail((768, 768)) | |
| image = self.pipe(prompts, init_image=init_image).images[0] | |
| # encode image as base 64 | |
| buffered = BytesIO() | |
| image.save(buffered, format="png") | |
| # post process the prediction | |
| return {"image": buffered.getvalue()} | |