-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
297 additions
and
24 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
File renamed without changes.
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,56 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
|
||
"github.com/devinyf/dashscopego" | ||
"github.com/devinyf/dashscopego/paraformer" | ||
) | ||
|
||
func main() { | ||
model := paraformer.ParaformerV1 | ||
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.ParaformerV1, | ||
Input: paraformer.AsyncInput{ | ||
// 官方示例中使用的远程文件. | ||
// FileURLs: []string{"https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world_female2.wav"}, | ||
// 本地文件. | ||
FileURLs: []string{filePath}, | ||
}, | ||
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 | ||
} | ||
} | ||
} |
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
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
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
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,111 @@ | ||
package paraformer | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
httpclient "github.com/devinyf/dashscopego/httpclient" | ||
) | ||
|
||
// | ||
|
||
func AsyncVoiceFileRecognitionTask(ctx context.Context, request *AsyncTaskRequest, cli httpclient.IHttpClient, token string) (*AsyncTaskResponse, error) { | ||
tokenHeader := httpclient.WithTokenHeaderOption(token) | ||
header := httpclient.HeaderMap{ | ||
"X-DashScope-Async": "enable", | ||
"Content-Type": "application/json", | ||
} | ||
|
||
if request.HasUploadOss { | ||
header["X-DashScope-OssResourceResolve"] = "enable" | ||
} | ||
|
||
contentHeader := httpclient.WithHeader(header) | ||
resp := AsyncTaskResponse{} | ||
err := cli.Post(ctx, ParaformerAsyncURL, request, &resp, tokenHeader, contentHeader) | ||
if err != nil { | ||
return &resp, err | ||
} | ||
|
||
return &resp, nil | ||
} | ||
|
||
type VoiceFileResponse struct { | ||
AsyncTaskResp *AsyncTaskResponse | ||
FileResults []*FileResult | ||
} | ||
|
||
func VoiceFileToTextGeneration(ctx context.Context, req *AsyncTaskRequest, cli httpclient.IHttpClient, token string) (*VoiceFileResponse, error) { | ||
resp := &VoiceFileResponse{} | ||
|
||
var resultList []*FileResult | ||
|
||
tokenHeader := httpclient.WithTokenHeaderOption(token) | ||
contentHeader := httpclient.WithHeader(httpclient.HeaderMap{ | ||
"Accept": "application/json", | ||
}) | ||
|
||
taskResp, err := AsyncVoiceFileRecognitionTask(ctx, req, cli, token) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp.AsyncTaskResp = taskResp | ||
|
||
if req.Download { | ||
taskReq := TaskResultRequest{TaskID: taskResp.Output.TaskID} | ||
|
||
taskStatusReap := &AsyncTaskResponse{} | ||
firstQuery := true | ||
for firstQuery || | ||
taskStatusReap.Output.TaskStatus == "PENDING" || | ||
taskStatusReap.Output.TaskStatus == "RUNNING" { | ||
firstQuery = false | ||
log.Println("TaskStatus: ", taskStatusReap.Output.TaskStatus) | ||
taskStatusReap, err = CheckTaskStatus(ctx, &taskReq, cli, tokenHeader, contentHeader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
time.Sleep(1 * time.Second) | ||
} | ||
|
||
// use taskID download file and read json content. | ||
for _, resultInfo := range taskStatusReap.Output.Results { | ||
result, err := downloadJsonfile(ctx, resultInfo.TranscriptionURL, cli) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resultList = append(resultList, result) | ||
resp.FileResults = resultList | ||
} | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
//nolint:lll | ||
func CheckTaskStatus(ctx context.Context, req *TaskResultRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*AsyncTaskResponse, error) { | ||
resp := AsyncTaskResponse{} | ||
err := httpcli.Get(ctx, TaskURL(req.TaskID), nil, &resp, options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp, nil | ||
} | ||
|
||
func downloadJsonfile(ctx context.Context, url string, httpcli httpclient.IHttpClient) (*FileResult, error) { | ||
contentHeader := httpclient.WithHeader(httpclient.HeaderMap{ | ||
"Accept": "application/json", | ||
}) | ||
|
||
resp := FileResult{} | ||
err := httpcli.Get(ctx, url, nil, &resp, contentHeader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp, nil | ||
} |
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
Oops, something went wrong.