sd

package module
v0.0.2-beta1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2023 License: MIT Imports: 10 Imported by: 6

README

stable-diffusion

pure go for stable-diffusion and support cross-platform.

Go Reference

sd.go is a wrapper around stable-diffusion-cpp, which is an adaption of ggml.cpp.

Installation

go get github.com/seasonjs/stable-diffusion

Compatibility

See deps folder for dylib compatibility, push request is welcome.

platform x32 x64 arm
windows not support support not support
linux not support not support not support
darwin not support support (not test) support

Usage

This stable-diffusion golang library provide two api Predict and ImagePredict. You must instantiate each separately and set the correct options for the corresponding model.

You don't need to download stable-diffusion dynamic library.

package main

import (
	"os"
	"github.com/seasonjs/stable-diffusion"
)

func main() {
	options := sd.DefaultStableDiffusionOptions
	//It's important to set image size,different model support different size
	options.Width = 256
	options.Height = 256
	model, err := sd.NewStableDiffusionAutoModel(options)
	if err != nil {
		panic(err)
	}
	defer model.Close()
	err = model.LoadFromFile("./data/miniSD-ggml-model-q5_0.bin")
	if err != nil {
		panic(err)
	}
	file, err := os.Create("./data/output2.png")
	defer file.Close()
	if err != nil {
		panic(err)
	}
	err = model.Predict("A lovely cat, high quality", file)
	if err != nil {
		panic(err)
	}
}

package main
import (
	"os"
	"github.com/seasonjs/stable-diffusion"
)

func main()  {
	options := sd.DefaultStableDiffusionOptions
	//It's important to set image size,different model support different size
	options.Width = 256
	options.Height = 256
	//ImagePredict model need set VaeDecodeOnly false
	options.VaeDecodeOnly = false
	model, err := sd.NewStableDiffusionAutoModel(options)
	if err != nil {
		panic(err)
	}
	defer model.Close()
	err = model.LoadFromFile("./data/miniSD-ggml-model-q5_0.bin")
	if err != nil {
		panic(err)
	}
	inFile, err := os.Open("./data/output2.png")
	defer inFile.Close()
	if err != nil {
		panic(err)
	}
	outfile, err := os.Create("./data/output3.png")
	if err != nil {
		panic(err)
	}
	defer outfile.Close()
	err = model.ImagePredict(inFile, "pink cat", outfile)
}

If NewStableDiffusionAutoModel can't automatic loading of dynamic library, please use NewStableDiffusionModel method load manually.

package main

import (
	"fmt"
	"runtime"
	"github.com/seasonjs/stable-diffusion"
)

func getLibrary() string {
	switch runtime.GOOS {
	case "darwin":
		return "./deps/darwin/libstable-diffusion_arm64.dylib"
	case "linux":
		return "./deps/linux/libstable-diffusion.so"
	case "windows":
		return "./deps/windows/stable-diffusion_avx2_x64.dll"
	default:
		panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
	}
}
func main()  {
	options := sd.DefaultStableDiffusionOptions
	//It's important to set image size,different model support different size
	options.Width = 256
	options.Height = 256
	model, err := sd.NewStableDiffusionModel(getLibrary(),options)
	print(model,err)
	//.... the usage is same as `NewStableDiffusionAutoModel`. they both return `StableDiffusionModel` struct

}

Packaging

To ship a working program that includes this AI, you will need to include the following files:

  • libstable-diffusion.dylib / libstable-diffusion.so / stable-diffusion.dll (buildin)
  • the model file
  • the tokenizer file (buildin)

Low level API

This package also provide low level Api which is same as stable-diffusion-cpp. See detail at stable-diffusion-doc.

Thanks

License

Copyright (c) seasonjs. All rights reserved. Licensed under the MIT License. See License.txt in the project root for license information.

Documentation

Index

Constants

View Source
const (
	DEBUG SDLogLevel = "DEBUG"
	INFO             = "INFO"
	WARN             = "WARN"
	ERROR            = "ERROR"
)
View Source
const (
	EULER_A          SampleMethod = "EULER_A"
	EULER                         = "EULER"
	HEUN                          = "HEUN"
	DPM2                          = "DPM2"
	DPMPP2S_A                     = "DPMPP2S_A"
	DPMPP2M                       = "DPMPP2M"
	DPMPP2Mv2                     = "DPMPP2Mv2"
	N_SAMPLE_METHODS              = "N_SAMPLE_METHODS"
)
View Source
const (
	DEFAULT     Schedule = "DEFAULT"
	DISCRETE             = "DISCRETE"
	KARRAS               = "KARRAS"
	N_SCHEDULES          = "N_SCHEDULES"
)

