#web-ui #api-calls #lora #image #openapi #api-client #stable-diffusion-webui

sdwebuiapi

A library for interacting with openapi of AUTOMATIC1111/stable-diffusion-webui

2 releases

0.0.6 Dec 3, 2023
0.0.5 Oct 6, 2023

#1562 in Web programming

Download history 36/week @ 2024-07-28 25/week @ 2024-09-29

55 downloads per month

MIT license

18KB
355 lines

sdwebuiapi

A Rust library for interacting with openapi of AUTOMATIC1111/stable-diffusion-webui.

Supports txt2img API calls, with controlnet 2.0 support and lora support.

The parameters for API calls are based on the documentation at http://localhost:7860/docs and have been successfully tested on webui 1.5.

Install

cargo add sdwebuiapi

Usage

webuiapi_demo.ipynb contains example code with original images. Images are compressed as jpeg in this document.

create API client

let api_auth = Some(sdwebuiapi::OpenApiV1Auth {
    username: "username set in webui's --api-auth arg",
    password: "password set in webui's --api-auth arg",
});
// or `let api_auth = None` if webui is not protected by api_auth
let client = sdwebuiapi::Client::new("http://localhost:7860", api_auth);

txt2img

let mut payload = sdwebuiapi::TextToImagePayload {
    prompt: "a cyberpunk cat".to_owned(),
    ..Default::default()
};

payload.set_base_model("v1-5-pruned-emaonly.safetensors [6ce0161689]");

let response: sdwebuiapi::TextToImageResponse = client.txt2img(payload).await;

// the generated image is in base64 format
let raw_b64_str: &str = &response.images[0];

txt2img with controlnet and lora

encode image to base64, controlnet accepts raw base64 png image string, without headers

let input_image = std::fs::read("input-image.png").unwrap();
let raw_b64_str = data_encoding::BASE64.encode(&input_image);

build txt2img params

let controlnet_payload = sdwebuiapi::ControlnetPayload {
    input_image: raw_b64_str.clone(),
    model: "control_v11p_sd15_canny [d14c016b]".to_owned(),
    module: "canny".to_owned(),
    ..Default::default()
};

let lora_paylod = sdwebuiapi::LoraPayload {
    name: "add_detail".to_owned(),
    weight: 1.0
};

let mut payload = sdwebuiapi::TextToImagePayload {
    prompt: "a cyberpunk cat".to_owned(),
    ..Default::default()
};

payload
    .set_base_model("v1-5-pruned-emaonly.safetensors [6ce0161689]")
    .add_loras(&vec![lora_paylod])
    .add_controlnet_units(&vec![controlnet_payload]);

generate image

let response = client.txt2img(payload).await;

Dependencies

~6–18MB
~249K SLoC