Code Monkey home page Code Monkey logo

webui-api's Introduction

AUTOMATIC1111's Webui API

AUTOMATIC1111's Webui API for GO. So you don't have to do it yourself.
Aim to be as easy to use as possible without performance in mind.

Currently Support (And also roadmap)

  • Auth Related ( DIDNT TEST | Please open an issue if you have encounter any problem )
  • Txt2Img
  • Img2Img
  • Extras (Single)
  • Extras (Batch)
  • PNG Info
  • Progress
  • Interrogate
  • Interrupt
  • Skip
  • Options
  • Get Available Model(s)

Getting Started

Required Stable Diffusion Web UI running with --api argument

go get github.com/colornote/webui-api

Then Import

import (
    ...
    "github.com/colornote/webui-api"
)

OR

Simply add package to import like this

import (
    ...
    "github.com/colornote/webui-api"
)

Then run go mod tidy


Initialize it like this

API := api.New()

Without passing anything it'll return http://127.0.0.1:7860 and all V1 API path as default.
If you wanna change it, just pass in a new config like this

API := api.New(api.Config{
    BaseURL: "colab.google.link",
    Path: &api.APIPath{
        Txt2Img: "/new/api/path/txt2img",
    },
})

Be aware, if you change Path field, you'll have to manually add all other path.

Say for above example, it'll be only Txt2Img path in there. When you call Img2Img, you'll get an unexpected response/error or worse like panic


Now that finished, we can start using it now. Let's say we'll do TXT2IMG

resp, err := API.Text2Image(&api.Txt2Image{
    Prompt: "masterpiece, best quality, solo, cute, blue hair, purple eyes",
    NegativePrompt: "lowres, bad anatomy, low quality, normal quality, worst quality",
})

Keep in mind that this will block your app until API done generating image(s)


When it's done, check for the error and then we can do

imageList, err := resp.DecodeAllImages()

Check for the error and then we can save it to disk

for index, image := range imageList {
    file, err := os.OpenFile(
        fmt.Sprintf("txt2img-result %v.png", index), 
        os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
        0777,
    )
    if err != nil {
        panic(err)
    }
    
    file.Write(image)
    file.Close()
}

Hol'up, one tip tho. If you really care about performance, you can decode it yourself like this

Before called resp.DecodeAllImages()

for index := range resp.Images {
    decoded, err := resp.DecodeImage(index)
    if err != nil {
        panic(err)
    }
    
    file, err := os.OpenFile(
        fmt.Sprintf("txt2img-result %v.png", index), 
        os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
        0777,
    )
    if err != nil {
        panic(err)
    }
    
    file.Write(image)
    file.Close()
}

Example

Move HERE

Default Value

var DefaultConfig = Config{
    BaseURL: "http://127.0.0.1:7860",
    Path: &APIPath{
        // Don't change any of these unless you know what you're doing. 
        // I purposely exported this as I don't know If I'll still maintain this pkg in the future
        Txt2Img:     "/sdapi/v1/txt2img",
        Img2Img:     "/sdapi/v1/img2img",
        ExtraSingle: "/sdapi/v1/extra-single-image",
        ExtraBatch:  "/sdapi/v1/extra-batch-images",
        PNGInfo:     "/sdapi/v1/png-info",
        Progress:    "/sdapi/v1/progress",
        Interrogate: "/sdapi/v1/interrogate",
        Interrupt:   "/sdapi/v1/interrupt",
        Skip:        "/sdapi/v1/skip",
        Options:     "/sdapi/v1/options",
        SDModels:    "/sdapi/v1/sd-models",
    },
}

Credits

webui-api's People

Contributors

lebenoa avatar colornote 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.