-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathcaptioning.js
64 lines (56 loc) · 1.91 KB
/
captioning.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {
Florence2ForConditionalGeneration,
AutoProcessor,
AutoTokenizer,
} from "@huggingface/transformers";
export class Caption {
/**
* Create a new Caption model.
* @param {import('@huggingface/transformers').PreTrainedModel} model The model to use for captioning
* @param {import('@huggingface/transformers').Processor} processor The processor to use for captioning
* @param {import('@huggingface/transformers').PreTrainedTokenizer} tokenizer The tokenizer to use for captioning
*/
constructor(model, processor, tokenizer) {
this.model = model;
this.processor = processor;
this.tokenizer = tokenizer;
// Prepare text inputs
this.task = "<CAPTION>";
const prompts = processor.construct_prompts(this.task);
this.text_inputs = tokenizer(prompts);
}
/**
* Generate a caption for an image.
* @param {import('@huggingface/transformers').RawImage} image The input image.
* @returns {Promise<string>} The caption for the image
*/
async describe(image) {
const vision_inputs = await this.processor(image);
// Generate text
const generated_ids = await this.model.generate({
...this.text_inputs,
...vision_inputs,
max_new_tokens: 256,
});
// Decode generated text
const generated_text = this.tokenizer.batch_decode(generated_ids, {
skip_special_tokens: false,
})[0];
// Post-process the generated text
const result = this.processor.post_process_generation(
generated_text,
this.task,
image.size,
);
return result[this.task];
}
static async from_pretrained(model_id) {
const model = await Florence2ForConditionalGeneration.from_pretrained(
model_id,
{ dtype: "fp32" },
);
const processor = await AutoProcessor.from_pretrained(model_id);
const tokenizer = await AutoTokenizer.from_pretrained(model_id);
return new Caption(model, processor, tokenizer);
}
}