Giter Site home page Giter Site logo

stable-diffusion-client's Introduction

stable-diffusion-client

stable-diffusion-webui is a awesome, and you may want to integrate it right now to your Javascript projects.

stable-diffusion-webui Client writen in Deno first.

Sample usage

By default stable-diffusion-webui run on localhost port 7860, it that the case you can omit the entry point: 'http://127.0.0.1:7860'

API are disabled by default to enable APIs add in COMMANDLINE_ARGS:

  • --api to enable API access
  • --server-name=0.0.0.0 to access stable-diffusion-webui from a remote host

set COMMANDLINE_ARGS=--xformers --api --server-name=0.0.0.0

stable-diffusion-client contains a lowlevel RESP API named SDClient and a higer level API named SDHelper, access the SDClient via SDHelper.client.

List available models using Helper

import SDHelper from 'https://deno.land/x/stable_diffusion_client/mod.ts';
import { * as pc } from 'https://deno.land/[email protected]/fmt/colors.ts';

const helper = new SDHelper('http://127.0.0.1:7860');

const { selected, list } = await helper.getModels();
console.log(`${list.length} Model available, Selected model is: ${helper.model_name}`);

List available models using SDClient

import SDHelper from 'https://deno.land/x/stable_diffusion_client/mod.ts';
import { * as pc } from 'https://deno.land/[email protected]/fmt/colors.ts';

// initialize your client
const helper = new SDHelper('http://127.0.0.1:7860');
const client = helper.client;
// access the stable-diffusion-webui API

let selectHash = '';
const options = await client.sdapi.v1.options.$get();
const sd_model_checkpoint = options.sd_model_checkpoint || '';
const m = sd_model_checkpoint?.match(/(.+) \[([a-f0-9]+)\]/);
if (m) {
  selectHash = m[2];
  console.log(`sd_model_checkpoint: ${pc.white(m[1])} [${pc.brightMagenta(m[2])}]`);
}
const models = await client.sdapi.v1["sd-models"].$get();
console.log('Available models:')
for (const model of models) {
  const selected = (selectHash === model.hash);
  const colorize = selected ? pc.green : pc.cyan;
  console.log(` ${selected ? '*' : '-'} ${colorize(model.model_name).padEnd(30, ' ')} [${pc.brightMagenta(model.hash)}] config File: ${pc.white(model.config)}`);
}

Start a txt2img generation using Helper

import { SDHelper } from 'https://deno.land/x/stable_diffusion_client/mod.ts';

const helper = new SDHelper('http://127.0.0.1:7860');

/**
 * use txt2img
 */
const body: StableDiffusionProcessingTxt2Img = {
    prompt: "a fluffy rabbit with a gun shooting at flying ducks",
    steps: 10,
    batch_size: 1,
    width: 768,
    height: 768,
    cfg_scale: 7,
    sampler_name: "Euler a",
};
Deno.mkdirSync('out', { recursive: true });
for (let k = 0; k < 1; k++) {
    const img = await helper.txt2img(body);
    const imgId = Date.now();
    console.log(`request ${imgId} done`);
    await Deno.writeTextFile(`out/${imgId}.txt`, `info: ${img.info}\nparams: ${JSON.stringify(img.parameters)}\n`);
    if (img.images) {
        for (let i = 0; i < img.images.length; i++) {
            const fn = `out/${imgId}.${i}.png`;
            await Deno.writeFile(fn, img.images[i]);
            console.log(`write img to ${pc.green(fn)}`)
        }
    }
}

stable-diffusion-client's People

Contributors

urielch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.