Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PaliGemma (& PaliGemma2) #1074

Merged
merged 6 commits into from
Dec 6, 2024
Merged

Add support for PaliGemma (& PaliGemma2) #1074

merged 6 commits into from
Dec 6, 2024

Conversation

xenova
Copy link
Collaborator

@xenova xenova commented Dec 6, 2024

Example:

import { AutoProcessor, PaliGemmaForConditionalGeneration, load_image } from '@huggingface/transformers';

// Load processor and model
const model_id = 'onnx-community/paligemma2-3b-ft-docci-448';
const processor = await AutoProcessor.from_pretrained(model_id);
const model = await PaliGemmaForConditionalGeneration.from_pretrained(model_id, {
    dtype: {
        embed_tokens: 'fp16', // or 'q8'
        vision_encoder: 'fp16', // or 'q4', 'q8'
        decoder_model_merged: 'q4', // or 'q4f16'
    },
});

// Prepare inputs
const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg'
const raw_image = await load_image(url);
const prompt = '<image>caption en'; // Caption the image in English
const inputs = await processor(raw_image, prompt);

// Generate a response
const output = await model.generate({
    ...inputs,
    max_new_tokens: 100,
})

const generated_ids = output.slice(null, [inputs.input_ids.dims[1], null]);
const answer = processor.batch_decode(
    generated_ids,
    { skip_special_tokens: true },
);
console.log(answer[0]);
// A side view of a light blue 1970s Volkswagen Beetle parked on a gray cement road. It is facing to the right. It has a reflection on the side of it. Behind it is a yellow building with a brown double door on the right. It has a white frame around it. Part of a gray cement wall is visible on the far left.

List of supported models: https://huggingface.co/models?library=transformers.js&other=paligemma

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@xenova xenova merged commit 6f27a10 into main Dec 6, 2024
4 checks passed
@xenova xenova deleted the add-paligemma branch December 6, 2024 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants