@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.
@@ -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) => [
@@ -64,7 +64,13 @@ result, message = detector.detect_watermark(watermarked_audio, sr)`;
64
64
  return [watermarkSnippet, detectorSnippet];
65
65
  };
66
66
  function get_base_diffusers_model(model) {
67
- return escapeStringForJson(model.cardData?.base_model?.toString() ?? "fill-in-base-model");
67
+ const baseModel = model.cardData?.base_model;
68
+ // several base models can be listed (e.g. a LoRA trained for a base and its turbo variant). When an original checkpoint
69
+ // and its Diffusers conversion are both listed (["Wan-AI/Wan2.1-I2V-14B-480P", "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"]),
70
+ // only the conversion has a model_index.json that DiffusionPipeline can load; otherwise the first one is used
71
+ const candidates = Array.isArray(baseModel) ? baseModel : baseModel ? [baseModel] : [];
72
+ const chosen = candidates.find((id) => id.toLowerCase().endsWith("-diffusers")) ?? candidates[0];
73
+ return escapeStringForJson(chosen ?? "fill-in-base-model");
68
74
  }
69
75
  function get_prompt_from_diffusers_model(model) {
70
76
  const prompt = model.widgetData?.[0]?.text ?? model.cardData?.instance_prompt;
@@ -145,7 +151,7 @@ detector = cvg.NeuralCornerDetector(checkpoint)
145
151
  # Embedder models, such as Milo:
146
152
  embedder = cvg.NeuralEmbedder(checkpoint)`,
147
153
  ];
