Skip to content

GO API for AUTOMATIC1111's SD Web UI.

License

Notifications You must be signed in to change notification settings

erlangs/sd-go-webui-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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/Meonako/webui-api

Then Import

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

OR

Simply add package to import like this

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

Then run go mod tidy


Initialize it like this

API := api.New()

Without passing anything it'll return https://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: "https://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

About

GO API for AUTOMATIC1111's SD Web UI.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%