Skip to content

Example

Meonako edited this page Dec 20, 2022 · 33 revisions

Txt2Img Example

package main

import (
    "fmt"
    "image/png"
    "os"
    "text/tabwriter"
    
    "github.com/Meonako/webui-api"
)

func main() {
    API := api.New(api.Config{
        UseDefault: true,
    })

    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

image

File named "Image (0).png"

Img2Img Example

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,
    })

    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

image

File named "result (0).png"

Extra Single

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,
    })

    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)
}

PNG Info

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,
    })

    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 image

Progress Example

package main

import (
    "fmt"
    "os"
    "text/tabwriter"

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

func main() {
    API := api.New(api.Config{
        UseDefault: true,
    })

    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()
}

Expect Output

image

Get Available Models (sd-models)

package main

import (
    "fmt"
    "os"
    "strings"
    "text/tabwriter"

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

func main() {
    API := api.New(api.Config{
        UseDefault: true,
    })

    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

image

Clone this wiki locally