lebenoa / webui-api

GO API for AUTOMATIC1111's SD Web UI.
MIT License
31 stars 13 forks source link

Why api need to be designed as private #13

Closed huangxi1020 closed 4 months ago

huangxi1020 commented 4 months ago

i want to init once

lebenoa commented 4 months ago

You have a lot of way to do that but I think GO has the "no cycle import" rule iirc so you can access API after you call New instead

var (
    httpClient = &http.Client{}
        // Export api instance
    API        = &api{
        Config: setDefault(),
    }
)

func New(newConfig ...Config) *api {
    // Will store every time this func is called in `API`
    API = &api{
        Config: setDefault(newConfig...),
    }
    return API
}
huangxi1020 commented 4 months ago

sry I carefully read what you said, but I still don't know what to do

huangxi1020 commented 4 months ago

this is my global config

var (
    Settings *config.ServerConfig

    Lg *zap.Logger

    Redis *redis.Client

    HttpClient *http.Client

    // The problem is here because the api is private
    SdApi *api.api
)

Then assign a value to him

func InitSDApi() {
    global.SdApi = api.New(api.Config{
        BaseURL: http://Endpoint,
    })
}
lebenoa commented 4 months ago

Sorry for the late reply but you can access it from the library as such

package main

import (
    "fmt"
    "os"

    "github.com/Meonako/webui-api"
    "github.com/Meonako/webui-api/sampler"
    "github.com/Meonako/webui-api/upscaler"
)

func main() {
    // You don't actually need the value
    api.New(api.Config{
        Default: &api.Default{
            Steps:    20,
            Sampler:  sampler.DPM_PLUS_PLUS_2M_KARRAS,
            Width:    384,
            Height:   512,
            CFGScale: 7.0,
        },
    })

    // Instead you can access `api` instance as such
    response, err := api.API.Text2Image(&api.Txt2Image{
        Prompt: "long hair, skinny, narrow waist, gothic lolita, twintails",
        NegativePrompt: api.BuildPrompt(
            "(worst quality, low quality:1.4)",
            "simple background, white background",
        ),
        BatchSize:         2,
        EnableHR:          true,
        DenoisingStrength: 0.5,
        HRScale:           2,
        HRUpscaler:        upscaler.R_ESRGAN_4x_PLUS_ANIME_6B,
    })
}

If you don't like it this way, please make a fork for yourself since I don't do GO (and programming in general) anymore

huangxi1020 commented 4 months ago

thinks for your help, I have decided to write a version of the code based on your project to adapt to our project. Please forgive me for not being able to provide PR for this project