# syntax=docker/dockerfile:1 # # DeepFilterNet 3 (Hendrik Schröter, MIT OR Apache-2.0), the speech # enhancement network, compiled to WebAssembly for johnmcm.com's # /remove-background-noise. libDF (the Rust runtime: STFT, ERB features, the # tract ONNX engine and deep filtering) is built with wasm-pack for ES-module # workers, with its wasm bindings replaced by src/wasm.rs (model passed in, # command-line thresholds, chunked processing, delay reported) and the model # left out of the binary: the page loads DeepFilterNet3_onnx.tar.gz beside it. # The Rust image is pinned by digest, DeepFilterNet by commit, the crates by # the repository's Cargo.lock. # # ./build.ps1 (or) docker build --target export --output type=local,dest=dist . ARG RUST_IMAGE=rust:1.82-bookworm@sha256:d9c3c6f1264a547d84560e06ffd79ed7a799ce0bff0980b26cf10d29af888377 FROM ${RUST_IMAGE} AS build # binaryen pinned to the version bookworm ships (as revenge pins gperf), so a # rebuild's wasm-opt output cannot drift with the archive. RUN apt-get update && apt-get install -y --no-install-recommends binaryen=108-1 && rm -rf /var/lib/apt/lists/* \ && rustup target add wasm32-unknown-unknown \ && cargo install --locked wasm-pack --version 0.13.1 \ && cargo install --locked wasm-bindgen-cli --version 0.2.92 ARG DFN_COMMIT=d375b2d8309e0935d165700c91da9de862a99c31 RUN git clone -q https://github.com/Rikorose/DeepFilterNet /dfn \ && git -C /dfn checkout -q ${DFN_COMMIT} \ && test "$(git -C /dfn rev-parse HEAD)" = "${DFN_COMMIT}" COPY src/wasm.rs /dfn/libDF/src/wasm.rs # The wasm feature compiles the 8 MB model in; it is served as a file instead. RUN perl -0pi -e 's/wasm = \[\n "tract",\n "default-model",/wasm = [\n "tract",/' /dfn/libDF/Cargo.toml \ && grep -A3 '^wasm = \[' /dfn/libDF/Cargo.toml | grep -vq default-model WORKDIR /dfn # WebAssembly SIMD (every current browser has it) for tract's matrix kernels, then binaryen's optimiser. ENV RUSTFLAGS="-C target-feature=+simd128" RUN wasm-pack build --release --mode no-install --target web --out-name deepfilter --out-dir /out/pkg libDF --no-default-features --features wasm RUN wasm-opt -O3 --enable-simd --enable-bulk-memory --enable-nontrapping-float-to-int --enable-sign-ext --enable-mutable-globals /out/pkg/deepfilter_bg.wasm -o /out/pkg/deepfilter_bg.wasm RUN mkdir -p /out/dist \ && cp /out/pkg/deepfilter.js /out/pkg/deepfilter_bg.wasm /out/dist/ \ && cp /dfn/models/DeepFilterNet3_onnx.tar.gz /out/dist/ \ && cp /dfn/LICENSE-MIT /dfn/LICENSE-APACHE /out/dist/ \ && sha256sum /out/dist/* FROM scratch AS export COPY --from=build /out/dist/ /