/* Copyright © 2024 Matteo Schiff */ package stt import ( "context" "fmt" "os" "github.com/sashabaranov/go-openai" ) type WhisperTranscriver struct { client *openai.Client useCache bool } func NewWhisperTranscriver(client *openai.Client) *WhisperTranscriver { return &WhisperTranscriver{ client: client, useCache: true, } } func (wt *WhisperTranscriver) cacheFileName(inputAudio string) string { return inputAudio + ".transcribed.txt" } func (wt *WhisperTranscriver) loadCache(inputAudio string) (string, error) { content, err := os.ReadFile(inputAudio) return string(content), err } func (wt *WhisperTranscriver) saveCache(inputAudio string, content string) error { err := os.WriteFile(inputAudio, []byte(content), 0666) return err } func (wt *WhisperTranscriver) Transcribe(inputAudio string, outFile string) (string, error) { cache, err := wt.loadCache(outFile) if wt.useCache && err == nil { return cache, nil } resp, err := wt.client.CreateTranscription( context.Background(), openai.AudioRequest{ Model: "whisper-large-q5_0", FilePath: inputAudio, Language: "en", }, ) if err != nil { fmt.Printf("Transcription error: %v\n", err) return "", err } _ = wt.saveCache(outFile, resp.Text) return resp.Text, nil }