From 46c1e8229c7f719cd2769cda00cef85b89685c03 Mon Sep 17 00:00:00 2001 From: Marwan Sulaiman Date: Fri, 3 Mar 2023 17:21:20 -0500 Subject: [PATCH] Add whisper to the examples directory (#4) * Add whisper to the examples directory * Add whisper to the readme --- README.md | 1 + examples/whisper/main.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 examples/whisper/main.go diff --git a/README.md b/README.md index b862279..936e9ab 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Go client libraries for OpenAI APIs. Supported APIs: ✅ edits 🚧 images ✅ moderations +✅ whisper ``` Set your [API key](https://platform.openai.com/account/api-keys) diff --git a/examples/whisper/main.go b/examples/whisper/main.go new file mode 100644 index 0000000..47cf793 --- /dev/null +++ b/examples/whisper/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "context" + "log" + "os" + + "github.com/rakyll/openai-go" + "github.com/rakyll/openai-go/whisper" +) + +func main() { + sesh := openai.NewSession(os.Getenv("OPENAI_API_KEY")) + wc := whisper.NewClient(sesh, "") + filePath := os.Getenv("AUDIO_FILE_PATH") + if filePath == "" { + log.Fatal("must provide an AUDIO_FILE_PATH env var") + } + f, err := os.Open(filePath) + if err != nil { + log.Fatalf("error opening audio file: %v", err) + } + defer f.Close() + resp, err := wc.Transcribe(context.TODO(), &whisper.CreateCompletionParams{ + Language: "en", + Audio: f, + AudioFormat: "mp3", + }) + if err != nil { + log.Fatalf("error transcribing file: %v", err) + } + log.Println(resp.Text) +}