-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (53 loc) · 1.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
tensorflow "github.com/galeone/tensorflow/tensorflow/go"
)
func loadModel(modelPath string) (*tensorflow.SavedModel, error){
model, err := tensorflow.LoadSavedModel(modelPath, []string{"serve"}, nil)
if err != nil {
return nil, fmt.Errorf("error loading model: %v", err)
}
return model, nil
}
func translate(model *tensorflow.SavedModel, inputText string) (string, error) {
// Create a tensor from the input text
inputTensor, err := tensorflow.NewTensor(inputText)
if err != nil {
return "", fmt.Errorf("error creating input tensor: %v", err)
}
// Run the model
output, err := model.Session.Run(
map[tensorflow.Output]*tensorflow.Tensor{
model.Graph.Operation("input_tensor_name").Output(0): inputTensor,
},
[]tensorflow.Output{
model.Graph.Operation("output_tensor_name").Output(0),
},
nil,
)
if err != nil {
return "", fmt.Errorf("error running model: %v", err)
}
// Convert the output tensor to a string
translatedText := output[0].Value().(string)
return translatedText, nil
}
func main (){
modelPath := "" // our model path here
model, err := loadModel(modelPath)
if err != nil {
fmt.Println(err)
return
}
defer model.Session.Close()
// Translate a sample text
inputText := "Hello, world!"
translatedText, err := translate(model, inputText)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Input text:", inputText)
fmt.Println("Translated text:", translatedText)
}