Views
No views yet
npm i @huggingface/transformersonnx-community/Florence-2-base.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-base';
9const model = await Florence2ForConditionalGeneration.from_pretrained(model_id, { dtype: 'fp32' });
10const processor = await AutoProcessor.from_pretrained(model_id);
11
12// Load image and prepare vision inputs
13const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg';
14const image = await load_image(url);
15
16// Specify task and prepare text inputs
17const task = '<MORE_DETAILED_CAPTION>';
18const prompts = processor.construct_prompts(task);
19
20// Pre-process the image and text inputs
21const inputs = await processor(image, prompts);
22
23// Generate text
24const generated_ids = await model.generate({
25 ...inputs,
26 max_new_tokens: 100,
27});
28
29// Decode generated text
30const generated_text = processor.batch_decode(generated_ids, { skip_special_tokens: false })[0];
31
32// Post-process the generated text
33const result = processor.post_process_generation(generated_text, task, image.size);
34console.log(result);
35// { '<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.' }onnx).