-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrecordfile2text.go
58 lines (49 loc) · 1.51 KB
/
recordfile2text.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
package main
import (
"context"
"fmt"
"os"
"os/user"
"path/filepath"
"github.com/devinyf/dashscopego"
"github.com/devinyf/dashscopego/paraformer"
)
func main() {
model := paraformer.ParaformerV2
token := os.Getenv("DASHSCOPE_API_KEY")
if token == "" {
panic("token is empty")
}
cli := dashscopego.NewTongyiClient(model, token)
usr, err := user.Current()
if err != nil {
panic(err)
}
voiceFile := filepath.Join(usr.HomeDir, "Desktop", "hello_world_female2.wav")
filePath := "file://" + voiceFile
req := ¶former.AsyncTaskRequest{
Model: paraformer.ParaformerV2,
Input: paraformer.AsyncInput{
// 官方示例中使用的远程文件.
// FileURLs: []string{"https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world_female2.wav"},
// 本地文件.
FileURLs: []string{filePath},
DisfluencyRemovalEnabled: true,
LanguageHints: []string{"zh", "en"},
},
Download: true, // 是否下载异步任务结果.
}
resp, err := cli.CreateVoiceFileToTextGeneration(context.TODO(), req)
if err != nil {
panic(err)
}
// 如果不需要下载异步任务的结果,仅获取异步任务的 task_id 后自行轮询结果.
fmt.Println("taskInfo: ", resp.AsyncTaskResp) //nolint:all
// 当 request 中设置了 Download = true 时, 等待语音识别结果输出.
fmt.Println("等待语音识别结果输出...") //nolint:all
for _, v := range resp.FileResults {
for _, v2 := range v.Transcripts {
fmt.Println(v2.Text) //nolint:all
}
}
}