mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2025-06-18 23:08:08 +00:00
wip : initial WASM port
Works but it is very slow because no SIMD is used. For example, jfk.wav is processed in ~23 seconds using "tiny.en" model
This commit is contained in:
14
examples/CMakeLists.txt
Normal file
14
examples/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
# dependencies
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
# third-party
|
||||
|
||||
#add_subdirectory(third-party)
|
||||
|
||||
# examples
|
||||
|
||||
if (EMSCRIPTEN)
|
||||
add_subdirectory(whisper.wasm)
|
||||
else()
|
||||
endif()
|
4
examples/whisper.wasm/CMakeLists.txt
Normal file
4
examples/whisper.wasm/CMakeLists.txt
Normal file
@ -0,0 +1,4 @@
|
||||
set(TARGET whisper.wasm)
|
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/index-tmpl.html ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/index.html @ONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/bindings/javascript/whisper.js ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/whisper.js COPYONLY)
|
3
examples/whisper.wasm/README.md
Normal file
3
examples/whisper.wasm/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# whisper.wasm
|
||||
|
||||
Live demo: https://whisper.ggerganov.com
|
153
examples/whisper.wasm/index-tmpl.html
Normal file
153
examples/whisper.wasm/index-tmpl.html
Normal file
@ -0,0 +1,153 @@
|
||||
<!doctype html>
|
||||
<html lang="en-us">
|
||||
<head>
|
||||
<title>whisper.cpp : WASM example</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="main-container">
|
||||
Minimal <b>whisper.cpp</b> example using Javascript bindings
|
||||
|
||||
<br><br>
|
||||
|
||||
Model:
|
||||
<input type="file" id="file" name="file" onchange="loadFile(event, 'ggml.bin')" />
|
||||
<br><br>
|
||||
|
||||
WAV:
|
||||
<input type="file" id="file" name="file" onchange="loadAudio(event)" />
|
||||
<br><br>
|
||||
|
||||
<button onclick="onTranscribe();">Transcribe</button>
|
||||
|
||||
<br><br>
|
||||
|
||||
<div class="cell-version">
|
||||
<span>
|
||||
|
|
||||
Build time: <span class="nav-link">@GIT_DATE@</span> |
|
||||
Commit hash: <a class="nav-link" href="https://github.com/ggerganov/whisper.cpp/commit/@GIT_SHA1@">@GIT_SHA1@</a> |
|
||||
Commit subject: <span class="nav-link">@GIT_COMMIT_SUBJECT@</span> |
|
||||
<a class="nav-link" href="https://github.com/ggerganov/whisper.cpp/tree/master/examples/whisper.wasm">Source Code</a> |
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript" src="whisper.js"></script>
|
||||
<script type='text/javascript'>
|
||||
window.AudioContext = window.AudioContext || window.webkitAudioContext;
|
||||
window.OfflineAudioContext = window.OfflineAudioContext || window.webkitOfflineAudioContext;
|
||||
|
||||
// web audio context
|
||||
var context = null;
|
||||
|
||||
// audio data
|
||||
var audio = null;
|
||||
|
||||
// the whisper module instance
|
||||
var whisper = null;
|
||||
var instance = null;
|
||||
|
||||
// instantiate the whisper instance
|
||||
// whisper_factory comes from the whisper.js module
|
||||
whisper_factory().then(function(obj) {
|
||||
whisper = obj;
|
||||
});
|
||||
|
||||
// helper function
|
||||
function convertTypedArray(src, type) {
|
||||
var buffer = new ArrayBuffer(src.byteLength);
|
||||
var baseView = new src.constructor(buffer).set(src);
|
||||
return new type(buffer);
|
||||
}
|
||||
|
||||
// initialize whisper
|
||||
function init() {
|
||||
if (!instance) {
|
||||
instance = whisper.init('ggml.bin');
|
||||
if (instance) {
|
||||
console.log('whisper instance initialized');
|
||||
}
|
||||
}
|
||||
|
||||
if (!instance) {
|
||||
console.log('whisper instance initialization failed');
|
||||
return;
|
||||
}
|
||||
|
||||
if (instance) {
|
||||
var ret = whisper.full_default(instance, audio);
|
||||
if (ret) {
|
||||
console.log('whisper full_default returned: ' + ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function loadFile(event, fname) {
|
||||
var file = event.target.files[0] || null;
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(
|
||||
"<p>File information: <strong>" + file.name +
|
||||
"</strong> type: <strong>" + file.type +
|
||||
"</strong> size: <strong>" + file.size +
|
||||
"</strong> bytes</p>"
|
||||
);
|
||||
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(event) {
|
||||
var buf = new Uint8Array(reader.result);
|
||||
|
||||
// write to WASM file using whisper.FS_createDataFile
|
||||
whisper.FS_createDataFile("/", fname, buf, true, true);
|
||||
}
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
function loadAudio(event) {
|
||||
if (!context) {
|
||||
context = new AudioContext({sampleRate: 16000});
|
||||
}
|
||||
|
||||
var file = event.target.files[0] || null;
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(
|
||||
"<p>Audio information: <strong>" + file.name +
|
||||
"</strong> type: <strong>" + file.type +
|
||||
"</strong> size: <strong>" + file.size +
|
||||
"</strong> bytes</p>"
|
||||
);
|
||||
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(event) {
|
||||
var buf = new Uint8Array(reader.result);
|
||||
|
||||
context.decodeAudioData(buf.buffer, function(audioBuffer) {
|
||||
var offlineContext = new OfflineAudioContext(audioBuffer.numberOfChannels, audioBuffer.length, audioBuffer.sampleRate);
|
||||
var source = offlineContext.createBufferSource();
|
||||
source.buffer = audioBuffer;
|
||||
source.connect(offlineContext.destination);
|
||||
source.start(0);
|
||||
|
||||
offlineContext.startRendering().then(function(renderedBuffer) {
|
||||
audio = renderedBuffer.getChannelData(0);
|
||||
//var audio16 = convertTypedArray(data, Int16Array);
|
||||
});
|
||||
});
|
||||
}
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
//
|
||||
// Transcribe
|
||||
//
|
||||
|
||||
function onTranscribe() {
|
||||
init();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user