Skip to content

Commit

Permalink
wip : polishing WASM example
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov committed Oct 22, 2022
1 parent db460b7 commit 491ecd7
Show file tree
Hide file tree
Showing 4 changed files with 397 additions and 85 deletions.
10 changes: 4 additions & 6 deletions bindings/javascript/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ if (WHISPER_WASM_SINGLE_FILE)
)
endif()

#-s TOTAL_MEMORY=536870912 \
set_target_properties(${TARGET} PROPERTIES LINK_FLAGS " \
--bind \
-s MODULARIZE=1 \
-s ASSERTIONS=1 \
-s USE_PTHREADS=1 \
-s PTHREAD_POOL_SIZE=9 \
-s ALLOW_MEMORY_GROWTH=1 \
-s PTHREAD_POOL_SIZE=8 \
-s INITIAL_MEMORY=1610612736 \
-s TOTAL_MEMORY=1610612736 \
-s FORCE_FILESYSTEM=1 \
-s EXPORT_NAME=\"'whisper_factory'\" \
-s EXPORTED_RUNTIME_METHODS=\"['print', 'printErr', 'ccall', 'cwrap']\" \
${EXTRA_FLAGS} \
")
17 changes: 13 additions & 4 deletions bindings/javascript/emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ EMSCRIPTEN_BINDINGS(whisper) {
for (size_t i = 0; i < g_contexts.size(); ++i) {
if (g_contexts[i] == nullptr) {
g_contexts[i] = whisper_init(path_model.c_str());
return i + 1;
if (g_contexts[i] != nullptr) {
return i + 1;
} else {
return (size_t) 0;
}
}
}

Expand All @@ -29,7 +33,7 @@ EMSCRIPTEN_BINDINGS(whisper) {
}
}));

emscripten::function("full_default", emscripten::optional_override([](size_t index, const emscripten::val & audio) {
emscripten::function("full_default", emscripten::optional_override([](size_t index, const emscripten::val & audio, const std::string & lang, bool translate) {
--index;

if (index >= g_contexts.size()) {
Expand All @@ -42,15 +46,20 @@ EMSCRIPTEN_BINDINGS(whisper) {

struct whisper_full_params params = whisper_full_default_params(whisper_sampling_strategy::WHISPER_SAMPLING_GREEDY);

printf("full_default: available threads %d\n", std::thread::hardware_concurrency());

params.print_realtime = true;
params.print_progress = false;
params.print_timestamps = true;
params.print_special_tokens = false;
params.translate = false;
params.language = "en";
params.translate = translate;
params.language = whisper_is_multilingual(g_contexts[index]) ? lang.c_str() : "en";
params.n_threads = std::min(8, (int) std::thread::hardware_concurrency());
params.offset_ms = 0;

printf("full_default: using %d threads\n", params.n_threads);
printf("full_default: language '%s'\n", params.language);

std::vector<float> pcmf32;
const int n = audio["length"].as<int>();

Expand Down
22 changes: 1 addition & 21 deletions bindings/javascript/whisper.js

Large diffs are not rendered by default.

Loading

0 comments on commit 491ecd7

Please sign in to comment.