ERNIE-Image Diffusers API Guide: Python Code from Zero to Production
HuggingFace Diffusers fully supports ERNIE-Image — learn to call ERNIE-Image with Python for image generation.
Installation
pip install diffusers transformers accelerate torch
pip install git+https://github.com/huggingface/diffusers.git
Basic Usage
Single Image Generation
from diffusers import ERNIEImagePipeline
import torch
# Load model
pipe = ERNIEImagePipeline.from_pretrained(
"baidu/ERNIE-Image",
torch_dtype=torch.float16
)
pipe = pipe.to("cuda")
# Generate
image = pipe(
prompt="a cat sitting on a windowsill, city view at night",
negative_prompt="",
width=1024,
height=1024,
num_inference_steps=28,
guidance_scale=7.0,
generator=torch.Generator("cuda").manual_seed(42)
).images[0]
image.save("output.png")
Batch Generation
from diffusers import ERNIEImagePipeline
import torch
pipe = ERNIEImagePipeline.from_pretrained(
"baidu/ERNIE-Image",
torch_dtype=torch.float16
).to("cuda")
prompts = [
"a cat on a table",
"a dog in a garden",
"a bird in a tree",
"a fish in a pond"
]
images = pipe(
prompt=prompts,
negative_prompt=[""] * len(prompts),
width=1024,
height=1024,
num_inference_steps=28,
guidance_scale=7.0
).images
for i, img in enumerate(images):
img.save(f"output_{i}.png")
Turbo Mode
from diffusers import ERNIEImageTurboPipeline
pipe = ERNIEImageTurboPipeline.from_pretrained(
"baidu/ERNIE-Image-Turbo",
torch_dtype=torch.float16
).to("cuda")
# Turbo: 8 steps
image = pipe(
prompt="a sunset over the ocean, cinematic lighting",
width=1024,
height=1024,
num_inference_steps=8,
guidance_scale=1.0
).images[0]
image.save("turbo_output.png")
Inpainting
from diffusers import ERNIEImageInpaintPipeline
from PIL import Image
pipe = ERNIEImageInpaintPipeline.from_pretrained(
"baidu/ERNIE-Image",
torch_dtype=torch.float16
).to("cuda")
init_image = Image.open("input.png").convert("RGB")
mask_image = Image.open("mask.png").convert("L")
image = pipe(
prompt="a beautiful garden with flowers",
image=init_image,
mask_image=mask_image,
width=1024,
height=1024,
num_inference_steps=28,
guidance_scale=7.0
).images[0]
image.save("inpaint_output.png")
PE Module
from diffusers import ERNIEImagePipeline
pipe = ERNIEImagePipeline.from_pretrained(
"baidu/ERNIE-Image",
torch_dtype=torch.float16,
enable_pe=True
).to("cuda")
image = pipe(
prompt="cat on window",
width=1024,
height=1024,
num_inference_steps=28,
guidance_scale=7.0
).images[0]
image.save("pe_output.png")
API Service Deployment
from fastapi import FastAPI
from pydantic import BaseModel
from diffusers import ERNIEImagePipeline
import torch
app = FastAPI()
pipe = ERNIEImagePipeline.from_pretrained(
"baidu/ERNIE-Image",
torch_dtype=torch.float16
).to("cuda")
class GenerateRequest(BaseModel):
prompt: str
width: int = 1024
height: int = 1024
steps: int = 28
@app.post("/generate")
async def generate(req: GenerateRequest):
image = pipe(
prompt=req.prompt,
width=req.width,
height=req.height,
num_inference_steps=req.steps,
guidance_scale=7.0
).images[0]
import io
buf = io.BytesIO()
image.save(buf, format="PNG")
buf.seek(0)
from fastapi.responses import Response
return Response(buf.read(), media_type="image/png")
Summary
Diffusers ERNIE-Image key points:
- Install: diffusers + transformers + accelerate
- Basic: Pipeline.from_pretrained → pipe() → save
- Batch: Pass prompt list
- Turbo: 8 steps, no high CFG needed
- Inpainting: Mask-guided local regeneration
- API: FastAPI + Diffusers = full service
Diffusers provides a clean API, making ERNIE-Image integration simple.
Based on HuggingFace Diffusers + ERNIE-Image 8B model.