-
Notifications
You must be signed in to change notification settings - Fork 12
Example
Text-To-Image
package main
import (
"fmt"
"image/png"
"os"
"text/tabwriter"
"github.com/Meonako/webui-api"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
resp, err := API.Text2Image(api.Txt2Image{
Prompt: api.BuildPrompt(
"solo", "cute", "adorable", "innocent",
"blush", "girly", "narrow waist", "absurdly long hair",
"(pink hair)", "hair ornament", "pink eyes", "hair flower",
),
//
// Or you can pass a long string
//
// Prompt: "solo, cute, adorable, innocent, blush, girly, narrow waist, absurdly long hair, (pink hair), hair ornament, pink eyes, hair flower",
//
// Or you can do this
//
// Prompt: "solo, cute, adorable, innocent" +
// "blush, girly, narrow waist, absurdly long hair" +
// "(pink hair), hair ornament, pink eyes, hair flower",
//
NegativePrompt: api.BuildPrompt(
"sketch by bad-artist", "lowres", "bad anatomy",
"bad hands", "text", "error", "missing fingers",
"extra digit", "fewer digits", "cropped",
"worst quality", "low quality", "normal quality",
"jpeg artifacts", "signature", "watermark",
"username", "blurry", "simple background",
"(track suit)", "(jacket)", "(name tag)",
"(sleeveless), (shoes), (socks), hat",
"covered navel, clothes lift, clothes pull, (head out of frame)",
),
})
if err != nil {
panic(err)
}
writer := tabwriter.NewWriter(os.Stdout, 1, 1, 3, ' ', 0)
fmt.Fprintf(writer, "Steps\t|\t%v\n", resp.Parameters.Steps)
fmt.Fprintf(writer, "Sampler Name\t|\t%v\n", resp.Parameters.SamplerName)
fmt.Fprintf(writer, "Sampler Index\t|\t%v\n", resp.Parameters.SamplerIndex)
fmt.Fprintf(writer, "Width\t|\t%v\n", resp.Parameters.Width)
fmt.Fprintf(writer, "Height\t|\t%v\n", resp.Parameters.Height)
writer.Flush()
readers, err := resp.MakeBytesReader()
if err != nil {
panic(err)
}
for index, reader := range readers {
file, err := os.OpenFile(fmt.Sprintf("Image (%v).png", index), os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
panic(err)
}
defer file.Close()
img, err := png.Decode(reader)
if err != nil {
panic(err)
}
err = png.Encode(file, img)
if err != nil {
panic(err)
}
}
}Expected Output
File named "Image (0).png"
Image-To-Image
Need any image file named original.png
package main
import (
"fmt"
"image/png"
"os"
"strings"
"text/tabwriter"
"github.com/Meonako/webui-api"
"github.com/Meonako/webui-api/img2img"
"github.com/Meonako/webui-api/utils"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
imageData, err := utils.Base64FromFile("original.png")
if err != nil {
panic(err)
}
result, err := API.Image2Image(api.Img2Img{
InitImages: []string{
imageData,
},
ResizeMode: img2img.RESIZE_MODE.RESIZE_AND_FILL,
Prompt: "solo, cute, innocent, blush, girly, blue hair, Unicorn",
NegativePrompt: api.BuildPrompt(
"sketch by bad-artist", "lowres", "bad anatomy", "bad hands",
"text", "error", "missing fingers", "extra digit", "fewer digits",
"cropped", "worst quality", "low quality", "normal quality",
"jpeg artifaces", "signature", "watermark", "username", "blurry", "artist name",
),
Seed: 123123,
})
if err != nil {
panic(err)
}
writer := tabwriter.NewWriter(os.Stdout, 1, 1, 3, ' ', 0)
fmt.Fprintf(writer, "Prompt\t|\t%v\n", result.Parameters.Prompt)
fmt.Fprintf(writer, "Negative Prompt\t|\t%v\n", result.Parameters.NegativePrompt)
fmt.Fprintf(writer, "Steps\t|\t%v\n", result.Parameters.Steps)
fmt.Fprintf(writer, "Denoising Strength\t|\t%v\n", result.Parameters.DenoisingStrength)
fmt.Fprintf(writer, "CFG Scale\t|\t%v\n", result.Parameters.CFGScale)
fmt.Fprintf(writer, "Sampler\t|\t%v\n", result.Parameters.SamplerName)
fmt.Fprintf(writer, "Width\t|\t%v\n", result.Parameters.Width)
fmt.Fprintf(writer, "Height\t|\t%v\n\n%v\n", result.Parameters.Height, strings.Repeat("-", 45))
writer.Flush()
images, err := result.MakeBytesReader()
if err != nil {
panic(err)
}
for index, image := range images {
file, err := os.OpenFile(fmt.Sprintf("result (%v).png", index), os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
panic(err)
}
defer file.Close()
img, err := png.Decode(image)
if err != nil {
panic(err)
}
err = png.Encode(file, img)
if err != nil {
panic(err)
}
}
}Expected Output
File named "result (0).png"
Single Image Upscale & Face Restoration
Need any image file named original.png
package main
import (
"fmt"
"os"
"text/tabwriter"
"github.com/Meonako/webui-api"
"github.com/Meonako/webui-api/extra"
"github.com/Meonako/webui-api/utils"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
fmt.Println("Reading image data...")
imageData, err := utils.Base64FromFile("original.png")
if err != nil {
panic(err)
}
params := api.ExtraSingleImage{
ResizeMode: extra.ResizeMode.SCALE_BY, // 0 || 1
UpscalingResize: 4, // Only use when ResizeMode = ScaleBy (0)
Upscaler1: extra.R_ESRGAN_4x_PLUS_ANIME_6B, // if you want to, you can use "extra.Model.R_ESRGAN_4x_PLUS_ANIME_6B" or the camelCase one
Image: imageData,
}
fmt.Println("Sending to API with params...")
writer := tabwriter.NewWriter(os.Stdout, 1, 1, 3, ' ', 0)
fmt.Fprintf(writer, "\tResize Mode: \t %v\n", params.ResizeMode)
fmt.Fprintf(writer, "\tUpscaling Resize: \t %v\n", params.UpscalingResize)
fmt.Fprintf(writer, "\tUpscaler: \t %v\n", params.Upscaler1)
writer.Flush()
result, err := API.ExtraSingleImage(params)
if err != nil {
panic(err)
}
fmt.Println("Decoding...")
fileBytes, err := result.DecodeImage()
if err != nil {
panic(err)
}
fmt.Println("Saving...")
file, err := os.OpenFile("upscaled.png", os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
panic(err)
}
defer file.Close()
file.Write(fileBytes)
}Expected Output
File named
upscaled.png
Multiple Upscale & Face Restoration
Need 3 image file named orignal1.png, original2.png and original3.png
package main
import (
"fmt"
"os"
"github.com/Meonako/webui-api"
"github.com/Meonako/webui-api/extra"
"github.com/Meonako/webui-api/sampler"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
images, err := extra.BuildBatchFromFiles("original1.png", "original2.png", "original3.png")
pif(err)
params := &api.ExtraBatchImages{
// ImagesList: []api.ImageData{
// {Data: "data:image/png;base64," + utils.Base64FromFileIgnore("image.png"), Name: "1"},
// {Data: "data:image/png;base64," + utils.Base64FromFileIgnore("original.png"), Name: "2"},
// {Data: "data:image/png;base64," + utils.Base64FromFileIgnore("test.png"), Name: "3"},
// },
//
// ----- Above is the manual way. DON'T FORGET THE "data:image/png;base64," because WITHOUT it, the server will raise an exception. -----
//
// ImagesList: api.BuildBatch(
// utils.Base64FromFileIgnore("test.png"), // ONLY DO THIS IF YOU'RE SO SURE THAT IT WON'T CAUSE ANY ERRORS
// utils.Base64FromFileIgnore("image.png"), // Possible Error is from OS package where it can't access || can't find the file
// utils.Base64FromFileIgnore("original.png"), // It'll be better if you caught the error before hand
// ),
ResizeMode: extra.ResizeMode.SCALE_BY,
ImagesList: images,
UpscalingResize: 4,
Upscaler1: extra.Model.R_ESRGAN_4x_PLUS_ANIME_6B,
DecodeAfterResult: true,
}
fmt.Println("Len:", len(params.ImagesList))
fmt.Println("Upscaler:", params.Upscaler1)
resp, err := API.ExtraBatchImages(params)
pif(err)
fmt.Println("Result:", len(resp.Images))
fmt.Println("Decoded Result:", len(resp.DecodedImages))
if _, err := os.Stat("result"); os.IsNotExist(err) {
pif(os.Mkdir("result", 0777))
}
for index, imageData := range resp.DecodedImages {
fmt.Println("Saving Index:", index)
writeToFile(index, imageData)
}
}
// Panic If Error.
func pif(err error) {
if err != nil {
panic(err)
}
}
// Save image to disk.
func writeToFile(index int, data []byte) {
file, err := os.OpenFile(fmt.Sprintf("result/upscaled %v.png", index), os.O_CREATE|os.O_WRONLY, 0777)
pif(err)
defer file.Close()
file.Write(data)
}Expected Output
Get Generation Parameters in image
Need any image file named original.png
package main
import (
"fmt"
"github.com/Meonako/webui-api"
"github.com/Meonako/webui-api/utils"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
imageData, err := utils.Base64FromFile("original.png")
if err != nil {
panic(err)
}
result, err := API.PNGInfo(imageData)
if err != nil {
panic(err)
}
fmt.Println(result)
}Expected Output
When contains any generation parameters
Get Generation Progress
package main
import (
"fmt"
"os"
"text/tabwriter"
"github.com/Meonako/webui-api"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
progress, err := API.Progress()
if err != nil {
panic(err)
}
writer := tabwriter.NewWriter(os.Stdout, 1, 1, 3, ' ', 0)
fmt.Fprintf(writer, "Progress\t|\t%v\n", progress.GetProgress(""))
fmt.Fprintf(writer, "ETA Name\t|\t%v\n", progress.GetETA())
fmt.Fprintf(writer, "Current Steps\t|\t%v\n", progress.State.CurrentStep)
fmt.Fprintf(writer, "Target Sampling Steps\t|\t%v\n", progress.State.TargetSamplingSteps)
fmt.Fprintf(writer, "Job\t|\t%v\n", progress.State.Job)
fmt.Fprintf(writer, "Job No\t|\t%v\n", progress.State.JobNo)
fmt.Fprintf(writer, "Job Count\t|\t%v\n", progress.State.JobCount)
fmt.Fprintf(writer, "Interrupted\t|\t%v\n", progress.State.Interrupted)
fmt.Fprintf(writer, "Skipped\t|\t%v\n", progress.State.Skipped)
writer.Flush()
}Expected Output
Get caption of an image
Need any image file named original.png
package main
import (
"fmt"
"github.com/Meonako/webui-api"
"github.com/Meonako/webui-api/interrogate"
"github.com/Meonako/webui-api/utils"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
image, err := utils.Base64FromFile("original.png")
pif(err)
result, err := API.Interrogate(&api.Interrogate{
Image: image,
Model: interrogate.Model.DEEPBOORU,
})
pif(err)
fmt.Println(result)
}
func pif(err error) {
if err != nil {
panic(err)
}
}Expected Output
Stop Image Generation and return any result accumulated so far [ NOT return in API ]
package main
import (
"github.com/Meonako/webui-api"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
err := API.Interrupt()
if err != nil {
panic(err)
}
}Expected Output
None on your console or error if any
Progress stop on server console
Skip processing current image and continue processing
package main
import (
"github.com/Meonako/webui-api"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
err := API.Skip()
if err != nil {
panic(err)
}
}Expected Output
None on your console or error if any
Get Current Settings without Extension Settings
package main
import (
"fmt"
"github.com/Meonako/webui-api"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
opt, err := API.Options()
if err != nil {
panic(err)
}
fmt.Println(opt.AddModelHashToInfo)
fmt.Println(opt.AddModelNameToInfo)
}Expected Output
or value of these options
Get All Current Settings
package main
import (
"fmt"
"github.com/Meonako/webui-api"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
opt, err := API.OptionsWithExtensionOptions()
if err != nil {
panic(err)
}
if value, ok := opt["images_history_preload"]; ok {
fmt.Println(value)
}
}Expected Output
None or value of your settings if you have
Image Browserextension installed
Change Settings without Extension Settings
package main
import (
"github.com/Meonako/webui-api"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
err := API.SetOptions(&api.Options{
CleanTempDirAtStart: true,
})
if err != nil {
panic(err)
}
}Expected Output
None but you can try and change value in settings or checkout
config.jsonat Web Ui root dir
Change All Settings
package main
import (
"github.com/Meonako/webui-api"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
err := API.SetOptionsWithExtensionOptions(api.Opt{
"images_history_preload": true,
})
if err != nil {
panic(err)
}
}Expected Output
None but you can try and change value in settings or checkout
config.jsonat Web Ui root dir
Get Available Stable Diffusion Models
package main
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/Meonako/webui-api"
)
func main() {
API := api.New(api.Config{
UseDefault: true,
Default: api.DefaultValue(),
})
result, err := API.SDModels()
if err != nil {
panic(err)
}
writer := tabwriter.NewWriter(os.Stdout, 1, 1, 3, ' ', 0)
for _, value := range result {
fmt.Fprintf(writer, "Title\t|\t%v\n", value.Title)
fmt.Fprintf(writer, "Model Name\t|\t%v\n", value.ModelName)
fmt.Fprintf(writer, "Hash\t|\t%v\n", value.Hash)
fmt.Fprintf(writer, "Filename\t|\t%v\n", value.Filename)
fmt.Fprintf(writer, "Config\t|\t%v\n\n%v\n", value.Config, strings.Repeat("-", 45))
}
writer.Flush()
}Expected Output








or value of these options