forked from rhaseven7h/gmmmkvsubsextract
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWhisper-mac-coreml.sh
More file actions
350 lines (300 loc) · 8.32 KB
/
Copy pathWhisper-mac-coreml.sh
File metadata and controls
350 lines (300 loc) · 8.32 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env bash
set -euo pipefail
PREFIX_DIR="${PREFIX_DIR:-$HOME/.whispercpp-coreml}"
REPO_DIR="${REPO_DIR:-$PREFIX_DIR/whisper.cpp}"
BUILD_DIR="${BUILD_DIR:-$REPO_DIR/build}"
MODELS_DIR="${MODELS_DIR:-$REPO_DIR/models}"
VENV_DIR="${VENV_DIR:-$PREFIX_DIR/venv}"
MODEL_NAME_DEFAULT="${MODEL_NAME_DEFAULT:-base.en}"
MODEL_FILE_DEFAULT="${MODEL_FILE_DEFAULT:-$MODELS_DIR/ggml-${MODEL_NAME_DEFAULT}.bin}"
usage() {
cat <<'EOF'
Usage:
./Whisper-mac-coreml.sh install
./Whisper-mac-coreml.sh install-deps
./Whisper-mac-coreml.sh list-models
./Whisper-mac-coreml.sh download-model [model_name]
./Whisper-mac-coreml.sh generate-coreml [model_name]
./Whisper-mac-coreml.sh convert <input_media> <output_wav>
./Whisper-mac-coreml.sh transcribe <input_media_or_wav> [--model <ggml_model_path>] [--out <output_prefix>] [--lang <code>] [--srt] [--vtt] [--txt]
Notes:
- Installs whisper.cpp with Core ML support for Apple Silicon (ANE acceleration).
- Requires Xcode command-line tools and Python 3.11+ (Core ML dependencies).
- After generating a Core ML model, whisper.cpp will use it automatically for the encoder.
Environment variables:
PREFIX_DIR (default: ~/.whispercpp-coreml)
MODEL_NAME_DEFAULT (default: base.en)
EOF
}
list_models() {
cat <<'EOF'
Supported whisper.cpp GGML model names (common):
English-only (smaller / faster):
tiny.en
base.en
small.en
medium.en
Multilingual (auto-detect language):
tiny
base
small
medium
large-v1
large-v2
large-v3
large-v3-turbo
Tips:
- Use *.en models if you ONLY need English (faster / smaller).
- Use non-.en models for other languages or mixed language content.
- Download example:
./Whisper-mac-coreml.sh download-model medium
- CoreML encoder example (Apple Silicon speedup):
./Whisper-mac-coreml.sh generate-coreml medium
EOF
}
need_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "Missing required command: $1" >&2
exit 1
}
}
ensure_brew_deps() {
need_cmd brew
brew install -q git cmake ffmpeg pkg-config || true
}
ensure_python_deps() {
need_cmd python3
# Prefer using a python3.11 if available; otherwise use system python3
local PYTHON_CMD=""
if command -v python3.11 >/dev/null 2>&1; then
PYTHON_CMD=python3.11
elif command -v python3 >/dev/null 2>&1; then
PYTHON_CMD=python3
else
echo "Python3 not found. Please install Python 3.11+." >&2
exit 1
fi
echo "Using Python: $("$PYTHON_CMD" --version)"
# Create isolated venv for CoreML dependencies (avoids conflicts with other venvs)
if [ ! -d "$VENV_DIR" ]; then
echo "Creating isolated venv at $VENV_DIR..."
"$PYTHON_CMD" -m venv "$VENV_DIR"
fi
# Use venv Python for all installs
local VENV_PY="$VENV_DIR/bin/python"
echo "Installing CoreML dependencies into venv..."
"$VENV_PY" -m pip install --upgrade pip setuptools wheel
"$VENV_PY" -m pip install 'numpy<2' torch torchvision torchaudio
"$VENV_PY" -m pip install ane_transformers openai-whisper coremltools
}
install_whispercpp() {
if ! command -v brew >/dev/null 2>&1; then
echo "Homebrew not found. Install it first:" >&2
echo " https://brew.sh" >&2
exit 1
fi
ensure_brew_deps
ensure_python_deps
mkdir -p "$PREFIX_DIR"
if [ ! -d "$REPO_DIR/.git" ]; then
git clone https://github.com/ggml-org/whisper.cpp.git "$REPO_DIR"
else
git -C "$REPO_DIR" pull --ff-only
fi
# Build with Core ML support
cmake -S "$REPO_DIR" -B "$BUILD_DIR" -DWHISPER_COREML=1
cmake --build "$BUILD_DIR" -j --config Release
echo "Install complete (with Core ML support)"
echo "Binary: $BUILD_DIR/bin/whisper-cli"
echo "Tip: run '$BUILD_DIR/bin/whisper-cli -h' for all options"
echo ""
echo "Next steps:"
echo " ./Whisper-mac-coreml.sh download-model"
echo " ./Whisper-mac-coreml.sh generate-coreml"
}
download_model() {
local model_name="${1:-$MODEL_NAME_DEFAULT}"
if [ ! -d "$REPO_DIR" ]; then
echo "whisper.cpp not installed. Run: ./Whisper-mac-coreml.sh install" >&2
exit 1
fi
(cd "$REPO_DIR" && sh ./models/download-ggml-model.sh "$model_name")
echo "Model downloaded: $MODELS_DIR/ggml-${model_name}.bin"
}
generate_coreml_model() {
local model_name="${1:-$MODEL_NAME_DEFAULT}"
if [ ! -d "$REPO_DIR" ]; then
echo "whisper.cpp not installed. Run: ./Whisper-mac-coreml.sh install" >&2
exit 1
fi
if [ ! -f "$MODELS_DIR/ggml-${model_name}.bin" ]; then
echo "GGML model not found. Run: ./Whisper-mac-coreml.sh download-model $model_name" >&2
exit 1
fi
# Use the isolated venv Python (must have CoreML deps installed)
local VENV_PY="$VENV_DIR/bin/python"
if [ ! -x "$VENV_PY" ]; then
echo "Venv not found. Run: ./Whisper-mac-coreml.sh install-deps" >&2
exit 1
fi
echo "Generating Core ML model for $model_name using venv Python..."
(cd "$REPO_DIR/models" && "$VENV_PY" ./convert-whisper-to-coreml.py --model "$model_name" --encoder-only True)
# Compile the mlpackage to mlmodelc
local mlpackage="$MODELS_DIR/models/coreml-encoder-${model_name}.mlpackage"
local mlmodelc="$MODELS_DIR/ggml-${model_name}-encoder.mlmodelc"
if [ -d "$mlpackage" ]; then
echo "Compiling Core ML model..."
xcrun coremlcompiler compile "$mlpackage" "$MODELS_DIR/"
# Rename to expected path
if [ -d "$MODELS_DIR/coreml-encoder-${model_name}.mlmodelc" ]; then
rm -rf "$mlmodelc"
mv "$MODELS_DIR/coreml-encoder-${model_name}.mlmodelc" "$mlmodelc"
fi
fi
if [ -d "$mlmodelc" ]; then
echo "Core ML model generated: $mlmodelc"
else
echo "Warning: Core ML model not found at expected path: $mlmodelc" >&2
fi
}
convert_media() {
local in="${1:-}"
local out="${2:-}"
if [ -z "$in" ] || [ -z "$out" ]; then
echo "convert requires: <input_media> <output_wav>" >&2
exit 1
fi
need_cmd ffmpeg
ffmpeg -y -i "$in" -ar 16000 -ac 1 -c:a pcm_s16le "$out"
}
transcribe() {
local input="${1:-}"
shift || true
if [ -z "$input" ]; then
echo "transcribe requires: <input_media_or_wav>" >&2
exit 1
fi
if [ ! -x "$BUILD_DIR/bin/whisper-cli" ]; then
echo "whisper.cpp not built. Run: ./Whisper-mac-coreml.sh install" >&2
exit 1
fi
local model_file="$MODEL_FILE_DEFAULT"
local out_prefix=""
local lang_code=""
local want_srt=0
local want_vtt=0
local want_txt=0
while [ $# -gt 0 ]; do
case "$1" in
--model)
model_file="$2"
shift 2
;;
--out)
out_prefix="$2"
shift 2
;;
--lang)
lang_code="$2"
shift 2
;;
--srt)
want_srt=1
shift
;;
--vtt)
want_vtt=1
shift
;;
--txt)
want_txt=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown arg: $1" >&2
exit 1
;;
esac
done
if [ -z "$out_prefix" ]; then
out_prefix="${input%.*}"
fi
if [ ! -f "$model_file" ]; then
echo "Model file not found: $model_file" >&2
echo "Run: ./Whisper-mac-coreml.sh download-model $MODEL_NAME_DEFAULT" >&2
exit 1
fi
local wav_input="$input"
local tmp_wav=""
case "${input##*.}" in
wav|WAV)
;;
*)
tmp_wav="$(mktemp -t whispercpp.XXXXXX).wav"
convert_media "$input" "$tmp_wav"
wav_input="$tmp_wav"
;;
esac
local args=("-m" "$model_file" "-f" "$wav_input")
if [ -n "$lang_code" ]; then
args+=("-l" "$lang_code")
fi
if [ -n "$out_prefix" ]; then
out_dir="$(dirname "$out_prefix")"
mkdir -p "$out_dir"
args+=("-of" "$out_prefix")
fi
if [ "$want_txt" -eq 1 ]; then
args+=("-otxt")
fi
if [ "$want_srt" -eq 1 ]; then
args+=("-osrt")
fi
if [ "$want_vtt" -eq 1 ]; then
args+=("-ovtt")
fi
"$BUILD_DIR/bin/whisper-cli" "${args[@]}"
if [ -n "$tmp_wav" ] && [ -f "$tmp_wav" ]; then
rm -f "$tmp_wav"
fi
}
cmd="${1:-}"
case "$cmd" in
install)
install_whispercpp
;;
install-deps)
ensure_python_deps
;;
list-models)
list_models
;;
download-model)
shift || true
download_model "${1:-$MODEL_NAME_DEFAULT}"
;;
generate-coreml)
shift || true
generate_coreml_model "${1:-$MODEL_NAME_DEFAULT}"
;;
convert)
shift || true
convert_media "${1:-}" "${2:-}"
;;
transcribe)
shift || true
transcribe "$@"
;;
-h|--help|help|"")
usage
;;
*)
echo "Unknown command: $cmd" >&2
usage
exit 1
;;
esac