@huggingface/tasks 0.21.53 → 0.21.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +3 -1
- package/dist/commonjs/model-libraries-snippets.d.ts.map +1 -1
- package/dist/commonjs/model-libraries-snippets.js +345 -150
- package/dist/commonjs/model-libraries-snippets.spec.js +300 -0
- package/dist/commonjs/model-libraries.d.ts +18 -2
- package/dist/commonjs/model-libraries.d.ts.map +1 -1
- package/dist/commonjs/model-libraries.js +17 -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 +3 -1
- package/dist/esm/model-libraries-snippets.d.ts.map +1 -1
- package/dist/esm/model-libraries-snippets.js +340 -147
- package/dist/esm/model-libraries-snippets.spec.js +301 -1
- package/dist/esm/model-libraries.d.ts +18 -2
- package/dist/esm/model-libraries.d.ts.map +1 -1
- package/dist/esm/model-libraries.js +17 -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 +360 -145
- package/src/model-libraries.ts +17 -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[] => [
|
|
@@ -855,16 +873,31 @@ print(f'Accuracy {accuracy_score(y_test, predictions):.2%}')
|
|
|
855
873
|
return [installSnippet, classificationSnippet, regressionsSnippet, matchingSnippet];
|
|
856
874
|
};
|
|
857
875
|
|
|
876
|
+
export const gaussianformer = (model: ModelData): string[] => [
|
|
877
|
+
`# Install from https://github.com/SVLwoof/gaussianformer
|
|
878
|
+
|
|
879
|
+
from gaussianformer.pipelines.rendering_pipeline import GaussianFormerRenderingPipeline
|
|
880
|
+
|
|
881
|
+
pipeline = GaussianFormerRenderingPipeline.from_pretrained("${model.id}")`,
|
|
882
|
+
];
|
|
883
|
+
|
|
858
884
|
export const gliner = (model: ModelData): string[] => [
|
|
859
885
|
`from gliner import GLiNER
|
|
860
886
|
|
|
861
|
-
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"])`,
|
|
862
895
|
];
|
|
863
896
|
|
|
864
897
|
export const gliner2 = (model: ModelData): string[] => [
|
|
865
|
-
`from gliner2 import
|
|
898
|
+
`from gliner2 import AutoExtractor
|
|
866
899
|
|
|
867
|
-
|
|
900
|
+
extractor = AutoExtractor.from_pretrained("${model.id}")
|
|
868
901
|
|
|
869
902
|
# Extract entities
|
|
870
903
|
text = "Apple CEO Tim Cook announced iPhone 15 in Cupertino yesterday."
|
|
@@ -877,7 +910,7 @@ export const indextts = (model: ModelData): string[] => [
|
|
|
877
910
|
`# Download model
|
|
878
911
|
from huggingface_hub import snapshot_download
|
|
879
912
|
|
|
880
|
-
snapshot_download(${model.id}, local_dir="checkpoints")
|
|
913
|
+
snapshot_download("${model.id}", local_dir="checkpoints")
|
|
881
914
|
|
|
882
915
|
from indextts.infer import IndexTTS
|
|
883
916
|
|
|
@@ -933,9 +966,11 @@ print(out["answers"]["team"]["choice"])`,
|
|
|
933
966
|
};
|
|
934
967
|
|
|
935
968
|
export const keras = (model: ModelData): string[] => [
|
|
936
|
-
`#
|
|
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.
|
|
937
972
|
import os
|
|
938
|
-
os.environ["KERAS_BACKEND"] = "
|
|
973
|
+
os.environ["KERAS_BACKEND"] = "tensorflow"
|
|
939
974
|
|
|
940
975
|
import keras
|
|
941
976
|
|
|
@@ -1062,13 +1097,15 @@ export const kernels = (model: ModelData): string[] => [
|
|
|
1062
1097
|
|
|
1063
1098
|
from kernels import get_kernel
|
|
1064
1099
|
|
|
1065
|
-
|
|
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)`,
|
|
1066
1102
|
];
|
|
1067
1103
|
|
|
1068
1104
|
export const kimi_audio = (model: ModelData): string[] => [
|
|
1069
1105
|
`# Example usage for KimiAudio
|
|
1070
|
-
# pip install git+https://github.com/MoonshotAI/Kimi-Audio.git
|
|
1106
|
+
# pip install git+https://github.com/MoonshotAI/Kimi-Audio.git "transformers<5"
|
|
1071
1107
|
|
|
1108
|
+
import soundfile as sf
|
|
1072
1109
|
from kimia_infer.api.kimia import KimiAudio
|
|
1073
1110
|
|
|
1074
1111
|
model = KimiAudio(model_path="${model.id}", load_detokenizer=True)
|
|
@@ -1509,14 +1546,17 @@ pip install -U litert-lm
|
|
|
1509
1546
|
|
|
1510
1547
|
# 2. Download and run this model locally:
|
|
1511
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).
|
|
1512
1551
|
litert-lm run \\
|
|
1513
1552
|
--from-huggingface-repo=${model.id} \\
|
|
1514
1553
|
--prompt="Write me a poem"`,
|
|
1515
1554
|
];
|
|
1516
1555
|
|
|
1517
1556
|
export const tf_keras = (model: ModelData): string[] => [
|
|
1518
|
-
`# 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.
|
|
1519
1558
|
# See https://github.com/keras-team/tf-keras for more details.
|
|
1559
|
+
# !pip install "huggingface_hub<1.0" tf_keras
|
|
1520
1560
|
from huggingface_hub import from_pretrained_keras
|
|
1521
1561
|
|
|
1522
1562
|
model = from_pretrained_keras("${model.id}")
|
|
@@ -1570,6 +1610,19 @@ from MeshAnything.models.meshanything import MeshAnything
|
|
|
1570
1610
|
model = MeshAnything(args)`,
|
|
1571
1611
|
];
|
|
1572
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
|
+
|
|
1573
1626
|
export const multimolecule = (model: ModelData): string[] => {
|
|
1574
1627
|
const widgetExample = model.widgetData?.[0] as WidgetExampleTextInput | undefined;
|
|
1575
1628
|
const exampleText = escapeStringForJson(widgetExample?.text ?? "");
|
|
@@ -1763,14 +1816,18 @@ model = pe.VisionTransformer.from_config("${model.id}", pretrained=True)`;
|
|
|
1763
1816
|
};
|
|
1764
1817
|
export const phantom_wan = (model: ModelData): string[] => [
|
|
1765
1818
|
`from huggingface_hub import snapshot_download
|
|
1766
|
-
from phantom_wan import
|
|
1819
|
+
from phantom_wan import WanI2V, configs
|
|
1820
|
+
from PIL import Image
|
|
1767
1821
|
|
|
1768
1822
|
checkpoint_dir = snapshot_download("${model.id}")
|
|
1769
1823
|
wan_i2v = WanI2V(
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
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)`,
|
|
1774
1831
|
];
|
|
1775
1832
|
|
|
1776
1833
|
export const pocket_tts = (model: ModelData): string[] => [
|
|
@@ -1895,7 +1952,7 @@ export const tensorflowtts = (model: ModelData): string[] => {
|
|
|
1895
1952
|
export const timm = (model: ModelData): string[] => [
|
|
1896
1953
|
`import timm
|
|
1897
1954
|
|
|
1898
|
-
model = timm.create_model("
|
|
1955
|
+
model = timm.create_model("hf-hub:${model.id}", pretrained=True)`,
|
|
1899
1956
|
];
|
|
1900
1957
|
|
|
1901
1958
|
export const saelens = (/* model: ModelData */): string[] => [
|
|
@@ -1923,13 +1980,15 @@ seed_story_cfg = OmegaConf.load(seed_story_cfg_path)
|
|
|
1923
1980
|
seed_story = hydra.utils.instantiate(seed_story_cfg, llm=llm) `,
|
|
1924
1981
|
];
|
|
1925
1982
|
|
|
1926
|
-
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) => {
|
|
1927
1987
|
return [
|
|
1928
|
-
`import
|
|
1929
|
-
|
|
1930
|
-
download("${model.id}", "path_to_folder")
|
|
1988
|
+
`from huggingface_hub import hf_hub_download
|
|
1989
|
+
import joblib
|
|
1931
1990
|
model = joblib.load(
|
|
1932
|
-
"${escapeStringForJson(modelFile)}"
|
|
1991
|
+
hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}")
|
|
1933
1992
|
)
|
|
1934
1993
|
# only load pickle files from sources you trust
|
|
1935
1994
|
# read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
|
|
@@ -1938,42 +1997,32 @@ model = joblib.load(
|
|
|
1938
1997
|
|
|
1939
1998
|
const skopsFormat = (model: ModelData, modelFile: string) => {
|
|
1940
1999
|
return [
|
|
1941
|
-
`from
|
|
2000
|
+
`from huggingface_hub import hf_hub_download
|
|
1942
2001
|
from skops.io import load
|
|
1943
|
-
download("${model.id}", "path_to_folder")
|
|
1944
2002
|
# make sure model file is in skops format
|
|
1945
2003
|
# if model is a pickle file, make sure it's from a source you trust
|
|
1946
|
-
model = load("
|
|
1947
|
-
];
|
|
1948
|
-
};
|
|
1949
|
-
|
|
1950
|
-
const skopsJobLib = (model: ModelData) => {
|
|
1951
|
-
return [
|
|
1952
|
-
`from huggingface_hub import hf_hub_download
|
|
1953
|
-
import joblib
|
|
1954
|
-
model = joblib.load(
|
|
1955
|
-
hf_hub_download("${model.id}", "sklearn_model.joblib")
|
|
1956
|
-
)
|
|
1957
|
-
# only load pickle files from sources you trust
|
|
1958
|
-
# read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
|
|
2004
|
+
model = load(hf_hub_download("${model.id}", "${escapeStringForJson(modelFile)}"))`,
|
|
1959
2005
|
];
|
|
1960
2006
|
};
|
|
1961
2007
|
|
|
1962
2008
|
export const sklearn = (model: ModelData): string[] => {
|
|
2009
|
+
const declaredFile = model.config?.sklearn?.model?.file;
|
|
1963
2010
|
if (model.tags.includes("skops")) {
|
|
1964
|
-
|
|
1965
|
-
const skopssaveFormat = model.config?.sklearn?.model_format;
|
|
1966
|
-
if (!skopsmodelFile) {
|
|
2011
|
+
if (!declaredFile) {
|
|
1967
2012
|
return [`# ⚠️ Model filename not specified in config.json`];
|
|
1968
2013
|
}
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
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);
|
|
1973
2017
|
}
|
|
1974
|
-
|
|
1975
|
-
return skopsJobLib(model);
|
|
2018
|
+
return skopsFormat(model, declaredFile);
|
|
1976
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
|
+
);
|
|
1977
2026
|
};
|
|
1978
2027
|
|
|
1979
2028
|
export const stable_audio_tools = (model: ModelData): string[] => [
|
|
@@ -2024,7 +2073,7 @@ export const sam2 = (model: ModelData): string[] => {
|
|
|
2024
2073
|
import torch
|
|
2025
2074
|
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
|
2026
2075
|
|
|
2027
|
-
predictor = SAM2ImagePredictor.from_pretrained(${model.id})
|
|
2076
|
+
predictor = SAM2ImagePredictor.from_pretrained("${model.id}")
|
|
2028
2077
|
|
|
2029
2078
|
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
|
|
2030
2079
|
predictor.set_image(<your_image>)
|
|
@@ -2034,13 +2083,13 @@ with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
|
|
|
2034
2083
|
import torch
|
|
2035
2084
|
from sam2.sam2_video_predictor import SAM2VideoPredictor
|
|
2036
2085
|
|
|
2037
|
-
predictor = SAM2VideoPredictor.from_pretrained(${model.id})
|
|
2086
|
+
predictor = SAM2VideoPredictor.from_pretrained("${model.id}")
|
|
2038
2087
|
|
|
2039
2088
|
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
|
|
2040
2089
|
state = predictor.init_state(<your_video>)
|
|
2041
2090
|
|
|
2042
2091
|
# add new prompts and instantly get the output on the same frame
|
|
2043
|
-
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>)
|
|
2044
2093
|
|
|
2045
2094
|
# propagate the prompts to get masklets throughout the video
|
|
2046
2095
|
for frame_idx, object_ids, masks in predictor.propagate_in_video(state):
|
|
@@ -2062,11 +2111,18 @@ output = inference(image, mask)`,
|
|
|
2062
2111
|
];
|
|
2063
2112
|
|
|
2064
2113
|
export const sam_3d_body = (model: ModelData): string[] => [
|
|
2065
|
-
`
|
|
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
|
|
2066
2118
|
|
|
2067
|
-
estimator = setup_sam_3d_body(${model.id})
|
|
2068
|
-
|
|
2069
|
-
|
|
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))
|
|
2123
|
+
|
|
2124
|
+
rend_img = visualize_sample_together(img_bgr, outputs, estimator.faces)
|
|
2125
|
+
cv2.imwrite("output.jpg", rend_img.astype(np.uint8))`,
|
|
2070
2126
|
];
|
|
2071
2127
|
|
|
2072
2128
|
export const sampleFactory = (model: ModelData): string[] => [
|
|
@@ -2163,7 +2219,17 @@ docs_emb = model.encode(documents, is_query=False)`,
|
|
|
2163
2219
|
];
|
|
2164
2220
|
}
|
|
2165
2221
|
|
|
2166
|
-
|
|
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) {
|
|
2167
2233
|
return [
|
|
2168
2234
|
`from sentence_transformers import CrossEncoder
|
|
2169
2235
|
|
|
@@ -2182,6 +2248,34 @@ print(scores)`,
|
|
|
2182
2248
|
];
|
|
2183
2249
|
}
|
|
2184
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
|
+
|
|
2185
2279
|
const exampleSentences = get_widget_examples_from_st_model(model) ?? [
|
|
2186
2280
|
"The weather is lovely today.",
|
|
2187
2281
|
"It's so sunny outside!",
|
|
@@ -2205,7 +2299,10 @@ print(similarities.shape)
|
|
|
2205
2299
|
export const setfit = (model: ModelData): string[] => [
|
|
2206
2300
|
`from setfit import SetFitModel
|
|
2207
2301
|
|
|
2208
|
-
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)`,
|
|
2209
2306
|
];
|
|
2210
2307
|
|
|
2211
2308
|
export const spacy = (model: ModelData): string[] => [
|
|
@@ -2223,7 +2320,10 @@ nlp = ${nameWithoutNamespace(model.id)}.load()`,
|
|
|
2223
2320
|
export const span_marker = (model: ModelData): string[] => [
|
|
2224
2321
|
`from span_marker import SpanMarkerModel
|
|
2225
2322
|
|
|
2226
|
-
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)`,
|
|
2227
2327
|
];
|
|
2228
2328
|
|
|
2229
2329
|
export const stanza = (model: ModelData): string[] => [
|
|
@@ -2244,6 +2344,8 @@ const speechBrainMethod = (speechbrainInterface: string) => {
|
|
|
2244
2344
|
return "enhance_file";
|
|
2245
2345
|
case "SepformerSeparation":
|
|
2246
2346
|
return "separate_file";
|
|
2347
|
+
case "SpeakerRecognition":
|
|
2348
|
+
return "verify_files";
|
|
2247
2349
|
default:
|
|
2248
2350
|
return undefined;
|
|
2249
2351
|
}
|
|
@@ -2260,12 +2362,14 @@ export const speechbrain = (model: ModelData): string[] => {
|
|
|
2260
2362
|
return [`# interface in config.json invalid`];
|
|
2261
2363
|
}
|
|
2262
2364
|
|
|
2365
|
+
// speaker verification compares two recordings
|
|
2366
|
+
const methodArgs = speechbrainMethod === "verify_files" ? '"speaker1.wav", "speaker2.wav"' : '"file.wav"';
|
|
2263
2367
|
return [
|
|
2264
2368
|
`from speechbrain.pretrained import ${speechbrainInterface}
|
|
2265
2369
|
model = ${speechbrainInterface}.from_hparams(
|
|
2266
2370
|
"${model.id}"
|
|
2267
2371
|
)
|
|
2268
|
-
model.${speechbrainMethod}(
|
|
2372
|
+
model.${speechbrainMethod}(${methodArgs})`,
|
|
2269
2373
|
];
|
|
2270
2374
|
};
|
|
2271
2375
|
|
|
@@ -2303,6 +2407,7 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2303
2407
|
? "extractor"
|
|
2304
2408
|
: "processor";
|
|
2305
2409
|
autoSnippet.push(
|
|
2410
|
+
"# pip install -U transformers accelerate",
|
|
2306
2411
|
"# Load model directly",
|
|
2307
2412
|
`from transformers import ${info.processor}, ${auto_model}`,
|
|
2308
2413
|
"",
|
|
@@ -2336,12 +2441,13 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2336
2441
|
' return_tensors="pt",',
|
|
2337
2442
|
").to(model.device)",
|
|
2338
2443
|
"",
|
|
2339
|
-
"outputs = model.generate(**inputs, max_new_tokens=
|
|
2444
|
+
"outputs = model.generate(**inputs, max_new_tokens=256)",
|
|
2340
2445
|
`print(${processorVarName}.decode(outputs[0][inputs["input_ids"].shape[-1]:]))`,
|
|
2341
2446
|
);
|
|
2342
2447
|
}
|
|
2343
2448
|
} else {
|
|
2344
2449
|
autoSnippet.push(
|
|
2450
|
+
"# pip install -U transformers accelerate",
|
|
2345
2451
|
"# Load model directly",
|
|
2346
2452
|
`from transformers import ${auto_model}`,
|
|
2347
2453
|
`model = ${auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ', device_map="auto")',
|
|
@@ -2354,7 +2460,7 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2354
2460
|
pipelineSnippet.push(
|
|
2355
2461
|
`# Warning: Pipeline type "${model.pipeline_tag}" is no longer supported in transformers v5.`,
|
|
2356
2462
|
`# You must load the model directly (see below) or downgrade to v4.x with:`,
|
|
2357
|
-
`#
|
|
2463
|
+
`# pip install "transformers<5.0.0"`,
|
|
2358
2464
|
);
|
|
2359
2465
|
}
|
|
2360
2466
|
|
|
@@ -2364,7 +2470,7 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2364
2470
|
`pipe = pipeline("${model.pipeline_tag}", model="${model.id}"` + remote_code_snippet + ")",
|
|
2365
2471
|
);
|
|
2366
2472
|
|
|
2367
|
-
if (model.tags.includes("conversational")) {
|
|
2473
|
+
if (model.tags.includes("conversational") && hasChatTemplate(model)) {
|
|
2368
2474
|
if (model.tags.includes("image-text-to-text")) {
|
|
2369
2475
|
pipelineSnippet.push(
|
|
2370
2476
|
"messages = [",
|
|
@@ -2402,6 +2508,11 @@ export const transformers = (model: ModelData): string[] => {
|
|
|
2402
2508
|
return [autoSnippet.join("\n")];
|
|
2403
2509
|
};
|
|
2404
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
|
+
|
|
2405
2516
|
export const transformersJS = (model: ModelData): string[] => {
|
|
2406
2517
|
if (!model.pipeline_tag) {
|
|
2407
2518
|
return [`// ⚠️ Unknown pipeline tag`];
|
|
@@ -2409,45 +2520,119 @@ export const transformersJS = (model: ModelData): string[] => {
|
|
|
2409
2520
|
|
|
2410
2521
|
const libName = "@huggingface/transformers";
|
|
2411
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
|
+
|
|
2412
2547
|
return [
|
|
2413
2548
|
`// npm i ${libName}
|
|
2414
2549
|
import { pipeline } from '${libName}';
|
|
2415
2550
|
|
|
2416
2551
|
// Allocate pipeline
|
|
2417
|
-
const pipe = await pipeline('${
|
|
2552
|
+
const pipe = await pipeline('${task}', '${model.id}');`,
|
|
2418
2553
|
];
|
|
2419
2554
|
};
|
|
2420
2555
|
|
|
2421
|
-
const
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
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";
|
|
2433
2589
|
}
|
|
2590
|
+
if (PEFT_SEAMLESS_M4T_BASE_MODEL.test(baseModel)) {
|
|
2591
|
+
return PEFT_TEXT_TASKS.some((task) => declaresTask(model, task)) ? undefined : "AutoModelForSpeechSeq2Seq";
|
|
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;
|
|
2434
2598
|
};
|
|
2435
2599
|
|
|
2436
2600
|
export const peft = (model: ModelData): string[] => {
|
|
2437
2601
|
const { base_model_name_or_path: peftBaseModel, task_type: peftTaskType } = model.config?.peft ?? {};
|
|
2438
|
-
|
|
2439
|
-
if (!
|
|
2602
|
+
let autoClass = peftTaskType ? PEFT_TASK_TYPE_TO_AUTO_CLASS[peftTaskType] : undefined;
|
|
2603
|
+
if (!autoClass) {
|
|
2440
2604
|
return [`Task type is invalid.`];
|
|
2441
2605
|
}
|
|
2442
|
-
|
|
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) {
|
|
2443
2612
|
return [`Base model is not found.`];
|
|
2444
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
|
+
}
|
|
2445
2630
|
|
|
2446
2631
|
return [
|
|
2447
2632
|
`from peft import PeftModel
|
|
2448
|
-
from transformers import
|
|
2633
|
+
from transformers import ${autoClass}
|
|
2449
2634
|
|
|
2450
|
-
base_model =
|
|
2635
|
+
base_model = ${autoClass}.from_pretrained("${escapeStringForJson(baseModel)}")
|
|
2451
2636
|
model = PeftModel.from_pretrained(base_model, "${model.id}")`,
|
|
2452
2637
|
];
|
|
2453
2638
|
};
|
|
@@ -2482,7 +2667,7 @@ transcriptions = asr_model.transcribe(["file.wav"])`,
|
|
|
2482
2667
|
};
|
|
2483
2668
|
|
|
2484
2669
|
export const mlAgents = (model: ModelData): string[] => [
|
|
2485
|
-
`mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./
|
|
2670
|
+
`mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./downloads"`,
|
|
2486
2671
|
];
|
|
2487
2672
|
|
|
2488
2673
|
export const sentis = (/* model: ModelData */): string[] => [
|
|
@@ -2557,10 +2742,10 @@ model = Model.from_pretrained("${model.id}")`,
|
|
|
2557
2742
|
|
|
2558
2743
|
export const lvface = (model: ModelData): string[] => [
|
|
2559
2744
|
`from huggingface_hub import hf_hub_download
|
|
2560
|
-
|
|
2745
|
+
from inference_onnx import LVFaceONNXInferencer
|
|
2561
2746
|
|
|
2562
2747
|
model_path = hf_hub_download("${model.id}", "LVFace-L_Glint360K/LVFace-L_Glint360K.onnx")
|
|
2563
|
-
inferencer = LVFaceONNXInferencer(model_path, use_gpu=True
|
|
2748
|
+
inferencer = LVFaceONNXInferencer(model_path, use_gpu=True)
|
|
2564
2749
|
img_path = 'path/to/image1.jpg'
|
|
2565
2750
|
embedding = inferencer.infer_from_image(img_path)`,
|
|
2566
2751
|
];
|
|
@@ -2599,8 +2784,7 @@ export const vui = (): string[] => [
|
|
|
2599
2784
|
|
|
2600
2785
|
import torchaudio
|
|
2601
2786
|
|
|
2602
|
-
from vui.
|
|
2603
|
-
from vui.model import Vui,
|
|
2787
|
+
from vui.legacy import Vui, render
|
|
2604
2788
|
|
|
2605
2789
|
model = Vui.from_pretrained().cuda()
|
|
2606
2790
|
waveform = render(
|
|
@@ -2608,16 +2792,17 @@ waveform = render(
|
|
|
2608
2792
|
"Hey, here is some random stuff, usually something quite long as the shorter the text the less likely the model can cope!",
|
|
2609
2793
|
)
|
|
2610
2794
|
print(waveform.shape)
|
|
2611
|
-
torchaudio.save("out.
|
|
2795
|
+
torchaudio.save("out.wav", waveform[0], 22050)
|
|
2612
2796
|
`,
|
|
2613
2797
|
];
|
|
2614
2798
|
|
|
2615
2799
|
export const chattts = (): string[] => [
|
|
2616
2800
|
`import ChatTTS
|
|
2801
|
+
import torch
|
|
2617
2802
|
import torchaudio
|
|
2618
2803
|
|
|
2619
2804
|
chat = ChatTTS.Chat()
|
|
2620
|
-
chat.
|
|
2805
|
+
chat.load(compile=False) # Set to True for better performance
|
|
2621
2806
|
|
|
2622
2807
|
texts = ["PUT YOUR TEXT HERE",]
|
|
2623
2808
|
|
|
@@ -2627,19 +2812,44 @@ torchaudio.save("output1.wav", torch.from_numpy(wavs[0]), 24000)`,
|
|
|
2627
2812
|
];
|
|
2628
2813
|
|
|
2629
2814
|
export const ultralytics = (model: ModelData): string[] => {
|
|
2630
|
-
//
|
|
2631
|
-
|
|
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
|
|
2632
2827
|
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
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
|
|
2836
|
+
|
|
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
|
+
}
|
|
2637
2844
|
|
|
2845
|
+
// mainline ultralytics loads weights from a local file
|
|
2638
2846
|
return [
|
|
2639
|
-
|
|
2640
|
-
|
|
2847
|
+
`from huggingface_hub import hf_hub_download
|
|
2848
|
+
from ultralytics import YOLO
|
|
2641
2849
|
|
|
2642
|
-
|
|
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)
|
|
2643
2853
|
source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
|
|
2644
2854
|
model.predict(source=source, save=True)`,
|
|
2645
2855
|
];
|
|
@@ -2718,14 +2928,16 @@ model = SwarmFormerModel.from_pretrained("${model.id}")
|
|
|
2718
2928
|
|
|
2719
2929
|
export const univa = (model: ModelData): string[] => [
|
|
2720
2930
|
`# Follow installation instructions at https://github.com/PKU-YuanGroup/UniWorld-V1
|
|
2721
|
-
|
|
2931
|
+
import torch
|
|
2932
|
+
from transformers import AutoProcessor
|
|
2722
2933
|
from univa.models.qwen2p5vl.modeling_univa_qwen2p5vl import UnivaQwen2p5VLForConditionalGeneration
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
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}")
|
|
2729
2941
|
`,
|
|
2730
2942
|
];
|
|
2731
2943
|
|
|
@@ -2733,7 +2945,7 @@ const mlx_unknown = (model: ModelData): string[] => [
|
|
|
2733
2945
|
`# Download the model from the Hub
|
|
2734
2946
|
pip install huggingface_hub[hf_xet]
|
|
2735
2947
|
|
|
2736
|
-
|
|
2948
|
+
hf download ${model.id} --local-dir ${nameWithoutNamespace(model.id)}`,
|
|
2737
2949
|
];
|
|
2738
2950
|
|
|
2739
2951
|
const mlxlm = (model: ModelData): string[] => [
|
|
@@ -2794,10 +3006,11 @@ output = generate(model, processor, formatted_prompt, image)
|
|
|
2794
3006
|
print(output)`,
|
|
2795
3007
|
];
|
|
2796
3008
|
|
|
3009
|
+
// create_model() takes a name from the mlx-image registry, not a repo id; the weights live in "mlx-vision/<name>-mlxim"
|
|
2797
3010
|
export const mlxim = (model: ModelData): string[] => [
|
|
2798
3011
|
`from mlxim.model import create_model
|
|
2799
3012
|
|
|
2800
|
-
model = create_model(${model.id})`,
|
|
3013
|
+
model = create_model("${nameWithoutNamespace(model.id).replace(/-mlxim$/, "")}")`,
|
|
2801
3014
|
];
|
|
2802
3015
|
|
|
2803
3016
|
export const mlx = (model: ModelData): string[] => {
|
|
@@ -2817,7 +3030,10 @@ export const mlx = (model: ModelData): string[] => {
|
|
|
2817
3030
|
export const model2vec = (model: ModelData): string[] => [
|
|
2818
3031
|
`from model2vec import StaticModel
|
|
2819
3032
|
|
|
2820
|
-
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)`,
|
|
2821
3037
|
];
|
|
2822
3038
|
|
|
2823
3039
|
export const mobilint = (model: ModelData): string[] => {
|
|
@@ -2863,8 +3079,9 @@ export const pruna = (model: ModelData): string[] => {
|
|
|
2863
3079
|
snippets = snippets.map(ensurePrunaModelImport);
|
|
2864
3080
|
|
|
2865
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")
|
|
2866
3083
|
return snippets.map((snippet) =>
|
|
2867
|
-
snippet.replace(
|
|
3084
|
+
snippet.replace(/^from pruna import /gm, "from pruna_pro import ").replace(/\bPrunaModel\b/g, "PrunaProModel"),
|
|
2868
3085
|
);
|
|
2869
3086
|
}
|
|
2870
3087
|
|
|
@@ -2943,21 +3160,19 @@ export const outetts = (model: ModelData): string[] => {
|
|
|
2943
3160
|
|
|
2944
3161
|
// v1.0 HF → minimal runnable snippet
|
|
2945
3162
|
return [
|
|
2946
|
-
`
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
).save("output.wav")
|
|
2960
|
-
`,
|
|
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")`,
|
|
2961
3176
|
];
|
|
2962
3177
|
};
|
|
2963
3178
|
|