🚀 Transformers.js v3.1 — any-to-any, text-to-image, image-to-text, pose estimation, time series forecasting, and more!
Table of contents:
- [🤖 New models: Janus, Qwen2-VL, JinaCLIP, LLaVA-OneVision, ViTPose, MGP-STR, PatchTST, PatchTSMixer.](new-models)
- [**Janus**: Any-to-Any generation](janus)
- [**Qwen2-VL**: Image-Text-to-Text](qwen2vl)
- [**JinaCLIP**: Multimodal embeddings](jina_clip)
- [**LLaVA-OneVision**: Image-Text-to-Text](llava_onevision)
- [**ViTPose**: Pose-estimation](vitpose)
- [**MGP-STR**: Optical Character Recognition (OCR)](mgp-str)
- [**PatchTST and PatchTSMixer**: Time series forecasting.](patchtst-and-patchtsmixer)
- [🐛 Bug fixes](bug-fixes)
- [📝 Documentation improvements](documentation-improvements)
- [🛠️ Other improvements](other-improvements)
- [🤗 New contributors](new-contributors)
<h2 id="new-models">🤖 New models: Janus, Qwen2-VL, JinaCLIP, LLaVA-OneVision, ViTPose, MGP-STR, PatchTST, PatchTSMixer.</h2>
<h3 id="janus">Janus for Any-to-Any generation (e.g., image-to-text and text-to-image)</h3>
First of all, this release adds support for Janus, a novel autoregressive framework that unifies multimodal understanding and generation. The most popular model, [deepseek-ai/Janus-1.3B](https://huggingface.co/deepseek-ai/Janus-1.3B), is tagged as an "any-to-any" model, and has specifically been trained for the following tasks:
**Example:** Image-Text-to-Text
js
import { AutoProcessor, MultiModalityCausalLM } from "huggingface/transformers";
// Load processor and model
const model_id = "onnx-community/Janus-1.3B-ONNX";
const processor = await AutoProcessor.from_pretrained(model_id);
const model = await MultiModalityCausalLM.from_pretrained(model_id);
// Prepare inputs
const conversation = [
{
role: "User",
content: "<image_placeholder>\nConvert the formula into latex code.",
images: ["https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/quadratic_formula.png"],
},
];
const inputs = await processor(conversation);
// Generate response
const outputs = await model.generate({
...inputs,
max_new_tokens: 150,
do_sample: false,
});
// Decode output
const new_tokens = outputs.slice(null, [inputs.input_ids.dims.at(-1), null]);
const decoded = processor.batch_decode(new_tokens, { skip_special_tokens: true });
console.log(decoded[0]);
Sample output:
`
Sure, here is the LaTeX code for the given formula:
x = \frac{-b \pm \sqrt{b^2 - 4a c}}{2a}
This code represents the mathematical expression for the variable \( x \).
`
**Example:** Text-to-Image
js
import { AutoProcessor, MultiModalityCausalLM } from "huggingface/transformers";
// Load processor and model
const model_id = "onnx-community/Janus-1.3B-ONNX";
const processor = await AutoProcessor.from_pretrained(model_id);
const model = await MultiModalityCausalLM.from_pretrained(model_id);
// Prepare inputs
const conversation = [
{
role: "User",
content: "A cute and adorable baby fox with big brown eyes, autumn leaves in the background enchanting,immortal,fluffy, shiny mane,Petals,fairyism,unreal engine 5 and Octane Render,highly detailed, photorealistic, cinematic, natural colors.",
},
];
const inputs = await processor(conversation, { chat_template: "text_to_image" });
// Generate response
const num_image_tokens = processor.num_image_tokens;
const outputs = await model.generate_images({
...inputs,
min_new_tokens: num_image_tokens,
max_new_tokens: num_image_tokens,
do_sample: true,
});
// Save the generated image
await outputs[0].save("test.png");
Sample outputs:
| ![fox_1](https://github.com/user-attachments/assets/c8a4f588-655f-440e-bd55-79d19505edae) | ![fox_2](https://github.com/user-attachments/assets/88b5003a-82de-4ef9-8315-6cb59aee607d) | ![fox_3](https://github.com/user-attachments/assets/f92ed498-4a32-4757-86de-cac37bc8fbf6) | ![fox_4](https://github.com/user-attachments/assets/51b9d0a6-c737-499d-983e-d89ff023282d) |
|---|---|---|---|
| ![fox_5](https://github.com/user-attachments/assets/8876ebb0-fea2-4443-b458-fdd6c035a69f) | ![fox_6](https://github.com/user-attachments/assets/1989f128-5fd4-4b0c-83b4-dc5f33b388c2) | ![fox_7](https://github.com/user-attachments/assets/1fa9ac58-ca14-4ee3-84ca-47e69de2589c) | ![fox_8](https://github.com/user-attachments/assets/20a20642-a336-4277-9056-f45d7ddb3bbe) |
What to play around with the model? Check out our [online WebGPU demo](https://huggingface.co/spaces/webml-community/Janus-1.3B-WebGPU)! 👇
https://github.com/user-attachments/assets/513b3119-ba8c-4a2d-b5fe-6869be47abfa
<h3 id="qwen2vl">Qwen2-VL for Image-Text-to-Text</h3>
**Example:** Image-Text-to-Text
Next, we added support for Qwen2-VL, the multimodal large language model series developed by Qwen team, Alibaba Cloud. It introduces the Naive Dynamic Resolution mechanism, allowing the model to process images of varying resolutions and leading to more efficient and accurate visual representations.
js
import { AutoProcessor, Qwen2VLForConditionalGeneration, RawImage } from "huggingface/transformers";
// Load processor and model
const model_id = "onnx-community/Qwen2-VL-2B-Instruct";
const processor = await AutoProcessor.from_pretrained(model_id);
const model = await Qwen2VLForConditionalGeneration.from_pretrained(model_id);
// Prepare inputs
const url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg";
const image = await (await RawImage.read(url)).resize(448, 448);
const conversation = [
{
role: "user",
content: [
{ type: "image" },
{ type: "text", text: "Describe this image." },
],
},
];
const text = processor.apply_chat_template(conversation, { add_generation_prompt: true });
const inputs = await processor(text, image);
// Perform inference
const outputs = await model.generate({
...inputs,
max_new_tokens: 128,
});
// Decode output
const decoded = processor.batch_decode(
outputs.slice(null, [inputs.input_ids.dims.at(-1), null]),
{ skip_special_tokens: true },
);
console.log(decoded[0]);
// The image depicts a serene beach scene with a woman and a dog. The woman is sitting on the sand, wearing a plaid shirt, and appears to be engaged in a playful interaction with the dog. The dog, which is a large breed, is sitting on its hind legs and appears to be reaching out to the woman, possibly to give her a high-five or a paw. The background shows the ocean with gentle waves, and the sky is clear, suggesting it might be either sunrise or sunset. The overall atmosphere is calm and relaxed, capturing a moment of connection between the woman and the dog.
<h3 id="jina_clip">JinaCLIP for multimodal embeddings</h3>
JinaCLIP is a series of general-purpose multilingual multimodal embedding models for text & images, created by Jina AI.
**Example:** Compute text and/or image embeddings with `jinaai/jina-clip-v2`:
js
import { AutoModel, AutoProcessor, RawImage, matmul } from "huggingface/transformers";
// Load processor and model
const model_id = "jinaai/jina-clip-v2";
const processor = await AutoProcessor.from_pretrained(model_id);
const model = await AutoModel.from_pretrained(model_id, { dtype: "q4" /* e.g., "fp16", "q8", or "q4" */ });
// Prepare inputs
const urls = ["https://i.ibb.co/nQNGqL0/beach1.jpg", "https://i.ibb.co/r5w8hG8/beach2.jpg"];
const images = await Promise.all(urls.map(url => RawImage.read(url)));
const sentences = [
"غروب جميل على الشاطئ", // Arabic
"海滩上美丽的日落", // Chinese
"Un beau coucher de soleil sur la plage", // French
"Ein wunderschöner Sonnenuntergang am Strand", // German
"Ένα όμορφο ηλιοβασίλεμα πάνω από την παραλία", // Greek
"समुद्र तट पर एक खूबसूरत सूर्यास्त", // Hindi
"Un bellissimo tramonto sulla spiaggia", // Italian
"浜辺に沈む美しい夕日", // Japanese
"해변 위로 아름다운 일몰", // Korean
];
// Encode text and images
const inputs = await processor(sentences, images, { padding: true, truncation: true });
const { l2norm_text_embeddings, l2norm_image_embeddings } = await model(inputs);
// Encode query (text-only)
const query_prefix = "Represent the query for retrieving evidence documents: ";
const query_inputs = await processor(query_prefix + "beautiful sunset over the beach");
const { l2norm_text_embeddings: query_embeddings } = await model(query_inputs);
// Compute text-image similarity scores
const text_to_image_scores = await matmul(query_embeddings, l2norm_image_embeddings.transpose(1, 0));