Instructions to use stabilityai/stable-diffusion-x4-upscaler with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use stabilityai/stable-diffusion-x4-upscaler with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler", 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
File size: 1,504 Bytes
9b535c8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | from typing import Dict, List, Any
from diffusers import StableDiffusionUpscalePipeline
import torch
from PIL import Image
import io
class EndpointHandler:
def __init__(self, path: str):
# Load the Stable Diffusion x4 upscaler model
self.pipeline = StableDiffusionUpscalePipeline.from_pretrained(
path,
torch_dtype=torch.float16
)
self.pipeline.to("cuda")
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
data args:
inputs: str - The text prompt for the upscaling.
image: bytes - The low-resolution image as byte data.
Return:
A list of dictionaries with the upscaled image.
"""
# Extract inputs and image from the payload
prompt = data.get("inputs", "")
image_bytes = data.get("image", None)
if image_bytes is None:
return [{"error": "No image provided"}]
# Convert the byte data to an image
low_res_img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
# Perform upscaling
upscaled_image = self.pipeline(prompt=prompt, image=low_res_img).images[0]
# Save the upscaled image to a byte stream
byte_io = io.BytesIO()
upscaled_image.save(byte_io, format="PNG")
byte_io.seek(0)
# Return the upscaled image as byte data
return [{"upscaled_image": byte_io.getvalue()}]
|