loadWhisperModel method

  1. @override
bool loadWhisperModel({
  1. String openVinoEncoderDevice = "CPU",
  2. required String whisperModelPath,
  3. bool isUseGpu = false,
  4. int gpuDevice = 0,
})
override

Implementation

@override
bool loadWhisperModel({
  String openVinoEncoderDevice = "CPU",
  required String whisperModelPath,
  bool isUseGpu = false,
  int gpuDevice = 0,
}) {
  {
    WhisperLibrary._openVinoEncoderDevice = openVinoEncoderDevice;
    WhisperLibrary._whisperModelPath = whisperModelPath;
    WhisperLibrary._isUseGpu = isUseGpu;
    WhisperLibrary._gpuDevice = gpuDevice;
  }
  if (_isInIsolate == false) {
    return true;
  }
  if (isDeviceSupport() == false || isCrash()) {
    return false;
  }
  final whisperModelPathNative =
      WhisperLibrary._whisperModelPath.toNativeUtf8().cast<Char>();
  {
    final whisperModelContext = WhisperLibrary._whisperModelContext;
    if (whisperModelContext != nullptr) {
      /// release memory
      _whisperLibraryDartSharedBindingsByGeneralDeveloper.whisper_free(
        whisperModelContext,
      );
    }
  }

  {
    WhisperLibrary._whisperLibraryDartSharedBindingsByGeneralDeveloper
        .whisper_log_set(
      Pointer.fromFunction(ggmlLogCallbackFunction),
      "log".toNativeUtf8().cast<Void>(),
    );
    WhisperLibrary._whisperLibraryDartSharedBindingsByGeneralDeveloper
        .ggml_log_set(
      Pointer.fromFunction(ggmlLogCallbackFunction),
      "log".toNativeUtf8().cast<Void>(),
    );
  }

  WhisperLibrary._whisperLibraryDartSharedBindingsByGeneralDeveloper
      .ggml_backend_load_all();

  final cparams = _whisperLibraryDartSharedBindingsByGeneralDeveloper
      .whisper_context_default_params();
  cparams.use_gpu = WhisperLibrary._isUseGpu;
  cparams.gpu_device = WhisperLibrary._gpuDevice;

  final whisperModelContext =
      _whisperLibraryDartSharedBindingsByGeneralDeveloper
          .whisper_init_from_file_with_params(
    whisperModelPathNative,
    cparams,
  );
  WhisperLibrary._whisperModelContext = whisperModelContext;
  if (whisperModelContext.address == 0) {
    _whisperLibraryDartSharedBindingsByGeneralDeveloper.whisper_free(
      whisperModelContext,
    );
    return false;
  }
  final openVinoEncoderDeviceNative =
      WhisperLibrary._openVinoEncoderDevice.toNativeUtf8().cast<Char>();
  _whisperLibraryDartSharedBindingsByGeneralDeveloper
      .whisper_ctx_init_openvino_encoder(
    whisperModelContext,
    nullptr,
    openVinoEncoderDeviceNative,
    nullptr,
  );
  return true;
}