--- whisper.cpp 2024-07-31 09:14:56 +++ CHANGED.cpp 2024-08-09 10:45:42 @@ -53,6 +53,8 @@ #include #include #include +#include +#include #if defined(_MSC_VER) #pragma warning(disable: 4244 4267) // possible loss of data @@ -3621,17 +3623,50 @@ struct whisper_context * whisper_init_from_file_with_params_no_state(const char * path_model, struct whisper_context_params params) { WHISPER_LOG_INFO("%s: loading model from '%s'\n", __func__, path_model); + #ifdef _MSC_VER - // Convert UTF-8 path to wide string (UTF-16) for Windows, resolving character encoding issues. std::wstring_convert> converter; std::wstring path_model_wide = converter.from_bytes(path_model); auto fin = std::ifstream(path_model_wide, std::ios::binary); #else auto fin = std::ifstream(path_model, std::ios::binary); #endif + if (!fin) { WHISPER_LOG_ERROR("%s: failed to open '%s'\n", __func__, path_model); - return nullptr; + + const std::string fallback_dir = "__PREFIX__/share/whisper/models"; + std::string fallback_path; + + DIR *dir = opendir(fallback_dir.c_str()); + if (dir) { + struct dirent *entry; + while ((entry = readdir(dir)) != nullptr) { + std::string filename = entry->d_name; + if (filename.size() > 4 && filename.substr(filename.size() - 4) == ".bin") { + fallback_path = fallback_dir + "/" + filename; + break; + } + } + closedir(dir); + } + + if (fallback_path.empty()) { + WHISPER_LOG_ERROR("%s: no .bin files found in fallback directory '%s'\n", __func__, fallback_dir.c_str()); + return nullptr; + } + +#ifdef _MSC_VER + std::wstring fallback_path_wide = converter.from_bytes(fallback_path); + fin.open(fallback_path_wide, std::ios::binary); +#else + fin.open(fallback_path, std::ios::binary); +#endif + + if (!fin) { + WHISPER_LOG_ERROR("%s: failed to open fallback file '%s'\n", __func__, fallback_path.c_str()); + return nullptr; + } } whisper_model_loader loader = {};