From 5f7a2e2adbeb59a9baf27dd6f3fb152d6e6b01ea Mon Sep 17 00:00:00 2001 From: StarPro Date: Wed, 2 Oct 2024 17:35:29 +0800 Subject: [PATCH] first --- .gitignore | 10 +++++ go.mod | 35 +++++++++++++++ huoshan.go | 44 +++++++++++++++++++ huoshan.ts | 10 +++++ image.go | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ test.js | 25 +++++++++++ textin_test.go | 22 ++++++++++ 7 files changed, 263 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 huoshan.go create mode 100644 huoshan.ts create mode 100644 image.go create mode 100644 test.js create mode 100644 textin_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8bb5480 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.* +!.gitignore +go.sum +env.yml +node_modules +package.json +*.bak.* +*.png +*.jpg +*.jpeg \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c331bb5 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/huoshan.go b/huoshan.go new file mode 100644 index 0000000..d180d5c --- /dev/null +++ b/huoshan.go @@ -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: "", + }) +} diff --git a/huoshan.ts b/huoshan.ts new file mode 100644 index 0000000..9a52c95 --- /dev/null +++ b/huoshan.ts @@ -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 ""} diff --git a/image.go b/image.go new file mode 100644 index 0000000..29adfdc --- /dev/null +++ b/image.go @@ -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 +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..cf90038 --- /dev/null +++ b/test.js @@ -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 + } +} \ No newline at end of file diff --git a/textin_test.go b/textin_test.go new file mode 100644 index 0000000..031f644 --- /dev/null +++ b/textin_test.go @@ -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)) +}