README ¶
stable-diffusion
pure go for stable-diffusion and support cross-platform.
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 avx/avx2/avx512 | not support |
linux | not support | support | not support |
darwin | not support | support (no 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, "the cat that wears shoes", 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
Successful Examples
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
- Variables
- type CSDCtx
- func (c *CSDCtx) Close()
- func (c *CSDCtx) StableDiffusionImageToImage(initImg []byte, prompt string, negativePrompt string, cfgScale float32, ...) ([]byte, error)
- func (c *CSDCtx) StableDiffusionLoadFromFile(path string, schedule Schedule)
- func (c *CSDCtx) StableDiffusionTextToImage(prompt string, negativePrompt string, cfgScale float32, width int, height int, ...) ([]byte, error)
- type CStableDiffusion
- type OutputsImageType
- type RNGType
- type SDLogLevel
- type SampleMethod
- type Schedule
- type StableDiffusionModel
- type StableDiffusionOptions
Constants ¶
const ( DEBUG SDLogLevel = "DEBUG" INFO = "INFO" WARN = "WARN" ERROR = "ERROR" )
const ( EULER_A SampleMethod = "EULER_A" EULER = "EULER" HEUN = "HEUN" DPM2 = "DPM2" DPMPP2S_A = "DPMPP2S_A" DPMPP2M = "DPMPP2M" DPMPP2Mv2 = "DPMPP2Mv2" LCM = "LCM" N_SAMPLE_METHODS = "N_SAMPLE_METHODS" )
const ( DEFAULT Schedule = "DEFAULT" DISCRETE = "DISCRETE" KARRAS = "KARRAS" N_SCHEDULES = "N_SCHEDULES" )
Variables ¶
var DefaultStableDiffusionOptions = StableDiffusionOptions{ Threads: -1, VaeDecodeOnly: true, FreeParamsImmediately: true, LoraModelDir: "", 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) StableDiffusionImageToImage ¶
func (*CSDCtx) StableDiffusionLoadFromFile ¶
type CStableDiffusion ¶
type CStableDiffusion struct {
// contains filtered or unexported fields
}
func NewCStableDiffusion ¶
func NewCStableDiffusion(libraryPath string) (*CStableDiffusion, error)
func (*CStableDiffusion) NewStableDiffusionCtx ¶
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 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 (*StableDiffusionModel) LoadFromFile ¶
func (sd *StableDiffusionModel) LoadFromFile(path string) error
type StableDiffusionOptions ¶
type StableDiffusionOptions struct { Threads int VaeDecodeOnly bool FreeParamsImmediately bool LoraModelDir string RngType RNGType Schedule Schedule NegativePrompt string CfgScale float32 Width int Height int SampleMethod SampleMethod SampleSteps int Strength float32 Seed int64 OutputsImageType OutputsImageType }