downloadModel function

Future<String> downloadModel({
  1. required WhisperModel model,
  2. required String destinationPath,
  3. String? downloadHost,
})

Download model to destinationPath

Implementation

Future<String> downloadModel(
    {required WhisperModel model,
    required String destinationPath,
    String? downloadHost}) async {
  if (kDebugMode) {
    debugPrint("Download model ${model.modelName}");
  }
  final httpClient = HttpClient();

  Uri modelUri;

  if (downloadHost == null || downloadHost.isEmpty) {
    /// Huggingface url to download model
    modelUri = Uri.parse(
      "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-${model.modelName}.bin",
    );
  } else {
    modelUri = Uri.parse(
      "$downloadHost/ggml-${model.modelName}.bin",
    );
  }

  final request = await httpClient.getUrl(
    modelUri,
  );

  final response = await request.close();

  final file = File("$destinationPath/ggml-${model.modelName}.bin");
  final raf = file.openSync(mode: FileMode.write);

  await for (var chunk in response) {
    raf.writeFromSync(chunk);
  }

  await raf.close();

  if (kDebugMode) {
    debugPrint("Download Down . Path = ${file.path}");
  }
  return file.path;
}