🎨 Image generation
(Generated with AnimagineXL)
LocalAI supports generating images with Stable diffusion, running on CPU using C++ and Python implementations.
Usage
OpenAI docs: https://platform.openai.com/docs/api-reference/images/create
To generate an image you can send a POST request to the /v1/images/generations endpoint with the instruction as the request body:
Available additional parameters: mode, step.
Note: To set a negative prompt, you can split the prompt with |, for instance: a cute baby sea otter|malformed.
Backends
stablediffusion-ggml
This backend is based on stable-diffusion.cpp. Every model supported by that backend is supported indeed with LocalAI.
Setup
There are already several models in the gallery that are available to install and get up and running with this backend, you can for example run flux by searching it in the Model gallery (flux.1-dev-ggml) or start LocalAI with run:
To use a custom model, you can follow these steps:
- Create a model file
stablediffusion.yamlin the models folder:
- Download the required assets to the
modelsrepository - Start LocalAI
Diffusers
Diffusers is the go-to library for state-of-the-art pretrained diffusion models for generating images, audio, and even 3D structures of molecules. LocalAI has a diffusers backend which allows image generation using the diffusers library.
(Generated with AnimagineXL)
Model setup
The models will be downloaded the first time you use the backend from huggingface automatically.
Create a model configuration file in the models directory, for instance to use Linaqruf/animagine-xl with CPU:
Dependencies
This is an extra backend - in the container is already available and there is nothing to do for the setup. Do not use core images (ending with -core). If you are building manually, see the build instructions.
Model setup
The models will be downloaded the first time you use the backend from huggingface automatically.
Create a model configuration file in the models directory, for instance to use Linaqruf/animagine-xl with CPU:
Local models
You can also use local models, or modify some parameters like clip_skip, scheduler_type, for instance:
Configuration parameters
The following parameters are available in the configuration file:
| Parameter | Description | Default |
|---|---|---|
f16 | Force the usage of float16 instead of float32 | false |
step | Number of steps to run the model for | 30 |
cuda | Enable CUDA acceleration | false |
enable_parameters | Parameters to enable for the model | negative_prompt,num_inference_steps,clip_skip |
scheduler_type | Scheduler type | k_dpp_sde |
cfg_scale | Configuration scale | 8 |
clip_skip | Clip skip | None |
pipeline_type | Pipeline type | AutoPipelineForText2Image |
lora_adapters | A list of lora adapters (file names relative to model directory) to apply | None |
lora_scales | A list of lora scales (floats) to apply | None |
There are available several types of schedulers:
| Scheduler | Description |
|---|---|
ddim | DDIM |
pndm | PNDM |
heun | Heun |
unipc | UniPC |
euler | Euler |
euler_a | Euler a |
lms | LMS |
k_lms | LMS Karras |
dpm_2 | DPM2 |
k_dpm_2 | DPM2 Karras |
dpm_2_a | DPM2 a |
k_dpm_2_a | DPM2 a Karras |
dpmpp_2m | DPM++ 2M |
k_dpmpp_2m | DPM++ 2M Karras |
dpmpp_sde | DPM++ SDE |
k_dpmpp_sde | DPM++ SDE Karras |
dpmpp_2m_sde | DPM++ 2M SDE |
k_dpmpp_2m_sde | DPM++ 2M SDE Karras |
Pipelines types available:
| Pipeline type | Description |
|---|---|
StableDiffusionPipeline | Stable diffusion pipeline |
StableDiffusionImg2ImgPipeline | Stable diffusion image to image pipeline |
StableDiffusionDepth2ImgPipeline | Stable diffusion depth to image pipeline |
DiffusionPipeline | Diffusion pipeline |
StableDiffusionXLPipeline | Stable diffusion XL pipeline |
StableVideoDiffusionPipeline | Stable video diffusion pipeline |
AutoPipelineForText2Image | Automatic detection pipeline for text to image |
VideoDiffusionPipeline | Video diffusion pipeline |
StableDiffusion3Pipeline | Stable diffusion 3 pipeline |
FluxPipeline | Flux pipeline |
FluxTransformer2DModel | Flux transformer 2D model |
SanaPipeline | Sana pipeline |
Advanced: Additional parameters
Additional arbitrarly parameters can be specified in the option field in key/value separated by ::
Note: There is no complete parameter list. Any parameter can be passed arbitrarly and is passed to the model directly as argument to the pipeline. Different pipelines/implementations support different parameters.
The example above, will result in the following python code when generating images:
Usage
Text to Image
Use the image generation endpoint with the model name from the configuration file:
Image to Image
https://huggingface.co/docs/diffusers/using-diffusers/img2img
An example model (GPU):
🖼️ Flux kontext with stable-diffusion.cpp
LocalAI supports Flux Kontext and can be used to edit images via the API:
Install with:
local-ai run flux.1-kontext-dev
To test:
Depth to Image
https://huggingface.co/docs/diffusers/using-diffusers/depth2img