This commit is contained in:
StarPro 2024-10-02 17:35:29 +08:00
parent 9ea57475f7
commit 5f7a2e2adb
7 changed files with 263 additions and 0 deletions

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
.*
!.gitignore
go.sum
env.yml
node_modules
package.json
*.bak.*
*.png
*.jpg
*.jpeg

35
go.mod Normal file
View File

@ -0,0 +1,35 @@
module Huoshan
go 1.18
require (
apigo.cc/apigo/gojs v0.1.1
github.com/ssgo/config v1.7.7
github.com/ssgo/log v1.7.7
github.com/ssgo/u v1.7.7
github.com/volcengine/volc-sdk-golang v1.0.178
)
require (
golang.org/x/net v0.29.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/ssgo/dao v0.1.5 // indirect
github.com/ssgo/db v1.7.9 // indirect
github.com/ssgo/httpclient v1.7.7 // indirect
github.com/ssgo/standard v1.7.7 // indirect
github.com/ssgo/tool v0.4.27 // indirect
golang.org/x/sys v0.25.0 // indirect
)

44
huoshan.go Normal file
View File

@ -0,0 +1,44 @@
package huoshan
import (
"apigo.cc/apigo/gojs"
_ "embed"
"github.com/ssgo/config"
"github.com/ssgo/log"
"github.com/ssgo/u"
)
//go:embed huoshan.ts
var huoshanTS string
type Conf struct {
AKey string
SKey string
}
var conf = Conf{}
var confAes = u.NewAes([]byte("?GQ$0K0GgLdO=f+~L68PLm$uhKr4'=tV"), []byte("VFs7@sK61cj^f?HZ"))
var keysIsSet = false
func SetSSKey(key, iv []byte) {
if !keysIsSet {
confAes = u.NewAes(key, iv)
keysIsSet = true
}
}
func init() {
obj := gojs.Map{
"image": RequireImage(),
}
config.LoadConfig("huoshan", &conf)
conf.AKey = confAes.DecryptUrlBase64ToString(conf.AKey)
conf.SKey = confAes.DecryptUrlBase64ToString(conf.SKey)
log.DefaultLogger.Info("Conf", "", conf)
gojs.Register("huoshan", gojs.Module{
Object: obj,
TsCode: huoshanTS,
Example: "",
})
}

10
huoshan.ts Normal file
View File

@ -0,0 +1,10 @@
export default {
image: {
text2image,
image2image,
readImage
}
}
function text2image(prompt:string, option?:Object):string {return ""}
function image2image(image:string[], prompt:string, option?:Object):string {return ""}
function readImage(path:string):string {return ""}

117
image.go Normal file
View File

@ -0,0 +1,117 @@
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
}

25
test.js Normal file
View File

@ -0,0 +1,25 @@
import {image} from "huoshan"
import u from "util"
function main() {
let a = image.image2image(u.base64(image.readImage("img_2.jpeg")), "在吃冰淇淋", {
style_reference_args: {
id_weight: 1.0
}
})
let b = image.image2image(image.readImage("img_2.jpeg"), "在吃冰淇淋", {
style_reference_args: {
id_weight: 1.0
}
})
let c = image.image2image([""], "在吃冰淇淋", {
style_reference_args: {
id_weight: 1.0
}
})
return {
a: a,
b: b,
c: c
}
}

22
textin_test.go Normal file
View File

@ -0,0 +1,22 @@
package huoshan
import (
"apigo.cc/apigo/gojs"
_ "apigo.cc/apigo/gojs/modules"
"fmt"
"github.com/ssgo/u"
"testing"
)
func TestExport(t *testing.T) {
gojs.ExportForDev()
}
func TestLLM(t *testing.T) {
r, err := gojs.RunFile("test.js")
if err != nil {
t.Fatal(err)
}
fmt.Println()
fmt.Println(u.JsonP(r))
}