Variables

View Source
var DefaultStableDiffusionOptions = StableDiffusionOptions{
	Threads:               -1,
	VaeDecodeOnly:         true,
	FreeParamsImmediately: true,
	RngType:               CUDA_RNG,

	Schedule: DEFAULT,

	NegativePrompt:   "out of frame, lowers, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature",
	CfgScale:         7.0,
	Width:            500,
	Height:           500,
	SampleMethod:     EULER_A,
	SampleSteps:      20,
	Strength:         0.4,
	Seed:             42,
	OutputsImageType: PNG,
}

Functions

This section is empty.

Types

type CSDCtx

type CSDCtx struct {
	// contains filtered or unexported fields
}

func (*CSDCtx) Close

func (c *CSDCtx) Close()

func (*CSDCtx) StableDiffusionImageToImage

func (c *CSDCtx) StableDiffusionImageToImage(initImg []byte, prompt string, negativePrompt string, cfgScale float32, width int, height int, sampleMethod SampleMethod, sampleSteps int, strength float32, seed int64) []byte

func (*CSDCtx) StableDiffusionLoadFromFile

func (c *CSDCtx) StableDiffusionLoadFromFile(path string, schedule Schedule)

func (*CSDCtx) StableDiffusionTextToImage

func (c *CSDCtx) StableDiffusionTextToImage(prompt string, negativePrompt string, cfgScale float32, width int, height int, sampleMethod SampleMethod, sampleSteps int, seed int64) []byte

type CStableDiffusion

type CStableDiffusion struct {
	// contains filtered or unexported fields
}

func NewCStableDiffusion

func NewCStableDiffusion(libraryPath string) (*CStableDiffusion, error)

func (*CStableDiffusion) NewStableDiffusionCtx

func (cSD *CStableDiffusion) NewStableDiffusionCtx(nThreads int, vaeDecodeOnly bool, freeParamsImmediately bool, rngType RNGType) *CSDCtx

func (*CStableDiffusion) StableDiffusionGetSystemInfo

func (cSD *CStableDiffusion) StableDiffusionGetSystemInfo() string

func (*CStableDiffusion) StableDiffusionSetLogLevel

func (cSD *CStableDiffusion) StableDiffusionSetLogLevel(level SDLogLevel)

type OutputsImageType

type OutputsImageType string
const (
	PNG  OutputsImageType = "PNG"
	JPEG                  = "JPEG"
)

type RNGType

type RNGType string
const (
	STD_DEFAULT_RNG RNGType = "STD_DEFAULT_RNG"
	CUDA_RNG                = "CUDA_RNG"
)

type SDLogLevel

type SDLogLevel string

type SampleMethod

type SampleMethod string

type Schedule

type Schedule string

type StableDiffusionModel

type StableDiffusionModel struct {
	// contains filtered or unexported fields
}

func NewStableDiffusionAutoModel

func NewStableDiffusionAutoModel(options StableDiffusionOptions) (*StableDiffusionModel, error)

func NewStableDiffusionModel

func NewStableDiffusionModel(dylibPath string, options StableDiffusionOptions) (*StableDiffusionModel, error)

func (*StableDiffusionModel) Close

func (sd *StableDiffusionModel) Close() error

func (*StableDiffusionModel) ImagePredict

func (sd *StableDiffusionModel) ImagePredict(reader io.Reader, prompt string, writer io.Writer) error

func (*StableDiffusionModel) LoadFromFile

func (sd *StableDiffusionModel) LoadFromFile(path string) error

func (*StableDiffusionModel) Predict

func (sd *StableDiffusionModel) Predict(prompt string, writer io.Writer) error

type StableDiffusionOptions

type StableDiffusionOptions struct {
	Threads               int
	VaeDecodeOnly         bool
	FreeParamsImmediately bool
	RngType               RNGType

	Schedule Schedule

	NegativePrompt   string
	CfgScale         float32
	Width            int
	Height           int
	SampleMethod     SampleMethod
	SampleSteps      int
	Strength         float32
	Seed             int64
	OutputsImageType OutputsImageType
}

Directories

Path Synopsis
deps

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL