Views
No views yet
npm i @huggingface/transformersonnx-community/Florence-2-base-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-base-ft';
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>': 'A green car is parked in front of a tan building. There is a brown door on the building behind the car. There are two windows on the front of the building. ' }onnx).