Skip to content

Commit d9dcbf2

Browse files
authored
update snippets for image-text-to-text models for transformers models (#1434)
1 parent 1dcc11a commit d9dcbf2

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

packages/tasks/src/model-libraries-snippets.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,13 +1142,31 @@ export const transformers = (model: ModelData): string[] => {
11421142
}
11431143

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

1147-
if (model.tags.includes("conversational") && model.config?.tokenizer_config?.chat_template) {
1148-
pipelineSnippet.push("messages = [", ' {"role": "user", "content": "Who are you?"},', "]");
1149-
}
1150-
pipelineSnippet.push(`pipe = pipeline("${model.pipeline_tag}", model="${model.id}"` + remote_code_snippet + ")");
1151-
if (model.tags.includes("conversational") && model.config?.tokenizer_config?.chat_template) {
1152+
if (model.tags.includes("conversational")) {
1153+
if (model.tags.includes("image-text-to-text")) {
1154+
pipelineSnippet.push(
1155+
"messages = [",
1156+
[
1157+
" {",
1158+
' "role": "user",',
1159+
' "content": [',
1160+
' {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"},',
1161+
' {"type": "text", "text": "What animal is on the candy?"}',
1162+
" ]",
1163+
" },",
1164+
].join("\n"),
1165+
"]"
1166+
);
1167+
} else {
1168+
pipelineSnippet.push("messages = [", ' {"role": "user", "content": "Who are you?"},', "]");
1169+
}
11521170
pipelineSnippet.push("pipe(messages)");
11531171
}
11541172

0 commit comments

Comments
 (0)