@@ -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+
4473export 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 ,
0 commit comments