From ae9275e9ff3157496df9139fcafe0c86503167b6 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Sun, 18 Jun 2023 18:38:38 +0200 Subject: [PATCH 1/7] Add Transformers.js as a supported library --- docs/hub/_toctree.yml | 2 + docs/hub/models-libraries.md | 1 + docs/hub/transformers-js.md | 74 +++++++++++++++++++++++ js/src/lib/interfaces/Libraries.ts | 21 +++++++ js/src/lib/interfaces/LibrariesToTasks.ts | 19 ++++++ tasks/src/const.ts | 34 +++++------ 6 files changed, 134 insertions(+), 17 deletions(-) create mode 100644 docs/hub/transformers-js.md diff --git a/docs/hub/_toctree.yml b/docs/hub/_toctree.yml index 41d5bbbc0..e4b5e1214 100644 --- a/docs/hub/_toctree.yml +++ b/docs/hub/_toctree.yml @@ -89,6 +89,8 @@ title: TensorBoard - local: timm title: timm + - local: transformers-js + title: Transformers.js - local: models-widgets title: Model Widgets sections: diff --git a/docs/hub/models-libraries.md b/docs/hub/models-libraries.md index ebbeb55eb..478087edb 100644 --- a/docs/hub/models-libraries.md +++ b/docs/hub/models-libraries.md @@ -33,6 +33,7 @@ The table below summarizes the supported libraries and their level of integratio | [Stable-Baselines3](https://github.com/DLR-RM/stable-baselines3) | Set of reliable implementations of deep reinforcement learning algorithms in PyTorch | ❌ | ✅ | ✅ | ✅ | | [TensorFlowTTS](https://github.com/TensorSpeech/TensorFlowTTS) | Real-time state-of-the-art speech synthesis architectures. | ❌ | ❌ | ✅ | ❌ | | [Timm](https://github.com/rwightman/pytorch-image-models) | Collection of image models, scripts, pretrained weights, etc. | ✅ | ✅ | ✅ | ✅ | +| [Transformers.js](https://github.com/xenova/transformers.js) | State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server! | ❌ | ❌ | ✅ | ❌ | ### How can I add a new library to the Inference API? diff --git a/docs/hub/transformers-js.md b/docs/hub/transformers-js.md new file mode 100644 index 000000000..e8f73f598 --- /dev/null +++ b/docs/hub/transformers-js.md @@ -0,0 +1,74 @@ +# Using `Transformers.js` at Hugging Face + +Transformers.js is a JavaScript library for running 🤗 Transformers directly in your browser, with no need for a server! It is designed to be functionally equivalent to the [python library](https://github.com/huggingface/transformers), meaning you can run the same pretrained models using a very similar API. + +## Exploring transformers.js in the Hub + +You can find `transformers.js` models by filtering at the left of the [models page](https://huggingface.co/models?library=transformers.js). + + + +## Quick tour + + +It's super simple to translate from existing code! Just like the python library, we support the `pipeline` API. Pipelines group together a pretrained model with preprocessing of inputs and postprocessing of outputs, making it the easiest way to run models with the library. + + + + + + + + + + +
Python (original)Javascript (ours)
+ +```python +from transformers import pipeline + +# Allocate a pipeline for sentiment-analysis +pipe = pipeline('sentiment-analysis') + +out = pipe('I love transformers!') +# [{'label': 'POSITIVE', 'score': 0.999806941}] +``` + + + +```javascript +import { pipeline } from '@xenova/transformers'; + +// Allocate a pipeline for sentiment-analysis +let pipe = await pipeline('sentiment-analysis'); + +let out = await pipe('I love transformers!'); +// [{'label': 'POSITIVE', 'score': 0.999817686}] +``` + +
+ + +You can also use a different model by specifying the model id or path as the second argument to the `pipeline` function. For example: +```javascript +// Use a different model for sentiment-analysis +let pipe = await pipeline('sentiment-analysis', 'nlptown/bert-base-multilingual-uncased-sentiment'); +``` + +Refer to the [documentation](https://huggingface.co/docs/transformers.js) for the full list of supported tasks and models. + +## Installation + +To install via [NPM](https://www.npmjs.com/package/@xenova/transformers), run: +```bash +npm i @xenova/transformers +``` + +For more information, including how to use it in vanilla JS (without any bundler) via a CDN or static hosting, refer to the [README](https://github.com/xenova/transformers.js/blob/main/README.md#installation). + + +## Additional resources + +* Transformers.js [repository](https://github.com/xenova/transformers.js) +* Transformers.js [docs](https://huggingface.co/docs/transformers.js) +* Transformers.js [demo](https://xenova.github.io/transformers.js/) diff --git a/js/src/lib/interfaces/Libraries.ts b/js/src/lib/interfaces/Libraries.ts index 618605a34..668c9039b 100644 --- a/js/src/lib/interfaces/Libraries.ts +++ b/js/src/lib/interfaces/Libraries.ts @@ -31,6 +31,7 @@ export enum ModelLibrary { "timm" = "Timm", "fastai" = "fastai", "transformers" = "Transformers", + "transformers.js" = "Transformers.js", "stanza" = "Stanza", "fasttext" = "fastText", "stable-baselines3" = "Stable-Baselines3", @@ -395,6 +396,20 @@ const transformers = (model: ModelData) => { } }; +const transformersJS = (model: ModelData) => { + if (!model.pipeline_tag) { + return `// ⚠️ Unknown pipeline tag`; + } + + const libName = '@xenova/transformers'; + + return `// npm i ${libName} +import { pipeline } from '${libName}'; + +// Allocate pipeline +let pipe = await pipeline('${model.pipeline_tag}', '${model.id}');`; +}; + const peftTask = (peftTaskType?: string) => { switch (peftTaskType) { case "CAUSAL_LM": @@ -623,6 +638,12 @@ export const MODEL_LIBRARIES_UI_ELEMENTS: Partial = { "audio-classification": ["speechbrain", "transformers"], "audio-to-audio": ["asteroid", "speechbrain"], - "automatic-speech-recognition": ["espnet", "nemo", "speechbrain", "transformers"], + "automatic-speech-recognition": ["espnet", "nemo", "speechbrain", "transformers", "transformers.js"], "conversational": ["transformers"], "depth-estimation": ["transformers"], "document-question-answering": ["transformers"], - "feature-extraction": ["sentence-transformers", "transformers"], - "fill-mask": ["transformers"], + "feature-extraction": ["sentence-transformers", "transformers", "transformers.js"], + "fill-mask": ["transformers", "transformers.js"], "graph-ml": ["transformers"], - "image-classification": ["keras", "timm", "transformers"], - "image-segmentation": ["transformers"], + "image-classification": ["keras", "timm", "transformers", "transformers.js"], + "image-segmentation": ["transformers", "transformers.js"], "image-to-image": [], - "image-to-text": [], + "image-to-text": ["transformers.js"], "video-classification": [], "multiple-choice": ["transformers"], - "object-detection": ["transformers"], + "object-detection": ["transformers", "transformers.js"], "other": [], - "question-answering": ["adapter-transformers", "allennlp", "transformers"], + "question-answering": ["adapter-transformers", "allennlp", "transformers", "transformers.js"], "robotics": [], "reinforcement-learning": ["transformers", "stable-baselines3", "ml-agents", "sample-factory"], - "sentence-similarity": ["sentence-transformers", "spacy"], - "summarization": ["transformers"], + "sentence-similarity": ["sentence-transformers", "spacy", "transformers.js"], + "summarization": ["transformers", "transformers.js"], "table-question-answering": ["transformers"], "table-to-text": ["transformers"], "tabular-classification": ["sklearn"], "tabular-regression": ["sklearn"], "tabular-to-text": ["transformers"], - "text-classification": ["adapter-transformers", "spacy", "transformers"], - "text-generation": ["transformers"], + "text-classification": ["adapter-transformers", "spacy", "transformers", "transformers.js"], + "text-generation": ["transformers", "transformers.js"], "text-retrieval": [], "text-to-image": [], "text-to-speech": ["espnet", "tensorflowtts"], "text-to-video": [], - "text2text-generation": ["transformers"], + "text2text-generation": ["transformers", "transformers.js"], "time-series-forecasting": [], - "token-classification": ["adapter-transformers", "flair", "spacy", "span-marker", "stanza", "transformers"], - "translation": ["transformers"], + "token-classification": ["adapter-transformers", "flair", "spacy", "span-marker", "stanza", "transformers", "transformers.js"], + "translation": ["transformers", "transformers.js"], "unconditional-image-generation": [], "visual-question-answering": [], "voice-activity-detection": [], - "zero-shot-classification": ["transformers"], - "zero-shot-image-classification": [], + "zero-shot-classification": ["transformers", "transformers.js"], + "zero-shot-image-classification": ["transformers.js"], }; From 86bbbcf0de99ce0e74ad4304a0930dcdbfa91b43 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Tue, 20 Jun 2023 13:09:37 +0200 Subject: [PATCH 2/7] Fix typo in docs/hub/transformers-js.md Co-authored-by: Omar Sanseviero --- docs/hub/transformers-js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/hub/transformers-js.md b/docs/hub/transformers-js.md index e8f73f598..200e79f4c 100644 --- a/docs/hub/transformers-js.md +++ b/docs/hub/transformers-js.md @@ -11,7 +11,7 @@ You can find `transformers.js` models by filtering at the left of the [models pa ## Quick tour -It's super simple to translate from existing code! Just like the python library, we support the `pipeline` API. Pipelines group together a pretrained model with preprocessing of inputs and postprocessing of outputs, making it the easiest way to run models with the library. +It's super simple to translate from existing code! Just like the Python library, we support the `pipeline` API. Pipelines group together a pretrained model with preprocessing of inputs and postprocessing of outputs, making it the easiest way to run models with the library. From b07acec2b9cbc5aa6111e8396c7f9b87e51e1cf6 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Tue, 20 Jun 2023 13:21:52 +0200 Subject: [PATCH 3/7] Update docs/hub/transformers-js.md Co-authored-by: Pedro Cuenca --- docs/hub/transformers-js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/hub/transformers-js.md b/docs/hub/transformers-js.md index 200e79f4c..faae12a3a 100644 --- a/docs/hub/transformers-js.md +++ b/docs/hub/transformers-js.md @@ -2,7 +2,7 @@ Transformers.js is a JavaScript library for running 🤗 Transformers directly in your browser, with no need for a server! It is designed to be functionally equivalent to the [python library](https://github.com/huggingface/transformers), meaning you can run the same pretrained models using a very similar API. -## Exploring transformers.js in the Hub +## Exploring `transformers.js` in the Hub You can find `transformers.js` models by filtering at the left of the [models page](https://huggingface.co/models?library=transformers.js). From 96608f84d4a675489f991e47ec5a38c4e2566b82 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Tue, 20 Jun 2023 13:45:38 +0200 Subject: [PATCH 4/7] Use `const` instead of `let` in example code See js/src/lib/interfaces/Libraries.ts Co-authored-by: Julien Chaumond --- js/src/lib/interfaces/Libraries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/src/lib/interfaces/Libraries.ts b/js/src/lib/interfaces/Libraries.ts index 668c9039b..78a7f79df 100644 --- a/js/src/lib/interfaces/Libraries.ts +++ b/js/src/lib/interfaces/Libraries.ts @@ -407,7 +407,7 @@ const transformersJS = (model: ModelData) => { import { pipeline } from '${libName}'; // Allocate pipeline -let pipe = await pipeline('${model.pipeline_tag}', '${model.id}');`; +const pipe = await pipeline('${model.pipeline_tag}', '${model.id}');`; }; const peftTask = (peftTaskType?: string) => { From 33e9bca39389db6f8b34a7dd8fa49eafdd50116e Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Tue, 20 Jun 2023 13:46:59 +0200 Subject: [PATCH 5/7] Update docs/hub/transformers-js.md Co-authored-by: Pedro Cuenca --- docs/hub/transformers-js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/hub/transformers-js.md b/docs/hub/transformers-js.md index faae12a3a..922c216cb 100644 --- a/docs/hub/transformers-js.md +++ b/docs/hub/transformers-js.md @@ -4,7 +4,7 @@ Transformers.js is a JavaScript library for running 🤗 Transformers directly i ## Exploring `transformers.js` in the Hub -You can find `transformers.js` models by filtering at the left of the [models page](https://huggingface.co/models?library=transformers.js). +You can find `transformers.js` models by filtering by library in the [models page](https://huggingface.co/models?library=transformers.js). From c736384ab1ca1599dbc4dac31f28105d123ce6e7 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Tue, 20 Jun 2023 13:49:53 +0200 Subject: [PATCH 6/7] Remove transformers.js from list of inference widgets --- js/src/lib/interfaces/LibrariesToTasks.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/js/src/lib/interfaces/LibrariesToTasks.ts b/js/src/lib/interfaces/LibrariesToTasks.ts index 0d0011aa6..cd528fa7b 100644 --- a/js/src/lib/interfaces/LibrariesToTasks.ts +++ b/js/src/lib/interfaces/LibrariesToTasks.ts @@ -98,25 +98,6 @@ export const LIBRARY_TASK_MAPPING_EXCLUDING_TRANSFORMERS: Partial Date: Tue, 20 Jun 2023 13:50:56 +0200 Subject: [PATCH 7/7] Update docs/hub/transformers-js.md Co-authored-by: Pedro Cuenca --- docs/hub/transformers-js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/hub/transformers-js.md b/docs/hub/transformers-js.md index 922c216cb..d095dbdda 100644 --- a/docs/hub/transformers-js.md +++ b/docs/hub/transformers-js.md @@ -1,6 +1,6 @@ # Using `Transformers.js` at Hugging Face -Transformers.js is a JavaScript library for running 🤗 Transformers directly in your browser, with no need for a server! It is designed to be functionally equivalent to the [python library](https://github.com/huggingface/transformers), meaning you can run the same pretrained models using a very similar API. +Transformers.js is a JavaScript library for running 🤗 Transformers directly in your browser, with no need for a server! It is designed to be functionally equivalent to the original [Python library](https://github.com/huggingface/transformers), meaning you can run the same pretrained models using a very similar API. ## Exploring `transformers.js` in the Hub