Git Product home page Git Product logo

spensercai / sd-webui-go Goto Github PK

View Code? Open in Web Editor NEW
361.0 4.0 3.0 3.84 MB

This is a Go language version of the SDK based on stable-diffusion-webui. In your code, you can directly use the API interfaces of stable-diffusion-webui through object-oriented operations, instead of dealing with cumbersome JSON. Support extensions API !

License: GNU General Public License v3.0

Go 100.00%
ai aigc api golang sd-webui sdk stable-diffusion stable-diffusion-webui webui

sd-webui-go's Introduction

sd-webui-go

SD-WEBUI-GO

Golang SDK for stable-diffusion-webui's API

Among the contributors is AUTOMATIC1111, a little joke on my partπŸ˜‚.

Here,sincerely thank AUTOMATIC1111 for its great contribution to AIGC

license go release GoReportCard Discord Server

This is a Go language version of the SDK based on stable-diffusion-webui. In your code, you can directly use the API interfaces of stable-diffusion-webui through object-oriented operations, instead of dealing with cumbersome JSON.

Support extensions API !

You can check the support list for intersvc in this wiki page.

Usage

There are two methods to use the SDK.

  • Using 'intersvc', which provides a highly encapsulated API interface for 'sd-webui'. Using this method feels like directly using the Go language version of 'sd-webui'.

  • Using 'go-swagger', which still involves object-oriented operations but is slightly more complex than 'intersvc'.

Almost all interfaces are supported by the second method, while the first one is gradually being supported.

In fact, most of the interfaces can be used with 'intersvc', but it requires defining the Response. The API documentation of 'sd-webui' does not provide the content of the Response, so it needs to be defined manually. You can refer to the "Participating" section below for specific instructions.

go-swagger text2img

package main

import (
	"encoding/base64"
	"fmt"

	SdClient "github.com/SpenserCai/sd-webui-go"
	SdApiOperation "github.com/SpenserCai/sd-webui-go/stablediffusion/client/operations"
	SdApiModel "github.com/SpenserCai/sd-webui-go/stablediffusion/models"
)

func MustBeNil(err error) {
	if err != nil {
		panic(err)
	}
}

var URL = "127.0.0.1:7860"

func main() {
	sdClient := SdClient.NewStableDiffInterface(URL)

	rd := SdApiOperation.NewText2imgapiSdapiV1Txt2imgPostParams()
	rd.Body = &SdApiModel.StableDiffusionProcessingTxt2Img{
		Prompt:         "dog",
		NegativePrompt: "ugly",
		ScriptArgs:     []interface{}{},
	}

	resp, err := sdClient.Client.Operations.Text2imgapiSdapiV1Txt2imgPost(rd)
	MustBeNil(err)

	for i, s := range resp.Payload.Images {
		b, err := base64.StdEncoding.DecodeString(s)
		MustBeNil(err)
		err = os.WriteFile(fmt.Sprintf("%d.png", i), b, 0644)
		MustBeNil(err)
	}
}

intersvc deoldify

import (
	SdClient "github.com/SpenserCai/sd-webui-go"
	"github.com/SpenserCai/sd-webui-go/intersvc"
)

func main() {
	// Create a client
	sdClient := SdClient.NewStableDiffInterface("127.0.0.1:7860")

	var f_factor int64 = 20
	var artistic bool = false

	// Set Request
	deoldify_inter := &intersvc.DeoldifyImage{
		RequestItem: &intersvc.DeoldifyImageRequest{
			InputImage:   "https://media.discordapp.net/attachments/1138408545277190237/1138508881635577947i7krs1njekla1.jpg",
			RenderFactor: &f_factor,
			Artistic:     &artistic,
		},
	}


	deoldify_inter.Action(sdClient)
	if deoldify_inter.Error != nil {
		panic(deoldify_inter.Error)
	}

	response := deoldify_inter.GetResponse()
}

Full example code: intersvc_example

