|
| 1 | +# Basic Example For WASI-NN with Whisper Backend |
| 2 | + |
| 3 | +This example is for a basic audio recognition with WASI-NN whisper backend in WasmEdge. |
| 4 | +In current status, WasmEdge implement the Whisper backend of WASI-NN in only English. We'll extend more options in the future. |
| 5 | + |
| 6 | +## Dependencies |
| 7 | + |
| 8 | +This crate depends on the `wasmedge-wasi-nn` in the `Cargo.toml`: |
| 9 | + |
| 10 | +```toml |
| 11 | +[dependencies] |
| 12 | +wasmedge-wasi-nn = "0.8.0" |
| 13 | +``` |
| 14 | + |
| 15 | +## Build |
| 16 | + |
| 17 | +Compile the application to WebAssembly: |
| 18 | + |
| 19 | +```bash |
| 20 | +cargo build --target=wasm32-wasi --release |
| 21 | +``` |
| 22 | + |
| 23 | +The output WASM file will be at [`target/wasm32-wasi/release/whisper-basic.wasm`](whisper-basic.wasm). |
| 24 | +To speed up the processing, we can enable the AOT mode in WasmEdge with: |
| 25 | + |
| 26 | +```bash |
| 27 | +wasmedge compile target/wasm32-wasi/release/whisper-basic.wasm whisper-basic_aot.wasm |
| 28 | +``` |
| 29 | + |
| 30 | +## Run |
| 31 | + |
| 32 | +### Test data |
| 33 | + |
| 34 | +The testing audio is located at `./test.wav`. |
| 35 | + |
| 36 | +Users should get the model by the guide from [whisper.cpp repository](https://github.com/ggerganov/whisper.cpp/tree/master/models): |
| 37 | + |
| 38 | +```bash |
| 39 | +curl -sSf https://raw.githubusercontent.com/ggerganov/whisper.cpp/master/models/download-ggml-model.sh | bash -s -- base.en |
| 40 | +``` |
| 41 | + |
| 42 | +The model will be stored at `./ggml-base.en.bin`. |
| 43 | + |
| 44 | +### Input Audio |
| 45 | + |
| 46 | +The WASI-NN whisper backend for WasmEdge currently supported 16kHz, 1 channel, and `pcm_s16le` format. |
| 47 | + |
| 48 | +Users can convert their input audio as following `ffmpeg` command: |
| 49 | + |
| 50 | +```bash |
| 51 | +ffmpeg -i test.m4a -acodec pcm_s16le -ac 1 -ar 16000 test.wav |
| 52 | +``` |
| 53 | + |
| 54 | +### Execute |
| 55 | + |
| 56 | +> Note: This is prepared for `0.14.2` or later release in the future. Please build from source now. |
| 57 | +
|
| 58 | +Users should [install the WasmEdge with WASI-NN plug-in in Whisper backend](https://wasmedge.org/docs/start/install/#wasi-nn-plug-ins). |
| 59 | + |
| 60 | +```bash |
| 61 | +curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- --plugins wasi_nn-whisper |
| 62 | +``` |
| 63 | + |
| 64 | +Execute the WASM with the `wasmedge` with WASI-NN plug-in: |
| 65 | + |
| 66 | +```bash |
| 67 | +wasmedge --dir .:. whisper-basic_aot.wasm ggml-base.en.bin test.wav |
| 68 | +``` |
| 69 | + |
| 70 | +You will get recognized string from the audio file in the output: |
| 71 | + |
| 72 | +```bash |
| 73 | +Read model, size in bytes: 147964211 |
| 74 | +Loaded graph into wasi-nn with ID: Graph#0 |
| 75 | +Read input tensor, size in bytes: 141408 |
| 76 | +Recognized from audio: |
| 77 | +[00:00:00.000 --> 00:00:04.300] This is a test record for whisper.cpp |
| 78 | +``` |
0 commit comments