@huggingface/tasks 0.21.54 → 0.21.56

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.
Files changed (35) hide show
  1. package/dist/commonjs/hardware-amd.d.ts.map +1 -1
  2. package/dist/commonjs/hardware-amd.js +48 -0
  3. package/dist/commonjs/hardware-nvidia.d.ts +1 -0
  4. package/dist/commonjs/hardware-nvidia.d.ts.map +1 -1
  5. package/dist/commonjs/hardware-nvidia.js +122 -0
  6. package/dist/commonjs/library-to-tasks.d.ts.map +1 -1
  7. package/dist/commonjs/library-to-tasks.js +8 -1
  8. package/dist/commonjs/model-libraries-snippets.d.ts +3 -1
  9. package/dist/commonjs/model-libraries-snippets.d.ts.map +1 -1
  10. package/dist/commonjs/model-libraries-snippets.js +349 -154
  11. package/dist/commonjs/model-libraries-snippets.spec.js +300 -0
  12. package/dist/commonjs/model-libraries.d.ts +11 -2
  13. package/dist/commonjs/model-libraries.d.ts.map +1 -1
  14. package/dist/commonjs/model-libraries.js +10 -1
  15. package/dist/esm/hardware-amd.d.ts.map +1 -1
  16. package/dist/esm/hardware-amd.js +48 -0
  17. package/dist/esm/hardware-nvidia.d.ts +1 -0
  18. package/dist/esm/hardware-nvidia.d.ts.map +1 -1
  19. package/dist/esm/hardware-nvidia.js +122 -0
  20. package/dist/esm/library-to-tasks.d.ts.map +1 -1
  21. package/dist/esm/library-to-tasks.js +8 -1
  22. package/dist/esm/model-libraries-snippets.d.ts +3 -1
  23. package/dist/esm/model-libraries-snippets.d.ts.map +1 -1
  24. package/dist/esm/model-libraries-snippets.js +344 -151
  25. package/dist/esm/model-libraries-snippets.spec.js +301 -1
  26. package/dist/esm/model-libraries.d.ts +11 -2
  27. package/dist/esm/model-libraries.d.ts.map +1 -1
  28. package/dist/esm/model-libraries.js +10 -1
  29. package/package.json +1 -1
  30. package/src/hardware-amd.ts +48 -0
  31. package/src/hardware-nvidia.ts +122 -0
  32. package/src/library-to-tasks.ts +8 -1
  33. package/src/model-libraries-snippets.spec.ts +389 -0
  34. package/src/model-libraries-snippets.ts +364 -149
  35. package/src/model-libraries.ts +10 -1
@@ -12,7 +12,7 @@ const isValidIdentifier = (str) => /^[A-Za-z_]\w*$/.test(str);
12
12
  export const adapters = (model) => [
13
13
  `from adapters import AutoAdapterModel
14
14
 
15
- model = AutoAdapterModel.from_pretrained("${escapeStringForJson(model.config?.adapter_transformers?.model_name ?? "fill-in-model-name")}")
15
+ model = AutoAdapterModel.from_pretrained("${escapeStringForJson(model.config?.adapter_transformers?.model_name ?? "<base-model-id>")}")
16
16
  model.load_adapter("${model.id}", set_active=True)`,
17
17
  ];
