-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from JacobLinCool/gc
WhisperModel can be automatically freed by GC
- Loading branch information
Showing
17 changed files
with
271 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"smart-whisper": minor | ||
--- | ||
|
||
WhisperModel will now be automatically freed by the Node.js garbage collector if `.free()` has not been called previously. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { WhisperModel, manager } from "../dist"; | ||
|
||
const fp = manager.resolve("tiny"); | ||
|
||
(async () => { | ||
for (let i = 0; i < 5; i++) { | ||
await scope(); | ||
global.gc?.(); | ||
} | ||
})(); | ||
|
||
async function scope() { | ||
const model = await WhisperModel.load(fp); | ||
console.log(model.handle); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <napi.h> | ||
|
||
#include "common.h" | ||
#include "model.h" | ||
#include "transcribe.h" | ||
|
||
Napi::Object Init(Napi::Env env, Napi::Object exports) { | ||
exports.Set("transcribe", Napi::Function::New(env, Transcribe)); | ||
WhisperModel::Init(env, exports); | ||
|
||
if (IsProduction(env.Global())) { | ||
whisper_log_set([](ggml_log_level level, const char *text, void *user_data) {}, nullptr); | ||
} | ||
|
||
return exports; | ||
} | ||
|
||
NODE_API_MODULE(whisper, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "common.h" | ||
|
||
Napi::Promise PromiseWorker::Promise() { return promise.Promise(); } | ||
|
||
bool IsProduction(const Napi::Object global_env) { | ||
Napi::Object process = global_env.Get("process").As<Napi::Object>(); | ||
Napi::Object env = process.Get("env").As<Napi::Object>(); | ||
Napi::Value node_env = env.Get("NODE_ENV"); | ||
|
||
if (!node_env.IsString()) { | ||
return false; | ||
} | ||
|
||
Napi::String node_env_str = node_env.As<Napi::String>(); | ||
return node_env_str.Utf8Value() == "production"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
#ifndef _GUARD_SW_COMMON_H | ||
#define _GUARD_SW_COMMON_H | ||
|
||
#ifndef NAPI_VERSION | ||
// Support Node.js 16+ | ||
#define NAPI_VERSION 8 | ||
#endif | ||
#include <napi.h> | ||
|
||
class PromiseWorker : public Napi::AsyncWorker { | ||
public: | ||
PromiseWorker(Napi::Env &env) : AsyncWorker(env), promise(Napi::Promise::Deferred::New(env)) {} | ||
|
||
Napi::Promise Promise(); | ||
|
||
protected: | ||
Napi::Promise::Deferred promise; | ||
}; | ||
|
||
bool IsProduction(const Napi::Object global_env); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
#ifndef _GUARD_SW_MODEL_H | ||
#define _GUARD_SW_MODEL_H | ||
|
||
#include "common.h" | ||
#include "whisper.h" | ||
|
||
Napi::Value LoadModel(const Napi::CallbackInfo& info); | ||
Napi::Value FreeModel(const Napi::CallbackInfo& info); | ||
class WhisperModel : public Napi::ObjectWrap<WhisperModel> { | ||
public: | ||
static Napi::Object Init(Napi::Env env, Napi::Object exports); | ||
|
||
WhisperModel(const Napi::CallbackInfo &info); | ||
void Finalize(Napi::Env env); | ||
|
||
private: | ||
whisper_context *context; | ||
static Napi::Value Load(const Napi::CallbackInfo &info); | ||
Napi::Value Free(const Napi::CallbackInfo &info); | ||
Napi::Value GetFreed(const Napi::CallbackInfo &info); | ||
Napi::Value GetHandle(const Napi::CallbackInfo &info); | ||
}; | ||
|
||
#endif |
Oops, something went wrong.