-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Asutorufa <[email protected]>
- Loading branch information
Showing
11 changed files
with
300 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"syscall/js" | ||
_ "unsafe" | ||
|
||
_ "github.com/syumai/workers" | ||
"github.com/syumai/workers/cloudflare" | ||
) | ||
|
||
type AI struct { | ||
instance js.Value | ||
} | ||
|
||
func NewAI() *AI { | ||
return &AI{ | ||
instance: cloudflare.GetBinding("AI"), | ||
} | ||
} | ||
|
||
func (a *AI) Translate(opts TranslateOptions) (string, error) { | ||
p := a.instance.Call("run", "@cf/meta/m2m100-1.2b", opts.toJS()) | ||
|
||
t, err := AwaitPromise(p) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return t.Get("translated_text").String(), nil | ||
} | ||
|
||
/* | ||
"@cf/meta/m2m100-1.2b", | ||
{ | ||
text: "I'll have an order of the moule frites", | ||
source_lang: "english", // defaults to english | ||
target_lang: "french", | ||
} | ||
Response | ||
{ | ||
"translated_text": "Je vais commander des moules frites" | ||
} | ||
*/ | ||
type TranslateOptions struct { | ||
Text string | ||
SourceLang string | ||
TargetLang string | ||
} | ||
|
||
func (opts *TranslateOptions) toJS() js.Value { | ||
if opts == nil { | ||
return js.Undefined() | ||
} | ||
obj := NewObject() | ||
if opts.Text != "" { | ||
obj.Set("text", opts.Text) | ||
} | ||
if opts.SourceLang != "" { | ||
obj.Set("source_lang", opts.SourceLang) | ||
} | ||
if opts.TargetLang != "" { | ||
obj.Set("target_lang", opts.TargetLang) | ||
} | ||
return obj | ||
} | ||
|
||
//go:linkname NewObject github.com/syumai/workers/internal/jsutil.NewObject | ||
func NewObject() js.Value | ||
|
||
//go:linkname AwaitPromise github.com/syumai/workers/internal/jsutil.AwaitPromise | ||
func AwaitPromise(promiseVal js.Value) (js.Value, error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"dependencies": { | ||
"wrangler": "^3.65.1" | ||
"wrangler": "^3.67.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
module github.com/Asutorufa/hujiang_dictionary | ||
|
||
go 1.21.3 | ||
|
||
toolchain go1.22.5 | ||
go 1.22.0 | ||
|
||
require ( | ||
github.com/PuerkitoBio/goquery v1.9.2 | ||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 | ||
github.com/syumai/tinyutil v0.3.2 | ||
github.com/syumai/workers v0.26.1 | ||
jaytaylor.com/html2text v0.0.0-20230321000545-74c2419ad056 | ||
) | ||
|
||
require ( | ||
github.com/andybalholm/cascadia v1.3.2 // indirect | ||
github.com/mattn/go-runewidth v0.0.9 // indirect | ||
github.com/olekukonko/tablewriter v0.0.5 // indirect | ||
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect | ||
golang.org/x/net v0.24.0 // indirect | ||
) |
Oops, something went wrong.