Skip to content

update snippets for image-text-to-text models for transformers models #1434

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
30 changes: 24 additions & 6 deletions packages/tasks/src/model-libraries-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1128,13 +1128,31 @@ export const transformers = (model: ModelData): string[] => {
}

if (model.pipeline_tag && LIBRARY_TASK_MAPPING.transformers?.includes(model.pipeline_tag)) {
const pipelineSnippet = ["# Use a pipeline as a high-level helper", "from transformers import pipeline", ""];
const pipelineSnippet = [
"# Use a pipeline as a high-level helper",
"from transformers import pipeline",
"",
`pipe = pipeline("${model.pipeline_tag}", model="${model.id}"` + remote_code_snippet + ")",
];

if (model.tags.includes("conversational") && model.config?.tokenizer_config?.chat_template) {
pipelineSnippet.push("messages = [", ' {"role": "user", "content": "Who are you?"},', "]");
}
pipelineSnippet.push(`pipe = pipeline("${model.pipeline_tag}", model="${model.id}"` + remote_code_snippet + ")");
if (model.tags.includes("conversational") && model.config?.tokenizer_config?.chat_template) {
if (model.tags.includes("conversational")) {
if (model.tags.includes("image-text-to-text")) {
pipelineSnippet.push(
"messages = [",
[
" {",
' "role": "user",',
' "content": [',
' {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"},',
' {"type": "text", "text": "What animal is on the candy?"}',
" ]",
" },",
].join("\n"),
"]"
);
} else {
pipelineSnippet.push("messages = [", ' {"role": "user", "content": "Who are you?"},', "]");
}
pipelineSnippet.push("pipe(messages)");
}

Expand Down