@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
@@ -22,7 +22,7 @@ export const adapters = (model: ModelData): string[] => [
22
22
  `from adapters import AutoAdapterModel
23
23
 
24
24
  model = AutoAdapterModel.from_pretrained("${escapeStringForJson(
25
- model.config?.adapter_transformers?.model_name ?? "fill-in-model-name",
25
+ model.config?.adapter_transformers?.model_name ?? "<base-model-id>",
26
26
  )}")
27
27
  model.load_adapter("${model.id}", set_active=True)`,
28
28
  ];
@@ -56,6 +56,14 @@ export const araclip = (model: ModelData): string[] => [
56
56
  model = AraClip.from_pretrained("${model.id}")`,
57
57
  ];
58
58
 
59
+ export const asimov = (model: ModelData): string[] => [
60
+ `# Run the policy in a browser simulation of the Asimov 1 humanoid (no GPU needed)
61
+ git clone https://github.com/menloresearch/humanoid-policy-viewer
62
+ cd humanoid-policy-viewer
63
+ npm install
64
+ npm run hf ${model.id}`,
65
+ ];
66
+
59
67
  export const asteroid = (model: ModelData): string[] => [
60
68
  `from asteroid.models import BaseModel
61
69
 
@@ -83,7 +91,13 @@ result, message = detector.detect_watermark(watermarked_audio, sr)`;
83
91
  };
84
92
 
85
93
  function get_base_diffusers_model(model: ModelData): string {
86
- return escapeStringForJson(model.cardData?.base_model?.toString() ?? "fill-in-base-model");
94
+ const baseModel = model.cardData?.base_model;
95
+ // several base models can be listed (e.g. a LoRA trained for a base and its turbo variant). When an original checkpoint
96
+ // and its Diffusers conversion are both listed (["Wan-AI/Wan2.1-I2V-14B-480P", "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"]),
97
+ // only the conversion has a model_index.json that DiffusionPipeline can load; otherwise the first one is used
98
+ const candidates = Array.isArray(baseModel) ? baseModel : baseModel ? [baseModel] : [];
99
+ const chosen = candidates.find((id) => id.toLowerCase().endsWith("-diffusers")) ?? candidates[0];
100
+ return escapeStringForJson(chosen ?? "fill-in-base-model");
87
101
  }
88
102
 
89
103
  function get_prompt_from_diffusers_model(model: ModelData): string | undefined {
@@ -174,7 +188,7 @@ detector = cvg.NeuralCornerDetector(checkpoint)
174
188
  embedder = cvg.NeuralEmbedder(checkpoint)`,
175
189
  ];
176
190
 
