-
Notifications
You must be signed in to change notification settings - Fork 12
Example
Text-To-Image
package main
import (
"fmt"
"os"
"github.com/Meonako/webui-api"
"github.com/Meonako/webui-api/sampler"
"github.com/Meonako/webui-api/upscaler"
)
func main() {
API := api.New(api.Config{
Default: &api.Default{
Steps: 20,
Sampler: sampler.DPM_PLUS_PLUS_2M_KARRAS,
Width: 384,
Height: 512,
CFGScale: 7.0,
},
})
response, err := API.Text2Image(&api.Txt2Image{
Prompt: "long hair, skinny, narrow waist, gothic lolita, twintails",
NegativePrompt: api.BuildPrompt(
"(worst quality, low quality:1.4)",
"simple background, white background",
),
BatchSize: 2,
EnableHR: true,
DenoisingStrength: 0.5,
HRScale: 2,
HRUpscaler: upscaler.R_ESRGAN_4x_PLUS_ANIME_6B,
})
pif(err)
for index := range response.Images {
decoded, err := response.DecodeImage(index)
pif(err)
file, err := os.Create(fmt.Sprintf("result %v.png", index))
pif(err)
_, err = file.Write(decoded)
pif(err)
}
}
func pif(err error) {
if err != nil {
panic(err)
}
}Expected Output
File named "result 0.png" and "result 1.png"
Image-To-Image
Need any image file named original.png
package main
import (
"fmt"
"os"
"github.com/Meonako/webui-api"
"github.com/Meonako/webui-api/sampler"
"github.com/Meonako/webui-api/utils"
)
func main() {
API := api.New(api.Config{
BaseURL: "https://universe-cooperation-rankings-disk.trycloudflare.com",
Default: &api.Default{
Steps: 20,
Sampler: sampler.DPM_PLUS_PLUS_2M_KARRAS,
Width: 384,
Height: 512,
CFGScale: 7.0,
},
})
imageData, err := utils.Base64FromFiles("result 0.png", "result 1.png")
pif(err)
response, err := API.Image2Image(&api.Img2Img{
InitImages: imageData,
Prompt: "long hair, skinny, narrow waist, medium breasts, gothic lolita",
NegativePrompt: "(worst quality, low quality:1.4), simple background, white background",
SamplerIndex: sampler.EULER_A,
Width: 768,
Height: 1024,
DenoisingStrength: 0.75,
BatchSize: 2,
})
pif(err)
for index := range response.Images {
decoded, err := response.DecodeImage(index)
pif(err)
file, err := os.Create(fmt.Sprintf("result %v.png", index))
pif(err)
_, err = file.Write(decoded)
pif(err)
}
}
func pif(err error) {
if err != nil {
panic(err)
}
}Expected Output
File named "result 0.png" and "result 1.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