Add base code

This commit is contained in:
2024-11-09 22:57:47 +01:00
parent 967f80d734
commit c7b40276cf
14 changed files with 975 additions and 0 deletions

68
internal/stt/whisper.go Normal file
View File

@@ -0,0 +1,68 @@
/*
Copyright © 2024 Matteo Schiff <matteo@underdesk.net>
*/
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
}