I'm not using GO anymore. While GO is definitely fun to use, it's "a bit verbose" and
I think Rust is more enjoyable with something like Option<T>
enum.
GO is a great language and maybe one of the first "Errors are values" and even with garbage collector, it is pretty fast.
but for me, Rust is just more powerful with all these macro stuff
(I can't write/create one obviously but I can't express enough how easy it is to use library like Tauri, Serde, poise-rs)
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.
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 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 callImg2Img
, you'll get anunexpected response
/error
or worse likepanic
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()
}
Move HERE
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",
},
}