Skip to content

Golang SDK for Stable Diffusion API (Txt2Img/Img2Img/ControlNet/VAE)

License

Notifications You must be signed in to change notification settings

omniinfer/golang-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Omniinfer Golang SDK

This SDK is based on the official API documentation.

Join our discord server for help

Installation

go get -u github.com/omniinfer/golang-sdk

Quick Start

Get api key refer to https://docs.omniinfer.io/get-started

package main

import (
	"context"
	"fmt"
	"github.com/omniinfer/golang-sdk/request"
	"github.com/omniinfer/golang-sdk/types"
	"time"
)

func main() {
	// get your api key refer to https://docs.omniinfer.io/get-started/
	const apiKey = "your-key"
	client, err := request.NewOmniClient(apiKey)
	if err != nil {
		fmt.Printf("new omniclient failed, %v\n", err)
		return
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute*3)
	defer cancel()
	txt2ImgReq := types.NewTxt2ImgRequest("a dog flying in the sky", "", "AnythingV5_v5PrtRE.safetensors")
	res, err := client.SyncTxt2img(ctx, txt2ImgReq,
		request.WithSaveImage("out", 0777, func(taskId string, fileIndex int, fileName string) string {
			return "test_txt2img_sync.png"
		}))
	if err != nil {
		fmt.Printf("generate image failed, %v\n", err)
		return
	}
	for _, s3Url := range res.Data.Imgs {
		fmt.Printf("generate image url: %v\n", s3Url)
	}
}

Examples

Txt2Img with LoRA

example_txt2img_with_lora

package main

import (
	"context"
	"fmt"
	"github.com/omniinfer/golang-sdk/request"
	"github.com/omniinfer/golang-sdk/types"
	"time"
)

func main() {
	// get your api key refer to https://docs.omniinfer.io/get-started/
	const apiKey = "your-key"
	client, err := request.NewOmniClient(apiKey)
	if err != nil {
		fmt.Printf("new omniclient failed, %v\n", err)
		return
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute*3)
	defer cancel()
	modelList, err := client.Models(ctx)
	if err != nil {
		fmt.Printf("get models list failed, %v\n", err)
		return
	}
	// Anything V5/Ink, https://civitai.com/models/9409/or-anything-v5ink
	modelName := modelList.FilterCivitaiVersionId(90854).SdName
	// Detail Tweaker LoRA, https://civitai.com/models/58390/detail-tweaker-lora-lora
	loraName := modelList.FilterCivitaiVersionId(62833).SdName
	txt2ImgReq := types.NewTxt2ImgRequest(fmt.Sprintf("a dog flying in the sky, <lora:%s:%d>", loraName, 1), "", modelName)
	res, err := client.SyncTxt2img(ctx, txt2ImgReq,
		request.WithSaveImage("out", 0777, func(taskId string, fileIndex int, fileName string) string {
			return "test_txt2img_sync.png"
		}))
	if err != nil {
		fmt.Printf("generate image failed, %v\n", err)
		return
	}
	for _, s3Url := range res.Data.Imgs {
		fmt.Printf("generate image url: %v\n", s3Url)
	}
}

Model Search

package main

import (
	"context"
	"fmt"
	"github.com/omniinfer/golang-sdk/request"
	"github.com/omniinfer/golang-sdk/types"
	"time"
)

func main() {
	// get your api key refer to https://docs.omniinfer.io/get-started/
	const apiKey = "your-key"
	client, err := request.NewOmniClient(apiKey)
	if err != nil {
		fmt.Printf("new omniclient failed, %v\n", err)
		return
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute*3)
	defer cancel()
	// get all models
	modelList, err := client.Models(ctx)
	if err != nil {
		fmt.Printf("get model list failed, %v\n", err)
		return
	}
	// top 10 checkpoint
	modelList = modelList.FilterType(types.Checkpoint).TopN(10, func(m *types.Model) float32 {
		return m.CivitaiRating
	})
	for _, m := range modelList {
		fmt.Println(m.Name)
	}
}

ControlNet QRCode

package main

import (
	"context"
	"fmt"
	"github.com/omniinfer/golang-sdk/request"
	"github.com/omniinfer/golang-sdk/types"
	"github.com/omniinfer/golang-sdk/util"
	"time"
)

func main() {
	// get your api key refer to https://docs.omniinfer.io/get-started/
	const apiKey = "your-key"
	client, err := request.NewOmniClient(apiKey)
	if err != nil {
		fmt.Printf("new omniclient failed, %v\n", err)
		return
	}
	initImage := "./example/qrcode/qrcode.png"
	initImageBase64, err := util.ReadImageToBase64(initImage)
	if err != nil {
		fmt.Printf("read image failed, %v\n", err)
		return
	}
	txt2ImgReq := &types.Txt2ImgRequest{
		Prompt:      "a beautify butterfly in the colorful flowers, best quality, best details, masterpiece",
		ModelName:   "AnythingV5_v5PrtRE.safetensors",
		SamplerName: types.DPMPPMKarras,
		BatchSize:   1,
		NIter:       1,
		Steps:       30,
		CfgScale:    7,
		Height:      512,
		Width:       512,
		Seed:        -1,
		ControlNetUnits: []*types.ControlNetUnit{
			{
				Model:         "control_v1p_sd15_qrcode_monster_v2",
				Weight:        2.0,
				Module:        types.None,
				InputImage:    initImageBase64,
				ControlMode:   types.Balanced,
				ResizeMode:    types.JustResize,
				GuidanceStart: 0,
				GuidanceEnd:   1,
			},
		},
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
	defer cancel()
	res, err := client.SyncTxt2img(ctx, txt2ImgReq)
	if err != nil {
		fmt.Printf("read image failed, %v\n", err)
		return
	}
	for _, s3Url := range res.Data.Imgs {
		fmt.Printf("generate image url: %v\n", s3Url)
	}
}

Testing

OMNI_API_KEY=<your-key> go test ./...

About

Golang SDK for Stable Diffusion API (Txt2Img/Img2Img/ControlNet/VAE)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages