npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stable-canvas/sd-webui-a1111-client

v1.1.6

Published

API client for AUTOMATIC1111/stable-diffusion-webui for Node.js and Browser.

Downloads

137

Readme

sd-webui-a1111-client

npm npm GitHub Repo stars

API client for AUTOMATIC1111/stable-diffusion-webui for Node.js and Browser.

Features

  • Full TypeScript support
  • Supports Node.js and browser environments
  • Extensions: ControlNet, Cutoff, DynamicCFG, TiledDiffusion, TiledVAE, agent scheduler
  • Batch processing support
  • Easy integration with popular extensions and models

Requisites & Supported Versions

Requisites

  • Enable webui-api: Use the --api command line argument.
  • Disable webui-gui (optional): Use --nowebui to disable the web GUI.
  • You can find and modify the COMMANDLINE_ARGS value in the webui-user.bat or webui-user.sh file.

Supported Versions

  • WebUI: 1.8.0
  • ControlNet: 3b4eedd

Getting Started

Client Instance

import { SDWebUIA1111Client } from "@stable-canvas/sd-webui-a1111-client";

const client = new SDWebUIA1111Client({
  BASE: "http://localhost:7860",
});

Authentication

Use the --api-auth command line argument with "username:password" on the server to enable API authentication.

const client = new SDWebUIA1111Client({
  BASE: "http://localhost:7860",
  USERNAME: 'your_username',
  PASSWORD: 'your_password'
});

Default Service

Allowing full control over the request body.

const response = await client.default.text2ImgapiSdapiV1Txt2ImgPost({
  requestBody: {
    prompt: 'an astronaut riding a horse on the moon'
  }
});

fully functions here: Service Functions

API Usage

Documentation

ServiceApi documentation

Instance

Use the A1111StableDiffusionApi object for a high-level API client with optimized types and parameter handling.

const api = new A1111StableDiffusionApi({
  client: {
    BASE: 'http://127.0.0.1:7860',
    // USERNAME: 'your_username',
    // PASSWORD: 'your_password'
  },
  // optional caching
  // cache: {
  //   disableCache: false,
  //   cacheTime: 60 * 1000
  // }
});

txt2img

const { image, info } = await api.Service.txt2img({
  prompt: '1girl'
});

img2img

const { image, info } = await api.Service.img2img({
  prompt: '1girl'
});

Batch Generation

Use txt2imgBatch and img2imgBatch for batch processing.

const batch = api.Service.txt2imgBatch(
  { /* ... */ },
  {
    batchSize: 2,
    numBatches: 10
  }
);

const responses = await batch.waitForComplete();

ControlNet Api Usage

ControlNetApi documentation

Requisites

txt2img with ControlNet

const { image, info } = await api.ControlNet.txt2img({
  params: {
    prompt: '...',
    // ...
  },
  units: [
    {
      image: '...', // base64 string
      module: 'openpose_full',
      model: 'control_v11p_sd15_openpose [cab727d4]'
    }
  ]
});

img2img with ControlNet

const { image, info } = await api.ControlNet.img2img({
  params: {
    prompt: '...',
    // ...
  },
  units: [
    {
      image: '...', // base64 string
      module: 'openpose_full',
      model: 'control_v11p_sd15_openpose [cab727d4]'
    }
  ]
});

ControlNet Batch Generation

const batch = api.ControlNet.txt2imgBatch({
  params: { /* ... */ },
  options: {
    batchSize: 2,
    numBatches: 10
  },
  units: [
    {
      image: '...', // base64 string
      module: 'openpose_full',
      model: 'control_v11p_sd15_openpose [cab727d4]'
    }
  ]
});

const responses = await batch.waitForComplete();

Detect

Thanks to the ControlNet plugin releasing an interface for detection, we can use this API when we only need to detect and not generate images.

It is worth mentioning that if you want to customize the preprocessing process of ControlNet, you need to set the module parameter of the ControlNet Unit to none, indicating that the input image has already been preprocessed.

Regarding the parameters controlnet_threshold_a and controlnet_threshold_b, you can use api.ControlNet.getModuleDetail to get the requirements for these parameters from the current plugin.

const {
  images,
} = await api.ControlNet.detect({
  controlnet_module: 'openpose_full',
  controlnet_input_images: [
    // image base64
  ],
  controlnet_processor_res: 512,
});

GetModelList

const modelList = await api.ControlNet.getModels();

GetModuleList

const moduleList = await api.ControlNet.getModules();

Processor Usage

Advanced processing pipeline, providing more control over requests.

The design of the processor lies between the API and the client. It is not as convenient as the API but offers more customized functionalities, which is useful for achieving workflows similar to ComfyUI.

Text to Image / Image to Image

const response = await client.default.text2ImgapiSdapiV1Txt2ImgPost({
  requestBody: {
    prompt: 'an astronaut riding a horse on the moon'
  }
});
const { images: [img] } = response;

const buffer = Buffer.from(img, "base64");
await fs.promises.writeFile('result.png', buffer);

Cutoff Extension

const pc1 = new Txt2imgProcess({ prompt: "1girl, black top, short pink hair" });
pc1.use(new CutoffExt({ targets: 'black, pink' }));
const { images } = await pc1.request(client);

ControlNet Extension

const input_image = fs.readFileSync('input_image.png', 'base64');
const pc1 = new Img2imgProcess({ prompt: "1girl", init_images: [input_image]});

const cnet_ext = new ControlNetExt();
cnet_ext.addUnit({
  model: 'openpose_full',
  module: 'control_v11p_sd15_openpose [cab727d4]',
  weight: 1,
  pixel_perfect: true,
});

pc1.use(cnet_ext);
const { images } = await pc1.request(client);

Browser Usage

CDN links

| Type | unpkg | jsdelivr | |-----------|------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------| | mjs | unpkg (mjs) | jsdelivr (mjs) | | umd | unpkg (umd) | jsdelivr (umd) |

Usage Example

<!DOCTYPE html>
<html lang="en">
<head>
  <script type="importmap">
    {
      "imports": {
        "@stable-canvas/sd-webui-a1111-client": "https://unpkg.com/@stable-canvas/sd-webui-a1111-client@latest/dist/main.module.mjs"
      }
    }
  </script>
</head>
<body>
  <h1>@stable-canvas/sd-webui-a1111-client DEMO</h1>
  <div id="message"></div>
  <img src="" alt="result" />
  <script type="module">
    import { SDWebUIA1111Client, Txt2imgProcess } from "@stable-canvas/sd-webui-a1111-client";
    
    window.onload = async () => {
      const $msg = document.querySelector("#message");
      const $img = document.querySelector("img");
      
      const client = new SDWebUIA1111Client({ BASE: "http://localhost:7860" });
      const pc1 = new Txt2imgProcess({ prompt: "1girl" });
      
      $msg.innerText = "Generating...";
      
      try {
        const { images } = await pc1.request(client);
        const image = images[0];
        
        $img.src = `data:image/png;base64,${image}`;
        $msg.innerText = "Done.";
      } catch (error) {
        $msg.innerText = error.message;
        console.error(error);
      }
    };
  </script>
</body>
</html>

Example Code

License

Apache-2.0