@huggingface/tasks 0.21.54 → 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.
- package/dist/commonjs/library-to-tasks.d.ts.map +1 -1
- package/dist/commonjs/library-to-tasks.js +8 -1
- package/dist/commonjs/model-libraries-snippets.d.ts +2 -1
- package/dist/commonjs/model-libraries-snippets.d.ts.map +1 -1
- package/dist/commonjs/model-libraries-snippets.js +336 -149
- package/dist/commonjs/model-libraries-snippets.spec.js +300 -0
- package/dist/commonjs/model-libraries.d.ts +2 -1
- package/dist/commonjs/model-libraries.d.ts.map +1 -1
- package/dist/commonjs/model-libraries.js +2 -1
- package/dist/esm/library-to-tasks.d.ts.map +1 -1
- package/dist/esm/library-to-tasks.js +8 -1
- package/dist/esm/model-libraries-snippets.d.ts +2 -1
- package/dist/esm/model-libraries-snippets.d.ts.map +1 -1
- package/dist/esm/model-libraries-snippets.js +333 -147
- package/dist/esm/model-libraries-snippets.spec.js +301 -1
- package/dist/esm/model-libraries.d.ts +2 -1
- package/dist/esm/model-libraries.d.ts.map +1 -1
- package/dist/esm/model-libraries.js +2 -1
- package/package.json +1 -1
- package/src/library-to-tasks.ts +8 -1
- package/src/model-libraries-snippets.spec.ts +389 -0
- package/src/model-libraries-snippets.ts +352 -145
- package/src/model-libraries.ts +2 -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 ?? "
|
|
25
|
+
model.config?.adapter_transformers?.model_name ?? "<base-model-id>",
|
|
26
26
|
)}")
|
|
27
27
|
model.load_adapter("${model.id}", set_active=True)`,
|
|
28
28
|
];
|
|
@@ -83,7 +83,13 @@ result, message = detector.detect_watermark(watermarked_audio, sr)`;
|
|
|
83
83
|
};
|
|
84
84
|
|
|
85
85
|
function get_base_diffusers_model(model: ModelData): string {
|
|
86
|
-
|
|
86
|
+
const baseModel = model.cardData?.base_model;
|
|
87
|
+
// several base models can be listed (e.g. a LoRA trained for a base and its turbo variant). When an original checkpoint
|
|
88
|
+
// and its Diffusers conversion are both listed (["Wan-AI/Wan2.1-I2V-14B-480P", "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"]),
|
|
89
|
+
// only the conversion has a model_index.json that DiffusionPipeline can load; otherwise the first one is used
|
|
90
|
+
const candidates = Array.isArray(baseModel) ? baseModel : baseModel ? [baseModel] : [];
|
|
91
|
+
const chosen = candidates.find((id) => id.toLowerCase().endsWith("-diffusers")) ?? candidates[0];
|
|
92
|
+
return escapeStringForJson(chosen ?? "fill-in-base-model");
|
|
87
93
|
}
|
|
88
94
|
|
|
89
95
|
function get_prompt_from_diffusers_model(model: ModelData): string | undefined {
|
|
@@ -174,7 +180,7 @@ detector = cvg.NeuralCornerDetector(checkpoint)
|
|
|
174
180
|
embedder = cvg.NeuralEmbedder(checkpoint)`,
|
|
175
181
|
];
|
|
176
182
|
|
|
177
|
-
export const colipri = (
|
|
183
|
+
export const colipri = (): string[] => {
|
|
178
184
|
const installSnippet = `pip install colipri`;
|
|
179
185
|
|
|
180
186
|
const exampleSnippet = `from colipri import get_model
|
|
@@ -184,7 +190,7 @@ from colipri import ZeroShotImageClassificationPipeline
|
|
|
184
190
|
|
|
185
191
|
model = get_model().cuda()
|
|
186
192
|
processor = get_processor()
|
|
187
|
-
pipeline = ZeroShotImageClassificationPipeline(
|
|
193
|
+
pipeline = ZeroShotImageClassificationPipeline(model, processor)
|
|
188
194
|
|
|
189
195
|
image = load_sample_ct()
|
|
190
196
|
|
|
@@ -308,7 +314,7 @@ export const depth_anything_v2 = (model: ModelData): string[] => {
|
|
|
308
314
|
} else if (model.id === "depth-anything/Depth-Anything-V2-Large") {
|
|
309
315
|
encoder = "vitl";
|
|
310
316
|
features = "256";
|
|
311
|
-
out_channels = "[256, 512, 1024, 1024";
|
|
317
|
+
out_channels = "[256, 512, 1024, 1024]";
|
|
312
318
|
}
|
|
313
319
|
|
|
314
320
|
return [
|
|
@@ -318,6 +324,7 @@ export const depth_anything_v2 = (model: ModelData): string[] => {
|
|
|
318
324
|
# Load the model and infer depth from an image
|
|
319
325
|
import cv2
|
|
320
326
|
import torch
|
|
327
|
+
from huggingface_hub import hf_hub_download
|
|
321
328
|
|
|
322
329
|
from depth_anything_v2.dpt import DepthAnythingV2
|
|
323
330
|
|
|
@@ -327,7 +334,8 @@ model = DepthAnythingV2(encoder="${encoder}", features=${features}, out_channels
|
|
|
327
334
|
# load the weights
|
|
328
335
|
filepath = hf_hub_download(repo_id="${model.id}", filename="depth_anything_v2_${encoder}.pth", repo_type="model")
|
|
329
336
|
state_dict = torch.load(filepath, map_location="cpu")
|
|
330
|
-
model.load_state_dict(state_dict)
|
|
337
|
+
model.load_state_dict(state_dict)
|
|
338
|
+
model.eval()
|
|
331
339
|
|
|
332
340
|
raw_img = cv2.imread("your/image/path")
|
|
333
341
|
depth = model.infer_image(raw_img) # HxW raw depth map in numpy
|
|
@@ -337,8 +345,8 @@ depth = model.infer_image(raw_img) # HxW raw depth map in numpy
|
|
|
337
345
|
|
|
338
346
|
export const depth_pro = (model: ModelData): string[] => {
|
|
339
347
|
const installSnippet = `# Download checkpoint
|
|
340
|
-
pip install
|
|
341
|
-
|
|
348
|
+
pip install huggingface_hub
|
|
349
|
+
hf download ${model.id} --local-dir checkpoints`;
|
|
342
350
|
|
|
343
351
|
const inferenceSnippet = `import depth_pro
|
|
344
352
|
|
|
@@ -412,7 +420,7 @@ export const describe_anything = (model: ModelData): string[] => [
|
|
|
412
420
|
from huggingface_hub import snapshot_download
|
|
413
421
|
from dam import DescribeAnythingModel
|
|
414
422
|
|
|
415
|
-
snapshot_download(${model.id}, local_dir="checkpoints")
|
|
423
|
+
snapshot_download("${model.id}", local_dir="checkpoints")
|
|
416
424
|
|
|
417
425
|
dam = DescribeAnythingModel(
|
|
418
426
|
model_path="checkpoints",
|
|
@@ -535,7 +543,7 @@ pipe.load_lora_weights("${model.id}")
|
|
|
535
543
|
prompt = "${get_prompt_from_diffusers_model(model) ?? diffusersVideoDefaultPrompt}"
|
|
536
544
|
input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/guitar-man.png")
|
|
537
545
|
|
|
538
|
-
|
|
546
|
+
output = pipe(image=input_image, prompt=prompt).frames[0]
|
|
539
547
|
export_to_video(output, "output.mp4")`,
|
|
540
548
|
];
|
|
541
549
|
|
|
@@ -578,7 +586,7 @@ from diffusers import AutoPipelineForInpainting
|
|
|
578
586
|
from diffusers.utils import load_image
|
|
579
587
|
|
|
580
588
|
# switch to "mps" for apple devices
|
|
581
|
-
pipe = AutoPipelineForInpainting.from_pretrained("${model.id}", dtype=torch.float16,
|
|
589
|
+
pipe = AutoPipelineForInpainting.from_pretrained("${model.id}", dtype=torch.float16, device_map="cuda")
|
|
582
590
|
|
|
583
591
|
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
|
|
584
592
|
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
|
|
@@ -601,11 +609,14 @@ image = pipe(
|
|
|
601
609
|
];
|
|
602
610
|
|
|
603
611
|
export const diffusers = (model: ModelData): string[] => {
|
|
612
|
+
// the Hub emits pipeline class tags as `diffusers:<ClassName>`; bare names are kept for manually added tags
|
|
613
|
+
const hasPipelineClass = (className: string): boolean =>
|
|
614
|
+
model.tags.includes(`diffusers:${className}`) ||
|
|
615
|
+
model.tags.includes(className) ||
|
|
616
|
+
model.config?.diffusers?._class_name === className;
|
|
617
|
+
|
|
604
618
|
let codeSnippets: string[];
|
|
605
|
-
if (
|
|
606
|
-
model.tags.includes("StableDiffusionInpaintPipeline") ||
|
|
607
|
-
model.tags.includes("StableDiffusionXLInpaintPipeline")
|
|
608
|
-
) {
|
|
619
|
+
if (hasPipelineClass("StableDiffusionInpaintPipeline") || hasPipelineClass("StableDiffusionXLInpaintPipeline")) {
|
|
609
620
|
codeSnippets = diffusers_inpainting(model);
|
|
610
621
|
} else if (model.tags.includes("controlnet")) {
|
|
611
622
|
codeSnippets = diffusers_controlnet(model);
|
|
@@ -621,7 +632,7 @@ export const diffusers = (model: ModelData): string[] => {
|
|
|
621
632
|
}
|
|
622
633
|
} else if (model.tags.includes("textual_inversion")) {
|
|
623
634
|
codeSnippets = diffusers_textual_inversion(model);
|
|
624
|
-
} else if (
|
|
635
|
+
} else if (hasPipelineClass("FluxFillPipeline")) {
|
|
625
636
|
codeSnippets = diffusers_flux_fill(model);
|
|
626
637
|
} else if (model.pipeline_tag === "image-to-video") {
|
|
627
638
|
codeSnippets = diffusers_image_to_video(model);
|
|
@@ -641,7 +652,7 @@ from diffusionkit.mlx import DiffusionPipeline
|
|
|
641
652
|
pipeline = DiffusionPipeline(
|
|
642
653
|
shift=3.0,
|
|
643
654
|
use_t5=False,
|
|
644
|
-
model_version
|
|
655
|
+
model_version="${model.id}",
|
|
645
656
|
low_memory_mode=True,
|
|
646
657
|
a16=True,
|
|
647
658
|
w16=True,
|
|
@@ -652,7 +663,7 @@ from diffusionkit.mlx import FluxPipeline
|
|
|
652
663
|
|
|
653
664
|
pipeline = FluxPipeline(
|
|
654
665
|
shift=1.0,
|
|
655
|
-
model_version
|
|
666
|
+
model_version="${model.id}",
|
|
656
667
|
low_memory_mode=True,
|
|
657
668
|
a16=True,
|
|
658
669
|
w16=True,
|
|
@@ -677,21 +688,22 @@ image, _ = pipeline.generate_image(
|
|
|
677
688
|
};
|
|
678
689
|
|
|
679
690
|
export const cartesia_pytorch = (model: ModelData): string[] => [
|
|
680
|
-
`#
|
|
681
|
-
|
|
691
|
+
`# The cartesia-pytorch package on PyPI ships no code: clone the repo and put its cartesia-pytorch folder on PYTHONPATH
|
|
692
|
+
# (it needs an Ampere or newer CUDA GPU, torch 2.4, mamba-ssm 2.2.2 and flash-attn 2.6.3)
|
|
693
|
+
# git clone https://github.com/cartesia-ai/edge && export PYTHONPATH="$PWD/edge/cartesia-pytorch:$PYTHONPATH"
|
|
694
|
+
from cartesia_pytorch.Rene import ReneLMHeadModel
|
|
682
695
|
from transformers import AutoTokenizer
|
|
683
696
|
|
|
684
|
-
model = ReneLMHeadModel.from_pretrained("${model.id}")
|
|
697
|
+
model = ReneLMHeadModel.from_pretrained("${model.id}").half().cuda()
|
|
685
698
|
tokenizer = AutoTokenizer.from_pretrained("allenai/OLMo-1B-hf")
|
|
686
699
|
|
|
687
700
|
in_message = ["Rene Descartes was"]
|
|
688
701
|
inputs = tokenizer(in_message, return_tensors="pt")
|
|
689
702
|
|
|
690
|
-
outputs = model.generate(inputs.input_ids, max_length=50, top_k=100, top_p=0.99)
|
|
703
|
+
outputs = model.generate(inputs.input_ids.cuda(), max_length=50, top_k=100, top_p=0.99)
|
|
691
704
|
out_message = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
|
692
705
|
|
|
693
|
-
print(out_message)
|
|
694
|
-
)`,
|
|
706
|
+
print(out_message)`,
|
|
695
707
|
];
|
|
696
708
|
|
|
697
709
|
export const cartesia_mlx = (model: ModelData): string[] => [
|
|
@@ -742,7 +754,8 @@ speech, *_ = model("text to generate speech from")`,
|
|
|
742
754
|
];
|
|
743
755
|
|
|
744
756
|
export const espnetASR = (model: ModelData): string[] => [
|
|
745
|
-
`
|
|
757
|
+
`import soundfile
|
|
758
|
+
from espnet2.bin.asr_inference import Speech2Text
|
|
746
759
|
|
|
747
760
|
model = Speech2Text.from_pretrained(
|
|
748
761
|
"${model.id}"
|
|
@@ -772,9 +785,14 @@ models, cfg, task = load_model_ensemble_and_task_from_hf_hub(
|
|
|
772
785
|
];
|
|
773
786
|
|
|
774
787
|
export const flair = (model: ModelData): string[] => [
|
|
775
|
-
`from flair.
|
|
788
|
+
`from flair.data import Sentence
|
|
789
|
+
from flair.models import SequenceTagger
|
|
776
790
|
|
|
777
|
-
tagger = SequenceTagger.load("${model.id}")
|
|
791
|
+
tagger = SequenceTagger.load("${model.id}")
|
|
792
|
+
|
|
793
|
+
sentence = Sentence("George Washington went to Washington.")
|
|
794
|
+
tagger.predict(sentence)
|
|
795
|
+
print(sentence)`,
|
|
778
796
|
];
|
|
779
797
|
|
|
780
798
|
export const flexray = (model: ModelData): string[] => [
|
|
@@ -866,13 +884,20 @@ pipeline = GaussianFormerRenderingPipeline.from_pretrained("${model.id}")`,
|
|
|
866
884
|
export const gliner = (model: ModelData): string[] => [
|
|
867
885
|
`from gliner import GLiNER
|
|
868
886
|
|
|
869
|
-
model = GLiNER.from_pretrained("${model.id}")
|
|
887
|
+
model = GLiNER.from_pretrained("${model.id}")
|
|
888
|
+
|
|
889
|
+
text = "Cristiano Ronaldo dos Santos Aveiro was born on 5 February 1985 in Funchal, Madeira, Portugal."
|
|
890
|
+
labels = ["person", "date", "location"]
|
|
891
|
+
|
|
892
|
+
entities = model.predict_entities(text, labels)
|
|
893
|
+
for entity in entities:
|
|
894
|
+
print(entity["text"], "=>", entity["label"])`,
|
|
870
895
|
];
|
|
871
896
|
|
|
872
897
|
export const gliner2 = (model: ModelData): string[] => [
|
|
873
|
-
`from gliner2 import
|
|
898
|
+
`from gliner2 import AutoExtractor
|
|
874
899
|
|
|
875
|
-
|
|
900
|
+
extractor = AutoExtractor.from_pretrained("${model.id}")
|
|
876
901
|
|
|
877
902
|
# Extract entities
|
|
878
903
|
text = "Apple CEO Tim Cook announced iPhone 15 in Cupertino yesterday."
|
|
@@ -885,7 +910,7 @@ export const indextts = (model: ModelData): string[] => [
|
|
|
885
910
|
`# Download model
|
|
886
911
|
from huggingface_hub import snapshot_download
|
|
887
912
|
|
|
888
|
-
snapshot_download(${model.id}, local_dir="checkpoints")
|
|
913
|
+
snapshot_download("${model.id}", local_dir="checkpoints")
|
|
889
914
|
|
|
890
915
|
from indextts.infer import IndexTTS
|
|
891
916
|
|
|
@@ -941,9 +966,11 @@ print(out["answers"]["team"]["choice"])`,
|
|
|
941
966
|
};
|
|
942
967
|
|
|
943
968
|
export const keras = (model: ModelData): string[] => [
|
|
944
|
-
`#
|
|
969
|
+
`# !pip install -U keras tensorflow huggingface_hub
|
|
970
|
+
# Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here;
|
|
971
|
+
# "jax" and "torch" also work for computation once TensorFlow is installed.
|
|
945
972
|
import os
|
|
946
|
-
os.environ["KERAS_BACKEND"] = "
|
|
973
|
+
os.environ["KERAS_BACKEND"] = "tensorflow"
|
|
947
974
|
|
|
948
975
|
import keras
|
|
949
976
|
|
|
@@ -1070,13 +1097,15 @@ export const kernels = (model: ModelData): string[] => [
|
|
|
1070
1097
|
|
|
1071
1098
|
from kernels import get_kernel
|
|
1072
1099
|
|
|
1073
|
-
|
|
1100
|
+
# a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones
|
|
1101
|
+
kernel = get_kernel("${model.id}", version=1)`,
|
|
1074
1102
|
];
|
|
1075
1103
|
|
|
1076
1104
|
export const kimi_audio = (model: ModelData): string[] => [
|
|
1077
1105
|
`# Example usage for KimiAudio
|
|
1078
|
-
# pip install git+https://github.com/MoonshotAI/Kimi-Audio.git
|
|
1106
|
+
# pip install git+https://github.com/MoonshotAI/Kimi-Audio.git "transformers<5"
|
|
1079
1107
|
|
|
1108
|
+
import soundfile as sf
|
|
1080
1109
|
from kimia_infer.api.kimia import KimiAudio
|
|
1081
1110
|
|
|
1082
1111
|
model = KimiAudio(model_path="${model.id}", load_detokenizer=True)
|
|
@@ -1517,14 +1546,17 @@ pip install -U litert-lm
|
|
|
1517
1546
|
|
|
1518
1547
|
# 2. Download and run this model locally:
|
|
1519
1548
|
# See: https://ai.google.dev/edge/litert-lm/cli
|
|
1549
|
+
# A single .litertlm file in the repo is picked automatically; otherwise the CLI asks which one to run
|
|
1550
|
+
# (or pass its name right after the repo id).
|
|
1520
1551
|
litert-lm run \\
|
|
1521
1552
|
--from-huggingface-repo=${model.id} \\
|
|
1522
1553
|
--prompt="Write me a poem"`,
|
|
1523
1554
|
];
|
|
1524
1555
|
|
|
1525
1556
|
export const tf_keras = (model: ModelData): string[] => [
|
|
1526
|
-
`# Note: 'keras<3.x' or 'tf_keras' must be installed (legacy)
|
|
1557
|
+
`# Note: 'keras<3.x' or 'tf_keras' must be installed (legacy), and from_pretrained_keras was removed in huggingface_hub 1.0.
|
|
1527
1558
|
# See https://github.com/keras-team/tf-keras for more details.
|
|
1559
|
+
# !pip install "huggingface_hub<1.0" tf_keras
|
|
1528
1560
|
from huggingface_hub import from_pretrained_keras
|
|
1529
1561
|
|
|
1530
1562
|
model = from_pretrained_keras("${model.id}")
|
|
@@ -1578,6 +1610,19 @@ from MeshAnything.models.meshanything import MeshAnything
|
|
|
1578
1610
|
model = MeshAnything(args)`,
|
|
1579
1611
|
];
|
|
1580
1612
|
|
|
1613
|
+
export const microduck = (model: ModelData): string[] => {
|
|
1614
|
+
// Model-card tag convention: tags: [microduck-slot:stand].
|
|
1615
|
+
const slots = ["walk", "stand", "sitstand", "ground_pick", "kick_left", "kick_right", "roulade"];
|
|
1616
|
+
const matchingSlots = slots.filter((slot) => model.tags.includes(`microduck-slot:${slot}`));
|
|
1617
|
+
const slot = matchingSlots.length === 1 ? matchingSlots[0] : "SLOT";
|
|
1618
|
+
const prefix =
|
|
1619
|
+
matchingSlots.length === 1
|
|
1620
|
+
? ""
|
|
1621
|
+
: `# Replace SLOT with the slot specified in the model card (${slots.join(", ")}).\n`;
|
|
1622
|
+
|
|
1623
|
+
return [`${prefix}sudo robotctl policy load ${slot} ${model.id}`];
|
|
1624
|
+
};
|
|
1625
|
+
|
|
1581
1626
|
export const multimolecule = (model: ModelData): string[] => {
|
|
1582
1627
|
const widgetExample = model.widgetData?.[0] as WidgetExampleTextInput | undefined;
|
|
1583
1628
|
const exampleText = escapeStringForJson(widgetExample?.text ?? "");
|
|
@@ -1771,14 +1816,18 @@ model = pe.VisionTransformer.from_config("${model.id}", pretrained=True)`;
|
|
|
1771
1816
|
};
|
|
1772
1817
|
export const phantom_wan = (model: ModelData): string[] => [
|
|
1773
1818
|
`from huggingface_hub import snapshot_download
|
|
1774
|
-
from phantom_wan import
|
|
1819
|
+
from phantom_wan import WanI2V, configs
|
|
1820
|
+
from PIL import Image
|
|
1775
1821
|
|
|
1776
1822
|
checkpoint_dir = snapshot_download("${model.id}")
|
|
1777
1823
|
wan_i2v = WanI2V(
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1824
|
+
config=configs.WAN_CONFIGS['i2v-14B'],
|
|
1825
|
+
checkpoint_dir=checkpoint_dir,
|
|
1826
|
+
)
|
|
1827
|
+
|
|
1828
|
+
text_prompt = "A cat playing with a ball of yarn"
|
|
1829
|
+
image_prompt = Image.open("path/to/image.jpg").convert("RGB")
|
|
1830
|
+
video = wan_i2v.generate(text_prompt, image_prompt)`,
|
|
1782
1831
|
];
|
|
1783
1832
|
|
|
1784
1833
|
export const pocket_tts = (model: ModelData): string[] => [
|
|
@@ -1903,7 +1952,7 @@ export const tensorflowtts = (model: ModelData): string[] => {
|
|
|
1903
1952
|
export const timm = (model: ModelData): string[] => [
|
|
1904
1953
|
`import timm
|
|
1905
1954
|
|
|
1906
|
-
model = timm.create_model("
|
|
1955
|
+
model = timm.create_model("hf-hub:${model.id}", pretrained=True)`,
|
|
1907
1956
|
];
|
|
1908
1957
|
|
|
1909
1958
|
export const saelens = (/* model: ModelData */): string[] => [
|
|
@@ -1931,13 +1980,15 @@ seed_story_cfg = OmegaConf.load(seed_story_cfg_path)
|
|
|
1931
1980
|
seed_story = hydra.utils.instantiate(seed_story_cfg, llm=llm) `,
|
|
1932
1981
|
];
|
|
1933
1982
|
|
|
1934
|
-
const
|
|
1983
|
+
const SKLEARN_PICKLE_FILE = /\.(joblib|pkl|pickle)$/i;
|
|
1984
|
+
|
|
1985
|
+
// skops.hub_utils was removed in skops 0.12, so files are downloaded with huggingface_hub
|
|
1986
|
+
const sklearnPickle = (model: ModelData, modelFile: string) => {
|
|
1935
1987
|
return [
|
|
1936
|
-
`import
|
|
1937
|
-
|
|
1938
|
-
download("${model.id}", "path_to_folder")
|
|
1988
|
+
`from huggingface_hub import hf_hub_download
|
|
1989
|
+
import joblib
|
|
1939
1990
|
model = joblib.load(
|
|
1940
|
-
"${escapeStringForJson(modelFile)}"
|
|
1991
|
+
hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}")
|
|
1941
1992
|
)
|
|
1942
1993
|
# only load pickle files from sources you trust
|
|
1943
1994
|
# read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
|
|
@@ -1946,42 +1997,32 @@ model = joblib.load(
|
|
|
1946
1997
|
|
|
1947
1998
|
const skopsFormat = (model: ModelData, modelFile: string) => {
|
|
1948
1999
|
return [
|
|
1949
|
-
`from
|
|
2000
|
+
`from huggingface_hub import hf_hub_download
|
|
1950
2001
|
from skops.io import load
|
|
1951
|
-
download("${model.id}", "path_to_folder")
|
|
1952
2002
|
# make sure model file is in skops format
|
|
1953
2003
|
# if model is a pickle file, make sure it's from a source you trust
|
|
1954
|
-
model = load("
|
|
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`,
|
|
2004
|
+
model = load(hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}"))`,
|
|
1967
2005
|
];
|
|
1968
2006
|
};
|
|
1969
2007
|
|
|
1970
2008
|
export const sklearn = (model: ModelData): string[] => {
|
|
2009
|
+
const declaredFile = model.config?.sklearn?.model?.file;
|
|
1971
2010
|
if (model.tags.includes("skops")) {
|
|
1972
|
-
|
|
1973
|
-
const skopssaveFormat = model.config?.sklearn?.model_format;
|
|
1974
|
-
if (!skopsmodelFile) {
|
|
2011
|
+
if (!declaredFile) {
|
|
1975
2012
|
return [`# ⚠️ Model filename not specified in config.json`];
|
|
1976
2013
|
}
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
return skopsFormat(model, skopsmodelFile);
|
|
2014
|
+
// some skops repos declare no model_format for a pickled file (e.g. julien-c/skops-digits), which skops.io cannot read
|
|
2015
|
+
if (model.config?.sklearn?.model_format === "pickle" || SKLEARN_PICKLE_FILE.test(declaredFile)) {
|
|
2016
|
+
return sklearnPickle(model, declaredFile);
|
|
1981
2017
|
}
|
|
1982
|
-
|
|
1983
|
-
return skopsJobLib(model);
|
|
2018
|
+
return skopsFormat(model, declaredFile);
|
|
1984
2019
|
}
|
|
2020
|
+
// repos pushed without skops often still declare their filename in config.json; it is only trusted when it names a
|
|
2021
|
+
// pickle, since some hand-written configs carry typos (e.g. "sklean_model.jpblib" next to a real sklearn_model.joblib)
|
|
2022
|
+
return sklearnPickle(
|
|
2023
|
+
model,
|
|
2024
|
+
declaredFile && SKLEARN_PICKLE_FILE.test(declaredFile) ? declaredFile : "sklearn_model.joblib",
|
|
2025
|
+
);
|
|
1985
2026
|
};
|
|
1986
2027
|
|
|
1987
2028
|
export const stable_audio_tools = (model: ModelData): string[] => [
|
|
@@ -2032,7 +2073,7 @@ export const sam2 = (model: ModelData): string[] => {
|
|
|
2032
2073
|
import torch
|
|
2033
2074
|
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
|
2034
2075
|
|
|
2035
|
-
predictor = SAM2ImagePredictor.from_pretrained(${model.id})
|
|
2076
|
+
predictor = SAM2ImagePredictor.from_pretrained("${model.id}")
|
|
2036
2077
|
|
|
2037
2078
|
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
|
|
2038
2079
|
predictor.set_image(<your_image>)
|
|
@@ -2042,13 +2083,13 @@ with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
|
|
|
2042
2083
|
import torch
|
|
2043
2084
|
from sam2.sam2_video_predictor import SAM2VideoPredictor
|
|
2044
2085
|
|
|
2045
|
-
predictor = SAM2VideoPredictor.from_pretrained(${model.id})
|
|
2086
|
+
predictor = SAM2VideoPredictor.from_pretrained("${model.id}")
|
|
2046
2087
|
|
|
2047
2088
|
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
|
|
2048
2089
|
state = predictor.init_state(<your_video>)
|
|
2049
2090
|
|
|
2050
2091
|
# 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>)
|
|
2092
|
+
frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>)
|
|
2052
2093
|
|
|
2053
2094
|
# propagate the prompts to get masklets throughout the video
|
|
2054
2095
|
for frame_idx, object_ids, masks in predictor.propagate_in_video(state):
|
|
@@ -2070,11 +2111,18 @@ output = inference(image, mask)`,
|
|
|
2070
2111
|
];
|
|
2071
2112
|
|
|
2072
2113
|
export const sam_3d_body = (model: ModelData): string[] => [
|
|
2073
|
-
`
|
|
2114
|
+
`import cv2
|
|
2115
|
+
import numpy as np
|
|
2116
|
+
from notebook.utils import setup_sam_3d_body
|
|
2117
|
+
from tools.vis_utils import visualize_sample_together
|
|
2118
|
+
|
|
2119
|
+
estimator = setup_sam_3d_body("${model.id}")
|
|
2120
|
+
|
|
2121
|
+
img_bgr = cv2.imread("path/to/image.jpg")
|
|
2122
|
+
outputs = estimator.process_one_image(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
|
|
2074
2123
|
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
rend_img = visualize_sample_together(image, outputs, estimator.faces)`,
|
|
2124
|
+
rend_img = visualize_sample_together(img_bgr, outputs, estimator.faces)
|
|
2125
|
+
cv2.imwrite("output.jpg", rend_img.astype(np.uint8))`,
|
|
2078
2126
|
];
|
|
2079
2127
|
|
|
2080
2128
|
export const sampleFactory = (model: ModelData): string[] => [
|
|
@@ -2171,7 +2219,17 @@ docs_emb = model.encode(documents, is_query=False)`,
|
|
|
2171
2219
|
];
|
|
2172
2220
|
}
|
|
2173
2221
|
|
|
2174
|
-
|
|
2222
|
+
// Rerankers saved in the CrossEncoder format are often tagged text-classification rather than text-ranking;
|
|
2223
|
+
// their architecture still identifies them (e.g. BAAI/bge-reranker-v2-m3). SetFit models can share that shape but
|
|
2224
|
+
// are bi-encoders with a separate head.
|
|
2225
|
+
const isCrossEncoder =
|
|
2226
|
+
model.tags.includes("cross-encoder") ||
|
|
2227
|
+
model.pipeline_tag == "text-ranking" ||
|
|
2228
|
+
(model.pipeline_tag === "text-classification" &&
|
|
2229
|
+
!model.tags.includes("setfit") &&
|
|
2230
|
+
(model.config?.architectures ?? []).some((arch) => arch.endsWith("ForSequenceClassification")));
|
|
2231
|
+
|
|
2232
|
+
if (isCrossEncoder) {
|
|
2175
2233
|
return [
|
|
2176
2234
|
`from sentence_transformers import CrossEncoder
|
|
2177
2235
|
|
|
@@ -2190,6 +2248,34 @@ print(scores)`,
|
|
|
2190
2248
|
];
|
|
2191
2249
|
}
|
|
2192
2250
|
|
|
2251
|
+
// sentence-transformers tags SparseEncoder repos (SPLADE & co) with "sparse-encoder" and MultiVectorEncoder repos
|
|
2252
|
+
// (ColBERT & co) with "multi-vector" when pushing them; both encode queries and documents separately
|
|
2253
|
+
const retrievalEncoder = model.tags.includes("sparse-encoder")
|
|
2254
|
+
? "SparseEncoder"
|
|
2255
|
+
: model.tags.includes("multi-vector")
|
|
2256
|
+
? "MultiVectorEncoder"
|
|
2257
|
+
: undefined;
|
|
2258
|
+
if (retrievalEncoder) {
|
|
2259
|
+
return [
|
|
2260
|
+
`from sentence_transformers import ${retrievalEncoder}
|
|
2261
|
+
|
|
2262
|
+
model = ${retrievalEncoder}("${model.id}"${remote_code_snippet})
|
|
2263
|
+
|
|
2264
|
+
queries = ["Which planet is known as the Red Planet?"]
|
|
2265
|
+
documents = [
|
|
2266
|
+
"Venus is often called Earth's twin because of its similar size and proximity.",
|
|
2267
|
+
"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
|
|
2268
|
+
"Jupiter, the largest planet in our solar system, has a prominent red spot.",
|
|
2269
|
+
]
|
|
2270
|
+
|
|
2271
|
+
query_embeddings = model.encode_query(queries)
|
|
2272
|
+
document_embeddings = model.encode_document(documents)
|
|
2273
|
+
|
|
2274
|
+
similarities = model.similarity(query_embeddings, document_embeddings)
|
|
2275
|
+
print(similarities)`,
|
|
2276
|
+
];
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2193
2279
|
const exampleSentences = get_widget_examples_from_st_model(model) ?? [
|
|
2194
2280
|
"The weather is lovely today.",
|
|
2195
2281
|
"It's so sunny outside!",
|
|
@@ -2213,7 +2299,10 @@ print(similarities.shape)
|
|
|
2213
2299
|
export const setfit = (model: ModelData): string[] => [
|
|
2214
2300
|
`from setfit import SetFitModel
|
|
2215
2301
|
|
|
2216
|
-
model = SetFitModel.from_pretrained("${model.id}")
|
|
2302
|
+
model = SetFitModel.from_pretrained("${model.id}")
|
|
2303
|
+
|
|
2304
|
+
preds = model.predict(["i loved the spiderman movie!", "pineapple on pizza is the worst"])
|
|
2305
|
+
print(preds)`,
|
|
2217
2306
|
];
|
|
2218
2307
|
|
|
2219
2308
|
export const spacy = (model: ModelData): string[] => [
|
|
@@ -2231,7 +2320,10 @@ nlp = ${nameWithoutNamespace(model.id)}.load()`,
|
|
|
2231
2320
|
export const span_marker = (model: ModelData): string[] => [
|
|
2232
2321
|
`from span_marker import SpanMarkerModel
|
|
2233
2322
|
|
|
2234
|
-
model = SpanMarkerModel.from_pretrained("${model.id}")
|
|
2323
|
+
model = SpanMarkerModel.from_pretrained("${model.id}")
|
|
2324
|
+
|
|
2325
|
+
entities = model.predict("Amelia Earhart flew her single engine Lockheed Vega 5B across the Atlantic to Paris.")
|
|
2326
|
+
print(entities)`,
|
|
2235
2327
|
];
|
|
2236
2328
|
|
|
2237
2329
|
export const stanza = (model: ModelData): string[] => [
|
|
@@ -2252,6 +2344,8 @@ const speechBrainMethod = (speechbrainInterface: string) => {
|
|
|
2252
2344
|
return "enhance_file";
|
|
2253
2345
|
case "SepformerSeparation":
|
|
2254
2346
|
return "separate_file";
|
|
2347
|
+
case "SpeakerRecognition":
|
|
2348
|
+
return "verify_files";
|
|
2255
2349
|
default:
|
|
2256
2350
|
return undefined;
|
|
2257
2351
|
}
|
|
@@ -2268,12 +2362,14 @@ export const speechbrain = (model: ModelData): string[] => {
|
|
|
2268
2362
|
return [`# interface in config.json invalid`];
|
|
2269
2363
|
}
|
|
2270
2364
|
|
|
2365
|
+
// speaker verification compares two recordings
|
|
2366
|
+
const methodArgs = speechbrainMethod === "verify_files" ? '"speaker1.wav", "speaker2.wav"' : '"file.wav"';
|
|
2271
2367
|
return [
|
|
2272
2368
|
`from speechbrain.pretrained import ${speechbrainInterface}
|
|
2273
2369
|
model = ${speechbrainInterface}.from_hparams(
|
|
2274
2370
|
"${model.id}"
|
|
2275
2371
|
)
|
|
2276
|
-
model.${speechbrainMethod}(
|
|
2372
|
+
model.${speechbrainMethod}(${methodArgs})`,
|
|
2277
2373
|
];
|
|
2278
2374
|
};
|
|
2279
2375
|
|
|
@@ -2311,6 +2407,7 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2311
2407
|
? "extractor"
|
|
2312
2408
|
: "processor";
|
|
2313
2409
|
autoSnippet.push(
|
|
2410
|
+
"# pip install -U transformers accelerate",
|
|
2314
2411
|
"# Load model directly",
|
|
2315
2412
|
`from transformers import ${info.processor}, ${auto_model}`,
|
|
2316
2413
|
"",
|
|
@@ -2344,12 +2441,13 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2344
2441
|
' return_tensors="pt",',
|
|
2345
2442
|
").to(model.device)",
|
|
2346
2443
|
"",
|
|
2347
|
-
"outputs = model.generate(**inputs, max_new_tokens=
|
|
2444
|
+
"outputs = model.generate(**inputs, max_new_tokens=256)",
|
|
2348
2445
|
`print(${processorVarName}.decode(outputs[0][inputs["input_ids"].shape[-1]:]))`,
|
|
2349
2446
|
);
|
|
2350
2447
|
}
|
|
2351
2448
|
} else {
|
|
2352
2449
|
autoSnippet.push(
|
|
2450
|
+
"# pip install -U transformers accelerate",
|
|
2353
2451
|
"# Load model directly",
|
|
2354
2452
|
`from transformers import ${auto_model}`,
|
|
2355
2453
|
`model = ${auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ', device_map="auto")',
|
|
@@ -2362,7 +2460,7 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2362
2460
|
pipelineSnippet.push(
|
|
2363
2461
|
`# Warning: Pipeline type "${model.pipeline_tag}" is no longer supported in transformers v5.`,
|
|
2364
2462
|
`# You must load the model directly (see below) or downgrade to v4.x with:`,
|
|
2365
|
-
`#
|
|
2463
|
+
`# pip install "transformers<5.0.0"`,
|
|
2366
2464
|
);
|
|
2367
2465
|
}
|
|
2368
2466
|
|
|
@@ -2372,7 +2470,7 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2372
2470
|
`pipe = pipeline("${model.pipeline_tag}", model="${model.id}"` + remote_code_snippet + ")",
|
|
2373
2471
|
);
|
|
2374
2472
|
|
|
2375
|
-
if (model.tags.includes("conversational")) {
|
|
2473
|
+
if (model.tags.includes("conversational") && hasChatTemplate(model)) {
|
|
2376
2474
|
if (model.tags.includes("image-text-to-text")) {
|
|
2377
2475
|
pipelineSnippet.push(
|
|
2378
2476
|
"messages = [",
|
|
@@ -2410,6 +2508,11 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2410
2508
|
return [autoSnippet.join("\n")];
|
|
2411
2509
|
};
|
|
2412
2510
|
|
|
2511
|
+
// Hub tasks that transformers.js exposes under a different pipeline() task name
|
|
2512
|
+
const TRANSFORMERS_JS_TASK_ALIASES: Record<string, string> = {
|
|
2513
|
+
"sentence-similarity": "feature-extraction",
|
|
2514
|
+
};
|
|
2515
|
+
|
|
2413
2516
|
export const transformersJS = (model: ModelData): string[] => {
|
|
2414
2517
|
if (!model.pipeline_tag) {
|
|
2415
2518
|
return [`// ⚠️ Unknown pipeline tag`];
|
|
@@ -2417,45 +2520,119 @@ export const transformersJS = (model: ModelData): string[] => {
|
|
|
2417
2520
|
|
|
2418
2521
|
const libName = "@huggingface/transformers";
|
|
2419
2522
|
|
|
2523
|
+
// Rerankers score (query, document) pairs. The text-classification pipeline cannot take pairs and applies a softmax
|
|
2524
|
+
// over their single logit, so every score comes out as 1; the model is called directly instead.
|
|
2525
|
+
if (model.pipeline_tag === "text-ranking") {
|
|
2526
|
+
return [
|
|
2527
|
+
`// npm i ${libName}
|
|
2528
|
+
import { AutoTokenizer, AutoModelForSequenceClassification } from '${libName}';
|
|
2529
|
+
|
|
2530
|
+
const tokenizer = await AutoTokenizer.from_pretrained('${model.id}');
|
|
2531
|
+
const model = await AutoModelForSequenceClassification.from_pretrained('${model.id}');
|
|
2532
|
+
|
|
2533
|
+
const query = 'Which planet is known as the Red Planet?';
|
|
2534
|
+
const documents = [
|
|
2535
|
+
'Mars, known for its reddish appearance, is often referred to as the Red Planet.',
|
|
2536
|
+
'Venus is often called the twin of Earth because of its similar size and proximity.',
|
|
2537
|
+
];
|
|
2538
|
+
|
|
2539
|
+
const inputs = tokenizer(new Array(documents.length).fill(query), { text_pair: documents, padding: true, truncation: true });
|
|
2540
|
+
const { logits } = await model(inputs);
|
|
2541
|
+
console.log(logits.sigmoid().tolist()); // one relevance score per document`,
|
|
2542
|
+
];
|
|
2543
|
+
}
|
|
2544
|
+
|
|
2545
|
+
const task = TRANSFORMERS_JS_TASK_ALIASES[model.pipeline_tag] ?? model.pipeline_tag;
|
|
2546
|
+
|
|
2420
2547
|
return [
|
|
2421
2548
|
`// npm i ${libName}
|
|
2422
2549
|
import { pipeline } from '${libName}';
|
|
2423
2550
|
|
|
2424
2551
|
// Allocate pipeline
|
|
2425
|
-
const pipe = await pipeline('${
|
|
2552
|
+
const pipe = await pipeline('${task}', '${model.id}');`,
|
|
2426
2553
|
];
|
|
2427
2554
|
};
|
|
2428
2555
|
|
|
2429
|
-
const
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2556
|
+
const PEFT_TASK_TYPE_TO_AUTO_CLASS: Record<string, string> = {
|
|
2557
|
+
CAUSAL_LM: "AutoModelForCausalLM",
|
|
2558
|
+
SEQ_2_SEQ_LM: "AutoModelForSeq2SeqLM",
|
|
2559
|
+
TOKEN_CLS: "AutoModelForTokenClassification",
|
|
2560
|
+
SEQ_CLS: "AutoModelForSequenceClassification",
|
|
2561
|
+
QUESTION_ANS: "AutoModelForQuestionAnswering",
|
|
2562
|
+
// transformers has no AutoModelForFeatureExtraction; PEFT's own AutoPeftModelForFeatureExtraction resolves to AutoModel
|
|
2563
|
+
FEATURE_EXTRACTION: "AutoModel",
|
|
2564
|
+
};
|
|
2565
|
+
|
|
2566
|
+
// PEFT has no speech-specific task type: Whisper-style adapters are tagged SEQ_2_SEQ_LM, but transformers registers
|
|
2567
|
+
// Whisper and Speech2Text only under AutoModelForSpeechSeq2Seq (SeamlessM4T is under both seq2seq auto classes, SpeechT5
|
|
2568
|
+
// under the speech-to-text and text-to-spectrogram ones).
|
|
2569
|
+
// Other audio seq2seq models (Qwen2-Audio, GLM-ASR) stay under AutoModelForSeq2SeqLM, so the base model name is the
|
|
2570
|
+
// main signal. Matched against Hub repo ids, which are hyphenated (facebook/s2t-small-librispeech-asr,
|
|
2571
|
+
// facebook/seamless-m4t-v2-large), not against transformers model_type identifiers.
|
|
2572
|
+
const PEFT_SPEECH_TO_TEXT_BASE_MODEL = /whisper|\bs2t\b|speech[-_]to[-_]text/i;
|
|
2573
|
+
// SeamlessM4T also has a text-to-text head under AutoModelForSeq2SeqLM, which text translation adapters need; most
|
|
2574
|
+
// SeamlessM4T adapters on the Hub are speech ones, so the speech class stays the default when no text task is declared.
|
|
2575
|
+
const PEFT_SEAMLESS_M4T_BASE_MODEL = /seamless[-_]m4t/i;
|
|
2576
|
+
const PEFT_TEXT_TASKS = ["translation", "text2text-generation", "summarization"];
|
|
2577
|
+
// SpeechT5 checkpoints are overwhelmingly text-to-speech fine-tunes (often without "tts" in their name), which load
|
|
2578
|
+
// with AutoModelForTextToSpectrogram; AutoModelForSpeechSeq2Seq would silently load the speech-to-text head, so only
|
|
2579
|
+
// ASR bases or adapters get it.
|
|
2580
|
+
const PEFT_SPEECHT5_BASE_MODEL = /speecht5/i;
|
|
2581
|
+
|
|
2582
|
+
const declaresTask = (model: ModelData, task: string): boolean =>
|
|
2583
|
+
model.pipeline_tag === task || model.tags.includes(task);
|
|
2584
|
+
|
|
2585
|
+
// the auto class a SEQ_2_SEQ_LM adapter needs when its base is a speech model, undefined for text seq2seq bases
|
|
2586
|
+
const peftSpeechAutoClass = (model: ModelData, baseModel: string): string | undefined => {
|
|
2587
|
+
if (PEFT_SPEECH_TO_TEXT_BASE_MODEL.test(baseModel)) {
|
|
2588
|
+
return "AutoModelForSpeechSeq2Seq";
|
|
2589
|
+
}
|
|
2590
|
+
if (PEFT_SEAMLESS_M4T_BASE_MODEL.test(baseModel)) {
|
|
2591
|
+
return PEFT_TEXT_TASKS.some((task) => declaresTask(model, task)) ? undefined : "AutoModelForSpeechSeq2Seq";
|
|
2441
2592
|
}
|
|
2593
|
+
if (PEFT_SPEECHT5_BASE_MODEL.test(baseModel)) {
|
|
2594
|
+
const isAsr = /(?:^|[^a-z])asr(?:[^a-z]|$)/i.test(baseModel) || declaresTask(model, "automatic-speech-recognition");
|
|
2595
|
+
return isAsr ? "AutoModelForSpeechSeq2Seq" : "AutoModelForTextToSpectrogram";
|
|
2596
|
+
}
|
|
2597
|
+
return undefined;
|
|
2442
2598
|
};
|
|
2443
2599
|
|
|
2444
2600
|
export const peft = (model: ModelData): string[] => {
|
|
2445
2601
|
const { base_model_name_or_path: peftBaseModel, task_type: peftTaskType } = model.config?.peft ?? {};
|
|
2446
|
-
|
|
2447
|
-
if (!
|
|
2602
|
+
let autoClass = peftTaskType ? PEFT_TASK_TYPE_TO_AUTO_CLASS[peftTaskType] : undefined;
|
|
2603
|
+
if (!autoClass) {
|
|
2448
2604
|
return [`Task type is invalid.`];
|
|
2449
2605
|
}
|
|
2450
|
-
|
|
2606
|
+
// adapter_config.json sometimes omits the base model; the model card's base_model is the next best source
|
|
2607
|
+
const cardBaseModel = Array.isArray(model.cardData?.base_model)
|
|
2608
|
+
? model.cardData.base_model[0]
|
|
2609
|
+
: model.cardData?.base_model;
|
|
2610
|
+
const baseModel = peftBaseModel || cardBaseModel;
|
|
2611
|
+
if (!baseModel) {
|
|
2451
2612
|
return [`Base model is not found.`];
|
|
2452
2613
|
}
|
|
2614
|
+
if (peftTaskType === "SEQ_2_SEQ_LM") {
|
|
2615
|
+
autoClass = peftSpeechAutoClass(model, baseModel) ?? autoClass;
|
|
2616
|
+
}
|
|
2617
|
+
|
|
2618
|
+
if (autoClass === "AutoModelForTextToSpectrogram") {
|
|
2619
|
+
return [
|
|
2620
|
+
`from peft import PeftConfig, PeftModel
|
|
2621
|
+
from transformers import AutoModelForTextToSpectrogram
|
|
2622
|
+
|
|
2623
|
+
base_model = AutoModelForTextToSpectrogram.from_pretrained("${escapeStringForJson(baseModel)}")
|
|
2624
|
+
# PEFT's seq2seq wrapper needs generate(), which SpeechT5's text-to-speech model lacks: load the adapter without its task type
|
|
2625
|
+
config = PeftConfig.from_pretrained("${model.id}")
|
|
2626
|
+
config.task_type = None
|
|
2627
|
+
model = PeftModel.from_pretrained(base_model, "${model.id}", config=config)`,
|
|
2628
|
+
];
|
|
2629
|
+
}
|
|
2453
2630
|
|
|
2454
2631
|
return [
|
|
2455
2632
|
`from peft import PeftModel
|
|
2456
|
-
from transformers import
|
|
2633
|
+
from transformers import ${autoClass}
|
|
2457
2634
|
|
|
2458
|
-
base_model =
|
|
2635
|
+
base_model = ${autoClass}.from_pretrained("${escapeStringForJson(baseModel)}")
|
|
2459
2636
|
model = PeftModel.from_pretrained(base_model, "${model.id}")`,
|
|
2460
2637
|
];
|
|
2461
2638
|
};
|
|
@@ -2490,7 +2667,7 @@ transcriptions = asr_model.transcribe(["file.wav"])`,
|
|
|
2490
2667
|
};
|
|
2491
2668
|
|
|
2492
2669
|
export const mlAgents = (model: ModelData): string[] => [
|
|
2493
|
-
`mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./
|
|
2670
|
+
`mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./downloads"`,
|
|
2494
2671
|
];
|
|
2495
2672
|
|
|
2496
2673
|
export const sentis = (/* model: ModelData */): string[] => [
|
|
@@ -2565,10 +2742,10 @@ model = Model.from_pretrained("${model.id}")`,
|
|
|
2565
2742
|
|
|
2566
2743
|
export const lvface = (model: ModelData): string[] => [
|
|
2567
2744
|
`from huggingface_hub import hf_hub_download
|
|
2568
|
-
|
|
2745
|
+
from inference_onnx import LVFaceONNXInferencer
|
|
2569
2746
|
|
|
2570
2747
|
model_path = hf_hub_download("${model.id}", "LVFace-L_Glint360K/LVFace-L_Glint360K.onnx")
|
|
2571
|
-
inferencer = LVFaceONNXInferencer(model_path, use_gpu=True
|
|
2748
|
+
inferencer = LVFaceONNXInferencer(model_path, use_gpu=True)
|
|
2572
2749
|
img_path = 'path/to/image1.jpg'
|
|
2573
2750
|
embedding = inferencer.infer_from_image(img_path)`,
|
|
2574
2751
|
];
|
|
@@ -2607,8 +2784,7 @@ export const vui = (): string[] => [
|
|
|
2607
2784
|
|
|
2608
2785
|
import torchaudio
|
|
2609
2786
|
|
|
2610
|
-
from vui.
|
|
2611
|
-
from vui.model import Vui,
|
|
2787
|
+
from vui.legacy import Vui, render
|
|
2612
2788
|
|
|
2613
2789
|
model = Vui.from_pretrained().cuda()
|
|
2614
2790
|
waveform = render(
|
|
@@ -2616,16 +2792,17 @@ waveform = render(
|
|
|
2616
2792
|
"Hey, here is some random stuff, usually something quite long as the shorter the text the less likely the model can cope!",
|
|
2617
2793
|
)
|
|
2618
2794
|
print(waveform.shape)
|
|
2619
|
-
torchaudio.save("out.
|
|
2795
|
+
torchaudio.save("out.wav", waveform[0], 22050)
|
|
2620
2796
|
`,
|
|
2621
2797
|
];
|
|
2622
2798
|
|
|
2623
2799
|
export const chattts = (): string[] => [
|
|
2624
2800
|
`import ChatTTS
|
|
2801
|
+
import torch
|
|
2625
2802
|
import torchaudio
|
|
2626
2803
|
|
|
2627
2804
|
chat = ChatTTS.Chat()
|
|
2628
|
-
chat.
|
|
2805
|
+
chat.load(compile=False) # Set to True for better performance
|
|
2629
2806
|
|
|
2630
2807
|
texts = ["PUT YOUR TEXT HERE",]
|
|
2631
2808
|
|
|
@@ -2635,19 +2812,44 @@ torchaudio.save("output1.wav", torch.from_numpy(wavs[0]), 24000)`,
|
|
|
2635
2812
|
];
|
|
2636
2813
|
|
|
2637
2814
|
export const ultralytics = (model: ModelData): string[] => {
|
|
2638
|
-
//
|
|
2639
|
-
|
|
2815
|
+
// the YOLOv10 fork (library "yolov10") ships its own `YOLOv10` class with a `from_pretrained` helper that reads the
|
|
2816
|
+
// model.safetensors it pushes. Mainline ultralytics repos often list "yolov10" among their version tags
|
|
2817
|
+
// (e.g. Ultralytics/YOLOv8) but only ship .pt files, so an ultralytics tag alone keeps them on the mainline loader;
|
|
2818
|
+
// fork pushes still tagged ultralytics are recognized by their safetensors weights.
|
|
2819
|
+
const isYolov10Fork =
|
|
2820
|
+
model.library_name === "yolov10" ||
|
|
2821
|
+
(model.tags.includes("yolov10") && (!model.tags.includes("ultralytics") || model.tags.includes("safetensors")));
|
|
2822
|
+
if (isYolov10Fork) {
|
|
2823
|
+
// from_pretrained reads the model.safetensors that fork pushes ship; older fork repos only ship .pt weights
|
|
2824
|
+
if (model.tags.includes("safetensors")) {
|
|
2825
|
+
return [
|
|
2826
|
+
`from ultralytics import YOLOv10
|
|
2827
|
+
|
|
2828
|
+
model = YOLOv10.from_pretrained("${model.id}")
|
|
2829
|
+
source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
|
|
2830
|
+
model.predict(source=source, save=True)`,
|
|
2831
|
+
];
|
|
2832
|
+
}
|
|
2833
|
+
return [
|
|
2834
|
+
`from huggingface_hub import hf_hub_download
|
|
2835
|
+
from ultralytics import YOLOv10
|
|
2640
2836
|
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2837
|
+
# pick the weights file from this repo's "Files and versions" tab
|
|
2838
|
+
weights = hf_hub_download("${model.id}", "<weights>.pt")
|
|
2839
|
+
model = YOLOv10(weights)
|
|
2840
|
+
source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
|
|
2841
|
+
model.predict(source=source, save=True)`,
|
|
2842
|
+
];
|
|
2843
|
+
}
|
|
2645
2844
|
|
|
2845
|
+
// mainline ultralytics loads weights from a local file
|
|
2646
2846
|
return [
|
|
2647
|
-
|
|
2648
|
-
|
|
2847
|
+
`from huggingface_hub import hf_hub_download
|
|
2848
|
+
from ultralytics import YOLO
|
|
2649
2849
|
|
|
2650
|
-
|
|
2850
|
+
# pick the weights file from this repo's "Files and versions" tab
|
|
2851
|
+
weights = hf_hub_download("${model.id}", "<weights>.pt")
|
|
2852
|
+
model = YOLO(weights)
|
|
2651
2853
|
source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
|
|
2652
2854
|
model.predict(source=source, save=True)`,
|
|
2653
2855
|
];
|
|
@@ -2726,14 +2928,16 @@ model = SwarmFormerModel.from_pretrained("${model.id}")
|
|
|
2726
2928
|
|
|
2727
2929
|
export const univa = (model: ModelData): string[] => [
|
|
2728
2930
|
`# Follow installation instructions at https://github.com/PKU-YuanGroup/UniWorld-V1
|
|
2729
|
-
|
|
2931
|
+
import torch
|
|
2932
|
+
from transformers import AutoProcessor
|
|
2730
2933
|
from univa.models.qwen2p5vl.modeling_univa_qwen2p5vl import UnivaQwen2p5VLForConditionalGeneration
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2934
|
+
|
|
2935
|
+
model = UnivaQwen2p5VLForConditionalGeneration.from_pretrained(
|
|
2936
|
+
"${model.id}",
|
|
2937
|
+
torch_dtype=torch.bfloat16,
|
|
2938
|
+
attn_implementation="flash_attention_2",
|
|
2939
|
+
).to("cuda")
|
|
2940
|
+
processor = AutoProcessor.from_pretrained("${model.id}")
|
|
2737
2941
|
`,
|
|
2738
2942
|
];
|
|
2739
2943
|
|
|
@@ -2741,7 +2945,7 @@ const mlx_unknown = (model: ModelData): string[] => [
|
|
|
2741
2945
|
`# Download the model from the Hub
|
|
2742
2946
|
pip install huggingface_hub[hf_xet]
|
|
2743
2947
|
|
|
2744
|
-
|
|
2948
|
+
hf download ${model.id} --local-dir ${nameWithoutNamespace(model.id)}`,
|
|
2745
2949
|
];
|
|
2746
2950
|
|
|
2747
2951
|
const mlxlm = (model: ModelData): string[] => [
|
|
@@ -2802,10 +3006,11 @@ output = generate(model, processor, formatted_prompt, image)
|
|
|
2802
3006
|
print(output)`,
|
|
2803
3007
|
];
|
|
2804
3008
|
|
|
3009
|
+
// create_model() takes a name from the mlx-image registry, not a repo id; the weights live in "mlx-vision/<name>-mlxim"
|
|
2805
3010
|
export const mlxim = (model: ModelData): string[] => [
|
|
2806
3011
|
`from mlxim.model import create_model
|
|
2807
3012
|
|
|
2808
|
-
model = create_model(${model.id})`,
|
|
3013
|
+
model = create_model("${nameWithoutNamespace(model.id).replace(/-mlxim$/, "")}")`,
|
|
2809
3014
|
];
|
|
2810
3015
|
|
|
2811
3016
|
export const mlx = (model: ModelData): string[] => {
|
|
@@ -2825,7 +3030,10 @@ export const mlx = (model: ModelData): string[] => {
|
|
|
2825
3030
|
export const model2vec = (model: ModelData): string[] => [
|
|
2826
3031
|
`from model2vec import StaticModel
|
|
2827
3032
|
|
|
2828
|
-
model = StaticModel.from_pretrained("${model.id}")
|
|
3033
|
+
model = StaticModel.from_pretrained("${model.id}")
|
|
3034
|
+
|
|
3035
|
+
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
|
|
3036
|
+
print(embeddings.shape)`,
|
|
2829
3037
|
];
|
|
2830
3038
|
|
|
2831
3039
|
export const mobilint = (model: ModelData): string[] => {
|
|
@@ -2871,8 +3079,9 @@ export const pruna = (model: ModelData): string[] => {
|
|
|
2871
3079
|
snippets = snippets.map(ensurePrunaModelImport);
|
|
2872
3080
|
|
|
2873
3081
|
if (model.tags.includes("pruna_pro-ai")) {
|
|
3082
|
+
// only rewrite the import and the class name, never the repo id (an org name can contain "pruna")
|
|
2874
3083
|
return snippets.map((snippet) =>
|
|
2875
|
-
snippet.replace(
|
|
3084
|
+
snippet.replace(/^from pruna import /gm, "from pruna_pro import ").replace(/\bPrunaModel\b/g, "PrunaProModel"),
|
|
2876
3085
|
);
|
|
2877
3086
|
}
|
|
2878
3087
|
|
|
@@ -2951,21 +3160,19 @@ export const outetts = (model: ModelData): string[] => {
|
|
|
2951
3160
|
|
|
2952
3161
|
// v1.0 HF → minimal runnable snippet
|
|
2953
3162
|
return [
|
|
2954
|
-
`
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
).save("output.wav")
|
|
2968
|
-
`,
|
|
3163
|
+
`import outetts
|
|
3164
|
+
|
|
3165
|
+
enum = outetts.Models("${model.id}".split("/", 1)[1]) # VERSION_1_0_SIZE_1B
|
|
3166
|
+
cfg = outetts.ModelConfig.auto_config(enum, outetts.Backend.HF)
|
|
3167
|
+
tts = outetts.Interface(cfg)
|
|
3168
|
+
|
|
3169
|
+
speaker = tts.load_default_speaker("EN-FEMALE-1-NEUTRAL")
|
|
3170
|
+
tts.generate(
|
|
3171
|
+
outetts.GenerationConfig(
|
|
3172
|
+
text="Hello there, how are you doing?",
|
|
3173
|
+
speaker=speaker,
|
|
3174
|
+
)
|
|
3175
|
+
).save("output.wav")`,
|
|
2969
3176
|
];
|
|
2970
3177
|
};
|
|
2971
3178
|
|