Skip to content

Commit

Permalink
Add whisper to the examples directory (#4)
Browse files Browse the repository at this point in the history
* Add whisper to the examples directory

* Add whisper to the readme
  • Loading branch information
Marwan Sulaiman authored Mar 3, 2023
1 parent e332e66 commit 46c1e82
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions examples/whisper/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 46c1e82

Please sign in to comment.