Blazor

Run an AI model in Blazor with no server and no API key

DEV Community: #blazor Todd Tanner
Blazor

A Blazor WebAssembly sample runs a 14 MB MobileNetV2 ONNX model on the visitor's GPU through WebGPU, with no server, API key or upload. The model file streams straight to the GPU and compiled shaders are cached for later visits.

Key points

  • SpawnDev.ILGPU.ML picks an accelerator in order: WebGPU first, then WebGL, then WebAssembly.
  • `OpenStreamFromUrlAsync` keeps the 14 MB model out of .NET memory: 2.9 MB against 82-97 MB via `HttpClient`.
  • `ClassifyAsync` takes raw pixels and returns top-K labels with confidence scores.
  • `ShaderArtifactSerializer.ExportCache()` stores compiled WebGPU shaders; a return visit's first photo took about 1.6 s.
  • Pixel decoding is left to the browser through `CreateImageBitmap` and canvas `GetImageData`.
  • A changed model needs a new file name, because the stored copy is keyed by URL only.

Why it matters

For teams already shipping Blazor WebAssembly, this is a realistic path to a model in the browser with no backend, and the memory numbers matter most: pulling the same ONNX file through `HttpClient` costs roughly 90 MB of .NET heap, while ModelHub keeps it near 3 MB.

Treat it as a narrow tool: MobileNetV2-class models, WebGPU-capable browsers and a cold first visit measured in seconds. Before adopting, verify that SpawnDev.ILGPU.ML supports your model, and keep server-side inference for anything needing larger models or answers you have to trust.

Real-world use

  • A photo picker where images must never leave the device, so labels come from local inference.
  • A public demo page that cannot carry an API key or a per-request bill.
  • A page that keeps working offline once the 14 MB model and its shaders are cached in the browser.

This summary was generated by the agent from the original post on DEV Community: #blazor. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message