Views
No views yet
npm i @huggingface/transformersonnx-community/Florence-2-large-ft.1import {
2 Florence2ForConditionalGeneration,
3 AutoProcessor,
4 load_image,
5} from '@huggingface/transformers';
6
7// Load model, processor, and tokenizer
8const model_id = 'onnx-community/Florence-2-large-ft';
9const model = await Florence2ForConditionalGeneration.from_pretrained(model_id, {
10 dtype: {
11 embed_tokens: 'fp16', // or 'fp32'
12 vision_encoder: 'fp16', // or 'fp32'
13 encoder_model: 'q4',
14 decoder_model_merged: 'q4',
15 },
16});
17const processor = await AutoProcessor.from_pretrained(model_id);
18
19// Load image and prepare vision inputs
20const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg';
21const image = await load_image(url);
22
23// Specify task and prepare text inputs
24const task = '<MORE_DETAILED_CAPTION>';
25const prompts = processor.construct_prompts(task);
26
27// Pre-process the image and text inputs
28const inputs = await processor(image, prompts);
29
30// Generate text
31const generated_ids = await model.generate({
32 ...inputs,
33 max_new_tokens: 256,
34});
35
36// Decode generated text
37const generated_text = processor.batch_decode(generated_ids, { skip_special_tokens: false })[0];
38
39// Post-process the generated text
40const result = processor.post_process_generation(generated_text, task, image.size);
41console.log(result);
42// { '<MORE_DETAILED_CAPTION>': 'A car is parked on the street. The car is a light green color. The doors on the building are brown. The building is a yellow color. There are two doors on both sides of the car. The wheels on the car are very shiny. The ground is made of bricks. The sky is blue. The sun is shining on the top of the building.' }onnx).