18
18
  const allennlpUnknown = (model) => [
@@ -40,6 +40,13 @@ export const araclip = (model) => [
40
40
 
41
41
  model = AraClip.from_pretrained("${model.id}")`,
42
42
  ];
43
+ export const asimov = (model) => [
44
+ `# Run the policy in a browser simulation of the Asimov 1 humanoid (no GPU needed)
45
+ git clone https://github.com/menloresearch/humanoid-policy-viewer
46
+ cd humanoid-policy-viewer
47
+ npm install
48
+ npm run hf ${model.id}`,
49
+ ];
43
50
  export const asteroid = (model) => [
44
51
  `from asteroid.models import BaseModel
45
52
 
@@ -64,7 +71,13 @@ result, message = detector.detect_watermark(watermarked_audio, sr)`;
64
71
  return [watermarkSnippet, detectorSnippet];
65
72
  };
66
73
  function get_base_diffusers_model(model) {
67
- return escapeStringForJson(model.cardData?.base_model?.toString() ?? "fill-in-base-model");
74
+ const baseModel = model.cardData?.base_model;
75
+ // several base models can be listed (e.g. a LoRA trained for a base and its turbo variant). When an original checkpoint
76
+ // and its Diffusers conversion are both listed (["Wan-AI/Wan2.1-I2V-14B-480P", "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"]),
77
+ // only the conversion has a model_index.json that DiffusionPipeline can load; otherwise the first one is used
78
+ const candidates = Array.isArray(baseModel) ? baseModel : baseModel ? [baseModel] : [];
79
+ const chosen = candidates.find((id) => id.toLowerCase().endsWith("-diffusers")) ?? candidates[0];
80
+ return escapeStringForJson(chosen ?? "fill-in-base-model");
68
81
  }
69
82
  function get_prompt_from_diffusers_model(model) {
70
83
  const prompt = model.widgetData?.[0]?.text ?? model.cardData?.instance_prompt;
@@ -145,7 +158,7 @@ detector = cvg.NeuralCornerDetector(checkpoint)
145
158
  # Embedder models, such as Milo:
146
159
  embedder = cvg.NeuralEmbedder(checkpoint)`,
147
160
  ];
148
- export const colipri = (model) => {
161
+ export const colipri = () => {
149
162
  const installSnippet = `pip install colipri`;
150
163
  const exampleSnippet = `from colipri import get_model
151
164
  from colipri import get_processor
@@ -154,7 +167,7 @@ from colipri import ZeroShotImageClassificationPipeline
154
167
 
155
168
  model = get_model().cuda()
156
169
  processor = get_processor()
157
- pipeline = ZeroShotImageClassificationPipeline("${model.id}", processor)
170
+ pipeline = ZeroShotImageClassificationPipeline(model, processor)
158
171
 
159
172
  image = load_sample_ct()
160
173
 
@@ -271,7 +284,7 @@ export const depth_anything_v2 = (model) => {
271
284
  else if (model.id === "depth-anything/Depth-Anything-V2-Large") {
272
285
  encoder = "vitl";
273
286
  features = "256";
274
- out_channels = "[256, 512, 1024, 1024";
287
+ out_channels = "[256, 512, 1024, 1024]";
275
288
  }
276
289
  return [
277
290
  `
@@ -280,6 +293,7 @@ export const depth_anything_v2 = (model) => {
280
293
  # Load the model and infer depth from an image
281
294
  import cv2
282
295
  import torch
296
+ from huggingface_hub import hf_hub_download
283
297
 
284
298
  from depth_anything_v2.dpt import DepthAnythingV2
285
299
 
@@ -289,7 +303,8 @@ model = DepthAnythingV2(encoder="${encoder}", features=${features}, out_channels
289
303
  # load the weights
290
304
  filepath = hf_hub_download(repo_id="${model.id}", filename="depth_anything_v2_${encoder}.pth", repo_type="model")
291
305
  state_dict = torch.load(filepath, map_location="cpu")
292
- model.load_state_dict(state_dict).eval()
306
+ model.load_state_dict(state_dict)
307
+ model.eval()
293
308
 
294
309
  raw_img = cv2.imread("your/image/path")
295
310
  depth = model.infer_image(raw_img) # HxW raw depth map in numpy
@@ -298,8 +313,8 @@ depth = model.infer_image(raw_img) # HxW raw depth map in numpy
298
313
  };
299
314
  export const depth_pro = (model) => {
300
315
  const installSnippet = `# Download checkpoint
301
- pip install huggingface-hub
302
- huggingface-cli download --local-dir checkpoints ${model.id}`;
316
+ pip install huggingface_hub
317
+ hf download ${model.id} --local-dir checkpoints`;
303
318
  const inferenceSnippet = `import depth_pro
304
319
 
305
320
  # Load model and preprocessing transform
@@ -367,7 +382,7 @@ export const describe_anything = (model) => [
367
382
  from huggingface_hub import snapshot_download
368
383
  from dam import DescribeAnythingModel
369
384
 
370
- snapshot_download(${model.id}, local_dir="checkpoints")
385
+ snapshot_download("${model.id}", local_dir="checkpoints")
371
386
 
372
387
  dam = DescribeAnythingModel(
373
388
  model_path="checkpoints",
@@ -478,7 +493,7 @@ pipe.load_lora_weights("${model.id}")
478
493
  prompt = "${get_prompt_from_diffusers_model(model) ?? diffusersVideoDefaultPrompt}"
479
494
  input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/guitar-man.png")
480
495
 
481
- image = pipe(image=input_image, prompt=prompt).frames[0]
496
+ output = pipe(image=input_image, prompt=prompt).frames[0]
482
497
  export_to_video(output, "output.mp4")`,
483
498
  ];
484
499
  const diffusers_textual_inversion = (model) => [
@@ -518,7 +533,7 @@ from diffusers import AutoPipelineForInpainting
518
533
  from diffusers.utils import load_image
519
534
 
520
535
  # switch to "mps" for apple devices
521
- pipe = AutoPipelineForInpainting.from_pretrained("${model.id}", dtype=torch.float16, variant="fp16", device_map="cuda")
536
+ pipe = AutoPipelineForInpainting.from_pretrained("${model.id}", dtype=torch.float16, device_map="cuda")
522
537
 
523
538
  img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
524
539
  mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
@@ -540,9 +555,12 @@ image = pipe(
540
555
  ).images[0]`,
541
556
  ];
542
557
  export const diffusers = (model) => {
558
+ // the Hub emits pipeline class tags as `diffusers:<ClassName>`; bare names are kept for manually added tags
559
+ const hasPipelineClass = (className) => model.tags.includes(`diffusers:${className}`) ||
560
+ model.tags.includes(className) ||
561
+ model.config?.diffusers?._class_name === className;
543
562
  let codeSnippets;
544
- if (model.tags.includes("StableDiffusionInpaintPipeline") ||
545
- model.tags.includes("StableDiffusionXLInpaintPipeline")) {
563
+ if (hasPipelineClass("StableDiffusionInpaintPipeline") || hasPipelineClass("StableDiffusionXLInpaintPipeline")) {
546
564
  codeSnippets = diffusers_inpainting(model);
547
565
  }
548
566
  else if (model.tags.includes("controlnet")) {
@@ -565,7 +583,7 @@ export const diffusers = (model) => {
565
583
  else if (model.tags.includes("textual_inversion")) {
566
584
  codeSnippets = diffusers_textual_inversion(model);
567
585
  }
568
- else if (model.tags.includes("FluxFillPipeline")) {
586
+ else if (hasPipelineClass("FluxFillPipeline")) {
569
587
  codeSnippets = diffusers_flux_fill(model);
570
588
  }
571
589
  else if (model.pipeline_tag === "image-to-video") {
@@ -586,7 +604,7 @@ from diffusionkit.mlx import DiffusionPipeline
586
604
  pipeline = DiffusionPipeline(
587
605
  shift=3.0,
588
606
  use_t5=False,
589
- model_version=${model.id},
607
+ model_version="${model.id}",
590
608
  low_memory_mode=True,
591
609
  a16=True,
592
610
  w16=True,
@@ -596,7 +614,7 @@ from diffusionkit.mlx import FluxPipeline
596
614
 
597
615
  pipeline = FluxPipeline(
598
616
  shift=1.0,
599
- model_version=${model.id},
617
+ model_version="${model.id}",
600
618
  low_memory_mode=True,
601
619
  a16=True,
602
620
  w16=True,
@@ -617,21 +635,22 @@ image, _ = pipeline.generate_image(
617
635
  return [pipelineSnippet, generateSnippet];
618
636
  };
619
637
  export const cartesia_pytorch = (model) => [
620
- `# pip install --no-binary :all: cartesia-pytorch
621
- from cartesia_pytorch import ReneLMHeadModel
638
+ `# The cartesia-pytorch package on PyPI ships no code: clone the repo and put its cartesia-pytorch folder on PYTHONPATH
639
+ # (it needs an Ampere or newer CUDA GPU, torch 2.4, mamba-ssm 2.2.2 and flash-attn 2.6.3)
640
+ # git clone https://github.com/cartesia-ai/edge && export PYTHONPATH="$PWD/edge/cartesia-pytorch:$PYTHONPATH"
641
+ from cartesia_pytorch.Rene import ReneLMHeadModel
622
642
  from transformers import AutoTokenizer
623
643
 
624
- model = ReneLMHeadModel.from_pretrained("${model.id}")
644
+ model = ReneLMHeadModel.from_pretrained("${model.id}").half().cuda()
625
645
  tokenizer = AutoTokenizer.from_pretrained("allenai/OLMo-1B-hf")
626
646
 
627
647
  in_message = ["Rene Descartes was"]
628
648
  inputs = tokenizer(in_message, return_tensors="pt")
629
649
 
630
- outputs = model.generate(inputs.input_ids, max_length=50, top_k=100, top_p=0.99)
650
+ outputs = model.generate(inputs.input_ids.cuda(), max_length=50, top_k=100, top_p=0.99)
631
651
  out_message = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
632
652
 
633
- print(out_message)
634
- )`,
653
+ print(out_message)`,
635
654
  ];
636
655
  export const cartesia_mlx = (model) => [
637
656
  `import mlx.core as mx
@@ -678,7 +697,8 @@ model = Text2Speech.from_pretrained("${model.id}")
678
697
  speech, *_ = model("text to generate speech from")`,
679
698
  ];
680
699
  export const espnetASR = (model) => [
681
- `from espnet2.bin.asr_inference import Speech2Text
700
+ `import soundfile
701
+ from espnet2.bin.asr_inference import Speech2Text
682
702
 
683
703
  model = Speech2Text.from_pretrained(
684
704
  "${model.id}"
@@ -705,9 +725,14 @@ models, cfg, task = load_model_ensemble_and_task_from_hf_hub(
705
725
  )`,
706
726
  ];
707
727
  export const flair = (model) => [
708
- `from flair.models import SequenceTagger
728
+ `from flair.data import Sentence
729
+ from flair.models import SequenceTagger
730
+
731
+ tagger = SequenceTagger.load("${model.id}")
709
732
 
710
- tagger = SequenceTagger.load("${model.id}")`,
733
+ sentence = Sentence("George Washington went to Washington.")
734
+ tagger.predict(sentence)
735
+ print(sentence)`,
711
736
  ];
712
737
  export const flexray = (model) => [
713
738
  `pip install flexray`,
@@ -783,21 +808,28 @@ print(f'Accuracy {accuracy_score(y_test, predictions):.2%}')
783
808
  return [installSnippet, classificationSnippet, regressionsSnippet, matchingSnippet];
784
809
  };
785
810
  export const gaussianformer = (model) => [
786
- `# Install from https://github.com/SVLwoof/gaussianformer
787
-
788
- from gaussianformer.pipelines.rendering_pipeline import GaussianFormerRenderingPipeline
811
+ `# pip install "gaussianformer @ git+https://github.com/SVLwoof/gaussianformer"
812
+ from gaussianformer import GaussianFormerRenderingPipeline, load_ply
789
813
 
790
- pipeline = GaussianFormerRenderingPipeline.from_pretrained("${model.id}")`,
814
+ pipeline = GaussianFormerRenderingPipeline.from_pretrained("${model.id}").to("cuda")
815
+ gaussians = load_ply("object.ply") # 3D Gaussian Splatting PLY -> [N, 14]`,
791
816
  ];
792
817
  export const gliner = (model) => [
793
818
  `from gliner import GLiNER
794
819
 
795
- model = GLiNER.from_pretrained("${model.id}")`,
820
+ model = GLiNER.from_pretrained("${model.id}")
821
+
822
+ text = "Cristiano Ronaldo dos Santos Aveiro was born on 5 February 1985 in Funchal, Madeira, Portugal."
823
+ labels = ["person", "date", "location"]
824
+
825
+ entities = model.predict_entities(text, labels)
826
+ for entity in entities:
827
+ print(entity["text"], "=>", entity["label"])`,
796
828
  ];
797
829
  export const gliner2 = (model) => [
798
- `from gliner2 import GLiNER2
830
+ `from gliner2 import AutoExtractor
799
831
 
800
- model = GLiNER2.from_pretrained("${model.id}")
832
+ extractor = AutoExtractor.from_pretrained("${model.id}")
801
833
 
802
834
  # Extract entities
803
835
  text = "Apple CEO Tim Cook announced iPhone 15 in Cupertino yesterday."
@@ -809,7 +841,7 @@ export const indextts = (model) => [
809
841
  `# Download model
810
842
  from huggingface_hub import snapshot_download
811
843
 
812
- snapshot_download(${model.id}, local_dir="checkpoints")
844
+ snapshot_download("${model.id}", local_dir="checkpoints")
813
845
 
814
846
  from indextts.infer import IndexTTS
815
847
 
@@ -863,9 +895,11 @@ print(out["answers"]["team"]["choice"])`,
863
895
  ];
864
896
  };
865
897
  export const keras = (model) => [
866
- `# Available backend options are: "jax", "torch", "tensorflow".
898
+ `# !pip install -U keras tensorflow huggingface_hub
899
+ # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here;
900
+ # "jax" and "torch" also work for computation once TensorFlow is installed.
867
901
  import os
868
- os.environ["KERAS_BACKEND"] = "jax"
902
+ os.environ["KERAS_BACKEND"] = "tensorflow"
869
903
 
870
904
  import keras
871
905
 
@@ -979,12 +1013,14 @@ export const kernels = (model) => [
979
1013
 
980
1014
  from kernels import get_kernel
981
1015
 
982
- kernel = get_kernel("${model.id}")`,
1016
+ # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones
1017
+ kernel = get_kernel("${model.id}", version=1)`,
983
1018
  ];
984
1019
  export const kimi_audio = (model) => [
985
1020
  `# Example usage for KimiAudio
986
- # pip install git+https://github.com/MoonshotAI/Kimi-Audio.git
1021
+ # pip install git+https://github.com/MoonshotAI/Kimi-Audio.git "transformers<5"
987
1022
 
1023
+ import soundfile as sf
988
1024
  from kimia_infer.api.kimia import KimiAudio
989
1025
 
990
1026
  model = KimiAudio(model_path="${model.id}", load_detokenizer=True)
@@ -1348,13 +1384,16 @@ pip install -U litert-lm
1348
1384
 
1349
1385
  # 2. Download and run this model locally:
1350
1386
  # See: https://ai.google.dev/edge/litert-lm/cli
1387
+ # A single .litertlm file in the repo is picked automatically; otherwise the CLI asks which one to run
1388
+ # (or pass its name right after the repo id).
1351
1389
  litert-lm run \\
1352
1390
  --from-huggingface-repo=${model.id} \\
1353
1391
  --prompt="Write me a poem"`,
1354
1392
  ];
1355
1393
  export const tf_keras = (model) => [
1356
- `# Note: 'keras<3.x' or 'tf_keras' must be installed (legacy)
1394
+ `# Note: 'keras<3.x' or 'tf_keras' must be installed (legacy), and from_pretrained_keras was removed in huggingface_hub 1.0.
1357
1395
  # See https://github.com/keras-team/tf-keras for more details.
1396
+ # !pip install "huggingface_hub<1.0" tf_keras
1358
1397
  from huggingface_hub import from_pretrained_keras
1359
1398
 
1360
1399
  model = from_pretrained_keras("${model.id}")
@@ -1400,6 +1439,16 @@ from MeshAnything.models.meshanything import MeshAnything
1400
1439
  # and https://github.com/buaacyw/MeshAnything/blob/main/app.py regarding usage
1401
1440
  model = MeshAnything(args)`,
1402
1441
  ];
1442
+ export const microduck = (model) => {
1443
+ // Model-card tag convention: tags: [microduck-slot:stand].
1444
+ const slots = ["walk", "stand", "sitstand", "ground_pick", "kick_left", "kick_right", "roulade"];
1445
+ const matchingSlots = slots.filter((slot) => model.tags.includes(`microduck-slot:${slot}`));
1446
+ const slot = matchingSlots.length === 1 ? matchingSlots[0] : "SLOT";
1447
+ const prefix = matchingSlots.length === 1
1448
+ ? ""
1449
+ : `# Replace SLOT with the slot specified in the model card (${slots.join(", ")}).\n`;
1450
+ return [`${prefix}sudo robotctl policy load ${slot} ${model.id}`];
1451
+ };
1403
1452
  export const multimolecule = (model) => {
1404
1453
  const widgetExample = model.widgetData?.[0];
1405
1454
  const exampleText = escapeStringForJson(widgetExample?.text ?? "");
@@ -1573,14 +1622,18 @@ model = pe.VisionTransformer.from_config("${model.id}", pretrained=True)`;
1573
1622
  };
1574
1623
  export const phantom_wan = (model) => [
1575
1624
  `from huggingface_hub import snapshot_download
1576
- from phantom_wan import WANI2V, configs
1625
+ from phantom_wan import WanI2V, configs
1626
+ from PIL import Image
1577
1627
 
1578
1628
  checkpoint_dir = snapshot_download("${model.id}")
1579
1629
  wan_i2v = WanI2V(
1580
- config=configs.WAN_CONFIGS['i2v-14B'],
1581
- checkpoint_dir=checkpoint_dir,
1582
- )
1583
- video = wan_i2v.generate(text_prompt, image_prompt)`,
1630
+ config=configs.WAN_CONFIGS['i2v-14B'],
1631
+ checkpoint_dir=checkpoint_dir,
1632
+ )
1633
+
1634
+ text_prompt = "A cat playing with a ball of yarn"
1635
+ image_prompt = Image.open("path/to/image.jpg").convert("RGB")
1636
+ video = wan_i2v.generate(text_prompt, image_prompt)`,
1584
1637
  ];
1585
1638
  export const pocket_tts = (model) => [
1586
1639
  `from pocket_tts import TTSModel
@@ -1694,7 +1747,7 @@ export const tensorflowtts = (model) => {
1694
1747
  export const timm = (model) => [
1695
1748
  `import timm
1696
1749
 
1697
- model = timm.create_model("hf_hub:${model.id}", pretrained=True)`,
1750
+ model = timm.create_model("hf-hub:${model.id}", pretrained=True)`,
1698
1751
  ];
1699
1752
  export const saelens = ( /* model: ModelData */) => [
1700
1753
  `# pip install sae-lens
@@ -1719,13 +1772,14 @@ llm = hydra.utils.instantiate(llm_cfg, torch_dtype="fp16")
1719
1772
  seed_story_cfg = OmegaConf.load(seed_story_cfg_path)
1720
1773
  seed_story = hydra.utils.instantiate(seed_story_cfg, llm=llm) `,
1721
1774
  ];
1722
- const skopsPickle = (model, modelFile) => {
1775
+ const SKLEARN_PICKLE_FILE = /\.(joblib|pkl|pickle)$/i;
1776
+ // skops.hub_utils was removed in skops 0.12, so files are downloaded with huggingface_hub
1777
+ const sklearnPickle = (model, modelFile) => {
1723
1778
  return [
1724
- `import joblib
1725
- from skops.hub_utils import download
1726
- download("${model.id}", "path_to_folder")
1779
+ `from huggingface_hub import hf_hub_download
1780
+ import joblib
1727
1781
  model = joblib.load(
1728
- "${escapeStringForJson(modelFile)}"
1782
+ hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}")
1729
1783
  )
1730
1784
  # only load pickle files from sources you trust
1731
1785
  # read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
@@ -1733,42 +1787,28 @@ model = joblib.load(
1733
1787
  };
1734
1788
  const skopsFormat = (model, modelFile) => {
1735
1789
  return [
1736
- `from skops.hub_utils import download
1790
+ `from huggingface_hub import hf_hub_download
1737
1791
  from skops.io import load
1738
- download("${model.id}", "path_to_folder")
1739
1792
  # make sure model file is in skops format
1740
1793
  # if model is a pickle file, make sure it's from a source you trust
1741
- model = load("path_to_folder/${escapeStringForJson(modelFile)}")`,
1742
- ];
1743
- };
1744
- const skopsJobLib = (model) => {
1745
- return [
1746
- `from huggingface_hub import hf_hub_download
1747
- import joblib
1748
- model = joblib.load(
1749
- hf_hub_download("${model.id}", "sklearn_model.joblib")
1750
- )
1751
- # only load pickle files from sources you trust
1752
- # read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
1794
+ model = load(hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}"))`,
1753
1795
  ];
1754
1796
  };
1755
1797
  export const sklearn = (model) => {
1798
+ const declaredFile = model.config?.sklearn?.model?.file;
1756
1799
  if (model.tags.includes("skops")) {
1757
- const skopsmodelFile = model.config?.sklearn?.model?.file;
1758
- const skopssaveFormat = model.config?.sklearn?.model_format;
1759
- if (!skopsmodelFile) {
1800
+ if (!declaredFile) {
1760
1801
  return [`# ⚠️ Model filename not specified in config.json`];
1761
1802
  }
1762
- if (skopssaveFormat === "pickle") {
1763
- return skopsPickle(model, skopsmodelFile);
1764
- }
1765
- else {
1766
- return skopsFormat(model, skopsmodelFile);
1803
+ // some skops repos declare no model_format for a pickled file (e.g. julien-c/skops-digits), which skops.io cannot read
1804
+ if (model.config?.sklearn?.model_format === "pickle" || SKLEARN_PICKLE_FILE.test(declaredFile)) {
1805
+ return sklearnPickle(model, declaredFile);
1767
1806
  }
1807
+ return skopsFormat(model, declaredFile);
1768
1808
  }
1769
- else {
1770
- return skopsJobLib(model);
1771
- }
1809
+ // repos pushed without skops often still declare their filename in config.json; it is only trusted when it names a
1810
+ // pickle, since some hand-written configs carry typos (e.g. "sklean_model.jpblib" next to a real sklearn_model.joblib)
1811
+ return sklearnPickle(model, declaredFile && SKLEARN_PICKLE_FILE.test(declaredFile) ? declaredFile : "sklearn_model.joblib");
1772
1812
  };
1773
1813
  export const stable_audio_tools = (model) => [
1774
1814
  `import torch
@@ -1816,7 +1856,7 @@ export const sam2 = (model) => {
1816
1856
  import torch
1817
1857
  from sam2.sam2_image_predictor import SAM2ImagePredictor
1818
1858
 
1819
- predictor = SAM2ImagePredictor.from_pretrained(${model.id})
1859
+ predictor = SAM2ImagePredictor.from_pretrained("${model.id}")
1820
1860
 
1821
1861
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
1822
1862
  predictor.set_image(<your_image>)
@@ -1825,13 +1865,13 @@ with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
1825
1865
  import torch
1826
1866
  from sam2.sam2_video_predictor import SAM2VideoPredictor
1827
1867
 
1828
- predictor = SAM2VideoPredictor.from_pretrained(${model.id})
1868
+ predictor = SAM2VideoPredictor.from_pretrained("${model.id}")
1829
1869
 
1830
1870
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
1831
1871
  state = predictor.init_state(<your_video>)
1832
1872
 
1833
1873
  # add new prompts and instantly get the output on the same frame
1834
- frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>):
1874
+ frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>)
1835
1875
 
1836
1876
  # propagate the prompts to get masklets throughout the video
1837
1877
  for frame_idx, object_ids, masks in predictor.propagate_in_video(state):
@@ -1851,11 +1891,18 @@ mask = load_single_mask("path_to_mask.png", index=14)
1851
1891
  output = inference(image, mask)`,
1852
1892
  ];
1853
1893
  export const sam_3d_body = (model) => [
1854
- `from notebook.utils import setup_sam_3d_body
1894
+ `import cv2
1895
+ import numpy as np
1896
+ from notebook.utils import setup_sam_3d_body
1897
+ from tools.vis_utils import visualize_sample_together
1898
+
1899
+ estimator = setup_sam_3d_body("${model.id}")
1855
1900
 
1856
- estimator = setup_sam_3d_body(${model.id})
1857
- outputs = estimator.process_one_image(image)
1858
- rend_img = visualize_sample_together(image, outputs, estimator.faces)`,
1901
+ img_bgr = cv2.imread("path/to/image.jpg")
1902
+ outputs = estimator.process_one_image(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
1903
+
1904
+ rend_img = visualize_sample_together(img_bgr, outputs, estimator.faces)
1905
+ cv2.imwrite("output.jpg", rend_img.astype(np.uint8))`,
1859
1906
  ];
1860
1907
  export const sampleFactory = (model) => [
1861
1908
  `python -m sample_factory.huggingface.load_from_hub -r ${model.id} -d ./train_dir`,
@@ -1946,7 +1993,15 @@ queries_emb = model.encode(queries, is_query=True)
1946
1993
  docs_emb = model.encode(documents, is_query=False)`,
1947
1994
  ];
1948
1995
  }
1949
- if (model.tags.includes("cross-encoder") || model.pipeline_tag == "text-ranking") {
1996
+ // Rerankers saved in the CrossEncoder format are often tagged text-classification rather than text-ranking;
1997
+ // their architecture still identifies them (e.g. BAAI/bge-reranker-v2-m3). SetFit models can share that shape but
1998
+ // are bi-encoders with a separate head.
1999
+ const isCrossEncoder = model.tags.includes("cross-encoder") ||
2000
+ model.pipeline_tag == "text-ranking" ||
2001
+ (model.pipeline_tag === "text-classification" &&
2002
+ !model.tags.includes("setfit") &&
2003
+ (model.config?.architectures ?? []).some((arch) => arch.endsWith("ForSequenceClassification")));
2004
+ if (isCrossEncoder) {
1950
2005
  return [
1951
2006
  `from sentence_transformers import CrossEncoder
1952
2007
 
@@ -1964,6 +2019,33 @@ scores = model.predict([(query, passage) for passage in passages])
1964
2019
  print(scores)`,
1965
2020
  ];
1966
2021
  }
2022
+ // sentence-transformers tags SparseEncoder repos (SPLADE & co) with "sparse-encoder" and MultiVectorEncoder repos
2023
+ // (ColBERT & co) with "multi-vector" when pushing them; both encode queries and documents separately
2024
+ const retrievalEncoder = model.tags.includes("sparse-encoder")
2025
+ ? "SparseEncoder"
2026
+ : model.tags.includes("multi-vector")
2027
+ ? "MultiVectorEncoder"
2028
+ : undefined;
2029
+ if (retrievalEncoder) {
2030
+ return [
2031
+ `from sentence_transformers import ${retrievalEncoder}
2032
+
2033
+ model = ${retrievalEncoder}("${model.id}"${remote_code_snippet})
2034
+
2035
+ queries = ["Which planet is known as the Red Planet?"]
2036
+ documents = [
2037
+ "Venus is often called Earth's twin because of its similar size and proximity.",
2038
+ "Mars, known for its reddish appearance, is often referred to as the Red Planet.",
2039
+ "Jupiter, the largest planet in our solar system, has a prominent red spot.",
2040
+ ]
2041
+
2042
+ query_embeddings = model.encode_query(queries)
2043
+ document_embeddings = model.encode_document(documents)
2044
+
2045
+ similarities = model.similarity(query_embeddings, document_embeddings)
2046
+ print(similarities)`,
2047
+ ];
2048
+ }
1967
2049
  const exampleSentences = get_widget_examples_from_st_model(model) ?? [
1968
2050
  "The weather is lovely today.",
1969
2051
  "It's so sunny outside!",
@@ -1985,7 +2067,10 @@ print(similarities.shape)
1985
2067
  export const setfit = (model) => [
1986
2068
  `from setfit import SetFitModel
1987
2069
 
1988
- model = SetFitModel.from_pretrained("${model.id}")`,
2070
+ model = SetFitModel.from_pretrained("${model.id}")
2071
+
2072
+ preds = model.predict(["i loved the spiderman movie!", "pineapple on pizza is the worst"])
2073
+ print(preds)`,
1989
2074
  ];
1990
2075
  export const spacy = (model) => [
1991
2076
  `!pip install https://huggingface.co/${model.id}/resolve/main/${nameWithoutNamespace(model.id)}-any-py3-none-any.whl
@@ -2001,7 +2086,10 @@ nlp = ${nameWithoutNamespace(model.id)}.load()`,
2001
2086
  export const span_marker = (model) => [
2002
2087
  `from span_marker import SpanMarkerModel
2003
2088
 
2004
- model = SpanMarkerModel.from_pretrained("${model.id}")`,
2089
+ model = SpanMarkerModel.from_pretrained("${model.id}")
2090
+
2091
+ entities = model.predict("Amelia Earhart flew her single engine Lockheed Vega 5B across the Atlantic to Paris.")
2092
+ print(entities)`,
2005
2093
  ];
2006
2094
  export const stanza = (model) => [
2007
2095
  `import stanza
@@ -2020,6 +2108,8 @@ const speechBrainMethod = (speechbrainInterface) => {
2020
2108
  return "enhance_file";
2021
2109
  case "SepformerSeparation":
2022
2110
  return "separate_file";
2111
+ case "SpeakerRecognition":
2112
+ return "verify_files";
2023
2113
  default:
2024
2114
  return undefined;
2025
2115
  }
@@ -2033,12 +2123,14 @@ export const speechbrain = (model) => {
2033
2123
  if (speechbrainMethod === undefined) {
2034
2124
  return [`# interface in config.json invalid`];
2035
2125
  }
2126
+ // speaker verification compares two recordings
2127
+ const methodArgs = speechbrainMethod === "verify_files" ? '"speaker1.wav", "speaker2.wav"' : '"file.wav"';
2036
2128
  return [
2037
2129
  `from speechbrain.pretrained import ${speechbrainInterface}
2038
2130
  model = ${speechbrainInterface}.from_hparams(
2039
2131
  "${model.id}"
2040
2132
  )
2041
- model.${speechbrainMethod}("file.wav")`,
2133
+ model.${speechbrainMethod}(${methodArgs})`,
2042
2134
  ];
2043
2135
  };
2044
2136
  export const terratorch = (model) => [
@@ -2068,7 +2160,7 @@ export const transformers = (model) => {
2068
2160
  : info.processor === "AutoFeatureExtractor"
2069
2161
  ? "extractor"
2070
2162
  : "processor";
2071
- autoSnippet.push("# Load model directly", `from transformers import ${info.processor}, ${auto_model}`, "", `${processorVarName} = ${info.processor}.from_pretrained("${model.id}"` + remote_code_snippet + ")", `model = ${auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ', device_map="auto")');
2163
+ autoSnippet.push("# pip install -U transformers accelerate", "# Load model directly", `from transformers import ${info.processor}, ${auto_model}`, "", `${processorVarName} = ${info.processor}.from_pretrained("${model.id}"` + remote_code_snippet + ")", `model = ${auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ', device_map="auto")');
2072
2164
  if (model.tags.includes("conversational") && hasChatTemplate(model)) {
2073
2165
  if (model.tags.includes("image-text-to-text")) {
2074
2166
  autoSnippet.push("messages = [", [
@@ -2084,19 +2176,19 @@ export const transformers = (model) => {
2084
2176
  else {
2085
2177
  autoSnippet.push("messages = [", ' {"role": "user", "content": "Who are you?"},', "]");
2086
2178
  }
2087
- autoSnippet.push(`inputs = ${processorVarName}.apply_chat_template(`, " messages,", " add_generation_prompt=True,", " tokenize=True,", " return_dict=True,", ' return_tensors="pt",', ").to(model.device)", "", "outputs = model.generate(**inputs, max_new_tokens=40)", `print(${processorVarName}.decode(outputs[0][inputs["input_ids"].shape[-1]:]))`);
2179
+ autoSnippet.push(`inputs = ${processorVarName}.apply_chat_template(`, " messages,", " add_generation_prompt=True,", " tokenize=True,", " return_dict=True,", ' return_tensors="pt",', ").to(model.device)", "", "outputs = model.generate(**inputs, max_new_tokens=256)", `print(${processorVarName}.decode(outputs[0][inputs["input_ids"].shape[-1]:]))`);
2088
2180
  }
2089
2181
  }
2090
2182
  else {
2091
- autoSnippet.push("# Load model directly", `from transformers import ${auto_model}`, `model = ${auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ', device_map="auto")');
2183
+ autoSnippet.push("# pip install -U transformers accelerate", "# Load model directly", `from transformers import ${auto_model}`, `model = ${auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ', device_map="auto")');
2092
2184
  }
2093
2185
  if (model.pipeline_tag && LIBRARY_TASK_MAPPING.transformers?.includes(model.pipeline_tag)) {
2094
2186
  const pipelineSnippet = ["# Use a pipeline as a high-level helper"];
2095
2187
  if (REMOVED_IN_V5_TRANSFORMERS_PIPELINES.includes(model.pipeline_tag)) {
2096
- pipelineSnippet.push(`# Warning: Pipeline type "${model.pipeline_tag}" is no longer supported in transformers v5.`, `# You must load the model directly (see below) or downgrade to v4.x with:`, `# 'pip install "transformers<5.0.0'`);
2188
+ pipelineSnippet.push(`# Warning: Pipeline type "${model.pipeline_tag}" is no longer supported in transformers v5.`, `# You must load the model directly (see below) or downgrade to v4.x with:`, `# pip install "transformers<5.0.0"`);
2097
2189
  }
2098
2190
  pipelineSnippet.push("from transformers import pipeline", "", `pipe = pipeline("${model.pipeline_tag}", model="${model.id}"` + remote_code_snippet + ")");
2099
- if (model.tags.includes("conversational")) {
2191
+ if (model.tags.includes("conversational") && hasChatTemplate(model)) {
2100
2192
  if (model.tags.includes("image-text-to-text")) {
2101
2193
  pipelineSnippet.push("messages = [", [
2102
2194
  " {",
@@ -2124,47 +2216,118 @@ export const transformers = (model) => {
2124
2216
  }
2125
2217
  return [autoSnippet.join("\n")];
2126
2218
  };
2219
+ // Hub tasks that transformers.js exposes under a different pipeline() task name
2220
+ const TRANSFORMERS_JS_TASK_ALIASES = {
2221
+ "sentence-similarity": "feature-extraction",
2222
+ };
2127
2223
  export const transformersJS = (model) => {
2128
2224
  if (!model.pipeline_tag) {
2129
2225
  return [`// ⚠️ Unknown pipeline tag`];
2130
2226
  }
2131
2227
  const libName = "@huggingface/transformers";
2228
+ // Rerankers score (query, document) pairs. The text-classification pipeline cannot take pairs and applies a softmax
2229
+ // over their single logit, so every score comes out as 1; the model is called directly instead.
2230
+ if (model.pipeline_tag === "text-ranking") {
2231
+ return [
2232
+ `// npm i ${libName}
2233
+ import { AutoTokenizer, AutoModelForSequenceClassification } from '${libName}';
2234
+
2235
+ const tokenizer = await AutoTokenizer.from_pretrained('${model.id}');
2236
+ const model = await AutoModelForSequenceClassification.from_pretrained('${model.id}');
2237
+
2238
+ const query = 'Which planet is known as the Red Planet?';
2239
+ const documents = [
2240
+ 'Mars, known for its reddish appearance, is often referred to as the Red Planet.',
2241
+ 'Venus is often called the twin of Earth because of its similar size and proximity.',
2242
+ ];
2243
+
2244
+ const inputs = tokenizer(new Array(documents.length).fill(query), { text_pair: documents, padding: true, truncation: true });
2245
+ const { logits } = await model(inputs);
2246
+ console.log(logits.sigmoid().tolist()); // one relevance score per document`,
2247
+ ];
2248
+ }
2249
+ const task = TRANSFORMERS_JS_TASK_ALIASES[model.pipeline_tag] ?? model.pipeline_tag;
2132
2250
  return [
2133
2251
  `// npm i ${libName}
2134
2252
  import { pipeline } from '${libName}';
2135
2253
 
2136
2254
  // Allocate pipeline
2137
- const pipe = await pipeline('${model.pipeline_tag}', '${model.id}');`,
2255
+ const pipe = await pipeline('${task}', '${model.id}');`,
2138
2256
  ];
2139
2257
  };
2140
- const peftTask = (peftTaskType) => {
2141
- switch (peftTaskType) {
2142
- case "CAUSAL_LM":
2143
- return "CausalLM";
2144
- case "SEQ_2_SEQ_LM":
2145
- return "Seq2SeqLM";
2146
- case "TOKEN_CLS":
2147
- return "TokenClassification";
2148
- case "SEQ_CLS":
2149
- return "SequenceClassification";
2150
- default:
2151
- return undefined;
2152
- }
2258
+ const PEFT_TASK_TYPE_TO_AUTO_CLASS = {
2259
+ CAUSAL_LM: "AutoModelForCausalLM",
2260
+ SEQ_2_SEQ_LM: "AutoModelForSeq2SeqLM",
2261
+ TOKEN_CLS: "AutoModelForTokenClassification",
2262
+ SEQ_CLS: "AutoModelForSequenceClassification",
2263
+ QUESTION_ANS: "AutoModelForQuestionAnswering",
2264
+ // transformers has no AutoModelForFeatureExtraction; PEFT's own AutoPeftModelForFeatureExtraction resolves to AutoModel
2265
+ FEATURE_EXTRACTION: "AutoModel",
2266
+ };
2267
+ // PEFT has no speech-specific task type: Whisper-style adapters are tagged SEQ_2_SEQ_LM, but transformers registers
2268
+ // Whisper and Speech2Text only under AutoModelForSpeechSeq2Seq (SeamlessM4T is under both seq2seq auto classes, SpeechT5
2269
+ // under the speech-to-text and text-to-spectrogram ones).
2270
+ // Other audio seq2seq models (Qwen2-Audio, GLM-ASR) stay under AutoModelForSeq2SeqLM, so the base model name is the
2271
+ // main signal. Matched against Hub repo ids, which are hyphenated (facebook/s2t-small-librispeech-asr,
2272
+ // facebook/seamless-m4t-v2-large), not against transformers model_type identifiers.
2273
+ const PEFT_SPEECH_TO_TEXT_BASE_MODEL = /whisper|\bs2t\b|speech[-_]to[-_]text/i;
2274
+ // SeamlessM4T also has a text-to-text head under AutoModelForSeq2SeqLM, which text translation adapters need; most
2275
+ // SeamlessM4T adapters on the Hub are speech ones, so the speech class stays the default when no text task is declared.
2276
+ const PEFT_SEAMLESS_M4T_BASE_MODEL = /seamless[-_]m4t/i;
2277
+ const PEFT_TEXT_TASKS = ["translation", "text2text-generation", "summarization"];
2278
+ // SpeechT5 checkpoints are overwhelmingly text-to-speech fine-tunes (often without "tts" in their name), which load
2279
+ // with AutoModelForTextToSpectrogram; AutoModelForSpeechSeq2Seq would silently load the speech-to-text head, so only
2280
+ // ASR bases or adapters get it.
2281
+ const PEFT_SPEECHT5_BASE_MODEL = /speecht5/i;
2282
+ const declaresTask = (model, task) => model.pipeline_tag === task || model.tags.includes(task);
2283
+ // the auto class a SEQ_2_SEQ_LM adapter needs when its base is a speech model, undefined for text seq2seq bases
2284
+ const peftSpeechAutoClass = (model, baseModel) => {
2285
+ if (PEFT_SPEECH_TO_TEXT_BASE_MODEL.test(baseModel)) {
2286
+ return "AutoModelForSpeechSeq2Seq";
2287
+ }
2288
+ if (PEFT_SEAMLESS_M4T_BASE_MODEL.test(baseModel)) {
2289
+ return PEFT_TEXT_TASKS.some((task) => declaresTask(model, task)) ? undefined : "AutoModelForSpeechSeq2Seq";
2290
+ }
2291
+ if (PEFT_SPEECHT5_BASE_MODEL.test(baseModel)) {
2292
+ const isAsr = /(?:^|[^a-z])asr(?:[^a-z]|$)/i.test(baseModel) || declaresTask(model, "automatic-speech-recognition");
2293
+ return isAsr ? "AutoModelForSpeechSeq2Seq" : "AutoModelForTextToSpectrogram";
2294
+ }
2295
+ return undefined;
2153
2296
  };
2154
2297
  export const peft = (model) => {
2155
2298
  const { base_model_name_or_path: peftBaseModel, task_type: peftTaskType } = model.config?.peft ?? {};
2156
- const pefttask = peftTask(peftTaskType);
2157
- if (!pefttask) {
2299
+ let autoClass = peftTaskType ? PEFT_TASK_TYPE_TO_AUTO_CLASS[peftTaskType] : undefined;
2300
+ if (!autoClass) {
2158
2301
  return [`Task type is invalid.`];
2159
2302
  }
2160
- if (!peftBaseModel) {
2303
+ // adapter_config.json sometimes omits the base model; the model card's base_model is the next best source
2304
+ const cardBaseModel = Array.isArray(model.cardData?.base_model)
2305
+ ? model.cardData.base_model[0]
2306
+ : model.cardData?.base_model;
2307
+ const baseModel = peftBaseModel || cardBaseModel;
2308
+ if (!baseModel) {
2161
2309
  return [`Base model is not found.`];
2162
2310
  }
2311
+ if (peftTaskType === "SEQ_2_SEQ_LM") {
2312
+ autoClass = peftSpeechAutoClass(model, baseModel) ?? autoClass;
2313
+ }
2314
+ if (autoClass === "AutoModelForTextToSpectrogram") {
2315
+ return [
2316
+ `from peft import PeftConfig, PeftModel
2317
+ from transformers import AutoModelForTextToSpectrogram
2318
+
2319
+ base_model = AutoModelForTextToSpectrogram.from_pretrained("${escapeStringForJson(baseModel)}")
2320
+ # PEFT's seq2seq wrapper needs generate(), which SpeechT5's text-to-speech model lacks: load the adapter without its task type
2321
+ config = PeftConfig.from_pretrained("${model.id}")
2322
+ config.task_type = None
2323
+ model = PeftModel.from_pretrained(base_model, "${model.id}", config=config)`,
2324
+ ];
2325
+ }
2163
2326
  return [
2164
2327
  `from peft import PeftModel
2165
- from transformers import AutoModelFor${pefttask}
2328
+ from transformers import ${autoClass}
2166
2329
 
2167
- base_model = AutoModelFor${pefttask}.from_pretrained("${escapeStringForJson(peftBaseModel)}")
2330
+ base_model = ${autoClass}.from_pretrained("${escapeStringForJson(baseModel)}")
2168
2331
  model = PeftModel.from_pretrained(base_model, "${model.id}")`,
2169
2332
  ];
2170
2333
  };
@@ -2195,7 +2358,7 @@ transcriptions = asr_model.transcribe(["file.wav"])`,
2195
2358
  }
2196
2359
  };
2197
2360
  export const mlAgents = (model) => [
2198
- `mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./download: string[]s"`,
2361
+ `mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./downloads"`,
2199
2362
  ];
2200
2363
  export const sentis = ( /* model: ModelData */) => [
2201
2364
  `string modelName = "[Your model name here].sentis";
@@ -2264,10 +2427,10 @@ model = Model.from_pretrained("${model.id}")`,
2264
2427
  ];
2265
2428
  export const lvface = (model) => [
2266
2429
  `from huggingface_hub import hf_hub_download
2267
- from inference_onnx import LVFaceONNXInferencer
2430
+ from inference_onnx import LVFaceONNXInferencer
2268
2431
 
2269
2432
  model_path = hf_hub_download("${model.id}", "LVFace-L_Glint360K/LVFace-L_Glint360K.onnx")
2270
- inferencer = LVFaceONNXInferencer(model_path, use_gpu=True, timeout=300)
2433
+ inferencer = LVFaceONNXInferencer(model_path, use_gpu=True)
2271
2434
  img_path = 'path/to/image1.jpg'
2272
2435
  embedding = inferencer.infer_from_image(img_path)`,
2273
2436
  ];
@@ -2303,8 +2466,7 @@ export const vui = () => [
2303
2466
 
2304
2467
  import torchaudio
2305
2468
 
2306
- from vui.inference import render
2307
- from vui.model import Vui,
2469
+ from vui.legacy import Vui, render
2308
2470
 
2309
2471
  model = Vui.from_pretrained().cuda()
2310
2472
  waveform = render(
@@ -2312,15 +2474,16 @@ waveform = render(
2312
2474
  "Hey, here is some random stuff, usually something quite long as the shorter the text the less likely the model can cope!",
2313
2475
  )
2314
2476
  print(waveform.shape)
2315
- torchaudio.save("out.opus", waveform[0], 22050)
2477
+ torchaudio.save("out.wav", waveform[0], 22050)
2316
2478
  `,
2317
2479
  ];
2318
2480
  export const chattts = () => [
2319
2481
  `import ChatTTS
2482
+ import torch
2320
2483
  import torchaudio
2321
2484
 
2322
2485
  chat = ChatTTS.Chat()
2323
- chat.load_models(compile=False) # Set to True for better performance
2486
+ chat.load(compile=False) # Set to True for better performance
2324
2487
 
2325
2488
  texts = ["PUT YOUR TEXT HERE",]
2326
2489
 
@@ -2329,17 +2492,42 @@ wavs = chat.infer(texts, )
2329
2492
  torchaudio.save("output1.wav", torch.from_numpy(wavs[0]), 24000)`,
2330
2493
  ];
2331
2494
  export const ultralytics = (model) => {
2332
- // ultralytics models must have a version tag (e.g. `yolov8`)
2333
- const versionTag = model.tags.find((tag) => tag.match(/^yolov\d+$/));
2334
- const className = versionTag ? `YOLOv${versionTag.slice(4)}` : "YOLOvXX";
2335
- const prefix = versionTag
2336
- ? ""
2337
- : `# Couldn't find a valid YOLO version tag.\n# Replace XX with the correct version.\n`;
2495
+ // the YOLOv10 fork (library "yolov10") ships its own `YOLOv10` class with a `from_pretrained` helper that reads the
2496
+ // model.safetensors it pushes. Mainline ultralytics repos often list "yolov10" among their version tags
2497
+ // (e.g. Ultralytics/YOLOv8) but only ship .pt files, so an ultralytics tag alone keeps them on the mainline loader;
2498
+ // fork pushes still tagged ultralytics are recognized by their safetensors weights.
2499
+ const isYolov10Fork = model.library_name === "yolov10" ||
2500
+ (model.tags.includes("yolov10") && (!model.tags.includes("ultralytics") || model.tags.includes("safetensors")));
2501
+ if (isYolov10Fork) {
2502
+ // from_pretrained reads the model.safetensors that fork pushes ship; older fork repos only ship .pt weights
2503
+ if (model.tags.includes("safetensors")) {
2504
+ return [
2505
+ `from ultralytics import YOLOv10
2506
+
2507
+ model = YOLOv10.from_pretrained("${model.id}")
2508
+ source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
2509
+ model.predict(source=source, save=True)`,
2510
+ ];
2511
+ }
2512
+ return [
2513
+ `from huggingface_hub import hf_hub_download
2514
+ from ultralytics import YOLOv10
2515
+
2516
+ # pick the weights file from this repo's "Files and versions" tab
2517
+ weights = hf_hub_download("${model.id}", "<weights>.pt")
2518
+ model = YOLOv10(weights)
2519
+ source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
2520
+ model.predict(source=source, save=True)`,
2521
+ ];
2522
+ }
2523
+ // mainline ultralytics loads weights from a local file
2338
2524
  return [
2339
- prefix +
2340
- `from ultralytics import ${className}
2525
+ `from huggingface_hub import hf_hub_download
2526
+ from ultralytics import YOLO
2341
2527
 
2342
- model = ${className}.from_pretrained("${model.id}")
2528
+ # pick the weights file from this repo's "Files and versions" tab
2529
+ weights = hf_hub_download("${model.id}", "<weights>.pt")
2530
+ model = YOLO(weights)
2343
2531
  source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
2344
2532
  model.predict(source=source, save=True)`,
2345
2533
  ];
@@ -2412,21 +2600,23 @@ model = SwarmFormerModel.from_pretrained("${model.id}")
2412
2600
  ];
2413
2601
  export const univa = (model) => [
2414
2602
  `# Follow installation instructions at https://github.com/PKU-YuanGroup/UniWorld-V1
2415
-
2603
+ import torch
2604
+ from transformers import AutoProcessor
2416
2605
  from univa.models.qwen2p5vl.modeling_univa_qwen2p5vl import UnivaQwen2p5VLForConditionalGeneration
2417
- model = UnivaQwen2p5VLForConditionalGeneration.from_pretrained(
2418
- "${model.id}",
2419
- torch_dtype=torch.bfloat16,
2420
- attn_implementation="flash_attention_2",
2421
- ).to("cuda")
2422
- processor = AutoProcessor.from_pretrained("${model.id}")
2606
+
2607
+ model = UnivaQwen2p5VLForConditionalGeneration.from_pretrained(
2608
+ "${model.id}",
2609
+ torch_dtype=torch.bfloat16,
2610
+ attn_implementation="flash_attention_2",
2611
+ ).to("cuda")
2612
+ processor = AutoProcessor.from_pretrained("${model.id}")
2423
2613
  `,
2424
2614
  ];
2425
2615
  const mlx_unknown = (model) => [
2426
2616
  `# Download the model from the Hub
2427
2617
  pip install huggingface_hub[hf_xet]
2428
2618
 
2429
- huggingface-cli download --local-dir ${nameWithoutNamespace(model.id)} ${model.id}`,
2619
+ hf download ${model.id} --local-dir ${nameWithoutNamespace(model.id)}`,
2430
2620
  ];
2431
2621
  const mlxlm = (model) => [
2432
2622
  `# Make sure mlx-lm is installed
@@ -2483,10 +2673,11 @@ formatted_prompt = apply_chat_template(
2483
2673
  output = generate(model, processor, formatted_prompt, image)
2484
2674
  print(output)`,
2485
2675
  ];
2676
+ // create_model() takes a name from the mlx-image registry, not a repo id; the weights live in "mlx-vision/<name>-mlxim"
2486
2677
  export const mlxim = (model) => [
2487
2678
  `from mlxim.model import create_model
2488
2679
 
2489
- model = create_model(${model.id})`,
2680
+ model = create_model("${nameWithoutNamespace(model.id).replace(/-mlxim$/, "")}")`,
2490
2681
  ];
2491
2682
  export const mlx = (model) => {
2492
2683
  if (model.pipeline_tag === "image-text-to-text") {
@@ -2505,7 +2696,10 @@ export const mlx = (model) => {
2505
2696
  export const model2vec = (model) => [
2506
2697
  `from model2vec import StaticModel
2507
2698
 
2508
- model = StaticModel.from_pretrained("${model.id}")`,
2699
+ model = StaticModel.from_pretrained("${model.id}")
2700
+
2701
+ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
2702
+ print(embeddings.shape)`,
2509
2703
  ];
2510
2704
  export const mobilint = (model) => {
2511
2705
  const modelName = nameWithoutNamespace(model.id);
@@ -2548,7 +2742,8 @@ export const pruna = (model) => {
2548
2742
  };
2549
2743
  snippets = snippets.map(ensurePrunaModelImport);
2550
2744
  if (model.tags.includes("pruna_pro-ai")) {
2551
- return snippets.map((snippet) => snippet.replace(/\bpruna\b/g, "pruna_pro").replace(/\bPrunaModel\b/g, "PrunaProModel"));
2745
+ // only rewrite the import and the class name, never the repo id (an org name can contain "pruna")
2746
+ return snippets.map((snippet) => snippet.replace(/^from pruna import /gm, "from pruna_pro import ").replace(/\bPrunaModel\b/g, "PrunaProModel"));
2552
2747
  }
2553
2748
  return snippets;
2554
2749
  };
@@ -2606,21 +2801,19 @@ export const outetts = (model) => {
2606
2801
  }
2607
2802
  // v1.0 HF → minimal runnable snippet
2608
2803
  return [
2609
- `
2610
- import outetts
2611
-
2612
- enum = outetts.Models("${model.id}".split("/", 1)[1]) # VERSION_1_0_SIZE_1B
2613
- cfg = outetts.ModelConfig.auto_config(enum, outetts.Backend.HF)
2614
- tts = outetts.Interface(cfg)
2615
-
2616
- speaker = tts.load_default_speaker("EN-FEMALE-1-NEUTRAL")
2617
- tts.generate(
2618
- outetts.GenerationConfig(
2619
- text="Hello there, how are you doing?",
2620
- speaker=speaker,
2621
- )
2622
- ).save("output.wav")
2623
- `,
2804
+ `import outetts
2805
+
2806
+ enum = outetts.Models("${model.id}".split("/", 1)[1]) # VERSION_1_0_SIZE_1B
2807
+ cfg = outetts.ModelConfig.auto_config(enum, outetts.Backend.HF)
2808
+ tts = outetts.Interface(cfg)
2809
+
2810
+ speaker = tts.load_default_speaker("EN-FEMALE-1-NEUTRAL")
2811
+ tts.generate(
2812
+ outetts.GenerationConfig(
2813
+ text="Hello there, how are you doing?",
2814
+ speaker=speaker,
2815
+ )
2816
+ ).save("output.wav")`,
2624
2817
  ];
2625
2818
  };
2626
2819
  export const pxia = (model) => [