Skip to content

Commit 405b741

Browse files
plutovfacebook-github-bot
authored andcommitted
Feat/dictation endpoint (#32)
Summary: Based on this request - #31 1. Add new endpoint method 2. Add test .mp3 file for integration tests Pull Request resolved: #32 Reviewed By: yuzh174 Differential Revision: D70269191 Pulled By: patapizza fbshipit-source-id: 1877424010543174a26cdf8976dba779df79abad
1 parent 9e51dcd commit 405b741

File tree

6 files changed

+96
-14
lines changed

6 files changed

+96
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
[![Go Reference](https://pkg.go.dev/badge/github.com/wit-ai/wit-go)](https://pkg.go.dev/github.com/wit-ai/wit-go)
44

5-
*This repository is community-maintained. We gladly accept pull requests. Please see the [Wit HTTP Reference](https://wit.ai/docs/http/latest) for all supported endpoints.*
5+
_This repository is community-maintained. We gladly accept pull requests. Please
6+
see the [Wit HTTP Reference](https://wit.ai/docs/http/latest) for all supported
7+
endpoints._
68

79
Go client for [wit.ai](https://wit.ai/) HTTP API.
810

@@ -20,26 +22,26 @@ go get -u github.com/wit-ai/wit-go/v2
2022
package main
2123

2224
import (
23-
"os"
24-
"fmt"
25+
"os"
26+
"fmt"
2527

26-
witai "github.com/wit-ai/wit-go/v2"
28+
witai "github.com/wit-ai/wit-go/v2"
2729
)
2830

2931
func main() {
30-
client := witai.NewClient(os.Getenv("WIT_AI_TOKEN"))
31-
// Use client.SetHTTPClient() to set custom http.Client
32+
client := witai.NewClient(os.Getenv("WIT_AI_TOKEN"))
33+
// Use client.SetHTTPClient() to set custom http.Client
3234

33-
msg, _ := client.Parse(&witai.MessageRequest{
34-
Query: "hello",
35-
})
36-
fmt.Printf("%v", msg)
35+
msg, _ := client.Parse(&witai.MessageRequest{
36+
Query: "hello",
37+
})
38+
fmt.Printf("%v", msg)
3739
}
3840
```
3941

4042
## Testing
4143

42-
Unit tests are executed by Github Actions, but Integration tests have to be executed manually by providing a valid token via `WITAI_INTEGRATION_TOKEN` env var.
44+
Unit tests are executed by Github Actions.
4345

4446
### Unit tests
4547

@@ -49,21 +51,25 @@ go test -race -v
4951

5052
### Integration tests
5153

52-
Integration tests are connecting to real Wit.ai API, so you need to provide a valid token:
54+
Integration tests have to be executed manually by providing a valid token via
55+
`WITAI_INTEGRATION_TOKEN` env var.
56+
57+
Integration tests are connecting to real Wit.ai API, so you need to provide a
58+
valid token:
5359

5460
```
5561
WITAI_INTEGRATION_TOKEN={SERVER_ACCESS_TOKEN} go test -v -tags=integration
5662
```
5763

5864
## License
5965

60-
The license for wit-go can be found in LICENSE file in the root directory of this source tree.
66+
The license for wit-go can be found in LICENSE file in the root directory of
67+
this source tree.
6168

6269
## Terms of Use
6370

6471
Our terms of use can be found at https://opensource.facebook.com/legal/terms.
6572

66-
6773
## Privacy Policy
6874

6975
Our privacy policy can be found at

dictation.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2+
3+
package witai
4+
5+
import (
6+
"encoding/json"
7+
"io"
8+
"net/http"
9+
)
10+
11+
type DictationRequest struct {
12+
File io.Reader
13+
ContentType string
14+
}
15+
16+
type DictationToken struct {
17+
End int `json:"end"`
18+
Start int `json:"start"`
19+
Token string `json:"token"`
20+
}
21+
22+
type DictationSpeech struct {
23+
Confidence float64 `json:"confidence"`
24+
Tokens []DictationToken `json:"tokens"`
25+
}
26+
27+
type DictationResponse struct {
28+
Speech DictationSpeech `json:"speech"`
29+
Text string `json:"text"`
30+
Type string `json:"type"`
31+
}
32+
33+
// Dictation - Returns the text transcription from an audio file or stream.
34+
func (c *Client) Dictation(req DictationRequest) (*DictationResponse, error) {
35+
resp, err := c.request(http.MethodPost, "/dictation", req.ContentType, req.File)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
defer resp.Close()
41+
42+
var msgResp *DictationResponse
43+
decoder := json.NewDecoder(resp)
44+
for {
45+
err := decoder.Decode(&msgResp)
46+
if err != nil {
47+
break
48+
}
49+
}
50+
return msgResp, err
51+
}

integration_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,27 @@ func getIntegrationClient() *Client {
146146
})
147147
return c
148148
}
149+
150+
func TestIntegrationDictation(t *testing.T) {
151+
c := getIntegrationClient()
152+
153+
f, err := os.Open("./testdata/test.mp3")
154+
if err != nil {
155+
t.Fatalf("unable to open test file, err: %v", err)
156+
}
157+
defer f.Close()
158+
159+
req := DictationRequest{
160+
File: f,
161+
ContentType: "audio/mpeg3",
162+
}
163+
164+
resp, err := c.Dictation(req)
165+
if err != nil {
166+
t.Fatalf("unexpected err, got %v", err)
167+
}
168+
169+
if resp.Text != "Hi this is a test file" {
170+
t.Fatalf("unexpected transcript, got %s", resp.Text)
171+
}
172+
}

testdata/.DS_Store

6 KB
Binary file not shown.

testdata/test.mp3

55.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)