-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio-example.go
73 lines (65 loc) · 1.81 KB
/
audio-example.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
package main
import (
"fmt"
"os"
"github.com/Kardbord/gopenai/audio"
"github.com/Kardbord/gopenai/authentication"
)
const OpenAITokenEnv = "OPENAI_API_KEY"
func init() {
key := os.Getenv(OpenAITokenEnv)
authentication.SetAPIKey(key)
}
const (
model = "whisper-1"
transcriptionFile = "transcription.m4a"
translationFile = "translation.m4a"
)
func main() {
{ // Transcription
fmt.Printf("Sending transcription request for file %s...\n", transcriptionFile)
r, err := audio.MakeTranscriptionRequest(&audio.TranscriptionRequest{
File: transcriptionFile,
Model: model,
Language: "en",
}, nil)
if err != nil {
fmt.Printf("Error with transcription request: %s\n", err)
} else {
fmt.Printf("Transcribed audio: %s\n", r.Text)
}
}
{ // Translation
fmt.Printf("Sending translation request for file %s...\n", translationFile)
r, err := audio.MakeTranslationRequest(&audio.TranslationRequest{
File: translationFile,
Model: model,
}, nil)
if err != nil {
fmt.Printf("Error with translation request: %s\n", err)
} else {
fmt.Printf("Translated audio: %s\n", r.Text)
}
}
{ // Speech
const s string = "The quick brown fox jumps over the lazy dog."
fmt.Printf("Sending speech creation request for \"%s\"\n", s)
resp, err := audio.MakeSpeechRequest(&audio.SpeechRequest{
Model: "tts-1",
Input: s,
Voice: audio.VoiceNova,
ResponseFormat: audio.SpeechFormatMp3,
}, nil)
if err != nil {
fmt.Printf("Error with speech creation request: %s\n", err)
}
if len(resp) == 0 {
fmt.Println("No TTS audio returned. :(")
} else {
err = os.WriteFile(fmt.Sprintf("speech-creation.%s", audio.SpeechFormatMp3), resp, 0644)
if err != nil {
fmt.Printf("Error writing %s to disk: %s\n", audio.SpeechFormatMp3, err)
}
}
}
}