Skip to content

docs: add Svelte tutorial#1589

Open
mvanhorn wants to merge 3 commits into
huggingface:mainfrom
mvanhorn:docs/svelte-example-guide
Open

docs: add Svelte tutorial#1589
mvanhorn wants to merge 3 commits into
huggingface:mainfrom
mvanhorn:docs/svelte-example-guide

Conversation

@mvanhorn

Copy link
Copy Markdown

Summary

  • Adds a step-by-step tutorial for building a multilingual translation app with Svelte + Vite + Transformers.js
  • Mirrors the structure of the existing React tutorial: project init, Web Worker for ML inference, Svelte components, CSS styling, and HF Spaces deployment
  • Adds the Svelte tutorial entry to _toctree.yml (placed between React and Next.js)

Closes #171

Test plan

  • Verify the tutorial renders correctly in the HuggingFace docs preview
  • Follow the tutorial steps to confirm a working Svelte app can be created
  • Confirm the _toctree.yml ordering is correct and the page appears in the sidebar

Add a step-by-step tutorial showing how to build a multilingual
translation app using Svelte and Vite with Transformers.js. The
tutorial mirrors the existing React tutorial structure and covers
project setup, Web Worker usage for ML inference, UI components,
and deployment to Hugging Face Spaces.

Closes huggingface#171
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

</script>

<div class="progress-container">
<div class="progress-bar" style="width: {percentage}%">

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid inline style, it could conflict with strict CSP

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in dd33f16: switched to Svelte's style:width directive, which avoids inline style attributes entirely.

Avoids CSP conflicts for strict Content-Security-Policy environments.
@nico-martin nico-martin self-assigned this Jun 8, 2026

@nico-martin nico-martin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mvanhorn, Thank you very much for the PR. I love to see more examples for different integrations!

There are a couple of small this I mentioned in the files. But otherwise the Transformers.js integration looks solid.


Useful links:

- [Source code](https://github.com/huggingface/transformers.js/tree/main/examples/svelte-translator)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not exist. Do you want to add the source here?
https://github.com/huggingface/transformers.js-examples/


First, let's create some child components. Create a folder called `lib` in the `src` directory, and create the following files:

1. `LanguageSelector.svelte`: This component will allow the user to select the input and output languages. Check out the full list of languages [here](https://github.com/huggingface/transformers.js-examples/tree/main/react-translator/src/components/LanguageSelector.jsx).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should then also point to the svelte example.

{#if ready === false}
<label>Loading models... (only run once)</label>
{/if}
{#each progressItems as data (data.file)}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since version 4.0 we have a new progress_total item in the progress_callback:

const pipe = await pipeline(
  "feature-extraction",
  "onnx-community/all-MiniLM-L6-v2-ONNX",
  {
    progress_callback: (i) => i.status === "progress_total" && console.log(i.progress),
  }
);

I would suggest to only pass the total progress doen to the UI and show that here because in the end thats what is important for the users.

});

function onMessageReceived(e) {
switch (e.data.status) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment on line 177

@pngwn pngwn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @mvanhorn! I left an inline comment about using the sv CLI instead of create-vite. Other than that, the main issue with the PR is that it is using the older Svelte 3/4 syntax. The syntax changed quite a bit in version 5 which has been out for some time, so I think it makes sense to use that syntax here.

The main change is migrating the reactivity system to use runes.

For this tutorial, we will use [Vite](https://vitejs.dev/) to initialise our project. Vite is a build tool that allows us to quickly set up a Svelte application with minimal configuration. Run the following command in your terminal:

```bash
npm create vite@latest svelte-translator -- --template svelte

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The canonical way to create a svelte app is with the svelte CLI sv create.

sv create only scaffolds SvelteKit projects, so the tutorial now targets the
minimal template end to end: src/routes/+page.svelte instead of App.svelte,
components under src/lib, the Web Worker created inside onMount (SvelteKit
server-renders by default), and the deploy step uses adapter-static for the
Static Space.
@mvanhorn

mvanhorn commented Jul 4, 2026

Copy link
Copy Markdown
Author

Done in 307482e - you were right that sv is the way. Since sv create scaffolds SvelteKit projects (there's no plain-SPA template), I migrated the whole tutorial to the SvelteKit minimal template rather than just swapping the scaffold command: src/routes/+page.svelte instead of App.svelte, components under src/lib, the worker created in onMount (with a note on why, since Kit SSRs by default), and the deploy step now points at adapter-static for the Static Space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Doc request] Add an example guide of how to use it in Svelte (and deploy to HF Spaces)

5 participants