@huggingface/tasks 0.21.53 → 0.21.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,14 +2,36 @@ import { describe, expect, it } from "vitest";
2
2
  import type { ModelData } from "./model-data.js";
3
3
  import {
4
4
  adapters,
5
+ cartesia_pytorch,
6
+ depth_anything_v2,
7
+ describe_anything,
5
8
  diffusers,
9
+ diffusionkit,
10
+ gliner2,
11
+ indextts,
6
12
  keras_hub,
13
+ kernels,
14
+ kimi_audio,
15
+ litert_lm,
7
16
  llama_cpp_python,
17
+ microduck,
18
+ mlAgents,
19
+ mlxim,
8
20
  multimolecule,
9
21
  paddlenlp,
10
22
  peft,
23
+ phantom_wan,
24
+ pruna,
25
+ sam2,
26
+ sam_3d_body,
27
+ sentenceTransformers,
11
28
  sklearn,
29
+ speechbrain,
30
+ timm,
12
31
  transformers,
32
+ transformersJS,
33
+ ultralytics,
34
+ vui,
13
35
  } from "./model-libraries-snippets.js";
14
36
 
15
37
  describe("model-libraries-snippets", () => {
@@ -66,6 +88,29 @@ output = llm(
66
88
  print(output)`);
67
89
  });
68
90
 
91
+ describe("microduck", () => {
92
+ it("uses an explicit slot tag even when the repo name and other tags suggest walking", () => {
93
+ const model: ModelData = {
94
+ id: "user/microduck-walk",
95
+ tags: ["microduck", "walk", "microduck-slot:stand", "microduck-slot:stand"],
96
+ inference: "",
97
+ };
98
+
99
+ expect(microduck(model)).toEqual(["sudo robotctl policy load stand user/microduck-walk"]);
100
+ });
101
+
102
+ it.each([
103
+ { scenario: "missing slot tags", tags: ["microduck", "walk"] },
104
+ { scenario: "unsupported slot tags", tags: ["microduck-slot:bow"] },
105
+ { scenario: "conflicting slot tags", tags: ["microduck-slot:walk", "microduck-slot:stand"] },
106
+ ])("requires a slot choice for $scenario", ({ tags }) => {
107
+ const snippet = microduck({ id: "user/microduck-walk", tags, inference: "" }).join("\n");
108
+
109
+ expect(snippet).toContain("# Replace SLOT with the slot specified in the model card");
110
+ expect(snippet).toContain("sudo robotctl policy load SLOT user/microduck-walk");
111
+ });
112
+ });
113
+
69
114
  // a repo owner can put anything in config.json / the model card, so every interpolated value
70
115
  // must either be escaped (string literals) or rejected (bare identifiers)
71
116
  describe("repo-controlled values are not injectable", () => {
@@ -75,6 +120,7 @@ print(output)`);
75
120
  ["adapters", adapters, { config: { adapter_transformers: { model_name: PAYLOAD } } }],
76
121
  ["diffusers", diffusers, { tags: ["lora"], cardData: { base_model: PAYLOAD, instance_prompt: PAYLOAD } }],
77
122
  ["keras_hub", keras_hub, { config: { keras_hub: { tasks: [PAYLOAD, "TextClassifier"] } } }],
123
+ ["microduck", microduck, { tags: [`microduck-slot:${PAYLOAD}`] }],
78
124
  ["paddlenlp", paddlenlp, { config: { architectures: [PAYLOAD] } }],
79
125
  ["peft", peft, { config: { peft: { base_model_name_or_path: PAYLOAD, task_type: "CAUSAL_LM" } } }],
80
126
  ["multimolecule", multimolecule, { widgetData: [{ text: PAYLOAD }] }],
@@ -100,4 +146,347 @@ print(output)`);
100
146
  expect(keras_hub(model as ModelData).join("\n")).toContain("keras_hub.models.TextClassifier.from_preset");
101
147
  });
102
148
  });
149
+
150
+ describe("snippets stay runnable for the common repo shapes", () => {
151
+ const base = { id: "user/model", tags: [] as string[], inference: "" };
152
+
153
+ it("transformers: the chat example needs a chat template, not only the conversational tag", () => {
154
+ const chatModel = {
155
+ ...base,
156
+ pipeline_tag: "text-generation",
157
+ tags: ["conversational"],
158
+ transformersInfo: { auto_model: "AutoModelForCausalLM", processor: "AutoTokenizer" },
159
+ } as ModelData;
160
+ const [pipelineWithoutTemplate] = transformers(chatModel);
161
+ expect(pipelineWithoutTemplate).not.toContain("messages");
162
+
163
+ const [pipelineSnippet, autoSnippet] = transformers({
164
+ ...chatModel,
165
+ config: { tokenizer_config: { chat_template: "{{ messages }}" } },
166
+ } as ModelData);
167
+ expect(pipelineSnippet).toContain("pipe(messages)");
168
+ expect(autoSnippet).toContain("max_new_tokens=256");
169
+ expect(autoSnippet).toContain("# pip install -U transformers accelerate");
170
+ });
171
+
172
+ it("transformers: pipelines removed in v5 get a warning with a valid pip command", () => {
173
+ const [snippet] = transformers({
174
+ ...base,
175
+ pipeline_tag: "question-answering",
176
+ transformersInfo: { auto_model: "AutoModelForQuestionAnswering", processor: "AutoTokenizer" },
177
+ } as ModelData);
178
+ expect(snippet).toContain("no longer supported in transformers v5");
179
+ expect(snippet).toContain(`# pip install "transformers<5.0.0"`);
180
+ });
181
+
182
+ it("sentence-transformers: rerankers tagged text-classification use CrossEncoder", () => {
183
+ const [reranker] = sentenceTransformers({
184
+ ...base,
185
+ pipeline_tag: "text-classification",
186
+ config: { architectures: ["XLMRobertaForSequenceClassification"] },
187
+ } as ModelData);
188
+ expect(reranker).toContain("CrossEncoder(");
189
+
190
+ const [biEncoder] = sentenceTransformers({
191
+ ...base,
192
+ pipeline_tag: "sentence-similarity",
193
+ config: { architectures: ["BertModel"] },
194
+ } as ModelData);
195
+ expect(biEncoder).toContain("SentenceTransformer(");
196
+
197
+ // SetFit bi-encoders can carry a *ForSequenceClassification config too (e.g. NW-temp/previous-best-my-awesome-setfit-model)
198
+ const [setfitModel] = sentenceTransformers({
199
+ ...base,
200
+ tags: ["setfit"],
201
+ pipeline_tag: "text-classification",
202
+ config: { architectures: ["DistilBertForSequenceClassification"] },
203
+ } as ModelData);
204
+ expect(setfitModel).not.toContain("CrossEncoder(");
205
+ });
206
+
207
+ it("sentence-transformers: sparse encoders use SparseEncoder", () => {
208
+ const [snippet] = sentenceTransformers({ ...base, tags: ["sparse-encoder"] } as ModelData);
209
+ expect(snippet).toContain("SparseEncoder(");
210
+ expect(snippet).toContain("encode_query(");
211
+ });
212
+
213
+ it("sentence-transformers: multi-vector encoders use MultiVectorEncoder", () => {
214
+ // e.g. answerdotai/answerai-colbert-small-v1, tagged multi-vector by the library on push
215
+ const [snippet] = sentenceTransformers({ ...base, tags: ["multi-vector"] } as ModelData);
216
+ expect(snippet).toContain(`MultiVectorEncoder("user/model")`);
217
+ expect(snippet).toContain("encode_document(");
218
+ });
219
+
220
+ it("diffusers: prefixed pipeline class tags reach the dedicated branches", () => {
221
+ const inpainting = diffusers({ ...base, tags: ["diffusers:StableDiffusionInpaintPipeline"] } as ModelData);
222
+ expect(inpainting.join("\n")).toContain("AutoPipelineForInpainting");
223
+ // several inpainting repos only ship non-variant weights
224
+ expect(inpainting.join("\n")).not.toContain('variant="fp16"');
225
+
226
+ const fluxFill = diffusers({ ...base, config: { diffusers: { _class_name: "FluxFillPipeline" } } } as ModelData);
227
+ expect(fluxFill.join("\n")).toContain("FluxFillPipeline");
228
+ });
229
+
230
+ it("diffusers: the first base model is used when several are listed", () => {
231
+ const snippet = diffusers({
232
+ ...base,
233
+ tags: ["lora"],
234
+ cardData: { base_model: ["org/base", "org/base-turbo"] },
235
+ } as ModelData).join("\n");
236
+ expect(snippet).toContain(`"org/base"`);
237
+ expect(snippet).not.toContain("org/base,org/base-turbo");
238
+ });
239
+
240
+ it("diffusers: a listed Diffusers conversion is preferred over the original checkpoint", () => {
241
+ // e.g. Remade-AI/Rotate: only the -Diffusers repo has a model_index.json
242
+ const snippet = diffusers({
243
+ ...base,
244
+ tags: ["lora"],
245
+ pipeline_tag: "image-to-video",
246
+ cardData: { base_model: ["Wan-AI/Wan2.1-I2V-14B-480P", "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"] },
247
+ } as ModelData).join("\n");
248
+ expect(snippet).toContain(`DiffusionPipeline.from_pretrained("Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"`);
249
+ });
250
+
251
+ it("diffusers: LoRA image-to-video exports the generated frames", () => {
252
+ const snippet = diffusers({
253
+ ...base,
254
+ tags: ["lora"],
255
+ pipeline_tag: "image-to-video",
256
+ cardData: { base_model: "org/base" },
257
+ } as ModelData).join("\n");
258
+ expect(snippet).toContain("output = pipe(");
259
+ expect(snippet).toContain(`export_to_video(output, "output.mp4")`);
260
+ });
261
+
262
+ it("transformers.js: Hub tasks without a pipeline() task of the same name are mapped", () => {
263
+ expect(transformersJS({ ...base, pipeline_tag: "sentence-similarity" } as ModelData)[0]).toContain(
264
+ "pipeline('feature-extraction'",
265
+ );
266
+ expect(transformersJS({ ...base, pipeline_tag: "fill-mask" } as ModelData)[0]).toContain("pipeline('fill-mask'");
267
+ });
268
+
269
+ it("transformers.js: rerankers score query/document pairs instead of using the text-classification pipeline", () => {
270
+ // the text-classification pipeline takes no text pairs and softmaxes a single logit, so every score is 1
271
+ const [snippet] = transformersJS({ ...base, pipeline_tag: "text-ranking" } as ModelData);
272
+ expect(snippet).not.toContain("pipeline(");
273
+ expect(snippet).toContain("AutoModelForSequenceClassification.from_pretrained('user/model')");
274
+ expect(snippet).toContain("text_pair: documents");
275
+ expect(snippet).toContain("logits.sigmoid()");
276
+ });
277
+
278
+ it("peft: every PEFT task type has a loader", () => {
279
+ const adapter = (task_type: string) =>
280
+ ({ ...base, config: { peft: { base_model_name_or_path: "org/base", task_type } } }) as ModelData;
281
+ expect(peft(adapter("FEATURE_EXTRACTION"))[0]).toContain("AutoModel.from_pretrained");
282
+ expect(peft(adapter("QUESTION_ANS"))[0]).toContain("AutoModelForQuestionAnswering");
283
+ expect(peft(adapter("CAUSAL_LM"))[0]).toContain("AutoModelForCausalLM");
284
+ expect(peft(adapter("UNKNOWN"))[0]).toEqual("Task type is invalid.");
285
+ });
286
+
287
+ it("peft: speech seq2seq adapters use the speech auto class", () => {
288
+ const whisper = peft({
289
+ ...base,
290
+ config: { peft: { base_model_name_or_path: "openai/whisper-large-v3", task_type: "SEQ_2_SEQ_LM" } },
291
+ } as ModelData)[0];
292
+ expect(whisper).toContain("AutoModelForSpeechSeq2Seq");
293
+ for (const baseId of [
294
+ "facebook/s2t-small-librispeech-asr",
295
+ "facebook/seamless-m4t-v2-large",
296
+ "microsoft/speecht5_asr",
297
+ ]) {
298
+ expect(
299
+ peft({
300
+ ...base,
301
+ config: { peft: { base_model_name_or_path: baseId, task_type: "SEQ_2_SEQ_LM" } },
302
+ } as ModelData)[0],
303
+ ).toContain("AutoModelForSpeechSeq2Seq");
304
+ }
305
+
306
+ const t5 = peft({
307
+ ...base,
308
+ config: { peft: { base_model_name_or_path: "google/flan-t5-base", task_type: "SEQ_2_SEQ_LM" } },
309
+ } as ModelData)[0];
310
+ expect(t5).toContain("AutoModelForSeq2SeqLM");
311
+
312
+ // audio models that transformers registers under the text seq2seq class must not be overridden
313
+ const qwenAudio = peft({
314
+ ...base,
315
+ pipeline_tag: "automatic-speech-recognition",
316
+ config: { peft: { base_model_name_or_path: "Qwen/Qwen2-Audio-7B", task_type: "SEQ_2_SEQ_LM" } },
317
+ } as ModelData)[0];
318
+ expect(qwenAudio).toContain("AutoModelForSeq2SeqLM");
319
+ });
320
+
321
+ it("peft: multi-task speech bases only get the speech class for speech-to-text adapters", () => {
322
+ const adapter = (baseId: string, extra: Partial<ModelData> = {}) =>
323
+ peft({
324
+ ...base,
325
+ ...extra,
326
+ config: { peft: { base_model_name_or_path: baseId, task_type: "SEQ_2_SEQ_LM" } },
327
+ } as ModelData)[0];
328
+
329
+ // SeamlessM4T text translation adapters need the text-to-text head (e.g. maxbsdv/LEO-SeamlessM4T-v2-Large-Roverplastik)
330
+ expect(adapter("facebook/seamless-m4t-v2-large", { pipeline_tag: "translation" })).toContain(
331
+ "AutoModelForSeq2SeqLM",
332
+ );
333
+ expect(adapter("facebook/seamless-m4t-v2-large", { tags: ["translation"] })).toContain("AutoModelForSeq2SeqLM");
334
+ // speech adapters, tagged or not, keep the speech-to-text head
335
+ expect(adapter("facebook/seamless-m4t-v2-large", { tags: ["speech-translation"] })).toContain(
336
+ "AutoModelForSpeechSeq2Seq",
337
+ );
338
+
339
+ // SpeechT5 text-to-speech bases load the text-to-spectrogram head, not the speech-to-text one
340
+ // e.g. Solo448/SpeechT5-Unified-TTS-PEFT; PEFT's seq2seq wrapper cannot wrap it, so the task type is dropped
341
+ const tts = adapter("microsoft/speecht5_tts");
342
+ expect(tts).toContain(`AutoModelForTextToSpectrogram.from_pretrained("microsoft/speecht5_tts")`);
343
+ expect(tts).toContain("config.task_type = None");
344
+ expect(adapter("user/speecht5_finetuned_voxpopuli_nl")).toContain("AutoModelForTextToSpectrogram");
345
+ expect(adapter("user/speecht5_tts_basrah_dialect")).not.toContain("AutoModelForSpeechSeq2Seq");
346
+ expect(
347
+ adapter("user/speecht5_finetuned_librispeech", { pipeline_tag: "automatic-speech-recognition" }),
348
+ ).toContain("AutoModelForSpeechSeq2Seq");
349
+ // the ASR task can also come from the tags alone
350
+ expect(adapter("user/speecht5_finetuned_librispeech", { tags: ["automatic-speech-recognition"] })).toContain(
351
+ "AutoModelForSpeechSeq2Seq",
352
+ );
353
+ });
354
+
355
+ it("peft: falls back to the model card's base model", () => {
356
+ const snippet = peft({
357
+ ...base,
358
+ cardData: { base_model: "org/base" },
359
+ config: { peft: { task_type: "CAUSAL_LM" } },
360
+ } as ModelData)[0];
361
+ expect(snippet).toContain(`"org/base"`);
362
+ });
363
+
364
+ it("ultralytics: mainline loads weights with YOLO(), the yolov10 fork keeps its own loader", () => {
365
+ expect(ultralytics(base as ModelData)[0]).toContain("model = YOLO(weights)");
366
+ expect(ultralytics({ ...base, tags: ["yolov10", "safetensors"] } as ModelData)[0]).toContain(
367
+ "YOLOv10.from_pretrained",
368
+ );
369
+ expect(
370
+ ultralytics({ ...base, library_name: "yolov10", tags: ["yolov10", "safetensors"] } as ModelData)[0],
371
+ ).toContain("YOLOv10.from_pretrained");
372
+ });
373
+
374
+ it("ultralytics: yolov10 fork repos that only ship .pt weights load them with YOLOv10()", () => {
375
+ // e.g. kadirnar/Yolov10: from_pretrained needs the config.json and model.safetensors of a fork push
376
+ const [snippet] = ultralytics({ ...base, library_name: "yolov10", tags: ["yolov10"] } as ModelData);
377
+ expect(snippet).toContain("model = YOLOv10(weights)");
378
+ expect(snippet).not.toContain("from_pretrained");
379
+ });
380
+
381
+ it("ultralytics: mainline repos listing a yolov10 tag stay on the mainline loader", () => {
382
+ // e.g. Ultralytics/YOLOv8 is tagged yolov3 ... yolov10 but only ships .pt files for mainline YOLO()
383
+ const versionTags = ["ultralytics", "yolov8", "yolov9", "yolov10"];
384
+ for (const model of [
385
+ { ...base, library_name: "ultralytics", tags: versionTags },
386
+ { ...base, tags: versionTags },
387
+ ]) {
388
+ const [snippet] = ultralytics(model as ModelData);
389
+ expect(snippet).toContain("model = YOLO(weights)");
390
+ expect(snippet).not.toContain("YOLOv10");
391
+ }
392
+ });
393
+
394
+ it("ultralytics: fork pushes tagged ultralytics keep the yolov10 loader", () => {
395
+ // e.g. kairess/baby-face-detection-yolov10: library ultralytics, but only the fork's model.safetensors
396
+ const [snippet] = ultralytics({
397
+ ...base,
398
+ library_name: "ultralytics",
399
+ tags: ["ultralytics", "safetensors", "yolov10"],
400
+ } as ModelData);
401
+ expect(snippet).toContain("YOLOv10.from_pretrained");
402
+ });
403
+
404
+ it("pruna: the pro variant never rewrites the repo id", () => {
405
+ const snippet = pruna({ ...base, id: "pruna-test/model", tags: ["pruna_pro-ai"] } as ModelData).join("\n");
406
+ expect(snippet).toContain("from pruna_pro import PrunaProModel");
407
+ expect(snippet).toContain(`"pruna-test/model"`);
408
+ expect(snippet).not.toContain("pruna_pro-test");
409
+ });
410
+
411
+ it("sklearn: joblib repos use the filename declared in config.json", () => {
412
+ expect(sklearn({ ...base, config: { sklearn: { model: { file: "model.pkl" } } } } as ModelData)[0]).toContain(
413
+ `"model.pkl"`,
414
+ );
415
+ expect(sklearn(base as ModelData)[0]).toContain(`"sklearn_model.joblib"`);
416
+ // a declared name that is not a pickle (typo'd in e.g. danupurnomo/dummy-titanic) falls back to the default
417
+ expect(
418
+ sklearn({ ...base, config: { sklearn: { model: { file: "sklean_model.jpblib" } } } } as ModelData)[0],
419
+ ).toContain(`"sklearn_model.joblib"`);
420
+ });
421
+
422
+ it("sklearn: skops repos download with huggingface_hub", () => {
423
+ // skops.hub_utils was removed in skops 0.12
424
+ const skops = (sklearnConfig: Record<string, unknown>) =>
425
+ sklearn({ ...base, tags: ["skops"], config: { sklearn: sklearnConfig } } as ModelData)[0];
426
+
427
+ const skopsFile = skops({ model: { file: "model.skops" }, model_format: "skops" });
428
+ expect(skopsFile).toContain(`model = load(hf_hub_download("user/model", "model.skops"))`);
429
+ expect(skopsFile).not.toContain("hub_utils");
430
+
431
+ const pickled = skops({ model: { file: "model.pkl" }, model_format: "pickle" });
432
+ expect(pickled).toContain(`joblib.load(\n\thf_hub_download("user/model", "model.pkl")`);
433
+ expect(pickled).not.toContain("hub_utils");
434
+
435
+ // a pickled file without a declared model_format (e.g. julien-c/skops-digits) is not read with skops.io
436
+ const undeclared = skops({ model: { file: "sklearn_model.joblib" } });
437
+ expect(undeclared).toContain("joblib.load(");
438
+ expect(undeclared).not.toContain("skops.io");
439
+ });
440
+
441
+ it("speechbrain: speaker verification compares two recordings", () => {
442
+ const [snippet] = speechbrain({
443
+ ...base,
444
+ config: { speechbrain: { speechbrain_interface: "SpeakerRecognition" } },
445
+ } as ModelData);
446
+ expect(snippet).toContain(`verify_files("speaker1.wav", "speaker2.wav")`);
447
+ });
448
+
449
+ it("repo ids are quoted in generated Python", () => {
450
+ const model = { ...base, id: "org/some-model" } as ModelData;
451
+ for (const snippetFn of [sam2, sam_3d_body, mlxim, indextts, describe_anything, diffusionkit, phantom_wan]) {
452
+ for (const snippet of snippetFn(model)) {
453
+ expect(snippet).not.toMatch(/[(=,]\s*org\/some-model/);
454
+ }
455
+ }
456
+ });
457
+
458
+ it("mlx-image: create_model receives the registry name, not the repo id", () => {
459
+ const [snippet] = mlxim({ ...base, id: "mlx-vision/vit_small_patch16_224.dinov3-mlxim" } as ModelData);
460
+ expect(snippet).toContain(`create_model("vit_small_patch16_224.dinov3")`);
461
+ });
462
+
463
+ it("litert-lm: the command runs as pasted, without a placeholder file argument", () => {
464
+ // litert-lm picks the repo's .litertlm file itself; an unedited `<file>` would be read by the shell as a redirection
465
+ const [snippet] = litert_lm(base as ModelData);
466
+ expect(snippet).toContain("--from-huggingface-repo=user/model \\\n");
467
+ expect(snippet.slice(snippet.indexOf("litert-lm run"))).not.toMatch(/[<>]/);
468
+ });
469
+
470
+ it("static templates no longer carry typos", () => {
471
+ expect(mlAgents(base as ModelData)[0]).toContain(`--local-dir="./downloads"`);
472
+ expect(timm(base as ModelData)[0]).toContain(`"hf-hub:user/model"`);
473
+ expect(kernels(base as ModelData)[0]).toContain(`get_kernel("user/model", version=1)`);
474
+ expect(gliner2(base as ModelData)[0]).toContain("extractor = AutoExtractor.from_pretrained");
475
+ expect(cartesia_pytorch(base as ModelData)[0].trimEnd()).toMatch(/print\(out_message\)$/);
476
+ expect(vui()[0]).toContain("from vui.legacy import Vui, render\n");
477
+ expect(phantom_wan(base as ModelData)[0]).toContain('Image.open("path/to/image.jpg")');
478
+ });
479
+
480
+ it("templates run as pasted", () => {
481
+ expect(sam2(base as ModelData)[1]).toContain("predictor.add_new_points(state, <your_prompts>)\n");
482
+ expect(depth_anything_v2(base as ModelData)[0]).toContain("model.load_state_dict(state_dict)\nmodel.eval()");
483
+ const [sam3dBody] = sam_3d_body(base as ModelData);
484
+ expect(sam3dBody).toContain("from tools.vis_utils import visualize_sample_together");
485
+ expect(sam3dBody).toContain('img_bgr = cv2.imread("path/to/image.jpg")');
486
+ expect(kimi_audio(base as ModelData)[0]).toContain(`"transformers<5"`);
487
+ const [cartesia] = cartesia_pytorch(base as ModelData);
488
+ expect(cartesia).toContain("from cartesia_pytorch.Rene import ReneLMHeadModel");
489
+ expect(cartesia).toContain(`.from_pretrained("user/model").half().cuda()`);
490
+ });
491
+ });
103
492
  });