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