This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscript.go
271 lines (214 loc) · 7.56 KB
/
transcript.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package assemblyai
import (
"context"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
"github.com/cenkalti/backoff"
"github.com/google/go-querystring/query"
)
const (
TranscriptStatusQueued TranscriptStatus = "queued"
TranscriptStatusProcessing TranscriptStatus = "processing"
TranscriptStatusCompleted TranscriptStatus = "completed"
TranscriptStatusError TranscriptStatus = "error"
)
const (
// The best model optimized for accuracy.
SpeechModelBest SpeechModel = "best"
// A lightweight, lower cost model for a wide range of languages.
SpeechModelNano SpeechModel = "nano"
// Conformer-2 is a heavy-duty model optimized for accuracy.
SpeechModelConformer2 SpeechModel = "conformer-2"
)
// TranscriptService groups the operations related to transcribing audio.
type TranscriptService struct {
client *Client
}
// SubmitFromURL submits an audio file for transcription without waiting for it
// to finish.
//
// https://www.assemblyai.com/docs/API%20reference/transcript#create-a-transcript
func (s *TranscriptService) SubmitFromURL(ctx context.Context, audioURL string, opts *TranscriptOptionalParams) (Transcript, error) {
var transcript Transcript
params := TranscriptParams{
AudioURL: String(audioURL),
}
if opts != nil {
params.TranscriptOptionalParams = *opts
}
req, err := s.client.newJSONRequest(ctx, "POST", "/v2/transcript", params)
if err != nil {
return Transcript{}, err
}
if err := s.client.do(req, &transcript); err != nil {
return Transcript{}, err
}
return transcript, nil
}
// SubmitFromReader submits audio for transcription without waiting for it to
// finish.
func (s *TranscriptService) SubmitFromReader(ctx context.Context, reader io.Reader, params *TranscriptOptionalParams) (Transcript, error) {
u, err := s.client.Upload(ctx, reader)
if err != nil {
return Transcript{}, err
}
return s.SubmitFromURL(ctx, u, params)
}
// Delete permanently deletes a transcript.
//
// https://www.assemblyai.com/docs/API%20reference/listing_and_deleting#deleting-transcripts-from-the-api
func (s *TranscriptService) Delete(ctx context.Context, transcriptID string) (Transcript, error) {
req, err := s.client.newJSONRequest(ctx, "DELETE", fmt.Sprint("/v2/transcript/", transcriptID), nil)
if err != nil {
return Transcript{}, err
}
var transcript Transcript
if err := s.client.do(req, &transcript); err != nil {
return Transcript{}, err
}
return transcript, nil
}
// Get returns a transcript.
//
// https://www.assemblyai.com/docs/API%20reference/transcript
func (s *TranscriptService) Get(ctx context.Context, transcriptID string) (Transcript, error) {
req, err := s.client.newJSONRequest(ctx, "GET", fmt.Sprint("/v2/transcript/", transcriptID), nil)
if err != nil {
return Transcript{}, err
}
var transcript Transcript
if err := s.client.do(req, &transcript); err != nil {
return Transcript{}, err
}
return transcript, nil
}
// GetSentences returns the sentences for a transcript.
func (s *TranscriptService) GetSentences(ctx context.Context, transcriptID string) (SentencesResponse, error) {
req, err := s.client.newJSONRequest(ctx, "GET", fmt.Sprint("/v2/transcript/", transcriptID, "/sentences"), nil)
if err != nil {
return SentencesResponse{}, err
}
var results SentencesResponse
if err := s.client.do(req, &results); err != nil {
return SentencesResponse{}, err
}
return results, nil
}
// GetParagraphs returns the paragraphs for a transcript.
func (s *TranscriptService) GetParagraphs(ctx context.Context, transcriptID string) (ParagraphsResponse, error) {
req, err := s.client.newJSONRequest(ctx, "GET", fmt.Sprint("/v2/transcript/", transcriptID, "/paragraphs"), nil)
if err != nil {
return ParagraphsResponse{}, err
}
var results ParagraphsResponse
if err := s.client.do(req, &results); err != nil {
return ParagraphsResponse{}, err
}
return results, nil
}
// GetRedactedAudio returns the redacted audio for a transcript.
//
// https://www.assemblyai.com/docs/Models/pii_redaction#create-a-redacted-audio-file
func (s *TranscriptService) GetRedactedAudio(ctx context.Context, transcriptID string) (RedactedAudioResponse, error) {
req, err := s.client.newJSONRequest(ctx, "GET", fmt.Sprint("/v2/transcript/", transcriptID, "/redacted-audio"), nil)
if err != nil {
return RedactedAudioResponse{}, err
}
var audio RedactedAudioResponse
if err := s.client.do(req, &audio); err != nil {
return RedactedAudioResponse{}, err
}
return audio, nil
}
type TranscriptGetSubtitlesOptions struct {
CharsPerCaption int64 `json:"chars_per_caption"`
}
func (s *TranscriptService) GetSubtitles(ctx context.Context, transcriptID string, format SubtitleFormat, opts *TranscriptGetSubtitlesOptions) ([]byte, error) {
req, err := s.client.newRequest(ctx, "GET", fmt.Sprintf("/v2/transcript/%s/%s", transcriptID, format), nil)
if err != nil {
return nil, err
}
if opts != nil {
values := make(url.Values)
values.Set("chars_per_caption", strconv.FormatInt(opts.CharsPerCaption, 10))
req.URL.RawQuery = values.Encode()
}
var res []byte
if err := s.client.do(req, &res); err != nil {
return nil, err
}
return res, nil
}
// List returns a collection of transcripts based on a filter.
//
// https://www.assemblyai.com/docs/API%20reference/listing_and_deleting#listing-historical-transcripts
func (s *TranscriptService) List(ctx context.Context, options ListTranscriptParams) (TranscriptList, error) {
req, err := s.client.newJSONRequest(ctx, "GET", "/v2/transcript", options)
if err != nil {
return TranscriptList{}, err
}
vs, err := query.Values(options)
if err != nil {
return TranscriptList{}, err
}
req.URL.RawQuery = vs.Encode()
var results TranscriptList
if err := s.client.do(req, &results); err != nil {
return TranscriptList{}, err
}
return results, nil
}
// Wait returns once a transcript has completed or failed.
func (s *TranscriptService) Wait(ctx context.Context, transcriptID string) (Transcript, error) {
b := backoff.NewExponentialBackOff()
b.InitialInterval = 3 * time.Second
ticker := backoff.NewTicker(b)
for {
select {
case <-ticker.C:
ts, err := s.Get(ctx, transcriptID)
if err != nil {
return ts, err
}
if ts.Status == "completed" || ts.Status == "error" {
return ts, err
}
case <-ctx.Done():
return Transcript{}, ctx.Err()
}
}
}
// TranscribeFromURL submits a URL to an audio file for transcription and waits for it to finish.
func (s *TranscriptService) TranscribeFromURL(ctx context.Context, audioURL string, opts *TranscriptOptionalParams) (Transcript, error) {
transcript, err := s.SubmitFromURL(ctx, audioURL, opts)
if err != nil {
return transcript, err
}
return s.Wait(ctx, *transcript.ID)
}
// TranscribeFromReader submits audio for transcription and waits for it to finish.
func (s *TranscriptService) TranscribeFromReader(ctx context.Context, reader io.Reader, opts *TranscriptOptionalParams) (Transcript, error) {
transcript, err := s.SubmitFromReader(ctx, reader, opts)
if err != nil {
return transcript, err
}
return s.Wait(ctx, *transcript.ID)
}
// WordSearch searches a transcript for any occurrences of the provided words.
func (s *TranscriptService) WordSearch(ctx context.Context, transcriptID string, words []string) (WordSearchResponse, error) {
values := url.Values{}
values.Set("words", strings.Join(words, ","))
req, err := s.client.newJSONRequest(ctx, "GET", fmt.Sprint("/v2/transcript/", transcriptID, "/word-search?", values.Encode()), nil)
if err != nil {
return WordSearchResponse{}, err
}
var results WordSearchResponse
if err := s.client.do(req, &results); err != nil {
return WordSearchResponse{}, err
}
return results, nil
}