Views
No views yet
npm i @xenova/transformerspipeline API.1const classifier = await pipeline('zero-shot-image-classification', 'Xenova/clip-vit-base-patch16');
2const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
3const output = await classifier(url, ['tiger', 'horse', 'dog']);
4// [
5// { score: 0.9993917942047119, label: 'tiger' },
6// { score: 0.0003519294841680676, label: 'horse' },
7// { score: 0.0002562698791734874, label: 'dog' }
8// ]CLIPModel.1import { AutoTokenizer, AutoProcessor, CLIPModel, RawImage } from '@xenova/transformers';
2
3// Load tokenizer, processor, and model
4const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16');
5const processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16');
6const model = await CLIPModel.from_pretrained('Xenova/clip-vit-base-patch16');
7
8// Run tokenization
9const texts = ['a photo of a car', 'a photo of a football match'];
10const text_inputs = tokenizer(texts, { padding: true, truncation: true });
11
12// Read image and run processor
13const image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg');
14const image_inputs = await processor(image);
15
16// Run model with both text and pixel inputs
17const output = await model({ ...text_inputs, ...image_inputs });
18// {
19// logits_per_image: Tensor {
20// dims: [ 1, 2 ],
21// data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ],
22// },
23// logits_per_text: Tensor {
24// dims: [ 2, 1 ],
25// data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ],
26// },
27// text_embeds: Tensor {
28// dims: [ 2, 512 ],
29// data: Float32Array(1024) [ ... ],
30// },
31// image_embeds: Tensor {
32// dims: [ 1, 512 ],
33// data: Float32Array(512) [ ... ],
34// }
35// }CLIPTextModelWithProjection.1import { AutoTokenizer, CLIPTextModelWithProjection } from '@xenova/transformers';
2
3// Load tokenizer and text model
4const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16');
5const text_model = await CLIPTextModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16');
6
7// Run tokenization
8const texts = ['a photo of a car', 'a photo of a football match'];
9const text_inputs = tokenizer(texts, { padding: true, truncation: true });
10
11// Compute embeddings
12const { text_embeds } = await text_model(text_inputs);
13// Tensor {
14// dims: [ 2, 512 ],
15// type: 'float32',
16// data: Float32Array(1024) [ ... ],
17// size: 1024
18// }CLIPVisionModelWithProjection.1import { AutoProcessor, CLIPVisionModelWithProjection, RawImage } from '@xenova/transformers';
2
3// Load processor and vision model
4const processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16');
5const vision_model = await CLIPVisionModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16');
6
7// Read image and run processor
8const image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg');
9const image_inputs = await processor(image);
10
11// Compute embeddings
12const { image_embeds } = await vision_model(image_inputs);
13// Tensor {
14// dims: [ 1, 512 ],
15// type: 'float32',
16// data: Float32Array(512) [ ... ],
17// size: 512
18// }onnx).