Skip to content

Example

Meonako edited this page Dec 16, 2022 · 33 revisions

Txt2Img Example

package main

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

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

and you'll have file named "Image (0).png" in your root folder

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