-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwhisper.go
More file actions
124 lines (103 loc) · 3.22 KB
/
whisper.go
File metadata and controls
124 lines (103 loc) · 3.22 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//go:build voice
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/mutablelogic/go-whisper/pkg/schema"
whisper "github.com/mutablelogic/go-whisper/pkg/whisper"
)
const voiceSupported = true
const whisperModelName = "ggml-small.bin"
const whisperModelURL = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.bin"
func getModelsDir() string {
return filepath.Join(cacheDir(), "models")
}
// ensureModel downloads the whisper model if not present
func ensureModel() (string, error) {
modelsDir := getModelsDir()
modelPath := filepath.Join(modelsDir, whisperModelName)
if _, err := os.Stat(modelPath); err == nil {
return modelPath, nil
}
if err := os.MkdirAll(modelsDir, 0755); err != nil {
return "", fmt.Errorf("failed to create models dir: %w", err)
}
fmt.Printf("Downloading whisper model %s...\n", whisperModelName)
resp, err := http.Get(whisperModelURL)
if err != nil {
return "", fmt.Errorf("failed to download model: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("failed to download model: HTTP %d", resp.StatusCode)
}
tmpPath := modelPath + ".tmp"
f, err := os.Create(tmpPath)
if err != nil {
return "", fmt.Errorf("failed to create model file: %w", err)
}
written, err := io.Copy(f, resp.Body)
f.Close()
if err != nil {
os.Remove(tmpPath)
return "", fmt.Errorf("failed to write model: %w", err)
}
if err := os.Rename(tmpPath, modelPath); err != nil {
os.Remove(tmpPath)
return "", fmt.Errorf("failed to rename model: %w", err)
}
fmt.Printf("Model downloaded: %s (%d MB)\n", whisperModelName, written/1024/1024)
return modelPath, nil
}
// transcribeAudio transcribes audio using native go-whisper
func transcribeAudio(config *Config, audioPath string) (string, error) {
modelsDir := getModelsDir()
// Ensure model exists
if _, err := ensureModel(); err != nil {
return "", fmt.Errorf("model setup failed: %w", err)
}
manager, err := whisper.New(modelsDir)
if err != nil {
return "", fmt.Errorf("failed to create whisper manager: %w", err)
}
defer manager.Close()
model := manager.GetModelById("ggml-small")
if model == nil {
return "", fmt.Errorf("model ggml-small not found in %s", modelsDir)
}
var result strings.Builder
err = manager.WithModel(model, func(task *whisper.Task) error {
if config.TranscriptionLang != "" {
if err := task.SetLanguage(config.TranscriptionLang); err != nil {
return fmt.Errorf("failed to set language: %w", err)
}
}
f, err := os.Open(audioPath)
if err != nil {
return fmt.Errorf("failed to open audio: %w", err)
}
defer f.Close()
return task.TranscribeReader(context.Background(), f, func(seg *schema.Segment) {
result.WriteString(seg.Text)
})
})
if err != nil {
return "", fmt.Errorf("transcription failed: %w", err)
}
return strings.TrimSpace(result.String()), nil
}
func doctorCheckWhisper() {
fmt.Print("whisper model..... ")
modelPath := filepath.Join(getModelsDir(), whisperModelName)
if _, err := os.Stat(modelPath); err == nil {
fmt.Printf("✅ %s\n", modelPath)
} else {
fmt.Println("⚠️ not downloaded (will auto-download on first voice message)")
fmt.Println(" Model: " + whisperModelName)
}
}