go-swagger deoldify

import (
	"encoding/base64"
	"os"
	
	SdClient "github.com/SpenserCai/sd-webui-go"
	intersvc "github.com/SpenserCai/sd-webui-go/intersvc"
	SdApiOperation "github.com/SpenserCai/sd-webui-go/stablediffusion/client/operations"
	SdApiModel "github.com/SpenserCai/sd-webui-go/stablediffusion/models"
)

type DeoldifyResponse struct {
	Image string `json:"image"`
}

func main() {
	var (
		err error
	)

	// init client
	sdClient := SdClient.NewStableDiffInterface("127.0.0.1:7860")

	var f_factor int64 = 20
	var artistic bool = false

	// set request data
	RequestData := SdApiOperation.NewDeoldifyImageDeoldifyImagePostParams()
	RequestData.Body = &SdApiModel.BodyDeoldifyImageDeoldifyImagePost{
		InputImage:   "https://media.discordapp.net/attachments/1138408545277190237/1138508881635577947/i7krs1njekla1.jpg",
		RenderFactor: &f_factor,
		Artistic:     &artistic,
	}

	// send request
	Response, err := sdClient.Client.Operations.DeoldifyImageDeoldifyImagePost(RequestData)
	if err != nil {
		panic(err)
	}

	// convert response
	deoldifyRep, err := intersvc.ConvertResponse(Response.Payload, &DeoldifyResponse{})
	if err != nil {
		panic(err)
	}

    response := deoldifyRep.(*DeoldifyResponse)
}

Full example code: go-swagger_example

Participating

Most of the code for intersvc has been generated using a code generator. However, due to the lack of response information in the API documentation for sd-webui, it needs to be manually written.

How to submit a PR

You need to fork the code of the dev branch, make the necessary code updates, create a branch named dev-[model filename] in your own repository, and then submit a pull request to the dev branch of this repository.

How to define the Response Model

In sd-webui-go/intersvc,you can see some file like ***_model.go. These files define the Response Model. You can refer to the following example to define the Response Model.

From

type DeoldifyImageResponse struct {
	
}

To

type DeoldifyImageResponse struct {
	Image string `json:"image"`
}

sd-webui-go's People

Contributors

automatic1111 avatar drgrib avatar spensercai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sd-webui-go's Issues

Instructions for anyone trying to do a simple txt2img request

I'm confused why you use DeoldifyImage as the first and only example for this repo.

I spent a couple hours trying to get this working for a simple and (imo) much more common txt2img request due to the choice of DeoldifyImage as the base example as well as relying on the intersvc package instead of basic idiomatic Go. For anyone looking for a simple txt2img example, here's one. For the repo owner, I recommend putting this in the README.

package main

import (
	"encoding/base64"
	"fmt"

	SdClient "github.com/SpenserCai/sd-webui-go"
	SdApiOperation "github.com/SpenserCai/sd-webui-go/stablediffusion/client/operations"
	SdApiModel "github.com/SpenserCai/sd-webui-go/stablediffusion/models"
)

func MustBeNil(err error) {
	if err != nil {
		panic(err)
	}
}

var URL = "127.0.0.1:7860"

func main() {
	sdClient := SdClient.NewStableDiffInterface(URL)

	rd := SdApiOperation.NewText2imgapiSdapiV1Txt2imgPostParams()
	rd.Body = &SdApiModel.StableDiffusionProcessingTxt2Img{
		Prompt:         "dog",
		NegativePrompt: "ugly",
		ScriptArgs:     []interface{}{},
	}

	resp, err := sdClient.Client.Operations.Text2imgapiSdapiV1Txt2imgPost(rd)
	MustBeNil(err)

	for i, s := range resp.Payload.Images {
		b, err := base64.StdEncoding.DecodeString(s)
		MustBeNil(err)
		err = os.WriteFile(fmt.Sprintf("%d.png", i), b, 0644)
		MustBeNil(err)
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.