148
- export const colipri = (model) => {
154
+ export const colipri = () => {
149
155
  const installSnippet = `pip install colipri`;
150
156
  const exampleSnippet = `from colipri import get_model
151
157
  from colipri import get_processor
@@ -154,7 +160,7 @@ from colipri import ZeroShotImageClassificationPipeline
154
160
 
155
161
  model = get_model().cuda()
156
162
  processor = get_processor()
157
- pipeline = ZeroShotImageClassificationPipeline("${model.id}", processor)
163
+ pipeline = ZeroShotImageClassificationPipeline(model, processor)
158
164
 
159
165
  image = load_sample_ct()
160
166
 
@@ -271,7 +277,7 @@ export const depth_anything_v2 = (model) => {
271
277
  else if (model.id === "depth-anything/Depth-Anything-V2-Large") {
272
278
  encoder = "vitl";
273
279
  features = "256";
274
- out_channels = "[256, 512, 1024, 1024";
280
+ out_channels = "[256, 512, 1024, 1024]";
275
281
  }
276
282
  return [
277
283
  `
@@ -280,6 +286,7 @@ export const depth_anything_v2 = (model) => {
280
286
  # Load the model and infer depth from an image
281
287
  import cv2
282
288
  import torch
289
+ from huggingface_hub import hf_hub_download
283
290
 
284
291
  from depth_anything_v2.dpt import DepthAnythingV2
285
292
 
@@ -289,7 +296,8 @@ model = DepthAnythingV2(encoder="${encoder}", features=${features}, out_channels
289
296
  # load the weights
290
297
  filepath = hf_hub_download(repo_id="${model.id}", filename="depth_anything_v2_${encoder}.pth", repo_type="model")
291
298
  state_dict = torch.load(filepath, map_location="cpu")
292
- model.load_state_dict(state_dict).eval()
299
+ model.load_state_dict(state_dict)
300
+ model.eval()
293
301
 
294
302
  raw_img = cv2.imread("your/image/path")
295
303
  depth = model.infer_image(raw_img) # HxW raw depth map in numpy
@@ -298,8 +306,8 @@ depth = model.infer_image(raw_img) # HxW raw depth map in numpy
298
306
  };
299
307
  export const depth_pro = (model) => {
300
308
  const installSnippet = `# Download checkpoint
301
- pip install huggingface-hub
302
- huggingface-cli download --local-dir checkpoints ${model.id}`;
309
+ pip install huggingface_hub
310
+ hf download ${model.id} --local-dir checkpoints`;
303
311
  const inferenceSnippet = `import depth_pro
304
312
 
305
313
  # Load model and preprocessing transform
@@ -367,7 +375,7 @@ export const describe_anything = (model) => [
367
375
  from huggingface_hub import snapshot_download
368
376
  from dam import DescribeAnythingModel
369
377
 
370
- snapshot_download(${model.id}, local_dir="checkpoints")
378
+ snapshot_download("${model.id}", local_dir="checkpoints")
371
379
 
372
380
  dam = DescribeAnythingModel(
373
381
  model_path="checkpoints",
@@ -478,7 +486,7 @@ pipe.load_lora_weights("${model.id}")
478
486
  prompt = "${get_prompt_from_diffusers_model(model) ?? diffusersVideoDefaultPrompt}"
479
487
  input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/guitar-man.png")
480
488
 
481
- image = pipe(image=input_image, prompt=prompt).frames[0]
489
+ output = pipe(image=input_image, prompt=prompt).frames[0]
482
490
  export_to_video(output, "output.mp4")`,
483
491
  ];
484
492
  const diffusers_textual_inversion = (model) => [
@@ -518,7 +526,7 @@ from diffusers import AutoPipelineForInpainting
518
526
  from diffusers.utils import load_image
519
527
 
520
528
  # switch to "mps" for apple devices
521
- pipe = AutoPipelineForInpainting.from_pretrained("${model.id}", dtype=torch.float16, variant="fp16", device_map="cuda")
529
+ pipe = AutoPipelineForInpainting.from_pretrained("${model.id}", dtype=torch.float16, device_map="cuda")
522
530
 
523
531
  img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
524
532
  mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
@@ -540,9 +548,12 @@ image = pipe(
540
548
  ).images[0]`,
541
549
  ];
542
550
  export const diffusers = (model) => {
551
+ // the Hub emits pipeline class tags as `diffusers:<ClassName>`; bare names are kept for manually added tags
552
+ const hasPipelineClass = (className) => model.tags.includes(`diffusers:${className}`) ||
553
+ model.tags.includes(className) ||
554
+ model.config?.diffusers?._class_name === className;
543
555
  let codeSnippets;
544
- if (model.tags.includes("StableDiffusionInpaintPipeline") ||
545
- model.tags.includes("StableDiffusionXLInpaintPipeline")) {
556
+ if (hasPipelineClass("StableDiffusionInpaintPipeline") || hasPipelineClass("StableDiffusionXLInpaintPipeline")) {
546
557
  codeSnippets = diffusers_inpainting(model);
547
558
  }
548
559
  else if (model.tags.includes("controlnet")) {
@@ -565,7 +576,7 @@ export const diffusers = (model) => {
565
576
  else if (model.tags.includes("textual_inversion")) {
566
577
  codeSnippets = diffusers_textual_inversion(model);
567
578
  }
568
- else if (model.tags.includes("FluxFillPipeline")) {
579
+ else if (hasPipelineClass("FluxFillPipeline")) {
569
580
  codeSnippets = diffusers_flux_fill(model);
570
581
  }
571
582
  else if (model.pipeline_tag === "image-to-video") {
@@ -586,7 +597,7 @@ from diffusionkit.mlx import DiffusionPipeline
586
597
  pipeline = DiffusionPipeline(
587
598
  shift=3.0,
588
599
  use_t5=False,
589
- model_version=${model.id},
600
+ model_version="${model.id}",
590
601
  low_memory_mode=True,
591
602
  a16=True,
592
603
  w16=True,
@@ -596,7 +607,7 @@ from diffusionkit.mlx import FluxPipeline
596
607
 
597
608
  pipeline = FluxPipeline(
598
609
  shift=1.0,
599
- model_version=${model.id},
610
+ model_version="${model.id}",
600
611
  low_memory_mode=True,
601
612
  a16=True,
602
613
  w16=True,
@@ -617,21 +628,22 @@ image, _ = pipeline.generate_image(
617
628
  return [pipelineSnippet, generateSnippet];
618
629
  };
619
630
  export const cartesia_pytorch = (model) => [
620
- `# pip install --no-binary :all: cartesia-pytorch
621
- from cartesia_pytorch import ReneLMHeadModel
631
+ `# The cartesia-pytorch package on PyPI ships no code: clone the repo and put its cartesia-pytorch folder on PYTHONPATH
632
+ # (it needs an Ampere or newer CUDA GPU, torch 2.4, mamba-ssm 2.2.2 and flash-attn 2.6.3)
633
+ # git clone https://github.com/cartesia-ai/edge && export PYTHONPATH="$PWD/edge/cartesia-pytorch:$PYTHONPATH"
634
+ from cartesia_pytorch.Rene import ReneLMHeadModel
622
635
  from transformers import AutoTokenizer
623
636
 
624
- model = ReneLMHeadModel.from_pretrained("${model.id}")
637
+ model = ReneLMHeadModel.from_pretrained("${model.id}").half().cuda()
625
638
  tokenizer = AutoTokenizer.from_pretrained("allenai/OLMo-1B-hf")
626
639
 
627
640
  in_message = ["Rene Descartes was"]
628
641
  inputs = tokenizer(in_message, return_tensors="pt")
629
642
 
630
- outputs = model.generate(inputs.input_ids, max_length=50, top_k=100, top_p=0.99)
643
+ outputs = model.generate(inputs.input_ids.cuda(), max_length=50, top_k=100, top_p=0.99)
631
644
  out_message = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
632
645
 
633
- print(out_message)
634
- )`,
646
+ print(out_message)`,
635
647
  ];
636
648
  export const cartesia_mlx = (model) => [
637
649
  `import mlx.core as mx
@@ -678,7 +690,8 @@ model = Text2Speech.from_pretrained("${model.id}")
678
690
  speech, *_ = model("text to generate speech from")`,
679
691
  ];
680
692
  export const espnetASR = (model) => [
681
- `from espnet2.bin.asr_inference import Speech2Text
693
+ `import soundfile
694
+ from espnet2.bin.asr_inference import Speech2Text
682
695
 
683
696
  model = Speech2Text.from_pretrained(
684
697
  "${model.id}"
@@ -705,9 +718,14 @@ models, cfg, task = load_model_ensemble_and_task_from_hf_hub(
705
718
  )`,
706
719
  ];
707
720
  export const flair = (model) => [
708
- `from flair.models import SequenceTagger
721
+ `from flair.data import Sentence
722
+ from flair.models import SequenceTagger
709
723
 
710
- tagger = SequenceTagger.load("${model.id}")`,
724
+ tagger = SequenceTagger.load("${model.id}")
725
+
726
+ sentence = Sentence("George Washington went to Washington.")
727
+ tagger.predict(sentence)
728
+ print(sentence)`,
711
729
  ];
712
730
  export const flexray = (model) => [
713
731
  `pip install flexray`,
@@ -782,15 +800,29 @@ print(f'Accuracy {accuracy_score(y_test, predictions):.2%}')
782
800
  # print(f'AUROC {roc_auc_score(y_test, probas[:, 1]):.2%}')`;
783
801
  return [installSnippet, classificationSnippet, regressionsSnippet, matchingSnippet];
784
802
  };
803
+ export const gaussianformer = (model) => [
804
+ `# Install from https://github.com/SVLwoof/gaussianformer
805
+
806
+ from gaussianformer.pipelines.rendering_pipeline import GaussianFormerRenderingPipeline
807
+
808
+ pipeline = GaussianFormerRenderingPipeline.from_pretrained("${model.id}")`,
809
+ ];
785
810
  export const gliner = (model) => [
786
811
  `from gliner import GLiNER
787
812
 
788
- model = GLiNER.from_pretrained("${model.id}")`,
813
+ model = GLiNER.from_pretrained("${model.id}")
814
+
815
+ text = "Cristiano Ronaldo dos Santos Aveiro was born on 5 February 1985 in Funchal, Madeira, Portugal."
816
+ labels = ["person", "date", "location"]
817
+
818
+ entities = model.predict_entities(text, labels)
819
+ for entity in entities:
820
+ print(entity["text"], "=>", entity["label"])`,
789
821
  ];
790
822
  export const gliner2 = (model) => [
791
- `from gliner2 import GLiNER2
823
+ `from gliner2 import AutoExtractor
792
824
 
793
- model = GLiNER2.from_pretrained("${model.id}")
825
+ extractor = AutoExtractor.from_pretrained("${model.id}")
794
826
 
795
827
  # Extract entities
796
828
  text = "Apple CEO Tim Cook announced iPhone 15 in Cupertino yesterday."
@@ -802,7 +834,7 @@ export const indextts = (model) => [
802
834
  `# Download model
803
835
  from huggingface_hub import snapshot_download
804
836
 
805
- snapshot_download(${model.id}, local_dir="checkpoints")
837
+ snapshot_download("${model.id}", local_dir="checkpoints")
806
838
 
807
839
  from indextts.infer import IndexTTS
808
840
 
@@ -856,9 +888,11 @@ print(out["answers"]["team"]["choice"])`,
856
888
  ];
857
889
  };
858
890
  export const keras = (model) => [
859
- `# Available backend options are: "jax", "torch", "tensorflow".
891
+ `# !pip install -U keras tensorflow huggingface_hub
892
+ # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here;
893
+ # "jax" and "torch" also work for computation once TensorFlow is installed.
860
894
  import os
861
- os.environ["KERAS_BACKEND"] = "jax"
895
+ os.environ["KERAS_BACKEND"] = "tensorflow"
862
896
 
863
897
  import keras
864
898
 
@@ -972,12 +1006,14 @@ export const kernels = (model) => [
972
1006
 
973
1007
  from kernels import get_kernel
974
1008
 
975
- kernel = get_kernel("${model.id}")`,
1009
+ # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones
1010
+ kernel = get_kernel("${model.id}", version=1)`,
976
1011
  ];
977
1012
  export const kimi_audio = (model) => [
978
1013
  `# Example usage for KimiAudio
979
- # pip install git+https://github.com/MoonshotAI/Kimi-Audio.git
1014
+ # pip install git+https://github.com/MoonshotAI/Kimi-Audio.git "transformers<5"
980
1015
 
1016
+ import soundfile as sf
981
1017
  from kimia_infer.api.kimia import KimiAudio
982
1018
 
983
1019
  model = KimiAudio(model_path="${model.id}", load_detokenizer=True)
@@ -1341,13 +1377,16 @@ pip install -U litert-lm
1341
1377
 
1342
1378
  # 2. Download and run this model locally:
1343
1379
  # See: https://ai.google.dev/edge/litert-lm/cli
1380
+ # A single .litertlm file in the repo is picked automatically; otherwise the CLI asks which one to run
1381
+ # (or pass its name right after the repo id).
1344
1382
  litert-lm run \\
1345
1383
  --from-huggingface-repo=${model.id} \\
1346
1384
  --prompt="Write me a poem"`,
1347
1385
  ];
1348
1386
  export const tf_keras = (model) => [
1349
- `# Note: 'keras<3.x' or 'tf_keras' must be installed (legacy)
1387
+ `# Note: 'keras<3.x' or 'tf_keras' must be installed (legacy), and from_pretrained_keras was removed in huggingface_hub 1.0.
1350
1388
  # See https://github.com/keras-team/tf-keras for more details.
1389
+ # !pip install "huggingface_hub<1.0" tf_keras
1351
1390
  from huggingface_hub import from_pretrained_keras
1352
1391
 
1353
1392
  model = from_pretrained_keras("${model.id}")
@@ -1393,6 +1432,16 @@ from MeshAnything.models.meshanything import MeshAnything
1393
1432
  # and https://github.com/buaacyw/MeshAnything/blob/main/app.py regarding usage
1394
1433
  model = MeshAnything(args)`,
1395
1434
  ];
1435
+ export const microduck = (model) => {
1436
+ // Model-card tag convention: tags: [microduck-slot:stand].
1437
+ const slots = ["walk", "stand", "sitstand", "ground_pick", "kick_left", "kick_right", "roulade"];
1438
+ const matchingSlots = slots.filter((slot) => model.tags.includes(`microduck-slot:${slot}`));
1439
+ const slot = matchingSlots.length === 1 ? matchingSlots[0] : "SLOT";
1440
+ const prefix = matchingSlots.length === 1
1441
+ ? ""
1442
+ : `# Replace SLOT with the slot specified in the model card (${slots.join(", ")}).\n`;
1443
+ return [`${prefix}sudo robotctl policy load ${slot} ${model.id}`];
1444
+ };
1396
1445
  export const multimolecule = (model) => {
1397
1446
  const widgetExample = model.widgetData?.[0];
1398
1447
  const exampleText = escapeStringForJson(widgetExample?.text ?? "");
@@ -1566,14 +1615,18 @@ model = pe.VisionTransformer.from_config("${model.id}", pretrained=True)`;
1566
1615
  };
1567
1616
  export const phantom_wan = (model) => [
1568
1617
  `from huggingface_hub import snapshot_download
1569
- from phantom_wan import WANI2V, configs
1618
+ from phantom_wan import WanI2V, configs
1619
+ from PIL import Image
1570
1620
 
1571
1621
  checkpoint_dir = snapshot_download("${model.id}")
1572
1622
  wan_i2v = WanI2V(
1573
- config=configs.WAN_CONFIGS['i2v-14B'],
1574
- checkpoint_dir=checkpoint_dir,
1575
- )
1576
- video = wan_i2v.generate(text_prompt, image_prompt)`,
1623
+ config=configs.WAN_CONFIGS['i2v-14B'],
1624
+ checkpoint_dir=checkpoint_dir,
1625
+ )
1626
+
1627
+ text_prompt = "A cat playing with a ball of yarn"
1628
+ image_prompt = Image.open("path/to/image.jpg").convert("RGB")
1629
+ video = wan_i2v.generate(text_prompt, image_prompt)`,
1577
1630
  ];
1578
1631
  export const pocket_tts = (model) => [
1579
1632
  `from pocket_tts import TTSModel
@@ -1687,7 +1740,7 @@ export const tensorflowtts = (model) => {
1687
1740
  export const timm = (model) => [
1688
1741
  `import timm
1689
1742
 
1690
- model = timm.create_model("hf_hub:${model.id}", pretrained=True)`,
1743
+ model = timm.create_model("hf-hub:${model.id}", pretrained=True)`,
1691
1744
  ];
1692
1745
  export const saelens = ( /* model: ModelData */) => [
1693
1746
  `# pip install sae-lens
@@ -1712,13 +1765,14 @@ llm = hydra.utils.instantiate(llm_cfg, torch_dtype="fp16")
1712
1765
  seed_story_cfg = OmegaConf.load(seed_story_cfg_path)
1713
1766
  seed_story = hydra.utils.instantiate(seed_story_cfg, llm=llm) `,
1714
1767
  ];
1715
- const skopsPickle = (model, modelFile) => {
1768
+ const SKLEARN_PICKLE_FILE = /\.(joblib|pkl|pickle)$/i;
1769
+ // skops.hub_utils was removed in skops 0.12, so files are downloaded with huggingface_hub
1770
+ const sklearnPickle = (model, modelFile) => {
1716
1771
  return [
1717
- `import joblib
1718
- from skops.hub_utils import download
1719
- download("${model.id}", "path_to_folder")
1772
+ `from huggingface_hub import hf_hub_download
1773
+ import joblib
1720
1774
  model = joblib.load(
1721
- "${escapeStringForJson(modelFile)}"
1775
+ hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}")
1722
1776
  )
1723
1777
  # only load pickle files from sources you trust
1724
1778
  # read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
@@ -1726,42 +1780,28 @@ model = joblib.load(
1726
1780
  };
1727
1781
  const skopsFormat = (model, modelFile) => {
1728
1782
  return [
1729
- `from skops.hub_utils import download
1783
+ `from huggingface_hub import hf_hub_download
1730
1784
  from skops.io import load
1731
- download("${model.id}", "path_to_folder")
1732
1785
  # make sure model file is in skops format
1733
1786
  # if model is a pickle file, make sure it's from a source you trust
1734
- model = load("path_to_folder/${escapeStringForJson(modelFile)}")`,
1735
- ];
1736
- };
1737
- const skopsJobLib = (model) => {
1738
- return [
1739
- `from huggingface_hub import hf_hub_download
1740
- import joblib
1741
- model = joblib.load(
1742
- hf_hub_download("${model.id}", "sklearn_model.joblib")
1743
- )
1744
- # only load pickle files from sources you trust
1745
- # read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
1787
+ model = load(hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}"))`,
1746
1788
  ];
1747
1789
  };
1748
1790
  export const sklearn = (model) => {
1791
+ const declaredFile = model.config?.sklearn?.model?.file;
1749
1792
  if (model.tags.includes("skops")) {
1750
- const skopsmodelFile = model.config?.sklearn?.model?.file;
1751
- const skopssaveFormat = model.config?.sklearn?.model_format;
1752
- if (!skopsmodelFile) {
1793
+ if (!declaredFile) {
1753
1794
  return [`# ⚠️ Model filename not specified in config.json`];
1754
1795
  }
1755
- if (skopssaveFormat === "pickle") {
1756
- return skopsPickle(model, skopsmodelFile);
1757
- }
1758
- else {
1759
- return skopsFormat(model, skopsmodelFile);
1796
+ // some skops repos declare no model_format for a pickled file (e.g. julien-c/skops-digits), which skops.io cannot read
1797
+ if (model.config?.sklearn?.model_format === "pickle" || SKLEARN_PICKLE_FILE.test(declaredFile)) {
1798
+ return sklearnPickle(model, declaredFile);
1760
1799
  }
1800
+ return skopsFormat(model, declaredFile);
1761
1801
  }
1762
- else {
1763
- return skopsJobLib(model);
1764
- }
1802
+ // repos pushed without skops often still declare their filename in config.json; it is only trusted when it names a
1803
+ // pickle, since some hand-written configs carry typos (e.g. "sklean_model.jpblib" next to a real sklearn_model.joblib)
1804
+ return sklearnPickle(model, declaredFile && SKLEARN_PICKLE_FILE.test(declaredFile) ? declaredFile : "sklearn_model.joblib");
1765
1805
  };
1766
1806
  export const stable_audio_tools = (model) => [
1767
1807
  `import torch
@@ -1809,7 +1849,7 @@ export const sam2 = (model) => {
1809
1849
  import torch
1810
1850
  from sam2.sam2_image_predictor import SAM2ImagePredictor
1811
1851
 
1812
- predictor = SAM2ImagePredictor.from_pretrained(${model.id})
1852
+ predictor = SAM2ImagePredictor.from_pretrained("${model.id}")
1813
1853
 
1814
1854
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
1815
1855
  predictor.set_image(<your_image>)
@@ -1818,13 +1858,13 @@ with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
1818
1858
  import torch
1819
1859
  from sam2.sam2_video_predictor import SAM2VideoPredictor
1820
1860
 
1821
- predictor = SAM2VideoPredictor.from_pretrained(${model.id})
1861
+ predictor = SAM2VideoPredictor.from_pretrained("${model.id}")
1822
1862
 
1823
1863
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
1824
1864
  state = predictor.init_state(<your_video>)
1825
1865
 
1826
1866
  # add new prompts and instantly get the output on the same frame
1827
- frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>):
1867
+ frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>)
1828
1868
 
1829
1869
  # propagate the prompts to get masklets throughout the video
1830
1870
  for frame_idx, object_ids, masks in predictor.propagate_in_video(state):
@@ -1844,11 +1884,18 @@ mask = load_single_mask("path_to_mask.png", index=14)
1844
1884
  output = inference(image, mask)`,
1845
1885
  ];
1846
1886
  export const sam_3d_body = (model) => [
1847
- `from notebook.utils import setup_sam_3d_body
1887
+ `import cv2
1888
+ import numpy as np
1889
+ from notebook.utils import setup_sam_3d_body
1890
+ from tools.vis_utils import visualize_sample_together
1848
1891
 
1849
- estimator = setup_sam_3d_body(${model.id})
1850
- outputs = estimator.process_one_image(image)
1851
- rend_img = visualize_sample_together(image, outputs, estimator.faces)`,
1892
+ estimator = setup_sam_3d_body("${model.id}")
1893
+
1894
+ img_bgr = cv2.imread("path/to/image.jpg")
1895
+ outputs = estimator.process_one_image(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
1896
+
1897
+ rend_img = visualize_sample_together(img_bgr, outputs, estimator.faces)
1898
+ cv2.imwrite("output.jpg", rend_img.astype(np.uint8))`,
1852
1899
  ];
1853
1900
  export const sampleFactory = (model) => [
1854
1901
  `python -m sample_factory.huggingface.load_from_hub -r ${model.id} -d ./train_dir`,
@@ -1939,7 +1986,15 @@ queries_emb = model.encode(queries, is_query=True)
1939
1986
  docs_emb = model.encode(documents, is_query=False)`,
1940
1987
  ];
1941
1988
  }
1942
- if (model.tags.includes("cross-encoder") || model.pipeline_tag == "text-ranking") {
1989
+ // Rerankers saved in the CrossEncoder format are often tagged text-classification rather than text-ranking;
1990
+ // their architecture still identifies them (e.g. BAAI/bge-reranker-v2-m3). SetFit models can share that shape but
1991
+ // are bi-encoders with a separate head.
1992
+ const isCrossEncoder = model.tags.includes("cross-encoder") ||
1993
+ model.pipeline_tag == "text-ranking" ||
1994
+ (model.pipeline_tag === "text-classification" &&
1995
+ !model.tags.includes("setfit") &&
1996
+ (model.config?.architectures ?? []).some((arch) => arch.endsWith("ForSequenceClassification")));
1997
+ if (isCrossEncoder) {
1943
1998
  return [
1944
1999
  `from sentence_transformers import CrossEncoder
1945
2000
 
@@ -1957,6 +2012,33 @@ scores = model.predict([(query, passage) for passage in passages])
1957
2012
  print(scores)`,
1958
2013
  ];
1959
2014
  }
2015
+ // sentence-transformers tags SparseEncoder repos (SPLADE & co) with "sparse-encoder" and MultiVectorEncoder repos
2016
+ // (ColBERT & co) with "multi-vector" when pushing them; both encode queries and documents separately
2017
+ const retrievalEncoder = model.tags.includes("sparse-encoder")
2018
+ ? "SparseEncoder"
2019
+ : model.tags.includes("multi-vector")
2020
+ ? "MultiVectorEncoder"
2021
+ : undefined;
2022
+ if (retrievalEncoder) {
2023
+ return [
2024
+ `from sentence_transformers import ${retrievalEncoder}
2025
+
2026
+ model = ${retrievalEncoder}("${model.id}"${remote_code_snippet})
2027
+
2028
+ queries = ["Which planet is known as the Red Planet?"]
2029
+ documents = [
2030
+ "Venus is often called Earth's twin because of its similar size and proximity.",
2031
+ "Mars, known for its reddish appearance, is often referred to as the Red Planet.",
2032
+ "Jupiter, the largest planet in our solar system, has a prominent red spot.",
2033
+ ]
2034
+
2035
+ query_embeddings = model.encode_query(queries)
2036
+ document_embeddings = model.encode_document(documents)
2037
+
2038
+ similarities = model.similarity(query_embeddings, document_embeddings)
2039
+ print(similarities)`,
2040
+ ];
2041
+ }
1960
2042
  const exampleSentences = get_widget_examples_from_st_model(model) ?? [
1961
2043
  "The weather is lovely today.",
1962
2044
  "It's so sunny outside!",
@@ -1978,7 +2060,10 @@ print(similarities.shape)
1978
2060
  export const setfit = (model) => [
1979
2061
  `from setfit import SetFitModel
1980
2062
 
1981
- model = SetFitModel.from_pretrained("${model.id}")`,
2063
+ model = SetFitModel.from_pretrained("${model.id}")
2064
+
2065
+ preds = model.predict(["i loved the spiderman movie!", "pineapple on pizza is the worst"])
2066
+ print(preds)`,
1982
2067
  ];
1983
2068
  export const spacy = (model) => [
1984
2069
  `!pip install https://huggingface.co/${model.id}/resolve/main/${nameWithoutNamespace(model.id)}-any-py3-none-any.whl
@@ -1994,7 +2079,10 @@ nlp = ${nameWithoutNamespace(model.id)}.load()`,
1994
2079
  export const span_marker = (model) => [
1995
2080
  `from span_marker import SpanMarkerModel
1996
2081
 
1997
- model = SpanMarkerModel.from_pretrained("${model.id}")`,
2082
+ model = SpanMarkerModel.from_pretrained("${model.id}")
2083
+
2084
+ entities = model.predict("Amelia Earhart flew her single engine Lockheed Vega 5B across the Atlantic to Paris.")
2085
+ print(entities)`,
1998
2086
  ];
1999
2087
  export const stanza = (model) => [
2000
2088
  `import stanza
@@ -2013,6 +2101,8 @@ const speechBrainMethod = (speechbrainInterface) => {
2013
2101
  return "enhance_file";
2014
2102
  case "SepformerSeparation":
2015
2103
  return "separate_file";
2104
+ case "SpeakerRecognition":
2105
+ return "verify_files";
2016
2106
  default:
2017
2107
  return undefined;
2018
2108
  }
@@ -2026,12 +2116,14 @@ export const speechbrain = (model) => {
2026
2116
  if (speechbrainMethod === undefined) {
2027
2117
  return [`# interface in config.json invalid`];
2028
2118
  }
2119
+ // speaker verification compares two recordings
2120
+ const methodArgs = speechbrainMethod === "verify_files" ? '"speaker1.wav", "speaker2.wav"' : '"file.wav"';
2029
2121
  return [
2030
2122
  `from speechbrain.pretrained import ${speechbrainInterface}
2031
2123
  model = ${speechbrainInterface}.from_hparams(
2032
2124
  "${model.id}"
2033
2125
  )
2034
- model.${speechbrainMethod}("file.wav")`,
2126
+ model.${speechbrainMethod}(${methodArgs})`,
2035
2127
  ];
2036
2128
  };
2037
2129
  export const terratorch = (model) => [
@@ -2061,7 +2153,7 @@ export const transformers = (model) => {
2061
2153
  : info.processor === "AutoFeatureExtractor"
2062
2154
  ? "extractor"
2063
2155
  : "processor";
2064
- 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")');
2156
+ 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")');
2065
2157
  if (model.tags.includes("conversational") && hasChatTemplate(model)) {
2066
2158
  if (model.tags.includes("image-text-to-text")) {
2067
2159
  autoSnippet.push("messages = [", [
@@ -2077,19 +2169,19 @@ export const transformers = (model) => {
2077
2169
  else {
2078
2170
  autoSnippet.push("messages = [", ' {"role": "user", "content": "Who are you?"},', "]");
2079
2171
  }
2080
- 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]:]))`);
2172
+ 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]:]))`);
2081
2173
  }
2082
2174
  }
2083
2175
  else {
2084
- autoSnippet.push("# Load model directly", `from transformers import ${auto_model}`, `model = ${auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ', device_map="auto")');
2176
+ 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")');
2085
2177
  }
2086
2178
  if (model.pipeline_tag && LIBRARY_TASK_MAPPING.transformers?.includes(model.pipeline_tag)) {
2087
2179
  const pipelineSnippet = ["# Use a pipeline as a high-level helper"];
2088
2180
  if (REMOVED_IN_V5_TRANSFORMERS_PIPELINES.includes(model.pipeline_tag)) {
2089
- 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'`);
2181
+ 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"`);
2090
2182
  }
2091
2183
  pipelineSnippet.push("from transformers import pipeline", "", `pipe = pipeline("${model.pipeline_tag}", model="${model.id}"` + remote_code_snippet + ")");
2092
- if (model.tags.includes("conversational")) {
2184
+ if (model.tags.includes("conversational") && hasChatTemplate(model)) {
2093
2185
  if (model.tags.includes("image-text-to-text")) {
2094
2186
  pipelineSnippet.push("messages = [", [
2095
2187
  " {",
@@ -2117,47 +2209,118 @@ export const transformers = (model) => {
2117
2209
  }
2118
2210
  return [autoSnippet.join("\n")];
2119
2211
  };
2212
+ // Hub tasks that transformers.js exposes under a different pipeline() task name
2213
+ const TRANSFORMERS_JS_TASK_ALIASES = {
2214
+ "sentence-similarity": "feature-extraction",
2215
+ };
2120
2216
  export const transformersJS = (model) => {
2121
2217
  if (!model.pipeline_tag) {
2122
2218
  return [`// ⚠️ Unknown pipeline tag`];
2123
2219
  }
2124
2220
  const libName = "@huggingface/transformers";
2221
+ // Rerankers score (query, document) pairs. The text-classification pipeline cannot take pairs and applies a softmax
2222
+ // over their single logit, so every score comes out as 1; the model is called directly instead.
2223
+ if (model.pipeline_tag === "text-ranking") {
2224
+ return [
2225
+ `// npm i ${libName}
2226
+ import { AutoTokenizer, AutoModelForSequenceClassification } from '${libName}';
2227
+
2228
+ const tokenizer = await AutoTokenizer.from_pretrained('${model.id}');
2229
+ const model = await AutoModelForSequenceClassification.from_pretrained('${model.id}');
2230
+
2231
+ const query = 'Which planet is known as the Red Planet?';
2232
+ const documents = [
2233
+ 'Mars, known for its reddish appearance, is often referred to as the Red Planet.',
2234
+ 'Venus is often called the twin of Earth because of its similar size and proximity.',
2235
+ ];
2236
+
2237
+ const inputs = tokenizer(new Array(documents.length).fill(query), { text_pair: documents, padding: true, truncation: true });
2238
+ const { logits } = await model(inputs);
2239
+ console.log(logits.sigmoid().tolist()); // one relevance score per document`,
2240
+ ];
2241
+ }
2242
+ const task = TRANSFORMERS_JS_TASK_ALIASES[model.pipeline_tag] ?? model.pipeline_tag;
2125
2243
  return [
2126
2244
  `// npm i ${libName}
2127
2245
  import { pipeline } from '${libName}';
2128
2246
 
2129
2247
  // Allocate pipeline
2130
- const pipe = await pipeline('${model.pipeline_tag}', '${model.id}');`,
2248
+ const pipe = await pipeline('${task}', '${model.id}');`,
2131
2249
  ];
2132
2250
  };
2133
- const peftTask = (peftTaskType) => {
2134
- switch (peftTaskType) {
2135
- case "CAUSAL_LM":
2136
- return "CausalLM";
2137
- case "SEQ_2_SEQ_LM":
2138
- return "Seq2SeqLM";
2139
- case "TOKEN_CLS":
2140
- return "TokenClassification";
2141
- case "SEQ_CLS":
2142
- return "SequenceClassification";
2143
- default:
2144
- return undefined;
2145
- }
2251
+ const PEFT_TASK_TYPE_TO_AUTO_CLASS = {
2252
+ CAUSAL_LM: "AutoModelForCausalLM",
2253
+ SEQ_2_SEQ_LM: "AutoModelForSeq2SeqLM",
2254
+ TOKEN_CLS: "AutoModelForTokenClassification",
2255
+ SEQ_CLS: "AutoModelForSequenceClassification",
2256
+ QUESTION_ANS: "AutoModelForQuestionAnswering",
2257
+ // transformers has no AutoModelForFeatureExtraction; PEFT's own AutoPeftModelForFeatureExtraction resolves to AutoModel
2258
+ FEATURE_EXTRACTION: "AutoModel",
2259
+ };
2260
+ // PEFT has no speech-specific task type: Whisper-style adapters are tagged SEQ_2_SEQ_LM, but transformers registers
2261
+ // Whisper and Speech2Text only under AutoModelForSpeechSeq2Seq (SeamlessM4T is under both seq2seq auto classes, SpeechT5
2262
+ // under the speech-to-text and text-to-spectrogram ones).
2263
+ // Other audio seq2seq models (Qwen2-Audio, GLM-ASR) stay under AutoModelForSeq2SeqLM, so the base model name is the
2264
+ // main signal. Matched against Hub repo ids, which are hyphenated (facebook/s2t-small-librispeech-asr,
2265
+ // facebook/seamless-m4t-v2-large), not against transformers model_type identifiers.
2266
+ const PEFT_SPEECH_TO_TEXT_BASE_MODEL = /whisper|\bs2t\b|speech[-_]to[-_]text/i;
2267
+ // SeamlessM4T also has a text-to-text head under AutoModelForSeq2SeqLM, which text translation adapters need; most
2268
+ // SeamlessM4T adapters on the Hub are speech ones, so the speech class stays the default when no text task is declared.
2269
+ const PEFT_SEAMLESS_M4T_BASE_MODEL = /seamless[-_]m4t/i;
2270
+ const PEFT_TEXT_TASKS = ["translation", "text2text-generation", "summarization"];
2271
+ // SpeechT5 checkpoints are overwhelmingly text-to-speech fine-tunes (often without "tts" in their name), which load
2272
+ // with AutoModelForTextToSpectrogram; AutoModelForSpeechSeq2Seq would silently load the speech-to-text head, so only
2273
+ // ASR bases or adapters get it.
2274
+ const PEFT_SPEECHT5_BASE_MODEL = /speecht5/i;
2275
+ const declaresTask = (model, task) => model.pipeline_tag === task || model.tags.includes(task);
2276
+ // the auto class a SEQ_2_SEQ_LM adapter needs when its base is a speech model, undefined for text seq2seq bases
2277
+ const peftSpeechAutoClass = (model, baseModel) => {
2278
+ if (PEFT_SPEECH_TO_TEXT_BASE_MODEL.test(baseModel)) {
2279
+ return "AutoModelForSpeechSeq2Seq";
2280
+ }
2281
+ if (PEFT_SEAMLESS_M4T_BASE_MODEL.test(baseModel)) {
2282
+ return PEFT_TEXT_TASKS.some((task) => declaresTask(model, task)) ? undefined : "AutoModelForSpeechSeq2Seq";
2283
+ }
2284
+ if (PEFT_SPEECHT5_BASE_MODEL.test(baseModel)) {
2285
+ const isAsr = /(?:^|[^a-z])asr(?:[^a-z]|$)/i.test(baseModel) || declaresTask(model, "automatic-speech-recognition");
2286
+ return isAsr ? "AutoModelForSpeechSeq2Seq" : "AutoModelForTextToSpectrogram";
2287
+ }
2288
+ return undefined;
2146
2289
  };
2147
2290
  export const peft = (model) => {
2148
2291
  const { base_model_name_or_path: peftBaseModel, task_type: peftTaskType } = model.config?.peft ?? {};
2149
- const pefttask = peftTask(peftTaskType);
2150
- if (!pefttask) {
2292
+ let autoClass = peftTaskType ? PEFT_TASK_TYPE_TO_AUTO_CLASS[peftTaskType] : undefined;
2293
+ if (!autoClass) {
2151
2294
  return [`Task type is invalid.`];
2152
2295
  }
2153
- if (!peftBaseModel) {
2296
+ // adapter_config.json sometimes omits the base model; the model card's base_model is the next best source
2297
+ const cardBaseModel = Array.isArray(model.cardData?.base_model)
2298
+ ? model.cardData.base_model[0]
2299
+ : model.cardData?.base_model;
2300
+ const baseModel = peftBaseModel || cardBaseModel;
2301
+ if (!baseModel) {
2154
2302
  return [`Base model is not found.`];
2155
2303
  }
2304
+ if (peftTaskType === "SEQ_2_SEQ_LM") {
2305
+ autoClass = peftSpeechAutoClass(model, baseModel) ?? autoClass;
2306
+ }
2307
+ if (autoClass === "AutoModelForTextToSpectrogram") {
2308
+ return [
2309
+ `from peft import PeftConfig, PeftModel
2310
+ from transformers import AutoModelForTextToSpectrogram
2311
+
2312
+ base_model = AutoModelForTextToSpectrogram.from_pretrained("${escapeStringForJson(baseModel)}")
2313
+ # PEFT's seq2seq wrapper needs generate(), which SpeechT5's text-to-speech model lacks: load the adapter without its task type
2314
+ config = PeftConfig.from_pretrained("${model.id}")
2315
+ config.task_type = None
2316
+ model = PeftModel.from_pretrained(base_model, "${model.id}", config=config)`,
2317
+ ];
2318
+ }
2156
2319
  return [
2157
2320
  `from peft import PeftModel
2158
- from transformers import AutoModelFor${pefttask}
2321
+ from transformers import ${autoClass}
2159
2322
 
2160
- base_model = AutoModelFor${pefttask}.from_pretrained("${escapeStringForJson(peftBaseModel)}")
2323
+ base_model = ${autoClass}.from_pretrained("${escapeStringForJson(baseModel)}")
2161
2324
  model = PeftModel.from_pretrained(base_model, "${model.id}")`,
2162
2325
  ];
2163
2326
  };
@@ -2188,7 +2351,7 @@ transcriptions = asr_model.transcribe(["file.wav"])`,
2188
2351
  }
2189
2352
  };
2190
2353
  export const mlAgents = (model) => [
2191
- `mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./download: string[]s"`,
2354
+ `mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./downloads"`,
2192
2355
  ];
2193
2356
  export const sentis = ( /* model: ModelData */) => [
2194
2357
  `string modelName = "[Your model name here].sentis";
@@ -2257,10 +2420,10 @@ model = Model.from_pretrained("${model.id}")`,
2257
2420
  ];
2258
2421
  export const lvface = (model) => [
2259
2422
  `from huggingface_hub import hf_hub_download
2260
- from inference_onnx import LVFaceONNXInferencer
2423
+ from inference_onnx import LVFaceONNXInferencer
2261
2424
 
2262
2425
  model_path = hf_hub_download("${model.id}", "LVFace-L_Glint360K/LVFace-L_Glint360K.onnx")
2263
- inferencer = LVFaceONNXInferencer(model_path, use_gpu=True, timeout=300)
2426
+ inferencer = LVFaceONNXInferencer(model_path, use_gpu=True)
2264
2427
  img_path = 'path/to/image1.jpg'
2265
2428
  embedding = inferencer.infer_from_image(img_path)`,
2266
2429
  ];
@@ -2296,8 +2459,7 @@ export const vui = () => [
2296
2459
 
2297
2460
  import torchaudio
2298
2461
 
2299
- from vui.inference import render
2300
- from vui.model import Vui,
2462
+ from vui.legacy import Vui, render
2301
2463
 
2302
2464
  model = Vui.from_pretrained().cuda()
2303
2465
  waveform = render(
@@ -2305,15 +2467,16 @@ waveform = render(
2305
2467
  "Hey, here is some random stuff, usually something quite long as the shorter the text the less likely the model can cope!",
2306
2468
  )
2307
2469
  print(waveform.shape)
2308
- torchaudio.save("out.opus", waveform[0], 22050)
2470
+ torchaudio.save("out.wav", waveform[0], 22050)
2309
2471
  `,
2310
2472
  ];
2311
2473
  export const chattts = () => [
2312
2474
  `import ChatTTS
2475
+ import torch
2313
2476
  import torchaudio
2314
2477
 
2315
2478
  chat = ChatTTS.Chat()
2316
- chat.load_models(compile=False) # Set to True for better performance
2479
+ chat.load(compile=False) # Set to True for better performance
2317
2480
 
2318
2481
  texts = ["PUT YOUR TEXT HERE",]
2319
2482
 
@@ -2322,17 +2485,42 @@ wavs = chat.infer(texts, )
2322
2485
  torchaudio.save("output1.wav", torch.from_numpy(wavs[0]), 24000)`,
2323
2486
  ];
2324
2487
  export const ultralytics = (model) => {
2325
- // ultralytics models must have a version tag (e.g. `yolov8`)
2326
- const versionTag = model.tags.find((tag) => tag.match(/^yolov\d+$/));
2327
- const className = versionTag ? `YOLOv${versionTag.slice(4)}` : "YOLOvXX";
2328
- const prefix = versionTag
2329
- ? ""
2330
- : `# Couldn't find a valid YOLO version tag.\n# Replace XX with the correct version.\n`;
2488
+ // the YOLOv10 fork (library "yolov10") ships its own `YOLOv10` class with a `from_pretrained` helper that reads the
2489
+ // model.safetensors it pushes. Mainline ultralytics repos often list "yolov10" among their version tags
2490
+ // (e.g. Ultralytics/YOLOv8) but only ship .pt files, so an ultralytics tag alone keeps them on the mainline loader;
2491
+ // fork pushes still tagged ultralytics are recognized by their safetensors weights.
2492
+ const isYolov10Fork = model.library_name === "yolov10" ||
2493
+ (model.tags.includes("yolov10") && (!model.tags.includes("ultralytics") || model.tags.includes("safetensors")));
2494
+ if (isYolov10Fork) {
2495
+ // from_pretrained reads the model.safetensors that fork pushes ship; older fork repos only ship .pt weights
2496
+ if (model.tags.includes("safetensors")) {
2497
+ return [
2498
+ `from ultralytics import YOLOv10
2499
+
2500
+ model = YOLOv10.from_pretrained("${model.id}")
2501
+ source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
2502
+ model.predict(source=source, save=True)`,
2503
+ ];
2504
+ }
2505
+ return [
2506
+ `from huggingface_hub import hf_hub_download
2507
+ from ultralytics import YOLOv10
2508
+
2509
+ # pick the weights file from this repo's "Files and versions" tab
2510
+ weights = hf_hub_download("${model.id}", "<weights>.pt")
2511
+ model = YOLOv10(weights)
2512
+ source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
2513
+ model.predict(source=source, save=True)`,
2514
+ ];
2515
+ }
2516
+ // mainline ultralytics loads weights from a local file
2331
2517
  return [
2332
- prefix +
2333
- `from ultralytics import ${className}
2518
+ `from huggingface_hub import hf_hub_download
2519
+ from ultralytics import YOLO
2334
2520
 
2335
- model = ${className}.from_pretrained("${model.id}")
2521
+ # pick the weights file from this repo's "Files and versions" tab
2522
+ weights = hf_hub_download("${model.id}", "<weights>.pt")
2523
+ model = YOLO(weights)
2336
2524
  source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
2337
2525
  model.predict(source=source, save=True)`,
2338
2526
  ];
@@ -2405,21 +2593,23 @@ model = SwarmFormerModel.from_pretrained("${model.id}")
2405
2593
  ];
2406
2594
  export const univa = (model) => [
2407
2595
  `# Follow installation instructions at https://github.com/PKU-YuanGroup/UniWorld-V1
2408
-
2596
+ import torch
2597
+ from transformers import AutoProcessor
2409
2598
  from univa.models.qwen2p5vl.modeling_univa_qwen2p5vl import UnivaQwen2p5VLForConditionalGeneration
2410
- model = UnivaQwen2p5VLForConditionalGeneration.from_pretrained(
2411
- "${model.id}",
2412
- torch_dtype=torch.bfloat16,
2413
- attn_implementation="flash_attention_2",
2414
- ).to("cuda")
2415
- processor = AutoProcessor.from_pretrained("${model.id}")
2599
+
2600
+ model = UnivaQwen2p5VLForConditionalGeneration.from_pretrained(
2601
+ "${model.id}",
2602
+ torch_dtype=torch.bfloat16,
2603
+ attn_implementation="flash_attention_2",
2604
+ ).to("cuda")
2605
+ processor = AutoProcessor.from_pretrained("${model.id}")
2416
2606
  `,
2417
2607
  ];
2418
2608
  const mlx_unknown = (model) => [
2419
2609
  `# Download the model from the Hub
2420
2610
  pip install huggingface_hub[hf_xet]
2421
2611
 
2422
- huggingface-cli download --local-dir ${nameWithoutNamespace(model.id)} ${model.id}`,
2612
+ hf download ${model.id} --local-dir ${nameWithoutNamespace(model.id)}`,
2423
2613
  ];
2424
2614
  const mlxlm = (model) => [
2425
2615
  `# Make sure mlx-lm is installed
@@ -2476,10 +2666,11 @@ formatted_prompt = apply_chat_template(
2476
2666
  output = generate(model, processor, formatted_prompt, image)
2477
2667
  print(output)`,
2478
2668
  ];
2669
+ // create_model() takes a name from the mlx-image registry, not a repo id; the weights live in "mlx-vision/<name>-mlxim"
2479
2670
  export const mlxim = (model) => [
2480
2671
  `from mlxim.model import create_model
2481
2672
 
2482
- model = create_model(${model.id})`,
2673
+ model = create_model("${nameWithoutNamespace(model.id).replace(/-mlxim$/, "")}")`,
2483
2674
  ];
2484
2675
  export const mlx = (model) => {
2485
2676
  if (model.pipeline_tag === "image-text-to-text") {
@@ -2498,7 +2689,10 @@ export const mlx = (model) => {
2498
2689
  export const model2vec = (model) => [
2499
2690
  `from model2vec import StaticModel
2500
2691
 
2501
- model = StaticModel.from_pretrained("${model.id}")`,
2692
+ model = StaticModel.from_pretrained("${model.id}")
2693
+
2694
+ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
2695
+ print(embeddings.shape)`,
2502
2696
  ];
2503
2697
  export const mobilint = (model) => {
2504
2698
  const modelName = nameWithoutNamespace(model.id);
@@ -2541,7 +2735,8 @@ export const pruna = (model) => {
2541
2735
  };
2542
2736
  snippets = snippets.map(ensurePrunaModelImport);
2543
2737
  if (model.tags.includes("pruna_pro-ai")) {
2544
- return snippets.map((snippet) => snippet.replace(/\bpruna\b/g, "pruna_pro").replace(/\bPrunaModel\b/g, "PrunaProModel"));
2738
+ // only rewrite the import and the class name, never the repo id (an org name can contain "pruna")
2739
+ return snippets.map((snippet) => snippet.replace(/^from pruna import /gm, "from pruna_pro import ").replace(/\bPrunaModel\b/g, "PrunaProModel"));
2545
2740
  }
2546
2741
  return snippets;
2547
2742
  };
@@ -2599,21 +2794,19 @@ export const outetts = (model) => {
2599
2794
  }
2600
2795
  // v1.0 HF → minimal runnable snippet
2601
2796
  return [
2602
- `
2603
- import outetts
2604
-
2605
- enum = outetts.Models("${model.id}".split("/", 1)[1]) # VERSION_1_0_SIZE_1B
2606
- cfg = outetts.ModelConfig.auto_config(enum, outetts.Backend.HF)
2607
- tts = outetts.Interface(cfg)
2608
-
2609
- speaker = tts.load_default_speaker("EN-FEMALE-1-NEUTRAL")
2610
- tts.generate(
2611
- outetts.GenerationConfig(
2612
- text="Hello there, how are you doing?",
2613
- speaker=speaker,
2614
- )
2615
- ).save("output.wav")
2616
- `,
2797
+ `import outetts
2798
+
2799
+ enum = outetts.Models("${model.id}".split("/", 1)[1]) # VERSION_1_0_SIZE_1B
2800
+ cfg = outetts.ModelConfig.auto_config(enum, outetts.Backend.HF)
2801
+ tts = outetts.Interface(cfg)
2802
+
2803
+ speaker = tts.load_default_speaker("EN-FEMALE-1-NEUTRAL")
2804
+ tts.generate(
2805
+ outetts.GenerationConfig(
2806
+ text="Hello there, how are you doing?",
2807
+ speaker=speaker,
2808
+ )
2809
+ ).save("output.wav")`,
2617
2810
  ];
2618
2811
  };
2619
2812
  export const pxia = (model) => [