Skip to content

Commit 38dbee7

Browse files
committed
Add code snippets for image-text-to-text
1 parent cee831a commit 38dbee7

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

packages/tasks/src/snippets/curl.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ export const snippetTextGeneration = (model: ModelDataMinimal, accessToken: stri
2727
}
2828
};
2929

30+
export const snippetImageTextToTextGeneration = (model: ModelDataMinimal, accessToken: string): string => {
31+
if (model.config?.tokenizer_config?.chat_template) {
32+
// Conversational model detected, so we display a code snippet that features the Messages API
33+
return `curl 'https://api-inference.huggingface.co/models/${model.id}/v1/chat/completions' \\
34+
-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}" \\
35+
-H 'Content-Type: application/json' \\
36+
-d '{
37+
"model": "${model.id}",
38+
"messages": [
39+
{
40+
"role": "user",
41+
"content": [
42+
{"type": "image_url", "image_url": {"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"}},
43+
{"type": "text", "text": "Describe this image in one sentence."},
44+
],
45+
}
46+
],
47+
"max_tokens": 500,
48+
"stream": false
49+
}'
50+
`;
51+
} else {
52+
return snippetBasic(model, accessToken);
53+
}
54+
};
55+
3056
export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string): string =>
3157
`curl https://api-inference.huggingface.co/models/${model.id} \\
3258
-X POST \\
@@ -51,6 +77,7 @@ export const curlSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal
5177
summarization: snippetBasic,
5278
"feature-extraction": snippetBasic,
5379
"text-generation": snippetTextGeneration,
80+
"image-text-to-text": snippetImageTextToTextGeneration,
5481
"text2text-generation": snippetBasic,
5582
"fill-mask": snippetBasic,
5683
"sentence-similarity": snippetBasic,

packages/tasks/src/snippets/js.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@ for await (const chunk of inference.chatCompletionStream({
4141
return snippetBasic(model, accessToken);
4242
}
4343
};
44+
45+
export const snippetImageTextToTextGeneration = (model: ModelDataMinimal, accessToken: string): string => {
46+
if (model.config?.tokenizer_config?.chat_template) {
47+
// Conversational model detected, so we display a code snippet that features the Messages API
48+
return `import { HfInference } from "@huggingface/inference";
49+
50+
const inference = new HfInference("${accessToken || `{API_TOKEN}`}");
51+
const image_url = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
52+
53+
for await (const chunk of inference.chatCompletionStream({
54+
model: "${model.id}",
55+
messages: [
56+
{
57+
"role": "user",
58+
"content": [
59+
{"type": "image_url", "image_url": {"url": image_url}},
60+
{"type": "text", "text": "Describe this image in one sentence."},
61+
],
62+
}
63+
],
64+
max_tokens: 500,
65+
})) {
66+
process.stdout.write(chunk.choices[0]?.delta?.content || "");
67+
}`;
68+
} else {
69+
return snippetBasic(model, accessToken);
70+
}
71+
};
72+
4473
export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string): string =>
4574
`async function query(data) {
4675
const response = await fetch(
@@ -156,6 +185,7 @@ export const jsSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal,
156185
summarization: snippetBasic,
157186
"feature-extraction": snippetBasic,
158187
"text-generation": snippetTextGeneration,
188+
"image-text-to-text": snippetImageTextToTextGeneration,
159189
"text2text-generation": snippetBasic,
160190
"fill-mask": snippetBasic,
161191
"sentence-similarity": snippetBasic,

packages/tasks/src/snippets/python.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@ for message in client.chat_completion(
1717
):
1818
print(message.choices[0].delta.content, end="")`;
1919

20+
export const snippetConversationalWithImage = (model: ModelDataMinimal, accessToken: string): string =>
21+
`from huggingface_hub import InferenceClient
22+
23+
client = InferenceClient(
24+
"${model.id}",
25+
token="${accessToken || "{API_TOKEN}"}",
26+
)
27+
image_url = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
28+
29+
for message in client.chat_completion(
30+
messages=[
31+
{
32+
"role": "user",
33+
"content": [
34+
{"type": "image_url", "image_url": {"url": image_url}},
35+
{"type": "text", "text": "Describe this image in one sentence."},
36+
],
37+
}
38+
],
39+
max_tokens=500,
40+
stream=True,
41+
):
42+
print(message.choices[0].delta.content, end="")`;
43+
2044
export const snippetZeroShotClassification = (model: ModelDataMinimal): string =>
2145
`def query(payload):
2246
response = requests.post(API_URL, headers=headers, json=payload)
@@ -156,6 +180,9 @@ export function getPythonInferenceSnippet(model: ModelDataMinimal, accessToken:
156180
if (model.pipeline_tag === "text-generation" && model.config?.tokenizer_config?.chat_template) {
157181
// Conversational model detected, so we display a code snippet that features the Messages API
158182
return snippetConversational(model, accessToken);
183+
} else if (model.pipeline_tag === "image-text-to-text" && model.config?.tokenizer_config?.chat_template) {
184+
// Example sending an image to the Message API
185+
return snippetConversationalWithImage(model, accessToken);
159186
} else {
160187
const body =
161188
model.pipeline_tag && model.pipeline_tag in pythonSnippets

0 commit comments

Comments
 (0)