118 lines
3.6 KiB
Go
118 lines
3.6 KiB
Go
|
package huoshan
|
|||
|
|
|||
|
import (
|
|||
|
"apigo.cc/apigo/gojs"
|
|||
|
"apigo.cc/apigo/gojs/dop251/goja"
|
|||
|
"errors"
|
|||
|
"github.com/ssgo/log"
|
|||
|
"github.com/ssgo/u"
|
|||
|
"github.com/volcengine/volc-sdk-golang/service/visual"
|
|||
|
"reflect"
|
|||
|
)
|
|||
|
|
|||
|
func RequireImage() gojs.Map {
|
|||
|
return map[string]interface{}{
|
|||
|
"text2image": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|||
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|||
|
return vm.ToValue(T2IXL(args.Str(0), args.Map(1), vm))
|
|||
|
},
|
|||
|
"image2image": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|||
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
|||
|
v := u.FinalValue(reflect.ValueOf(args.Arguments[0].Export()))
|
|||
|
switch v.Type().Kind() {
|
|||
|
case reflect.String:
|
|||
|
option := args.Map(2)
|
|||
|
if option == nil {
|
|||
|
option = map[string]interface{}{}
|
|||
|
}
|
|||
|
option["binary_data_base64"] = []string{
|
|||
|
args.Str(0),
|
|||
|
}
|
|||
|
return vm.ToValue(I2IXLbyUrl(nil, args.Str(1), option, vm))
|
|||
|
case reflect.Slice:
|
|||
|
vv := u.FinalValue(v.Index(0))
|
|||
|
if vv.Type().Kind() == reflect.Uint8 {
|
|||
|
option := args.Map(2)
|
|||
|
if option == nil {
|
|||
|
option = map[string]interface{}{}
|
|||
|
}
|
|||
|
option["binary_data_base64"] = []string{
|
|||
|
u.Base64(args.Bytes(0)),
|
|||
|
}
|
|||
|
return vm.ToValue(I2IXLbyUrl(nil, args.Str(1), option, vm))
|
|||
|
} else if vv.Type().Kind() == reflect.String {
|
|||
|
arr := make([]string, 0)
|
|||
|
u.Convert(args.Any(0), &arr)
|
|||
|
return vm.ToValue(I2IXLbyUrl(arr, args.Str(1), args.Map(2), vm))
|
|||
|
} else {
|
|||
|
log.DefaultLogger.Error("image type is slice but not supported", "type", args.Arguments[0].ExportType().Kind().String(), "elemType", vv.Type().Kind().String())
|
|||
|
panic(vm.NewGoError(errors.New(u.Red("image type is slice but not supported"))))
|
|||
|
return nil
|
|||
|
}
|
|||
|
}
|
|||
|
log.DefaultLogger.Error("image type not supported", "type", args.Arguments[0].ExportType().Kind().String())
|
|||
|
panic(vm.NewGoError(errors.New(u.Red("image type not supported"))))
|
|||
|
return nil
|
|||
|
},
|
|||
|
"readImage": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|||
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|||
|
if r, err := u.ReadFileBytes(args.Path(0)); err == nil {
|
|||
|
return vm.ToValue(r)
|
|||
|
} else {
|
|||
|
panic(vm.NewGoError(err))
|
|||
|
}
|
|||
|
},
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// T2IXL used t2i_xl_sft
|
|||
|
//
|
|||
|
// option 会覆盖已有参数,可传空,return_url不可更改
|
|||
|
func T2IXL(prompt string, option map[string]interface{}, vm *goja.Runtime) []string {
|
|||
|
vis := visual.NewInstance()
|
|||
|
vis.Client.SetAccessKey(conf.AKey)
|
|||
|
vis.Client.SetSecretKey(conf.SKey)
|
|||
|
data := map[string]interface{}{
|
|||
|
"req_key": "t2i_xl_sft",
|
|||
|
"prompt": prompt,
|
|||
|
}
|
|||
|
if option != nil {
|
|||
|
u.Convert(option, &data)
|
|||
|
}
|
|||
|
data["return_url"] = true
|
|||
|
resp, status, err := vis.Text2ImgXLSft(data)
|
|||
|
if status != 200 || err != nil {
|
|||
|
log.DefaultLogger.Error("request error", status, err)
|
|||
|
panic(vm.NewGoError(err))
|
|||
|
return []string{}
|
|||
|
}
|
|||
|
return resp.Data.ImageUrls
|
|||
|
}
|
|||
|
|
|||
|
// I2IXLbyUrl used t2i_xl_sft
|
|||
|
//
|
|||
|
// option 会覆盖已有参数,可传空,return_url不可更改
|
|||
|
func I2IXLbyUrl(imageUrls []string, prompt string, option map[string]interface{}, vm *goja.Runtime) []string {
|
|||
|
vis := visual.NewInstance()
|
|||
|
vis.Client.SetAccessKey(conf.AKey)
|
|||
|
vis.Client.SetSecretKey(conf.SKey)
|
|||
|
data := map[string]interface{}{
|
|||
|
"req_key": "i2i_xl_sft",
|
|||
|
"prompt": prompt,
|
|||
|
}
|
|||
|
if imageUrls != nil {
|
|||
|
data["image_urls"] = imageUrls
|
|||
|
}
|
|||
|
if option != nil {
|
|||
|
u.Convert(option, &data)
|
|||
|
}
|
|||
|
data["return_url"] = true
|
|||
|
resp, status, err := vis.Img2ImgXLSft(data)
|
|||
|
if status != 200 || err != nil {
|
|||
|
log.DefaultLogger.Error("request error", "status", status, "err", err, "resp", resp)
|
|||
|
panic(vm.NewGoError(err))
|
|||
|
return []string{}
|
|||
|
}
|
|||
|
return resp.Data.ImageUrls
|
|||
|
}
|