177
- export const colipri = (model: ModelData): string[] => {
191
+ export const colipri = (): string[] => {
178
192
  const installSnippet = `pip install colipri`;
179
193
 
180
194
  const exampleSnippet = `from colipri import get_model
@@ -184,7 +198,7 @@ from colipri import ZeroShotImageClassificationPipeline
184
198
 
185
199
  model = get_model().cuda()
186
200
  processor = get_processor()
187
- pipeline = ZeroShotImageClassificationPipeline("${model.id}", processor)
201
+ pipeline = ZeroShotImageClassificationPipeline(model, processor)
188
202
 
189
203
  image = load_sample_ct()
190
204
 
@@ -308,7 +322,7 @@ export const depth_anything_v2 = (model: ModelData): string[] => {
308
322
  } else if (model.id === "depth-anything/Depth-Anything-V2-Large") {
309
323
  encoder = "vitl";
310
324
  features = "256";
311
- out_channels = "[256, 512, 1024, 1024";
325
+ out_channels = "[256, 512, 1024, 1024]";
312
326
  }
313
327
 
314
328
  return [
@@ -318,6 +332,7 @@ export const depth_anything_v2 = (model: ModelData): string[] => {
318
332
  # Load the model and infer depth from an image
319
333
  import cv2
320
334
  import torch
335
+ from huggingface_hub import hf_hub_download
321
336
 
322
337
  from depth_anything_v2.dpt import DepthAnythingV2
323
338
 
@@ -327,7 +342,8 @@ model = DepthAnythingV2(encoder="${encoder}", features=${features}, out_channels
327
342
  # load the weights
328
343
  filepath = hf_hub_download(repo_id="${model.id}", filename="depth_anything_v2_${encoder}.pth", repo_type="model")
329
344
  state_dict = torch.load(filepath, map_location="cpu")
330
- model.load_state_dict(state_dict).eval()
345
+ model.load_state_dict(state_dict)
346
+ model.eval()
331
347
 
332
348
  raw_img = cv2.imread("your/image/path")
333
349
  depth = model.infer_image(raw_img) # HxW raw depth map in numpy
@@ -337,8 +353,8 @@ depth = model.infer_image(raw_img) # HxW raw depth map in numpy
337
353
 
338
354
  export const depth_pro = (model: ModelData): string[] => {
339
355
  const installSnippet = `# Download checkpoint
340
- pip install huggingface-hub
341
- huggingface-cli download --local-dir checkpoints ${model.id}`;
356
+ pip install huggingface_hub
357
+ hf download ${model.id} --local-dir checkpoints`;
342
358
 
343
359
  const inferenceSnippet = `import depth_pro
344
360
 
@@ -412,7 +428,7 @@ export const describe_anything = (model: ModelData): string[] => [
412
428
  from huggingface_hub import snapshot_download
413
429
  from dam import DescribeAnythingModel
414
430
 
415
- snapshot_download(${model.id}, local_dir="checkpoints")
431
+ snapshot_download("${model.id}", local_dir="checkpoints")
416
432
 
417
433
  dam = DescribeAnythingModel(
418
434
  model_path="checkpoints",
@@ -535,7 +551,7 @@ pipe.load_lora_weights("${model.id}")
535
551
  prompt = "${get_prompt_from_diffusers_model(model) ?? diffusersVideoDefaultPrompt}"
536
552
  input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/guitar-man.png")
537
553
 
538
- image = pipe(image=input_image, prompt=prompt).frames[0]
554
+ output = pipe(image=input_image, prompt=prompt).frames[0]
539
555
  export_to_video(output, "output.mp4")`,
540
556
  ];
541
557
 
@@ -578,7 +594,7 @@ from diffusers import AutoPipelineForInpainting
578
594
  from diffusers.utils import load_image
579
595
 
580
596
  # switch to "mps" for apple devices
581
- pipe = AutoPipelineForInpainting.from_pretrained("${model.id}", dtype=torch.float16, variant="fp16", device_map="cuda")
597
+ pipe = AutoPipelineForInpainting.from_pretrained("${model.id}", dtype=torch.float16, device_map="cuda")
582
598
 
583
599
  img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
584
600
  mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
@@ -601,11 +617,14 @@ image = pipe(
601
617
  ];
602
618
 
603
619
  export const diffusers = (model: ModelData): string[] => {
620
+ // the Hub emits pipeline class tags as `diffusers:<ClassName>`; bare names are kept for manually added tags
621
+ const hasPipelineClass = (className: string): boolean =>
622
+ model.tags.includes(`diffusers:${className}`) ||
623
+ model.tags.includes(className) ||
624
+ model.config?.diffusers?._class_name === className;
625
+
604
626
  let codeSnippets: string[];
605
- if (
606
- model.tags.includes("StableDiffusionInpaintPipeline") ||
607
- model.tags.includes("StableDiffusionXLInpaintPipeline")
608
- ) {
627
+ if (hasPipelineClass("StableDiffusionInpaintPipeline") || hasPipelineClass("StableDiffusionXLInpaintPipeline")) {
609
628
  codeSnippets = diffusers_inpainting(model);
610
629
  } else if (model.tags.includes("controlnet")) {
611
630
  codeSnippets = diffusers_controlnet(model);
@@ -621,7 +640,7 @@ export const diffusers = (model: ModelData): string[] => {
621
640
  }
622
641
  } else if (model.tags.includes("textual_inversion")) {
623
642
  codeSnippets = diffusers_textual_inversion(model);
624
- } else if (model.tags.includes("FluxFillPipeline")) {
643
+ } else if (hasPipelineClass("FluxFillPipeline")) {
625
644
  codeSnippets = diffusers_flux_fill(model);
626
645
  } else if (model.pipeline_tag === "image-to-video") {
627
646
  codeSnippets = diffusers_image_to_video(model);
@@ -641,7 +660,7 @@ from diffusionkit.mlx import DiffusionPipeline
641
660
  pipeline = DiffusionPipeline(
642
661
  shift=3.0,
643
662
  use_t5=False,
644
- model_version=${model.id},
663
+ model_version="${model.id}",
645
664
  low_memory_mode=True,
646
665
  a16=True,
647
666
  w16=True,
@@ -652,7 +671,7 @@ from diffusionkit.mlx import FluxPipeline
652
671
 
653
672
  pipeline = FluxPipeline(
654
673
  shift=1.0,
655
- model_version=${model.id},
674
+ model_version="${model.id}",
656
675
  low_memory_mode=True,
657
676
  a16=True,
658
677
  w16=True,
@@ -677,21 +696,22 @@ image, _ = pipeline.generate_image(
677
696
  };
678
697
 
679
698
  export const cartesia_pytorch = (model: ModelData): string[] => [
680
- `# pip install --no-binary :all: cartesia-pytorch
681
- from cartesia_pytorch import ReneLMHeadModel
699
+ `# The cartesia-pytorch package on PyPI ships no code: clone the repo and put its cartesia-pytorch folder on PYTHONPATH
700
+ # (it needs an Ampere or newer CUDA GPU, torch 2.4, mamba-ssm 2.2.2 and flash-attn 2.6.3)
701
+ # git clone https://github.com/cartesia-ai/edge && export PYTHONPATH="$PWD/edge/cartesia-pytorch:$PYTHONPATH"
702
+ from cartesia_pytorch.Rene import ReneLMHeadModel
682
703
  from transformers import AutoTokenizer
683
704
 
684
- model = ReneLMHeadModel.from_pretrained("${model.id}")
705
+ model = ReneLMHeadModel.from_pretrained("${model.id}").half().cuda()
685
706
  tokenizer = AutoTokenizer.from_pretrained("allenai/OLMo-1B-hf")
686
707
 
687
708
  in_message = ["Rene Descartes was"]
688
709
  inputs = tokenizer(in_message, return_tensors="pt")
689
710
 
690
- outputs = model.generate(inputs.input_ids, max_length=50, top_k=100, top_p=0.99)
711
+ outputs = model.generate(inputs.input_ids.cuda(), max_length=50, top_k=100, top_p=0.99)
691
712
  out_message = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
692
713
 
693
- print(out_message)
694
- )`,
714
+ print(out_message)`,
695
715
  ];
696
716
 
697
717
  export const cartesia_mlx = (model: ModelData): string[] => [
@@ -742,7 +762,8 @@ speech, *_ = model("text to generate speech from")`,
742
762
  ];
743
763
 
744
764
  export const espnetASR = (model: ModelData): string[] => [
745
- `from espnet2.bin.asr_inference import Speech2Text
765
+ `import soundfile
766
+ from espnet2.bin.asr_inference import Speech2Text
746
767
 
747
768
  model = Speech2Text.from_pretrained(
748
769
  "${model.id}"
@@ -772,9 +793,14 @@ models, cfg, task = load_model_ensemble_and_task_from_hf_hub(
772
793
  ];
773
794
 
774
795
  export const flair = (model: ModelData): string[] => [
775
- `from flair.models import SequenceTagger
796
+ `from flair.data import Sentence
797
+ from flair.models import SequenceTagger
798
+
799
+ tagger = SequenceTagger.load("${model.id}")
776
800
 
777
- tagger = SequenceTagger.load("${model.id}")`,
801
+ sentence = Sentence("George Washington went to Washington.")
802
+ tagger.predict(sentence)
803
+ print(sentence)`,
778
804
  ];
779
805
 
780
806
  export const flexray = (model: ModelData): string[] => [
@@ -856,23 +882,30 @@ print(f'Accuracy {accuracy_score(y_test, predictions):.2%}')
856
882
  };
857
883
 
858
884
  export const gaussianformer = (model: ModelData): string[] => [
859
- `# Install from https://github.com/SVLwoof/gaussianformer
885
+ `# pip install "gaussianformer @ git+https://github.com/SVLwoof/gaussianformer"
886
+ from gaussianformer import GaussianFormerRenderingPipeline, load_ply
860
887
 
861
- from gaussianformer.pipelines.rendering_pipeline import GaussianFormerRenderingPipeline
862
-
863
- pipeline = GaussianFormerRenderingPipeline.from_pretrained("${model.id}")`,
888
+ pipeline = GaussianFormerRenderingPipeline.from_pretrained("${model.id}").to("cuda")
889
+ gaussians = load_ply("object.ply") # 3D Gaussian Splatting PLY -> [N, 14]`,
864
890
  ];
865
891
 
866
892
  export const gliner = (model: ModelData): string[] => [
867
893
  `from gliner import GLiNER
868
894
 
869
- model = GLiNER.from_pretrained("${model.id}")`,
895
+ model = GLiNER.from_pretrained("${model.id}")
896
+
897
+ text = "Cristiano Ronaldo dos Santos Aveiro was born on 5 February 1985 in Funchal, Madeira, Portugal."
898
+ labels = ["person", "date", "location"]
899
+
900
+ entities = model.predict_entities(text, labels)
901
+ for entity in entities:
902
+ print(entity["text"], "=>", entity["label"])`,
870
903
  ];
871
904
 
872
905
  export const gliner2 = (model: ModelData): string[] => [
873
- `from gliner2 import GLiNER2
906
+ `from gliner2 import AutoExtractor
874
907
 
875
- model = GLiNER2.from_pretrained("${model.id}")
908
+ extractor = AutoExtractor.from_pretrained("${model.id}")
876
909
 
877
910
  # Extract entities
878
911
  text = "Apple CEO Tim Cook announced iPhone 15 in Cupertino yesterday."
@@ -885,7 +918,7 @@ export const indextts = (model: ModelData): string[] => [
885
918
  `# Download model
886
919
  from huggingface_hub import snapshot_download
887
920
 
888
- snapshot_download(${model.id}, local_dir="checkpoints")
921
+ snapshot_download("${model.id}", local_dir="checkpoints")
889
922
 
890
923
  from indextts.infer import IndexTTS
891
924
 
@@ -941,9 +974,11 @@ print(out["answers"]["team"]["choice"])`,
941
974
  };
942
975
 
943
976
  export const keras = (model: ModelData): string[] => [
944
- `# Available backend options are: "jax", "torch", "tensorflow".
977
+ `# !pip install -U keras tensorflow huggingface_hub
978
+ # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here;
979
+ # "jax" and "torch" also work for computation once TensorFlow is installed.
945
980
  import os
946
- os.environ["KERAS_BACKEND"] = "jax"
981
+ os.environ["KERAS_BACKEND"] = "tensorflow"
947
982
 
948
983
  import keras
949
984
 
@@ -1070,13 +1105,15 @@ export const kernels = (model: ModelData): string[] => [
1070
1105
 
1071
1106
  from kernels import get_kernel
1072
1107
 
1073
- kernel = get_kernel("${model.id}")`,
1108
+ # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones
1109
+ kernel = get_kernel("${model.id}", version=1)`,
1074
1110
  ];
1075
1111
 
1076
1112
  export const kimi_audio = (model: ModelData): string[] => [
1077
1113
  `# Example usage for KimiAudio
1078
- # pip install git+https://github.com/MoonshotAI/Kimi-Audio.git
1114
+ # pip install git+https://github.com/MoonshotAI/Kimi-Audio.git "transformers<5"
1079
1115
 
1116
+ import soundfile as sf
1080
1117
  from kimia_infer.api.kimia import KimiAudio
1081
1118
 
1082
1119
  model = KimiAudio(model_path="${model.id}", load_detokenizer=True)
@@ -1517,14 +1554,17 @@ pip install -U litert-lm
1517
1554
 
1518
1555
  # 2. Download and run this model locally:
1519
1556
  # See: https://ai.google.dev/edge/litert-lm/cli
1557
+ # A single .litertlm file in the repo is picked automatically; otherwise the CLI asks which one to run
1558
+ # (or pass its name right after the repo id).
1520
1559
  litert-lm run \\
1521
1560
  --from-huggingface-repo=${model.id} \\
1522
1561
  --prompt="Write me a poem"`,
1523
1562
  ];
1524
1563
 
1525
1564
  export const tf_keras = (model: ModelData): string[] => [
1526
- `# Note: 'keras<3.x' or 'tf_keras' must be installed (legacy)
1565
+ `# Note: 'keras<3.x' or 'tf_keras' must be installed (legacy), and from_pretrained_keras was removed in huggingface_hub 1.0.
1527
1566
  # See https://github.com/keras-team/tf-keras for more details.
1567
+ # !pip install "huggingface_hub<1.0" tf_keras
1528
1568
  from huggingface_hub import from_pretrained_keras
1529
1569
 
1530
1570
  model = from_pretrained_keras("${model.id}")
@@ -1578,6 +1618,19 @@ from MeshAnything.models.meshanything import MeshAnything
1578
1618
  model = MeshAnything(args)`,
1579
1619
  ];
1580
1620
 
1621
+ export const microduck = (model: ModelData): string[] => {
1622
+ // Model-card tag convention: tags: [microduck-slot:stand].
1623
+ const slots = ["walk", "stand", "sitstand", "ground_pick", "kick_left", "kick_right", "roulade"];
1624
+ const matchingSlots = slots.filter((slot) => model.tags.includes(`microduck-slot:${slot}`));
1625
+ const slot = matchingSlots.length === 1 ? matchingSlots[0] : "SLOT";
1626
+ const prefix =
1627
+ matchingSlots.length === 1
1628
+ ? ""
1629
+ : `# Replace SLOT with the slot specified in the model card (${slots.join(", ")}).\n`;
1630
+
1631
+ return [`${prefix}sudo robotctl policy load ${slot} ${model.id}`];
1632
+ };
1633
+
1581
1634
  export const multimolecule = (model: ModelData): string[] => {
1582
1635
  const widgetExample = model.widgetData?.[0] as WidgetExampleTextInput | undefined;
1583
1636
  const exampleText = escapeStringForJson(widgetExample?.text ?? "");
@@ -1771,14 +1824,18 @@ model = pe.VisionTransformer.from_config("${model.id}", pretrained=True)`;
1771
1824
  };
1772
1825
  export const phantom_wan = (model: ModelData): string[] => [
1773
1826
  `from huggingface_hub import snapshot_download
1774
- from phantom_wan import WANI2V, configs
1827
+ from phantom_wan import WanI2V, configs
1828
+ from PIL import Image
1775
1829
 
1776
1830
  checkpoint_dir = snapshot_download("${model.id}")
1777
1831
  wan_i2v = WanI2V(
1778
- config=configs.WAN_CONFIGS['i2v-14B'],
1779
- checkpoint_dir=checkpoint_dir,
1780
- )
1781
- video = wan_i2v.generate(text_prompt, image_prompt)`,
1832
+ config=configs.WAN_CONFIGS['i2v-14B'],
1833
+ checkpoint_dir=checkpoint_dir,
1834
+ )
1835
+
1836
+ text_prompt = "A cat playing with a ball of yarn"
1837
+ image_prompt = Image.open("path/to/image.jpg").convert("RGB")
1838
+ video = wan_i2v.generate(text_prompt, image_prompt)`,
1782
1839
  ];
1783
1840
 
1784
1841
  export const pocket_tts = (model: ModelData): string[] => [
@@ -1903,7 +1960,7 @@ export const tensorflowtts = (model: ModelData): string[] => {
1903
1960
  export const timm = (model: ModelData): string[] => [
1904
1961
  `import timm
1905
1962
 
1906
- model = timm.create_model("hf_hub:${model.id}", pretrained=True)`,
1963
+ model = timm.create_model("hf-hub:${model.id}", pretrained=True)`,
1907
1964
  ];
1908
1965
 
1909
1966
  export const saelens = (/* model: ModelData */): string[] => [
@@ -1931,13 +1988,15 @@ seed_story_cfg = OmegaConf.load(seed_story_cfg_path)
1931
1988
  seed_story = hydra.utils.instantiate(seed_story_cfg, llm=llm) `,
1932
1989
  ];
1933
1990
 
1934
- const skopsPickle = (model: ModelData, modelFile: string) => {
1991
+ const SKLEARN_PICKLE_FILE = /\.(joblib|pkl|pickle)$/i;
1992
+
1993
+ // skops.hub_utils was removed in skops 0.12, so files are downloaded with huggingface_hub
1994
+ const sklearnPickle = (model: ModelData, modelFile: string) => {
1935
1995
  return [
1936
- `import joblib
1937
- from skops.hub_utils import download
1938
- download("${model.id}", "path_to_folder")
1996
+ `from huggingface_hub import hf_hub_download
1997
+ import joblib
1939
1998
  model = joblib.load(
1940
- "${escapeStringForJson(modelFile)}"
1999
+ hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}")
1941
2000
  )
1942
2001
  # only load pickle files from sources you trust
1943
2002
  # read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
@@ -1946,42 +2005,32 @@ model = joblib.load(
1946
2005
 
1947
2006
  const skopsFormat = (model: ModelData, modelFile: string) => {
1948
2007
  return [
1949
- `from skops.hub_utils import download
2008
+ `from huggingface_hub import hf_hub_download
1950
2009
  from skops.io import load
1951
- download("${model.id}", "path_to_folder")
1952
2010
  # make sure model file is in skops format
1953
2011
  # if model is a pickle file, make sure it's from a source you trust
1954
- model = load("path_to_folder/${escapeStringForJson(modelFile)}")`,
1955
- ];
1956
- };
1957
-
1958
- const skopsJobLib = (model: ModelData) => {
1959
- return [
1960
- `from huggingface_hub import hf_hub_download
1961
- import joblib
1962
- model = joblib.load(
1963
- hf_hub_download("${model.id}", "sklearn_model.joblib")
1964
- )
1965
- # only load pickle files from sources you trust
1966
- # read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
2012
+ model = load(hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}"))`,
1967
2013
  ];
1968
2014
  };
1969
2015
 
1970
2016
  export const sklearn = (model: ModelData): string[] => {
2017
+ const declaredFile = model.config?.sklearn?.model?.file;
1971
2018
  if (model.tags.includes("skops")) {
1972
- const skopsmodelFile = model.config?.sklearn?.model?.file;
1973
- const skopssaveFormat = model.config?.sklearn?.model_format;
1974
- if (!skopsmodelFile) {
2019
+ if (!declaredFile) {
1975
2020
  return [`# ⚠️ Model filename not specified in config.json`];
1976
2021
  }
1977
- if (skopssaveFormat === "pickle") {
1978
- return skopsPickle(model, skopsmodelFile);
1979
- } else {
1980
- return skopsFormat(model, skopsmodelFile);
2022
+ // some skops repos declare no model_format for a pickled file (e.g. julien-c/skops-digits), which skops.io cannot read
2023
+ if (model.config?.sklearn?.model_format === "pickle" || SKLEARN_PICKLE_FILE.test(declaredFile)) {
2024
+ return sklearnPickle(model, declaredFile);
1981
2025
  }
1982
- } else {
1983
- return skopsJobLib(model);
2026
+ return skopsFormat(model, declaredFile);
1984
2027
  }
2028
+ // repos pushed without skops often still declare their filename in config.json; it is only trusted when it names a
2029
+ // pickle, since some hand-written configs carry typos (e.g. "sklean_model.jpblib" next to a real sklearn_model.joblib)
2030
+ return sklearnPickle(
2031
+ model,
2032
+ declaredFile && SKLEARN_PICKLE_FILE.test(declaredFile) ? declaredFile : "sklearn_model.joblib",
2033
+ );
1985
2034
  };
1986
2035
 
1987
2036
  export const stable_audio_tools = (model: ModelData): string[] => [
@@ -2032,7 +2081,7 @@ export const sam2 = (model: ModelData): string[] => {
2032
2081
  import torch
2033
2082
  from sam2.sam2_image_predictor import SAM2ImagePredictor
2034
2083
 
2035
- predictor = SAM2ImagePredictor.from_pretrained(${model.id})
2084
+ predictor = SAM2ImagePredictor.from_pretrained("${model.id}")
2036
2085
 
2037
2086
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
2038
2087
  predictor.set_image(<your_image>)
@@ -2042,13 +2091,13 @@ with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
2042
2091
  import torch
2043
2092
  from sam2.sam2_video_predictor import SAM2VideoPredictor
2044
2093
 
2045
- predictor = SAM2VideoPredictor.from_pretrained(${model.id})
2094
+ predictor = SAM2VideoPredictor.from_pretrained("${model.id}")
2046
2095
 
2047
2096
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
2048
2097
  state = predictor.init_state(<your_video>)
2049
2098
 
2050
2099
  # add new prompts and instantly get the output on the same frame
2051
- frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>):
2100
+ frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>)
2052
2101
 
2053
2102
  # propagate the prompts to get masklets throughout the video
2054
2103
  for frame_idx, object_ids, masks in predictor.propagate_in_video(state):
@@ -2070,11 +2119,18 @@ output = inference(image, mask)`,
2070
2119
  ];
2071
2120
 
2072
2121
  export const sam_3d_body = (model: ModelData): string[] => [
2073
- `from notebook.utils import setup_sam_3d_body
2122
+ `import cv2
2123
+ import numpy as np
2124
+ from notebook.utils import setup_sam_3d_body
2125
+ from tools.vis_utils import visualize_sample_together
2074
2126
 
2075
- estimator = setup_sam_3d_body(${model.id})
2076
- outputs = estimator.process_one_image(image)
2077
- rend_img = visualize_sample_together(image, outputs, estimator.faces)`,
2127
+ estimator = setup_sam_3d_body("${model.id}")
2128
+
2129
+ img_bgr = cv2.imread("path/to/image.jpg")
2130
+ outputs = estimator.process_one_image(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
2131
+
2132
+ rend_img = visualize_sample_together(img_bgr, outputs, estimator.faces)
2133
+ cv2.imwrite("output.jpg", rend_img.astype(np.uint8))`,
2078
2134
  ];
2079
2135
 
2080
2136
  export const sampleFactory = (model: ModelData): string[] => [
@@ -2171,7 +2227,17 @@ docs_emb = model.encode(documents, is_query=False)`,
2171
2227
  ];
2172
2228
  }
2173
2229
 
2174
- if (model.tags.includes("cross-encoder") || model.pipeline_tag == "text-ranking") {
2230
+ // Rerankers saved in the CrossEncoder format are often tagged text-classification rather than text-ranking;
2231
+ // their architecture still identifies them (e.g. BAAI/bge-reranker-v2-m3). SetFit models can share that shape but
2232
+ // are bi-encoders with a separate head.
2233
+ const isCrossEncoder =
2234
+ model.tags.includes("cross-encoder") ||
2235
+ model.pipeline_tag == "text-ranking" ||
2236
+ (model.pipeline_tag === "text-classification" &&
2237
+ !model.tags.includes("setfit") &&
2238
+ (model.config?.architectures ?? []).some((arch) => arch.endsWith("ForSequenceClassification")));
2239
+
2240
+ if (isCrossEncoder) {
2175
2241
  return [
2176
2242
  `from sentence_transformers import CrossEncoder
2177
2243
 
@@ -2190,6 +2256,34 @@ print(scores)`,
2190
2256
  ];
2191
2257
  }
2192
2258
 
2259
+ // sentence-transformers tags SparseEncoder repos (SPLADE & co) with "sparse-encoder" and MultiVectorEncoder repos
2260
+ // (ColBERT & co) with "multi-vector" when pushing them; both encode queries and documents separately
2261
+ const retrievalEncoder = model.tags.includes("sparse-encoder")
2262
+ ? "SparseEncoder"
2263
+ : model.tags.includes("multi-vector")
2264
+ ? "MultiVectorEncoder"
2265
+ : undefined;
2266
+ if (retrievalEncoder) {
2267
+ return [
2268
+ `from sentence_transformers import ${retrievalEncoder}
2269
+
2270
+ model = ${retrievalEncoder}("${model.id}"${remote_code_snippet})
2271
+
2272
+ queries = ["Which planet is known as the Red Planet?"]
2273
+ documents = [
2274
+ "Venus is often called Earth's twin because of its similar size and proximity.",
2275
+ "Mars, known for its reddish appearance, is often referred to as the Red Planet.",
2276
+ "Jupiter, the largest planet in our solar system, has a prominent red spot.",
2277
+ ]
2278
+
2279
+ query_embeddings = model.encode_query(queries)
2280
+ document_embeddings = model.encode_document(documents)
2281
+
2282
+ similarities = model.similarity(query_embeddings, document_embeddings)
2283
+ print(similarities)`,
2284
+ ];
2285
+ }
2286
+
2193
2287
  const exampleSentences = get_widget_examples_from_st_model(model) ?? [
2194
2288
  "The weather is lovely today.",
2195
2289
  "It's so sunny outside!",
@@ -2213,7 +2307,10 @@ print(similarities.shape)
2213
2307
  export const setfit = (model: ModelData): string[] => [
2214
2308
  `from setfit import SetFitModel
2215
2309
 
2216
- model = SetFitModel.from_pretrained("${model.id}")`,
2310
+ model = SetFitModel.from_pretrained("${model.id}")
2311
+
2312
+ preds = model.predict(["i loved the spiderman movie!", "pineapple on pizza is the worst"])
2313
+ print(preds)`,
2217
2314
  ];
2218
2315
 
2219
2316
  export const spacy = (model: ModelData): string[] => [
@@ -2231,7 +2328,10 @@ nlp = ${nameWithoutNamespace(model.id)}.load()`,
2231
2328
  export const span_marker = (model: ModelData): string[] => [
2232
2329
  `from span_marker import SpanMarkerModel
2233
2330
 
2234
- model = SpanMarkerModel.from_pretrained("${model.id}")`,
2331
+ model = SpanMarkerModel.from_pretrained("${model.id}")
2332
+
2333
+ entities = model.predict("Amelia Earhart flew her single engine Lockheed Vega 5B across the Atlantic to Paris.")
2334
+ print(entities)`,
2235
2335
  ];
2236
2336
 
2237
2337
  export const stanza = (model: ModelData): string[] => [
@@ -2252,6 +2352,8 @@ const speechBrainMethod = (speechbrainInterface: string) => {
2252
2352
  return "enhance_file";
2253
2353
  case "SepformerSeparation":
2254
2354
  return "separate_file";
2355
+ case "SpeakerRecognition":
2356
+ return "verify_files";
2255
2357
  default:
2256
2358
  return undefined;
2257
2359
  }
@@ -2268,12 +2370,14 @@ export const speechbrain = (model: ModelData): string[] => {
2268
2370
  return [`# interface in config.json invalid`];
2269
2371
  }
2270
2372
 
2373
+ // speaker verification compares two recordings
2374
+ const methodArgs = speechbrainMethod === "verify_files" ? '"speaker1.wav", "speaker2.wav"' : '"file.wav"';
2271
2375
  return [
2272
2376
  `from speechbrain.pretrained import ${speechbrainInterface}
2273
2377
  model = ${speechbrainInterface}.from_hparams(
2274
2378
  "${model.id}"
2275
2379
  )
2276
- model.${speechbrainMethod}("file.wav")`,
2380
+ model.${speechbrainMethod}(${methodArgs})`,
2277
2381
  ];
2278
2382
  };
2279
2383
 
@@ -2311,6 +2415,7 @@ export const transformers = (model: ModelData): string[] => {
2311
2415
  ? "extractor"
2312
2416
  : "processor";
2313
2417
  autoSnippet.push(
2418
+ "# pip install -U transformers accelerate",
2314
2419
  "# Load model directly",
2315
2420
  `from transformers import ${info.processor}, ${auto_model}`,
2316
2421
  "",
@@ -2344,12 +2449,13 @@ export const transformers = (model: ModelData): string[] => {
2344
2449
  ' return_tensors="pt",',
2345
2450
  ").to(model.device)",
2346
2451
  "",
2347
- "outputs = model.generate(**inputs, max_new_tokens=40)",
2452
+ "outputs = model.generate(**inputs, max_new_tokens=256)",
2348
2453
  `print(${processorVarName}.decode(outputs[0][inputs["input_ids"].shape[-1]:]))`,
2349
2454
  );
2350
2455
  }
2351
2456
  } else {
2352
2457
  autoSnippet.push(
2458
+ "# pip install -U transformers accelerate",
2353
2459
  "# Load model directly",
2354
2460
  `from transformers import ${auto_model}`,
2355
2461
  `model = ${auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ', device_map="auto")',
@@ -2362,7 +2468,7 @@ export const transformers = (model: ModelData): string[] => {
2362
2468
  pipelineSnippet.push(
2363
2469
  `# Warning: Pipeline type "${model.pipeline_tag}" is no longer supported in transformers v5.`,
2364
2470
  `# You must load the model directly (see below) or downgrade to v4.x with:`,
2365
- `# 'pip install "transformers<5.0.0'`,
2471
+ `# pip install "transformers<5.0.0"`,
2366
2472
  );
2367
2473
  }
2368
2474
 
@@ -2372,7 +2478,7 @@ export const transformers = (model: ModelData): string[] => {
2372
2478
  `pipe = pipeline("${model.pipeline_tag}", model="${model.id}"` + remote_code_snippet + ")",
2373
2479
  );
2374
2480
 
2375
- if (model.tags.includes("conversational")) {
2481
+ if (model.tags.includes("conversational") && hasChatTemplate(model)) {
2376
2482
  if (model.tags.includes("image-text-to-text")) {
2377
2483
  pipelineSnippet.push(
2378
2484
  "messages = [",
@@ -2410,6 +2516,11 @@ export const transformers = (model: ModelData): string[] => {
2410
2516
  return [autoSnippet.join("\n")];
2411
2517
  };
2412
2518
 
2519
+ // Hub tasks that transformers.js exposes under a different pipeline() task name
2520
+ const TRANSFORMERS_JS_TASK_ALIASES: Record<string, string> = {
2521
+ "sentence-similarity": "feature-extraction",
2522
+ };
2523
+
2413
2524
  export const transformersJS = (model: ModelData): string[] => {
2414
2525
  if (!model.pipeline_tag) {
2415
2526
  return [`// ⚠️ Unknown pipeline tag`];
@@ -2417,45 +2528,119 @@ export const transformersJS = (model: ModelData): string[] => {
2417
2528
 
2418
2529
  const libName = "@huggingface/transformers";
2419
2530
 
2531
+ // Rerankers score (query, document) pairs. The text-classification pipeline cannot take pairs and applies a softmax
2532
+ // over their single logit, so every score comes out as 1; the model is called directly instead.
2533
+ if (model.pipeline_tag === "text-ranking") {
2534
+ return [
2535
+ `// npm i ${libName}
2536
+ import { AutoTokenizer, AutoModelForSequenceClassification } from '${libName}';
2537
+
2538
+ const tokenizer = await AutoTokenizer.from_pretrained('${model.id}');
2539
+ const model = await AutoModelForSequenceClassification.from_pretrained('${model.id}');
2540
+
2541
+ const query = 'Which planet is known as the Red Planet?';
2542
+ const documents = [
2543
+ 'Mars, known for its reddish appearance, is often referred to as the Red Planet.',
2544
+ 'Venus is often called the twin of Earth because of its similar size and proximity.',
2545
+ ];
2546
+
2547
+ const inputs = tokenizer(new Array(documents.length).fill(query), { text_pair: documents, padding: true, truncation: true });
2548
+ const { logits } = await model(inputs);
2549
+ console.log(logits.sigmoid().tolist()); // one relevance score per document`,
2550
+ ];
2551
+ }
2552
+
2553
+ const task = TRANSFORMERS_JS_TASK_ALIASES[model.pipeline_tag] ?? model.pipeline_tag;
2554
+
2420
2555
  return [
2421
2556
  `// npm i ${libName}
2422
2557
  import { pipeline } from '${libName}';
2423
2558
 
2424
2559
  // Allocate pipeline
2425
- const pipe = await pipeline('${model.pipeline_tag}', '${model.id}');`,
2560
+ const pipe = await pipeline('${task}', '${model.id}');`,
2426
2561
  ];
2427
2562
  };
2428
2563
 
2429
- const peftTask = (peftTaskType?: string) => {
2430
- switch (peftTaskType) {
2431
- case "CAUSAL_LM":
2432
- return "CausalLM";
2433
- case "SEQ_2_SEQ_LM":
2434
- return "Seq2SeqLM";
2435
- case "TOKEN_CLS":
2436
- return "TokenClassification";
2437
- case "SEQ_CLS":
2438
- return "SequenceClassification";
2439
- default:
2440
- return undefined;
2564
+ const PEFT_TASK_TYPE_TO_AUTO_CLASS: Record<string, string> = {
2565
+ CAUSAL_LM: "AutoModelForCausalLM",
2566
+ SEQ_2_SEQ_LM: "AutoModelForSeq2SeqLM",
2567
+ TOKEN_CLS: "AutoModelForTokenClassification",
2568
+ SEQ_CLS: "AutoModelForSequenceClassification",
2569
+ QUESTION_ANS: "AutoModelForQuestionAnswering",
2570
+ // transformers has no AutoModelForFeatureExtraction; PEFT's own AutoPeftModelForFeatureExtraction resolves to AutoModel
2571
+ FEATURE_EXTRACTION: "AutoModel",
2572
+ };
2573
+
2574
+ // PEFT has no speech-specific task type: Whisper-style adapters are tagged SEQ_2_SEQ_LM, but transformers registers
2575
+ // Whisper and Speech2Text only under AutoModelForSpeechSeq2Seq (SeamlessM4T is under both seq2seq auto classes, SpeechT5
2576
+ // under the speech-to-text and text-to-spectrogram ones).
2577
+ // Other audio seq2seq models (Qwen2-Audio, GLM-ASR) stay under AutoModelForSeq2SeqLM, so the base model name is the
2578
+ // main signal. Matched against Hub repo ids, which are hyphenated (facebook/s2t-small-librispeech-asr,
2579
+ // facebook/seamless-m4t-v2-large), not against transformers model_type identifiers.
2580
+ const PEFT_SPEECH_TO_TEXT_BASE_MODEL = /whisper|\bs2t\b|speech[-_]to[-_]text/i;
2581
+ // SeamlessM4T also has a text-to-text head under AutoModelForSeq2SeqLM, which text translation adapters need; most
2582
+ // SeamlessM4T adapters on the Hub are speech ones, so the speech class stays the default when no text task is declared.
2583
+ const PEFT_SEAMLESS_M4T_BASE_MODEL = /seamless[-_]m4t/i;
2584
+ const PEFT_TEXT_TASKS = ["translation", "text2text-generation", "summarization"];
2585
+ // SpeechT5 checkpoints are overwhelmingly text-to-speech fine-tunes (often without "tts" in their name), which load
2586
+ // with AutoModelForTextToSpectrogram; AutoModelForSpeechSeq2Seq would silently load the speech-to-text head, so only
2587
+ // ASR bases or adapters get it.
2588
+ const PEFT_SPEECHT5_BASE_MODEL = /speecht5/i;
2589
+
2590
+ const declaresTask = (model: ModelData, task: string): boolean =>
2591
+ model.pipeline_tag === task || model.tags.includes(task);
2592
+
2593
+ // the auto class a SEQ_2_SEQ_LM adapter needs when its base is a speech model, undefined for text seq2seq bases
2594
+ const peftSpeechAutoClass = (model: ModelData, baseModel: string): string | undefined => {
2595
+ if (PEFT_SPEECH_TO_TEXT_BASE_MODEL.test(baseModel)) {
2596
+ return "AutoModelForSpeechSeq2Seq";
2441
2597
  }
2598
+ if (PEFT_SEAMLESS_M4T_BASE_MODEL.test(baseModel)) {
2599
+ return PEFT_TEXT_TASKS.some((task) => declaresTask(model, task)) ? undefined : "AutoModelForSpeechSeq2Seq";
2600
+ }
2601
+ if (PEFT_SPEECHT5_BASE_MODEL.test(baseModel)) {
2602
+ const isAsr = /(?:^|[^a-z])asr(?:[^a-z]|$)/i.test(baseModel) || declaresTask(model, "automatic-speech-recognition");
2603
+ return isAsr ? "AutoModelForSpeechSeq2Seq" : "AutoModelForTextToSpectrogram";
2604
+ }
2605
+ return undefined;
2442
2606
  };
2443
2607
 
2444
2608
  export const peft = (model: ModelData): string[] => {
2445
2609
  const { base_model_name_or_path: peftBaseModel, task_type: peftTaskType } = model.config?.peft ?? {};
2446
- const pefttask = peftTask(peftTaskType);
2447
- if (!pefttask) {
2610
+ let autoClass = peftTaskType ? PEFT_TASK_TYPE_TO_AUTO_CLASS[peftTaskType] : undefined;
2611
+ if (!autoClass) {
2448
2612
  return [`Task type is invalid.`];
2449
2613
  }
2450
- if (!peftBaseModel) {
2614
+ // adapter_config.json sometimes omits the base model; the model card's base_model is the next best source
2615
+ const cardBaseModel = Array.isArray(model.cardData?.base_model)
2616
+ ? model.cardData.base_model[0]
2617
+ : model.cardData?.base_model;
2618
+ const baseModel = peftBaseModel || cardBaseModel;
2619
+ if (!baseModel) {
2451
2620
  return [`Base model is not found.`];
2452
2621
  }
2622
+ if (peftTaskType === "SEQ_2_SEQ_LM") {
2623
+ autoClass = peftSpeechAutoClass(model, baseModel) ?? autoClass;
2624
+ }
2625
+
2626
+ if (autoClass === "AutoModelForTextToSpectrogram") {
2627
+ return [
2628
+ `from peft import PeftConfig, PeftModel
2629
+ from transformers import AutoModelForTextToSpectrogram
2630
+
2631
+ base_model = AutoModelForTextToSpectrogram.from_pretrained("${escapeStringForJson(baseModel)}")
2632
+ # PEFT's seq2seq wrapper needs generate(), which SpeechT5's text-to-speech model lacks: load the adapter without its task type
2633
+ config = PeftConfig.from_pretrained("${model.id}")
2634
+ config.task_type = None
2635
+ model = PeftModel.from_pretrained(base_model, "${model.id}", config=config)`,
2636
+ ];
2637
+ }
2453
2638
 
2454
2639
  return [
2455
2640
  `from peft import PeftModel
2456
- from transformers import AutoModelFor${pefttask}
2641
+ from transformers import ${autoClass}
2457
2642
 
2458
- base_model = AutoModelFor${pefttask}.from_pretrained("${escapeStringForJson(peftBaseModel)}")
2643
+ base_model = ${autoClass}.from_pretrained("${escapeStringForJson(baseModel)}")
2459
2644
  model = PeftModel.from_pretrained(base_model, "${model.id}")`,
2460
2645
  ];
2461
2646
  };
@@ -2490,7 +2675,7 @@ transcriptions = asr_model.transcribe(["file.wav"])`,
2490
2675
  };
2491
2676
 
2492
2677
  export const mlAgents = (model: ModelData): string[] => [
2493
- `mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./download: string[]s"`,
2678
+ `mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./downloads"`,
2494
2679
  ];
2495
2680
 
2496
2681
  export const sentis = (/* model: ModelData */): string[] => [
@@ -2565,10 +2750,10 @@ model = Model.from_pretrained("${model.id}")`,
2565
2750
 
2566
2751
  export const lvface = (model: ModelData): string[] => [
2567
2752
  `from huggingface_hub import hf_hub_download
2568
- from inference_onnx import LVFaceONNXInferencer
2753
+ from inference_onnx import LVFaceONNXInferencer
2569
2754
 
2570
2755
  model_path = hf_hub_download("${model.id}", "LVFace-L_Glint360K/LVFace-L_Glint360K.onnx")
2571
- inferencer = LVFaceONNXInferencer(model_path, use_gpu=True, timeout=300)
2756
+ inferencer = LVFaceONNXInferencer(model_path, use_gpu=True)
2572
2757
  img_path = 'path/to/image1.jpg'
2573
2758
  embedding = inferencer.infer_from_image(img_path)`,
2574
2759
  ];
@@ -2607,8 +2792,7 @@ export const vui = (): string[] => [
2607
2792
 
2608
2793
  import torchaudio
2609
2794
 
2610
- from vui.inference import render
2611
- from vui.model import Vui,
2795
+ from vui.legacy import Vui, render
2612
2796
 
2613
2797
  model = Vui.from_pretrained().cuda()
2614
2798
  waveform = render(
@@ -2616,16 +2800,17 @@ waveform = render(
2616
2800
  "Hey, here is some random stuff, usually something quite long as the shorter the text the less likely the model can cope!",
2617
2801
  )
2618
2802
  print(waveform.shape)
2619
- torchaudio.save("out.opus", waveform[0], 22050)
2803
+ torchaudio.save("out.wav", waveform[0], 22050)
2620
2804
  `,
2621
2805
  ];
2622
2806
 
2623
2807
  export const chattts = (): string[] => [
2624
2808
  `import ChatTTS
2809
+ import torch
2625
2810
  import torchaudio
2626
2811
 
2627
2812
  chat = ChatTTS.Chat()
2628
- chat.load_models(compile=False) # Set to True for better performance
2813
+ chat.load(compile=False) # Set to True for better performance
2629
2814
 
2630
2815
  texts = ["PUT YOUR TEXT HERE",]
2631
2816
 
@@ -2635,19 +2820,44 @@ torchaudio.save("output1.wav", torch.from_numpy(wavs[0]), 24000)`,
2635
2820
  ];
2636
2821
 
2637
2822
  export const ultralytics = (model: ModelData): string[] => {
2638
- // ultralytics models must have a version tag (e.g. `yolov8`)
2639
- const versionTag = model.tags.find((tag) => tag.match(/^yolov\d+$/));
2823
+ // the YOLOv10 fork (library "yolov10") ships its own `YOLOv10` class with a `from_pretrained` helper that reads the
2824
+ // model.safetensors it pushes. Mainline ultralytics repos often list "yolov10" among their version tags
2825
+ // (e.g. Ultralytics/YOLOv8) but only ship .pt files, so an ultralytics tag alone keeps them on the mainline loader;
2826
+ // fork pushes still tagged ultralytics are recognized by their safetensors weights.
2827
+ const isYolov10Fork =
2828
+ model.library_name === "yolov10" ||
2829
+ (model.tags.includes("yolov10") && (!model.tags.includes("ultralytics") || model.tags.includes("safetensors")));
2830
+ if (isYolov10Fork) {
2831
+ // from_pretrained reads the model.safetensors that fork pushes ship; older fork repos only ship .pt weights
2832
+ if (model.tags.includes("safetensors")) {
2833
+ return [
2834
+ `from ultralytics import YOLOv10
2640
2835
 
2641
- const className = versionTag ? `YOLOv${versionTag.slice(4)}` : "YOLOvXX";
2642
- const prefix = versionTag
2643
- ? ""
2644
- : `# Couldn't find a valid YOLO version tag.\n# Replace XX with the correct version.\n`;
2836
+ model = YOLOv10.from_pretrained("${model.id}")
2837
+ source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
2838
+ model.predict(source=source, save=True)`,
2839
+ ];
2840
+ }
2841
+ return [
2842
+ `from huggingface_hub import hf_hub_download
2843
+ from ultralytics import YOLOv10
2844
+
2845
+ # pick the weights file from this repo's "Files and versions" tab
2846
+ weights = hf_hub_download("${model.id}", "<weights>.pt")
2847
+ model = YOLOv10(weights)
2848
+ source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
2849
+ model.predict(source=source, save=True)`,
2850
+ ];
2851
+ }
2645
2852
 
2853
+ // mainline ultralytics loads weights from a local file
2646
2854
  return [
2647
- prefix +
2648
- `from ultralytics import ${className}
2855
+ `from huggingface_hub import hf_hub_download
2856
+ from ultralytics import YOLO
2649
2857
 
2650
- model = ${className}.from_pretrained("${model.id}")
2858
+ # pick the weights file from this repo's "Files and versions" tab
2859
+ weights = hf_hub_download("${model.id}", "<weights>.pt")
2860
+ model = YOLO(weights)
2651
2861
  source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
2652
2862
  model.predict(source=source, save=True)`,
2653
2863
  ];
@@ -2726,14 +2936,16 @@ model = SwarmFormerModel.from_pretrained("${model.id}")
2726
2936
 
2727
2937
  export const univa = (model: ModelData): string[] => [
2728
2938
  `# Follow installation instructions at https://github.com/PKU-YuanGroup/UniWorld-V1
2729
-
2939
+ import torch
2940
+ from transformers import AutoProcessor
2730
2941
  from univa.models.qwen2p5vl.modeling_univa_qwen2p5vl import UnivaQwen2p5VLForConditionalGeneration
2731
- model = UnivaQwen2p5VLForConditionalGeneration.from_pretrained(
2732
- "${model.id}",
2733
- torch_dtype=torch.bfloat16,
2734
- attn_implementation="flash_attention_2",
2735
- ).to("cuda")
2736
- processor = AutoProcessor.from_pretrained("${model.id}")
2942
+
2943
+ model = UnivaQwen2p5VLForConditionalGeneration.from_pretrained(
2944
+ "${model.id}",
2945
+ torch_dtype=torch.bfloat16,
2946
+ attn_implementation="flash_attention_2",
2947
+ ).to("cuda")
2948
+ processor = AutoProcessor.from_pretrained("${model.id}")
2737
2949
  `,
2738
2950
  ];
2739
2951
 
@@ -2741,7 +2953,7 @@ const mlx_unknown = (model: ModelData): string[] => [
2741
2953
  `# Download the model from the Hub
2742
2954
  pip install huggingface_hub[hf_xet]
2743
2955
 
2744
- huggingface-cli download --local-dir ${nameWithoutNamespace(model.id)} ${model.id}`,
2956
+ hf download ${model.id} --local-dir ${nameWithoutNamespace(model.id)}`,
2745
2957
  ];
2746
2958
 
2747
2959
  const mlxlm = (model: ModelData): string[] => [
@@ -2802,10 +3014,11 @@ output = generate(model, processor, formatted_prompt, image)
2802
3014
  print(output)`,
2803
3015
  ];
2804
3016
 
3017
+ // create_model() takes a name from the mlx-image registry, not a repo id; the weights live in "mlx-vision/<name>-mlxim"
2805
3018
  export const mlxim = (model: ModelData): string[] => [
2806
3019
  `from mlxim.model import create_model
2807
3020
 
2808
- model = create_model(${model.id})`,
3021
+ model = create_model("${nameWithoutNamespace(model.id).replace(/-mlxim$/, "")}")`,
2809
3022
  ];
2810
3023
 
2811
3024
  export const mlx = (model: ModelData): string[] => {
@@ -2825,7 +3038,10 @@ export const mlx = (model: ModelData): string[] => {
2825
3038
  export const model2vec = (model: ModelData): string[] => [
2826
3039
  `from model2vec import StaticModel
2827
3040
 
2828
- model = StaticModel.from_pretrained("${model.id}")`,
3041
+ model = StaticModel.from_pretrained("${model.id}")
3042
+
3043
+ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
3044
+ print(embeddings.shape)`,
2829
3045
  ];
2830
3046
 
2831
3047
  export const mobilint = (model: ModelData): string[] => {
@@ -2871,8 +3087,9 @@ export const pruna = (model: ModelData): string[] => {
2871
3087
  snippets = snippets.map(ensurePrunaModelImport);
2872
3088
 
2873
3089
  if (model.tags.includes("pruna_pro-ai")) {
3090
+ // only rewrite the import and the class name, never the repo id (an org name can contain "pruna")
2874
3091
  return snippets.map((snippet) =>
2875
- snippet.replace(/\bpruna\b/g, "pruna_pro").replace(/\bPrunaModel\b/g, "PrunaProModel"),
3092
+ snippet.replace(/^from pruna import /gm, "from pruna_pro import ").replace(/\bPrunaModel\b/g, "PrunaProModel"),
2876
3093
  );
2877
3094
  }
2878
3095
 
@@ -2951,21 +3168,19 @@ export const outetts = (model: ModelData): string[] => {
2951
3168
 
2952
3169
  // v1.0 HF → minimal runnable snippet
2953
3170
  return [
2954
- `
2955
- import outetts
2956
-
2957
- enum = outetts.Models("${model.id}".split("/", 1)[1]) # VERSION_1_0_SIZE_1B
2958
- cfg = outetts.ModelConfig.auto_config(enum, outetts.Backend.HF)
2959
- tts = outetts.Interface(cfg)
2960
-
2961
- speaker = tts.load_default_speaker("EN-FEMALE-1-NEUTRAL")
2962
- tts.generate(
2963
- outetts.GenerationConfig(
2964
- text="Hello there, how are you doing?",
2965
- speaker=speaker,
2966
- )
2967
- ).save("output.wav")
2968
- `,
3171
+ `import outetts
3172
+
3173
+ enum = outetts.Models("${model.id}".split("/", 1)[1]) # VERSION_1_0_SIZE_1B
3174
+ cfg = outetts.ModelConfig.auto_config(enum, outetts.Backend.HF)
3175
+ tts = outetts.Interface(cfg)
3176
+
3177
+ speaker = tts.load_default_speaker("EN-FEMALE-1-NEUTRAL")
3178
+ tts.generate(
3179
+ outetts.GenerationConfig(
3180
+ text="Hello there, how are you doing?",
3181
+ speaker=speaker,
3182
+ )
3183
+ ).save("output.wav")`,
2969
3184
  ];
2970
3185
  };
2971
3186