-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_manager.cpp
More file actions
936 lines (778 loc) · 32.6 KB
/
model_manager.cpp
File metadata and controls
936 lines (778 loc) · 32.6 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
#include "model_manager.h"
#include <iostream>
#include <fstream>
#include <filesystem>
#include <system_error>
#include <cstdlib>
#include <thread>
#include <chrono>
#include <set>
#ifdef __APPLE__
#include <sys/stat.h>
#include <unistd.h>
#endif
ModelManager::ModelManager() {
// Use global models directory if installed system-wide
const char* home = getenv("HOME");
if (home && std::filesystem::exists("/usr/local/bin/recognize")) {
models_dir_ = std::string(home) + "/.recognize/models";
} else {
models_dir_ = "models";
}
init_model_registry();
ensure_models_directory();
}
void ModelManager::init_model_registry() {
// English-only models (recommended for most users)
models_["tiny.en"] = {
"tiny.en",
"Tiny English model (39 MB) - Fastest processing, lower accuracy",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.en.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.en-encoder.mlmodelc.zip",
"ggml-tiny.en.bin",
"ggml-tiny.en-encoder.mlmodelc",
39,
false
};
models_["base.en"] = {
"base.en",
"Base English model (148 MB) - Good balance of speed and accuracy",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en-encoder.mlmodelc.zip",
"ggml-base.en.bin",
"ggml-base.en-encoder.mlmodelc",
148,
false
};
models_["small.en"] = {
"small.en",
"Small English model (488 MB) - Higher accuracy than base",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.en.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.en-encoder.mlmodelc.zip",
"ggml-small.en.bin",
"ggml-small.en-encoder.mlmodelc",
488,
false
};
models_["medium.en"] = {
"medium.en",
"Medium English model (1.5 GB) - Very high accuracy, slower",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium.en.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium.en-encoder.mlmodelc.zip",
"ggml-medium.en.bin",
"ggml-medium.en-encoder.mlmodelc",
1540,
false
};
models_["large"] = {
"large",
"Large English model (3.1 GB) - Highest accuracy, slowest",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-encoder.mlmodelc.zip",
"ggml-large-v3.bin",
"ggml-large-v3-encoder.mlmodelc",
3100,
false
};
// Multilingual models
models_["tiny"] = {
"tiny",
"Tiny multilingual model (39 MB) - 99 languages, lower accuracy",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny-encoder.mlmodelc.zip",
"ggml-tiny.bin",
"ggml-tiny-encoder.mlmodelc",
39,
true
};
models_["base"] = {
"base",
"Base multilingual model (148 MB) - 99 languages, good balance",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base-encoder.mlmodelc.zip",
"ggml-base.bin",
"ggml-base-encoder.mlmodelc",
148,
true
};
models_["small"] = {
"small",
"Small multilingual model (488 MB) - 99 languages, higher accuracy",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small-encoder.mlmodelc.zip",
"ggml-small.bin",
"ggml-small-encoder.mlmodelc",
488,
true
};
models_["medium"] = {
"medium",
"Medium multilingual model (1.5 GB) - 99 languages, very high accuracy",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium-encoder.mlmodelc.zip",
"ggml-medium.bin",
"ggml-medium-encoder.mlmodelc",
1540,
true
};
models_["large-v3"] = {
"large-v3",
"Large multilingual model (3.1 GB) - 99 languages, highest accuracy",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-encoder.mlmodelc.zip",
"ggml-large-v3.bin",
"ggml-large-v3-encoder.mlmodelc",
3100,
true
};
models_["large-v3-turbo"] = {
"large-v3-turbo",
"Large v3 Turbo (1.5 GB) - 99 languages, ~40% faster than large-v3",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-turbo.bin",
"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-turbo-encoder.mlmodelc.zip",
"ggml-large-v3-turbo.bin",
"ggml-large-v3-turbo-encoder.mlmodelc",
1500,
true
};
}
void ModelManager::ensure_models_directory() {
std::filesystem::create_directories(models_dir_);
}
void ModelManager::set_models_directory(const std::string& models_dir) {
models_dir_ = models_dir;
ensure_models_directory();
}
std::string ModelManager::get_models_directory() const {
return models_dir_;
}
bool ModelManager::model_exists(const std::string& model_name) {
if (models_.find(model_name) == models_.end()) {
return false;
}
std::string path = get_model_path(model_name);
return std::filesystem::exists(path);
}
bool ModelManager::coreml_model_exists(const std::string& model_name) {
if (models_.find(model_name) == models_.end()) {
return false;
}
std::string path = get_coreml_model_path(model_name);
return std::filesystem::exists(path);
}
std::string ModelManager::get_model_path(const std::string& model_name) {
if (models_.find(model_name) == models_.end()) {
return "";
}
return models_dir_ + "/" + models_[model_name].filename;
}
std::string ModelManager::get_coreml_model_path(const std::string& model_name) {
if (models_.find(model_name) == models_.end()) {
return "";
}
return models_dir_ + "/" + models_[model_name].coreml_filename;
}
void ModelManager::list_available_models() {
std::cout << "\n🤖 Available Whisper Models:\n\n";
std::cout << "📱 English-only models (recommended for English speech):\n";
for (const auto& [name, info] : models_) {
if (!info.multilingual) {
std::string status = model_exists(name) ? "✅ Downloaded" : "⬇️ Available";
std::cout << " " << name << " - " << info.description << " [" << status << "]\n";
}
}
std::cout << "\n🌍 Multilingual models (99 languages):\n";
for (const auto& [name, info] : models_) {
if (info.multilingual) {
std::string status = model_exists(name) ? "✅ Downloaded" : "⬇️ Available";
std::cout << " " << name << " - " << info.description << " [" << status << "]\n";
}
}
std::cout << "\n💡 Recommendation: Start with 'base.en' for English speech (good speed/accuracy balance)\n";
}
std::vector<std::string> ModelManager::get_model_names() {
std::vector<std::string> names;
for (const auto& [name, info] : models_) {
names.push_back(name);
}
return names;
}
std::vector<std::string> ModelManager::get_downloaded_models() {
std::vector<std::string> downloaded;
for (const auto& [name, info] : models_) {
if (model_exists(name)) {
downloaded.push_back(name);
}
}
return downloaded;
}
void ModelManager::list_downloaded_models() {
auto downloaded = get_downloaded_models();
if (downloaded.empty()) {
std::cout << "\n📁 No models downloaded yet.\n";
std::cout << "💡 Run 'recognize --list-models' to see available models for download.\n\n";
return;
}
std::cout << "\n📁 Downloaded Models:\n\n";
size_t total_size = 0;
for (const auto& name : downloaded) {
ModelInfo info = get_model_info(name);
std::string model_path = get_model_path(name);
std::string coreml_path = get_coreml_model_path(name);
// Calculate actual file sizes
size_t model_size = 0;
size_t coreml_size = 0;
if (std::filesystem::exists(model_path)) {
model_size = std::filesystem::file_size(model_path);
}
if (coreml_model_exists(name)) {
// For CoreML models, we need to calculate directory size
if (std::filesystem::exists(coreml_path) && std::filesystem::is_directory(coreml_path)) {
for (const auto& entry : std::filesystem::recursive_directory_iterator(coreml_path)) {
if (entry.is_regular_file()) {
coreml_size += entry.file_size();
}
}
}
}
total_size += model_size + coreml_size;
std::cout << " ✅ " << name << " - " << info.description << "\n";
std::cout << " 📊 Size: " << (model_size / 1024 / 1024) << " MB";
if (coreml_size > 0) {
std::cout << " + " << (coreml_size / 1024 / 1024) << " MB CoreML";
}
std::cout << "\n";
std::cout << " 📂 Path: " << model_path << "\n";
if (coreml_size > 0) {
std::cout << " 🚀 CoreML: " << coreml_path << "\n";
}
std::cout << "\n";
}
std::cout << "📊 Total storage used: " << (total_size / 1024 / 1024) << " MB\n\n";
}
void ModelManager::show_storage_usage() {
std::cout << "\n📊 Model Storage Usage:\n\n";
if (!std::filesystem::exists(models_dir_)) {
std::cout << "📁 Models directory doesn't exist yet: " << models_dir_ << "\n\n";
return;
}
size_t total_size = 0;
size_t model_files = 0;
size_t coreml_files = 0;
size_t other_files = 0;
std::cout << "📂 Directory: " << models_dir_ << "\n\n";
for (const auto& entry : std::filesystem::directory_iterator(models_dir_)) {
if (entry.is_regular_file()) {
size_t file_size = entry.file_size();
total_size += file_size;
std::string filename = entry.path().filename().string();
if (filename.length() >= 4 && filename.substr(filename.length() - 4) == ".bin") {
model_files++;
std::cout << " 📄 " << filename << " - " << (file_size / 1024 / 1024) << " MB\n";
} else if (filename.length() >= 4 && filename.substr(filename.length() - 4) == ".zip") {
coreml_files++;
std::cout << " 📦 " << filename << " - " << (file_size / 1024 / 1024) << " MB (CoreML zip)\n";
} else {
other_files++;
std::cout << " 📄 " << filename << " - " << (file_size / 1024 / 1024) << " MB\n";
}
} else if (entry.is_directory()) {
std::string dirname = entry.path().filename().string();
if (dirname.length() >= 9 && dirname.substr(dirname.length() - 9) == ".mlmodelc") {
size_t dir_size = 0;
for (const auto& subentry : std::filesystem::recursive_directory_iterator(entry.path())) {
if (subentry.is_regular_file()) {
dir_size += subentry.file_size();
}
}
total_size += dir_size;
coreml_files++;
std::cout << " 🚀 " << dirname << "/ - " << (dir_size / 1024 / 1024) << " MB (CoreML)\n";
}
}
}
std::cout << "\n📈 Summary:\n";
std::cout << " 📄 Model files: " << model_files << "\n";
std::cout << " 🚀 CoreML files: " << coreml_files << "\n";
if (other_files > 0) {
std::cout << " 📁 Other files: " << other_files << "\n";
}
std::cout << " 📊 Total size: " << (total_size / 1024 / 1024) << " MB\n\n";
}
std::string ModelManager::prompt_model_selection() {
std::cout << "\n🤔 No model specified. Let's choose one!\n";
list_available_models();
while (true) {
std::cout << "\nWhich model would you like to use? ";
std::cout << "(or 'q' to quit): ";
std::string choice;
if (!std::getline(std::cin, choice) || choice == "q" || choice == "quit") {
return "";
}
if (models_.find(choice) != models_.end()) {
return choice;
}
std::cout << "❌ Invalid model name. Please choose from the list above.\n";
}
}
bool ModelManager::prompt_download_confirmation(const std::string& model_name) {
ModelInfo info = get_model_info(model_name);
std::cout << "\n📦 Model '" << model_name << "' not found locally.\n";
std::cout << "📄 " << info.description << "\n";
std::cout << "📁 Size: " << info.size_mb << " MB\n";
#ifdef __APPLE__
std::cout << "🚀 CoreML acceleration: Available\n";
#endif
std::cout << "\nChoose an option:\n";
std::cout << " 1. Download '" << model_name << "' (" << info.size_mb << " MB)\n";
std::cout << " 2. Choose a different model\n";
std::cout << " 3. Cancel\n";
std::cout << "\nEnter choice [1-3]: ";
std::string response;
std::getline(std::cin, response);
if (response == "1" || response.empty()) {
return true; // Download the requested model
} else if (response == "2") {
return false; // This will be handled differently - we'll trigger model selection
} else {
return false; // Cancel
}
}
std::string ModelManager::prompt_model_not_found(const std::string& model_name, bool use_coreml) {
ModelInfo info = get_model_info(model_name);
std::cout << "\n📦 Model '" << model_name << "' not found locally.\n";
std::cout << "📄 " << info.description << "\n";
std::cout << "📁 Size: " << info.size_mb << " MB\n";
#ifdef __APPLE__
std::cout << "🚀 CoreML acceleration: Available\n";
#endif
std::cout << "\nChoose an option:\n";
std::cout << " 1. Download '" << model_name << "' (" << info.size_mb << " MB)\n";
std::cout << " 2. Choose a different model\n";
std::cout << " 3. Cancel\n";
std::cout << "\nEnter choice [1-3]: ";
std::string response;
std::getline(std::cin, response);
if (response == "1" || response.empty()) {
// Download the requested model
std::cout << "\n🚀 Starting download...\n";
if (!download_model(model_name)) {
return "";
}
#ifdef __APPLE__
if (use_coreml) {
std::cout << "\n🤖 Downloading CoreML acceleration model...\n";
if (!download_coreml_model(model_name)) {
std::cout << "⚠️ CoreML download failed, will use regular model\n";
}
}
#endif
show_usage_examples(model_name);
return get_model_path(model_name);
} else if (response == "2") {
// Let user choose a different model
std::string selected_model = prompt_model_selection();
if (selected_model.empty()) {
return "";
}
// Recursively resolve the selected model
return resolve_model(selected_model, use_coreml);
} else {
std::cout << "\n❌ Operation cancelled.\n";
return "";
}
}
ModelInfo ModelManager::get_model_info(const std::string& model_name) {
if (models_.find(model_name) != models_.end()) {
return models_[model_name];
}
return {};
}
bool ModelManager::download_file(const std::string& url, const std::string& filepath, bool show_progress) {
std::cout << "⬇️ Downloading: " << std::filesystem::path(filepath).filename().string() << "\n";
std::cout << "🔗 From: " << url << "\n";
// Use curl to download with progress
std::string command = "curl -L --progress-bar \"" + url + "\" -o \"" + filepath + "\"";
if (show_progress) {
std::cout << "📊 Progress:\n";
}
int result = std::system(command.c_str());
if (result == 0 && std::filesystem::exists(filepath)) {
std::cout << "✅ Download completed: " << filepath << "\n";
return true;
} else {
std::cout << "❌ Download failed for: " << filepath << "\n";
return false;
}
}
bool ModelManager::extract_coreml_model(const std::string& zip_path, const std::string& extract_dir) {
std::cout << "📦 Extracting CoreML model...\n";
// Check if zip file exists
if (!std::filesystem::exists(zip_path)) {
std::cout << "❌ Zip file not found: " << zip_path << "\n";
return false;
}
// Get absolute paths
std::filesystem::path abs_zip_path = std::filesystem::absolute(zip_path);
std::filesystem::path abs_extract_dir = std::filesystem::absolute(extract_dir);
// Ensure extract directory exists
std::filesystem::create_directories(abs_extract_dir);
std::string command = "cd \"" + abs_extract_dir.string() + "\" && unzip -q \"" + abs_zip_path.string() + "\"";
std::cerr << "Extracting CoreML model: " << abs_zip_path.filename().string() << "\n";
int result = std::system(command.c_str());
if (result == 0) {
std::cerr << "✅ CoreML model extracted successfully\n";
// Remove the zip file to save space
std::filesystem::remove(zip_path);
return true;
} else {
std::cerr << "❌ Failed to extract CoreML model (exit code: " << result << ")\n";
std::cerr << "Command: " << command << "\n";
return false;
}
}
bool ModelManager::download_model(const std::string& model_name, bool show_progress) {
if (models_.find(model_name) == models_.end()) {
std::cout << "❌ Unknown model: " << model_name << "\n";
return false;
}
ModelInfo info = models_[model_name];
std::string filepath = get_model_path(model_name);
return download_file(info.url, filepath, show_progress);
}
bool ModelManager::download_coreml_model(const std::string& model_name, bool show_progress) {
#ifndef __APPLE__
std::cout << "ℹ️ CoreML models are only available on macOS\n";
return false;
#endif
if (models_.find(model_name) == models_.end()) {
std::cout << "❌ Unknown model: " << model_name << "\n";
return false;
}
ModelInfo info = models_[model_name];
std::string zip_path = models_dir_ + "/" + info.coreml_filename + ".zip";
// Download the zip file
if (!download_file(info.coreml_url, zip_path, show_progress)) {
return false;
}
// Extract it
return extract_coreml_model(zip_path, models_dir_);
}
void ModelManager::show_usage_examples(const std::string& model_name) {
std::cout << "\n🎉 Setup complete! Here's how to use your model:\n\n";
std::cout << "🎤 Basic real-time transcription:\n";
std::cout << " recognize -m " << model_name << "\n\n";
std::cout << "🎯 VAD mode (recommended - only transcribes when you speak):\n";
std::cout << " recognize -m " << model_name << " --step 0 --length 30000\n\n";
std::cout << "⚡ Continuous mode (transcribes every 500ms):\n";
std::cout << " recognize -m " << model_name << " --step 500 --length 5000\n\n";
std::cout << "💾 Save transcription to file:\n";
std::cout << " recognize -m " << model_name << " -f transcript.txt\n\n";
std::cout << "🎛️ Use specific microphone:\n";
std::cout << " recognize -m " << model_name << " -c 3\n\n";
if (models_[model_name].multilingual) {
std::cout << "🌍 Transcribe other languages:\n";
std::cout << " recognize -m " << model_name << " -l es # Spanish\n";
std::cout << " recognize -m " << model_name << " -l fr # French\n\n";
std::cout << "🔄 Translate to English:\n";
std::cout << " recognize -m " << model_name << " -l es --translate\n\n";
}
std::cout << "📚 For more options: recognize --help\n\n";
std::cout << "🚀 Ready to start? Try the VAD mode command above!\n";
}
std::string ModelManager::resolve_model(const std::string& model_arg, bool use_coreml) {
std::string model_name = model_arg;
// If no model specified, prompt user
if (model_name.empty()) {
model_name = prompt_model_selection();
if (model_name.empty()) {
return "";
}
}
// Check if it's a direct file path
if (std::filesystem::exists(model_name)) {
std::cout << "✅ Using existing model file: " << model_name << "\n";
return model_name;
}
// Check if it's a known model name
if (models_.find(model_name) == models_.end()) {
std::cout << "❌ Unknown model: " << model_name << "\n";
std::cout << "Available models:\n";
for (const auto& name : get_model_names()) {
std::cout << " - " << name << "\n";
}
return "";
}
// Check if model exists locally
if (model_exists(model_name)) {
// Model name logged silently — clean loading UX handles user feedback
// Also check/download CoreML model if requested and not exists
#ifdef __APPLE__
if (use_coreml && !coreml_model_exists(model_name)) {
std::cout << "🚀 CoreML acceleration requested but CoreML model not found.\n";
std::cout << "Would you like to download the CoreML version? [Y/n]: ";
std::string response;
std::getline(std::cin, response);
if (response.empty() || response == "y" || response == "Y") {
download_coreml_model(model_name);
}
}
#endif
return get_model_path(model_name);
}
// Model doesn't exist, use enhanced prompt with model selection option
return prompt_model_not_found(model_name, use_coreml);
}
bool ModelManager::delete_model(const std::string& model_name, bool confirm) {
if (models_.find(model_name) == models_.end()) {
std::cout << "❌ Unknown model: " << model_name << "\n";
std::cout << "💡 Run 'recognize --list-models' to see available models.\n\n";
return false;
}
if (!model_exists(model_name)) {
std::cout << "❌ Model '" << model_name << "' is not downloaded.\n\n";
return false;
}
ModelInfo info = get_model_info(model_name);
std::string model_path = get_model_path(model_name);
std::string coreml_path = get_coreml_model_path(model_name);
bool has_coreml = coreml_model_exists(model_name);
// Calculate total size
size_t total_size = 0;
if (std::filesystem::exists(model_path)) {
total_size += std::filesystem::file_size(model_path);
}
if (has_coreml && std::filesystem::exists(coreml_path)) {
if (std::filesystem::is_directory(coreml_path)) {
for (const auto& entry : std::filesystem::recursive_directory_iterator(coreml_path)) {
if (entry.is_regular_file()) {
total_size += entry.file_size();
}
}
}
}
if (confirm) {
std::cout << "\n🗑️ Delete Model: " << model_name << "\n";
std::cout << "📄 " << info.description << "\n";
std::cout << "📁 Size: " << (total_size / 1024 / 1024) << " MB\n";
std::cout << "📂 Path: " << model_path << "\n";
if (has_coreml) {
std::cout << "🚀 CoreML: " << coreml_path << "\n";
}
std::cout << "\n⚠️ This action cannot be undone.\n";
std::cout << "Are you sure you want to delete this model? [y/N]: ";
std::string response;
std::getline(std::cin, response);
if (response != "y" && response != "Y") {
std::cout << "❌ Deletion cancelled.\n\n";
return false;
}
}
bool success = true;
// Delete main model file
try {
if (std::filesystem::exists(model_path)) {
std::filesystem::remove(model_path);
std::cout << "✅ Deleted: " << model_path << "\n";
}
} catch (const std::exception& e) {
std::cout << "❌ Failed to delete " << model_path << ": " << e.what() << "\n";
success = false;
}
// Delete CoreML model
if (has_coreml) {
try {
if (std::filesystem::exists(coreml_path)) {
if (std::filesystem::is_directory(coreml_path)) {
std::filesystem::remove_all(coreml_path);
} else {
std::filesystem::remove(coreml_path);
}
std::cout << "✅ Deleted CoreML: " << coreml_path << "\n";
}
} catch (const std::exception& e) {
std::cout << "❌ Failed to delete CoreML " << coreml_path << ": " << e.what() << "\n";
success = false;
}
}
// Also clean up any zip files
std::string zip_path = models_dir_ + "/" + info.coreml_filename + ".zip";
if (std::filesystem::exists(zip_path)) {
try {
std::filesystem::remove(zip_path);
std::cout << "✅ Deleted zip: " << zip_path << "\n";
} catch (const std::exception& e) {
std::cout << "❌ Failed to delete zip " << zip_path << ": " << e.what() << "\n";
}
}
if (success) {
std::cout << "✅ Model '" << model_name << "' deleted successfully.\n";
std::cout << "💾 Freed " << (total_size / 1024 / 1024) << " MB of storage.\n\n";
}
return success;
}
bool ModelManager::delete_all_models(bool confirm) {
auto downloaded = get_downloaded_models();
if (downloaded.empty()) {
std::cout << "📁 No models to delete.\n\n";
return true;
}
if (confirm) {
std::cout << "\n🗑️ Delete All Models\n\n";
std::cout << "This will delete the following models:\n";
size_t total_size = 0;
for (const auto& name : downloaded) {
ModelInfo info = get_model_info(name);
std::string model_path = get_model_path(name);
std::string coreml_path = get_coreml_model_path(name);
size_t model_size = 0;
if (std::filesystem::exists(model_path)) {
model_size += std::filesystem::file_size(model_path);
}
if (coreml_model_exists(name) && std::filesystem::exists(coreml_path)) {
if (std::filesystem::is_directory(coreml_path)) {
for (const auto& entry : std::filesystem::recursive_directory_iterator(coreml_path)) {
if (entry.is_regular_file()) {
model_size += entry.file_size();
}
}
}
}
total_size += model_size;
std::cout << " ❌ " << name << " - " << (model_size / 1024 / 1024) << " MB\n";
}
std::cout << "\n📊 Total size: " << (total_size / 1024 / 1024) << " MB\n";
std::cout << "⚠️ This action cannot be undone.\n";
std::cout << "Are you sure you want to delete ALL models? [y/N]: ";
std::string response;
std::getline(std::cin, response);
if (response != "y" && response != "Y") {
std::cout << "❌ Deletion cancelled.\n\n";
return false;
}
}
bool all_success = true;
for (const auto& name : downloaded) {
if (!delete_model(name, false)) { // Skip individual confirmations
all_success = false;
}
}
if (all_success) {
std::cout << "\n✅ All models deleted successfully.\n\n";
} else {
std::cout << "\n⚠️ Some models could not be deleted.\n\n";
}
return all_success;
}
void ModelManager::cleanup_orphaned_files() {
std::cout << "\n🧹 Cleaning up orphaned files...\n\n";
if (!std::filesystem::exists(models_dir_)) {
std::cout << "📁 Models directory doesn't exist.\n\n";
return;
}
std::vector<std::string> orphaned_files;
std::set<std::string> known_files;
// Collect all known filenames
for (const auto& [name, info] : models_) {
known_files.insert(info.filename);
known_files.insert(info.coreml_filename);
known_files.insert(info.coreml_filename + ".zip");
}
// Find orphaned files
for (const auto& entry : std::filesystem::directory_iterator(models_dir_)) {
std::string filename = entry.path().filename().string();
if (entry.is_regular_file()) {
if (known_files.find(filename) == known_files.end()) {
orphaned_files.push_back(entry.path().string());
}
} else if (entry.is_directory()) {
if (known_files.find(filename) == known_files.end()) {
orphaned_files.push_back(entry.path().string());
}
}
}
if (orphaned_files.empty()) {
std::cout << "✅ No orphaned files found.\n\n";
return;
}
std::cout << "🗑️ Found orphaned files:\n";
size_t total_size = 0;
for (const auto& file_path : orphaned_files) {
if (std::filesystem::is_regular_file(file_path)) {
size_t size = std::filesystem::file_size(file_path);
total_size += size;
std::cout << " 📄 " << std::filesystem::path(file_path).filename().string()
<< " - " << (size / 1024 / 1024) << " MB\n";
} else if (std::filesystem::is_directory(file_path)) {
size_t size = 0;
for (const auto& entry : std::filesystem::recursive_directory_iterator(file_path)) {
if (entry.is_regular_file()) {
size += entry.file_size();
}
}
total_size += size;
std::cout << " 📁 " << std::filesystem::path(file_path).filename().string()
<< "/ - " << (size / 1024 / 1024) << " MB\n";
}
}
std::cout << "\n📊 Total size: " << (total_size / 1024 / 1024) << " MB\n";
std::cout << "Delete these orphaned files? [y/N]: ";
std::string response;
std::getline(std::cin, response);
if (response != "y" && response != "Y") {
std::cout << "❌ Cleanup cancelled.\n\n";
return;
}
for (const auto& file_path : orphaned_files) {
try {
if (std::filesystem::is_directory(file_path)) {
std::filesystem::remove_all(file_path);
} else {
std::filesystem::remove(file_path);
}
std::cout << "✅ Deleted: " << std::filesystem::path(file_path).filename().string() << "\n";
} catch (const std::exception& e) {
std::cout << "❌ Failed to delete " << file_path << ": " << e.what() << "\n";
}
}
std::cout << "\n✅ Cleanup completed.\n\n";
}
// VAD model management
static const std::string VAD_MODEL_FILENAME = "ggml-silero-v5.1.2.bin";
static const std::string VAD_MODEL_URL = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-silero-v5.1.2.bin";
std::string ModelManager::get_vad_model_path() const {
return models_dir_ + "/" + VAD_MODEL_FILENAME;
}
bool ModelManager::download_vad_model(bool show_progress) {
ensure_models_directory();
std::string filepath = get_vad_model_path();
if (std::filesystem::exists(filepath)) {
return true;
}
std::cout << "Downloading Silero VAD model (~864 KB)..." << std::endl;
return download_file(VAD_MODEL_URL, filepath, show_progress);
}
std::string ModelManager::resolve_vad_model(const std::string& vad_model_arg) {
if (vad_model_arg.empty()) {
return "";
}
// "auto" means auto-download
if (vad_model_arg == "auto") {
if (download_vad_model()) {
return get_vad_model_path();
}
std::cerr << "Warning: Failed to download Silero VAD model" << std::endl;
return "";
}
// Treat as explicit path
if (std::filesystem::exists(vad_model_arg)) {
return vad_model_arg;
}
// Try as filename in models directory
std::string in_models = models_dir_ + "/" + vad_model_arg;
if (std::filesystem::exists(in_models)) {
return in_models;
}
std::cerr << "Warning: VAD model not found: " << vad_model_arg << std::endl;
return "";
}