-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
158 lines (137 loc) · 4.77 KB
/
engine.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"context"
"fmt"
"github.com/quynhdang-vt/vt-pocketsphinx/cmu_sphinx"
"github.com/quynhdang-vt/vt-pocketsphinx/models"
utils "github.com/quynhdang-vt/vt-pocketsphinx/utils"
veritoneAPI "github.com/veritone/go-veritone-api"
"github.com/xlab/pocketsphinx-go/sphinx"
"log"
"sort"
"time"
"os"
)
func getAsset(payload models.Payload, recording *veritoneAPI.Recording) (chosenAsset *veritoneAPI.Asset) {
if payload.AssetID != "" {
for _, asset := range recording.Assets {
if asset.AssetID == payload.AssetID {
return &asset
}
}
} else {
// Use oldest asset as default
sort.SliceStable(recording.Assets, func(i, j int) bool {
return recording.Assets[i].CreatedDateTime < recording.Assets[j].CreatedDateTime
})
// Need to loop thru and looking for files that we can do,
// either audio/wav or audio/mpeg
for _, asset := range recording.Assets {
if utils.IsSupportedContentType(asset.ContentType) {
return &asset
}
}
}
return nil
}
/** as outlined in https://veritone-developer.atlassian.net/wiki/spaces/DOC/pages/15106076/Engine+Execution+Process */
func RunEngine(payload models.Payload, engineContext models.EngineContext,
dec *sphinx.Decoder,
veritoneAPIClient veritoneAPI.VeritoneAPIClient) (err error) {
log.Println("==== RunEngine started..")
// Set task to running
err = veritoneAPIClient.UpdateTaskStatus(context.Background(), payload.JobID, payload.TaskID, veritoneAPI.TaskStatusRunning, nil)
if err != nil {
// continue on failure
fmt.Printf("Failed to set task to running, ignored: %s\n", err)
}
defer func() {
// if there's error, we should update the task as failing
if err != nil {
_ = veritoneAPIClient.UpdateTaskStatus(context.Background(), payload.JobID, payload.TaskID, veritoneAPI.TaskStatusFailed, err)
}
log.Println("==== RunEngine exited..")
}()
// Get Recording Assets
log.Printf("Getting recording %v\n", payload.RecordingID)
recording, err := veritoneAPIClient.GetRecording(context.Background(), payload.RecordingID)
if err != nil {
return fmt.Errorf("error getting recording: %s", err)
}
if recording == nil {
return fmt.Errorf("recording not found for %s", payload.RecordingID)
}
if len(recording.Assets) == 0 {
return fmt.Errorf("recording has no assets")
}
chosenAsset := getAsset(payload, recording)
if chosenAsset == nil {
return fmt.Errorf("No suitable asset can be found..")
}
log.Printf("FOUND ASSET: %v, %v...\n", chosenAsset.AssetID, chosenAsset.ContentType)
prefix := "vt-pocketsphinx-" + payload.RecordingID + "-" + payload.JobID + "-" + payload.TaskID
origFilepath, err := utils.DownloadFile(chosenAsset.SignedURI, prefix)
if err != nil {
return fmt.Errorf("Failed to download recording with prefix == " + prefix)
}
// go ahead and convert the file -- for now
// TODO eventually we may have to "break" the file up if it is too long 5' is maxed for now
filepath, err := utils.ConvertFileToWave16KMono(origFilepath)
w := &cmu_sphinx.UnitOfWork{InfileName: &filepath, Dec: dec}
ttml, latticeJson, interesting_tidbits, err := w.Decode()
if err != nil {
return fmt.Errorf("Failed to process file %v, err=%v", filepath, err)
}
engineId := os.Getenv("ENGINE_ID")
nowTime := time.Now()
interesting_tidbits["engine_id"]=engineId
interesting_tidbits["now"]=nowTime.String()
// VLF file
log.Printf("Creating VLF asset..")
jsonAsset := veritoneAPI.Asset{
AssetType: "v-vlf",
ContentType: "application/json",
Metadata: map[string]interface{}{
"fileName": fmt.Sprintf("qd-pocketsphinx-%d.json", time.Now().Unix()),
"source": engineId,
"utime": time.Now().String(),
"size": len(latticeJson),
},
}
asset, _, err := veritoneAPIClient.CreateAsset(context.Background(), payload.RecordingID, bytes.NewReader(latticeJson), jsonAsset)
if err != nil {
return fmt.Errorf("Failed to create JSON Asset: %s", err)
}
log.Printf("Created VLF asset %v\n", asset)
// TTML file
log.Printf("Creating TTML asset..")
ttmlAsset := veritoneAPI.Asset{
AssetType: "transcript",
ContentType: "application/ttml+xml",
Metadata: map[string]interface{}{
"fileName": fmt.Sprintf("qd-pocketsphinx-%d.ttml", nowTime.Unix()),
"source": engineId,
"utime": nowTime.String(),
"size": len(ttml),
},
}
asset, _, err = veritoneAPIClient.CreateAsset(context.Background(), payload.RecordingID, bytes.NewReader(ttml), ttmlAsset)
if err != nil {
return fmt.Errorf("Failed to create TTML Asset: %s", err)
}
log.Printf("Created TTML asset %v\n", asset)
// now creating ttml
// Set task to complete
err = veritoneAPIClient.UpdateTaskStatus(
context.Background(),
payload.JobID,
payload.TaskID,
veritoneAPI.TaskStatusComplete,
interesting_tidbits,
)
if err != nil {
return fmt.Errorf("Failed to set task %d to complete: %s", err)
}
return err
}