/
transcribe_diarization.go
103 lines (84 loc) · 3.07 KB
/
transcribe_diarization.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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package snippets
// [START speech_transcribe_diarization_gcs_beta]
import (
"context"
"fmt"
"io"
"strings"
speech "cloud.google.com/go/speech/apiv1"
"cloud.google.com/go/speech/apiv1/speechpb"
)
// transcribe_diarization_gcs_beta Transcribes a remote audio file using speaker diarization.
func transcribe_diarization_gcs_beta(w io.Writer, gcsUri string) error {
// Google Cloud Storage URI pointing to the audio content.
// gcsUri := "gs://bucket-name/path_to_audio_file"
ctx := context.Background()
client, err := speech.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewClient: %w", err)
}
defer client.Close()
diarizationConfig := &speechpb.SpeakerDiarizationConfig{
EnableSpeakerDiarization: true,
MinSpeakerCount: 2,
MaxSpeakerCount: 2,
}
recognitionConfig := &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: 8000,
LanguageCode: "en-US",
DiarizationConfig: diarizationConfig,
}
// Set the remote path for the audio file
audio := &speechpb.RecognitionAudio{
AudioSource: &speechpb.RecognitionAudio_Uri{Uri: gcsUri},
}
longRunningRecognizeRequest := &speechpb.LongRunningRecognizeRequest{
Config: recognitionConfig,
Audio: audio,
}
operation, err := client.LongRunningRecognize(ctx, longRunningRecognizeRequest)
if err != nil {
return fmt.Errorf("error running recognize %v", err)
}
response, err := operation.Wait(ctx)
if err != nil {
return err
}
// Speaker Tags are only included in the last result object, which has only one
// alternative.
alternative := response.Results[len(response.Results)-1].Alternatives[0]
wordInfo := alternative.GetWords()[0]
currentSpeakerTag := wordInfo.GetSpeakerTag()
var speakerWords strings.Builder
speakerWords.WriteString(fmt.Sprintf("Speaker %d: %s", wordInfo.GetSpeakerTag(), wordInfo.GetWord()))
// For each word, get all the words associated with one speaker, once the speaker changes,
// add a new line with the new speaker and their spoken words.
for i := 1; i < len(alternative.Words); i++ {
wordInfo := alternative.Words[i]
if currentSpeakerTag == wordInfo.GetSpeakerTag() {
speakerWords.WriteString(" ")
speakerWords.WriteString(wordInfo.GetWord())
} else {
speakerWords.WriteString(fmt.Sprintf("\nSpeaker %d: %s",
wordInfo.GetSpeakerTag(), wordInfo.GetWord()))
currentSpeakerTag = wordInfo.GetSpeakerTag()
}
}
fmt.Fprintf(w, speakerWords.String())
return nil
}
// [END speech_transcribe_diarization_gcs_beta]