https://huggingface.co/microsoft/Florence-2-base with ONNX weights to be compatible with Transformers.js.
If you haven't already, you can install the Transformers.js JavaScript library from NPM using:
npm i @huggingface/transformers
Example: Perform image captioning with onnx-community/Florence-2-base.
import {
Florence2ForConditionalGeneration,
AutoProcessor,
load_image,
} from '@huggingface/transformers';
// Load model, processor, and tokenizer
const model_id = 'onnx-community/Florence-2-base';
const model = await Florence2ForConditionalGeneration.from_pretrained(model_id, { dtype: 'fp32' });
const processor = await AutoProcessor.from_pretrained(model_id);
// Load image and prepare vision inputs
const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg';
const image = await load_image(url);
// Specify task and prepare text inputs
const task = '<MORE_DETAILED_CAPTION>';
const prompts = processor.construct_prompts(task);
// Pre-process the image and text inputs
const inputs = await processor(image, prompts);
// Generate text
const generated_ids = await model.generate({
...inputs,
max_new_tokens: 100,
});
// Decode generated text
const generated_text = processor.batch_decode(generated_ids, { skip_special_tokens: false })[0];
// Post-process the generated text
const result = processor.post_process_generation(generated_text, task, image.size);
console.log(result);
// { '<MORE_DETAILED_CAPTION>': 'The image shows a vintage Volkswagen Beetle car parked on a cobblestone street in front of a yellow building with two wooden doors. The car is a light green color with silver rims and appears to be in good condition. The building has a sloping roof and is painted in a combination of yellow and beige colors. The sky is blue and there are trees in the background. The overall mood of the image is peaceful and serene.' }
We also released an online demo, which you can try yourself: https://huggingface.co/spaces/Xenova/florence2-webgpu
Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using 🤗 Optimum and structuring your repo like this one (with ONNX weights located in a subfolder named onnx).
onnx/decoder_model_merged.onnx 388 MBonnx/decoder_model.onnx 388 MBonnx/vision_encoder.onnx 367 MBonnx/decoder_with_past_model.onnx 360 MBonnx/decoder_model_merged_fp16.onnx 195 MBonnx/decoder_model_fp16.onnx 194 MBonnx/vision_encoder_fp16.onnx 184 MBonnx/decoder_with_past_model_fp16.onnx 180 MBonnx/encoder_model.onnx 173 MBonnx/embed_tokens_bnb4.onnx 158 MBonnx/embed_tokens_q4.onnx 158 MBonnx/embed_tokens.onnx 158 MBonnx/decoder_model_merged_int8.onnx 98 MBonnx/decoder_model_merged_quantized.onnx 98 MBonnx/decoder_model_merged_uint8.onnx 98 MBonnx/decoder_model_uint8.onnx 98 MBonnx/decoder_model_int8.onnx 98 MBonnx/decoder_model_quantized.onnx 98 MBonnx/vision_encoder_quantized.onnx 94 MBonnx/vision_encoder_uint8.onnx 94 MBonnx/vision_encoder_int8.onnx 94 MBonnx/decoder_with_past_model_uint8.onnx 91 MBonnx/decoder_with_past_model_int8.onnx 91 MBonnx/decoder_with_past_model_quantized.onnx 91 MBonnx/encoder_model_fp16.onnx 87 MBonnx/vision_encoder_q4.onnx 81 MBonnx/embed_tokens_q4f16.onnx 79 MBonnx/embed_tokens_fp16.onnx 79 MBonnx/vision_encoder_bnb4.onnx 76 MBonnx/decoder_model_merged_q4.onnx 64 MBonnx/decoder_model_q4.onnx 64 MBonnx/vision_encoder_q4f16.onnx 62 MBonnx/decoder_with_past_model_q4.onnx 60 MBonnx/decoder_model_merged_bnb4.onnx 58 MBonnx/decoder_model_bnb4.onnx 58 MBonnx/decoder_model_merged_q4f16.onnx 57 MBonnx/decoder_model_q4f16.onnx 56 MBonnx/decoder_with_past_model_bnb4.onnx 54 MBonnx/decoder_with_past_model_q4f16.onnx 52 MBonnx/encoder_model_uint8.onnx 44 MBonnx/encoder_model_int8.onnx 44 MBonnx/encoder_model_quantized.onnx 44 MBonnx/embed_tokens_int8.onnx 39 MBonnx/embed_tokens_quantized.onnx 39 MBonnx/embed_tokens_uint8.onnx 39 MBonnx/encoder_model_q4.onnx 30 MBonnx/encoder_model_bnb4.onnx 27 MBonnx/encoder_model_q4f16.onnx 26 MB