@huggingface/tasks 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/README.md +16 -2
  2. package/dist/index.d.ts +358 -5
  3. package/dist/index.js +1906 -72
  4. package/dist/index.mjs +1905 -71
  5. package/package.json +1 -1
  6. package/src/default-widget-inputs.ts +718 -0
  7. package/src/index.ts +35 -4
  8. package/src/library-to-tasks.ts +47 -0
  9. package/src/library-ui-elements.ts +765 -0
  10. package/src/model-data.ts +239 -0
  11. package/src/pipelines.ts +15 -0
  12. package/src/snippets/curl.ts +63 -0
  13. package/src/snippets/index.ts +6 -0
  14. package/src/snippets/inputs.ts +129 -0
  15. package/src/snippets/js.ts +150 -0
  16. package/src/snippets/python.ts +114 -0
  17. package/src/{audio-classification → tasks/audio-classification}/about.md +2 -1
  18. package/src/{audio-classification → tasks/audio-classification}/data.ts +3 -3
  19. package/src/{audio-to-audio → tasks/audio-to-audio}/data.ts +1 -1
  20. package/src/{automatic-speech-recognition → tasks/automatic-speech-recognition}/about.md +3 -2
  21. package/src/{automatic-speech-recognition → tasks/automatic-speech-recognition}/data.ts +6 -6
  22. package/src/{conversational → tasks/conversational}/data.ts +1 -1
  23. package/src/{depth-estimation → tasks/depth-estimation}/data.ts +1 -1
  24. package/src/{document-question-answering → tasks/document-question-answering}/data.ts +1 -1
  25. package/src/{feature-extraction → tasks/feature-extraction}/data.ts +2 -7
  26. package/src/{fill-mask → tasks/fill-mask}/data.ts +1 -1
  27. package/src/{image-classification → tasks/image-classification}/data.ts +1 -1
  28. package/src/{image-segmentation → tasks/image-segmentation}/data.ts +1 -1
  29. package/src/{image-to-image → tasks/image-to-image}/about.md +8 -7
  30. package/src/{image-to-image → tasks/image-to-image}/data.ts +1 -1
  31. package/src/{image-to-text → tasks/image-to-text}/data.ts +1 -1
  32. package/src/{tasksData.ts → tasks/index.ts} +140 -15
  33. package/src/{object-detection → tasks/object-detection}/data.ts +1 -1
  34. package/src/{placeholder → tasks/placeholder}/data.ts +1 -1
  35. package/src/{question-answering → tasks/question-answering}/data.ts +1 -1
  36. package/src/{reinforcement-learning → tasks/reinforcement-learning}/data.ts +1 -1
  37. package/src/{sentence-similarity → tasks/sentence-similarity}/data.ts +1 -1
  38. package/src/{summarization → tasks/summarization}/data.ts +1 -1
  39. package/src/{table-question-answering → tasks/table-question-answering}/data.ts +1 -1
  40. package/src/{tabular-classification → tasks/tabular-classification}/data.ts +1 -1
  41. package/src/{tabular-regression → tasks/tabular-regression}/data.ts +1 -1
  42. package/src/{text-classification → tasks/text-classification}/data.ts +1 -1
  43. package/src/{text-generation → tasks/text-generation}/about.md +3 -3
  44. package/src/{text-generation → tasks/text-generation}/data.ts +2 -2
  45. package/src/{text-to-image → tasks/text-to-image}/data.ts +1 -1
  46. package/src/{text-to-speech → tasks/text-to-speech}/about.md +2 -1
  47. package/src/{text-to-speech → tasks/text-to-speech}/data.ts +4 -4
  48. package/src/{text-to-video → tasks/text-to-video}/data.ts +1 -1
  49. package/src/{token-classification → tasks/token-classification}/data.ts +1 -1
  50. package/src/{translation → tasks/translation}/data.ts +1 -1
  51. package/src/{unconditional-image-generation → tasks/unconditional-image-generation}/data.ts +1 -1
  52. package/src/{video-classification → tasks/video-classification}/about.md +8 -28
  53. package/src/{video-classification → tasks/video-classification}/data.ts +1 -1
  54. package/src/{visual-question-answering → tasks/visual-question-answering}/data.ts +1 -1
  55. package/src/{zero-shot-classification → tasks/zero-shot-classification}/data.ts +1 -1
  56. package/src/{zero-shot-image-classification → tasks/zero-shot-image-classification}/data.ts +1 -1
  57. package/src/Types.ts +0 -64
  58. package/src/const.ts +0 -59
  59. /package/src/{modelLibraries.ts → model-libraries.ts} +0 -0
  60. /package/src/{audio-to-audio → tasks/audio-to-audio}/about.md +0 -0
  61. /package/src/{conversational → tasks/conversational}/about.md +0 -0
  62. /package/src/{depth-estimation → tasks/depth-estimation}/about.md +0 -0
  63. /package/src/{document-question-answering → tasks/document-question-answering}/about.md +0 -0
  64. /package/src/{feature-extraction → tasks/feature-extraction}/about.md +0 -0
  65. /package/src/{fill-mask → tasks/fill-mask}/about.md +0 -0
  66. /package/src/{image-classification → tasks/image-classification}/about.md +0 -0
  67. /package/src/{image-segmentation → tasks/image-segmentation}/about.md +0 -0
  68. /package/src/{image-to-text → tasks/image-to-text}/about.md +0 -0
  69. /package/src/{object-detection → tasks/object-detection}/about.md +0 -0
  70. /package/src/{placeholder → tasks/placeholder}/about.md +0 -0
  71. /package/src/{question-answering → tasks/question-answering}/about.md +0 -0
  72. /package/src/{reinforcement-learning → tasks/reinforcement-learning}/about.md +0 -0
  73. /package/src/{sentence-similarity → tasks/sentence-similarity}/about.md +0 -0
  74. /package/src/{summarization → tasks/summarization}/about.md +0 -0
  75. /package/src/{table-question-answering → tasks/table-question-answering}/about.md +0 -0
  76. /package/src/{tabular-classification → tasks/tabular-classification}/about.md +0 -0
  77. /package/src/{tabular-regression → tasks/tabular-regression}/about.md +0 -0
  78. /package/src/{text-classification → tasks/text-classification}/about.md +0 -0
  79. /package/src/{text-to-image → tasks/text-to-image}/about.md +0 -0
  80. /package/src/{text-to-video → tasks/text-to-video}/about.md +0 -0
  81. /package/src/{token-classification → tasks/token-classification}/about.md +0 -0
  82. /package/src/{translation → tasks/translation}/about.md +0 -0
  83. /package/src/{unconditional-image-generation → tasks/unconditional-image-generation}/about.md +0 -0
  84. /package/src/{visual-question-answering → tasks/visual-question-answering}/about.md +0 -0
  85. /package/src/{zero-shot-classification → tasks/zero-shot-classification}/about.md +0 -0
  86. /package/src/{zero-shot-image-classification → tasks/zero-shot-image-classification}/about.md +0 -0
@@ -0,0 +1,765 @@
1
+ import type { ModelData } from "./model-data";
2
+ import type { ModelLibraryKey } from "./model-libraries";
3
+
4
+ /**
5
+ * Elements configurable by a model library.
6
+ */
7
+ export interface LibraryUiElement {
8
+ /**
9
+ * Name displayed on the main
10
+ * call-to-action button on the model page.
11
+ */
12
+ btnLabel: string;
13
+ /**
14
+ * Repo name
15
+ */
16
+ repoName: string;
17
+ /**
18
+ * URL to library's repo
19
+ */
20
+ repoUrl: string;
21
+ /**
22
+ * URL to library's docs
23
+ */
24
+ docsUrl?: string;
25
+ /**
26
+ * Code snippet displayed on model page
27
+ */
28
+ snippets: (model: ModelData) => string[];
29
+ }
30
+
31
+ function nameWithoutNamespace(modelId: string): string {
32
+ const splitted = modelId.split("/");
33
+ return splitted.length === 1 ? splitted[0] : splitted[1];
34
+ }
35
+
36
+ //#region snippets
37
+
38
+ const adapter_transformers = (model: ModelData) => [
39
+ `from transformers import ${model.config?.adapter_transformers?.model_class}
40
+
41
+ model = ${model.config?.adapter_transformers?.model_class}.from_pretrained("${model.config?.adapter_transformers?.model_name}")
42
+ model.load_adapter("${model.id}", source="hf")`,
43
+ ];
44
+
45
+ const allennlpUnknown = (model: ModelData) => [
46
+ `import allennlp_models
47
+ from allennlp.predictors.predictor import Predictor
48
+
49
+ predictor = Predictor.from_path("hf://${model.id}")`,
50
+ ];
51
+
52
+ const allennlpQuestionAnswering = (model: ModelData) => [
53
+ `import allennlp_models
54
+ from allennlp.predictors.predictor import Predictor
55
+
56
+ predictor = Predictor.from_path("hf://${model.id}")
57
+ predictor_input = {"passage": "My name is Wolfgang and I live in Berlin", "question": "Where do I live?"}
58
+ predictions = predictor.predict_json(predictor_input)`,
59
+ ];
60
+
61
+ const allennlp = (model: ModelData) => {
62
+ if (model.tags?.includes("question-answering")) {
63
+ return allennlpQuestionAnswering(model);
64
+ }
65
+ return allennlpUnknown(model);
66
+ };
67
+
68
+ const asteroid = (model: ModelData) => [
69
+ `from asteroid.models import BaseModel
70
+
71
+ model = BaseModel.from_pretrained("${model.id}")`,
72
+ ];
73
+
74
+ function get_base_diffusers_model(model: ModelData): string {
75
+ return model.cardData?.base_model ?? "fill-in-base-model";
76
+ }
77
+
78
+ const bertopic = (model: ModelData) => [
79
+ `from bertopic import BERTopic
80
+
81
+ model = BERTopic.load("${model.id}")`,
82
+ ];
83
+
84
+ const diffusers_default = (model: ModelData) => [
85
+ `from diffusers import DiffusionPipeline
86
+
87
+ pipeline = DiffusionPipeline.from_pretrained("${model.id}")`,
88
+ ];
89
+
90
+ const diffusers_controlnet = (model: ModelData) => [
91
+ `from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
92
+
93
+ controlnet = ControlNetModel.from_pretrained("${model.id}")
94
+ pipeline = StableDiffusionControlNetPipeline.from_pretrained(
95
+ "${get_base_diffusers_model(model)}", controlnet=controlnet
96
+ )`,
97
+ ];
98
+
99
+ const diffusers_lora = (model: ModelData) => [
100
+ `from diffusers import DiffusionPipeline
101
+
102
+ pipeline = DiffusionPipeline.from_pretrained("${get_base_diffusers_model(model)}")
103
+ pipeline.load_lora_weights("${model.id}")`,
104
+ ];
105
+
106
+ const diffusers_textual_inversion = (model: ModelData) => [
107
+ `from diffusers import DiffusionPipeline
108
+
109
+ pipeline = DiffusionPipeline.from_pretrained("${get_base_diffusers_model(model)}")
110
+ pipeline.load_textual_inversion("${model.id}")`,
111
+ ];
112
+
113
+ const diffusers = (model: ModelData) => {
114
+ if (model.tags?.includes("controlnet")) {
115
+ return diffusers_controlnet(model);
116
+ } else if (model.tags?.includes("lora")) {
117
+ return diffusers_lora(model);
118
+ } else if (model.tags?.includes("textual_inversion")) {
119
+ return diffusers_textual_inversion(model);
120
+ } else {
121
+ return diffusers_default(model);
122
+ }
123
+ };
124
+
125
+ const espnetTTS = (model: ModelData) => [
126
+ `from espnet2.bin.tts_inference import Text2Speech
127
+
128
+ model = Text2Speech.from_pretrained("${model.id}")
129
+
130
+ speech, *_ = model("text to generate speech from")`,
131
+ ];
132
+
133
+ const espnetASR = (model: ModelData) => [
134
+ `from espnet2.bin.asr_inference import Speech2Text
135
+
136
+ model = Speech2Text.from_pretrained(
137
+ "${model.id}"
138
+ )
139
+
140
+ speech, rate = soundfile.read("speech.wav")
141
+ text, *_ = model(speech)[0]`,
142
+ ];
143
+
144
+ const espnetUnknown = () => [`unknown model type (must be text-to-speech or automatic-speech-recognition)`];
145
+
146
+ const espnet = (model: ModelData) => {
147
+ if (model.tags?.includes("text-to-speech")) {
148
+ return espnetTTS(model);
149
+ } else if (model.tags?.includes("automatic-speech-recognition")) {
150
+ return espnetASR(model);
151
+ }
152
+ return espnetUnknown();
153
+ };
154
+
155
+ const fairseq = (model: ModelData) => [
156
+ `from fairseq.checkpoint_utils import load_model_ensemble_and_task_from_hf_hub
157
+
158
+ models, cfg, task = load_model_ensemble_and_task_from_hf_hub(
159
+ "${model.id}"
160
+ )`,
161
+ ];
162
+
163
+ const flair = (model: ModelData) => [
164
+ `from flair.models import SequenceTagger
165
+
166
+ tagger = SequenceTagger.load("${model.id}")`,
167
+ ];
168
+
169
+ const keras = (model: ModelData) => [
170
+ `from huggingface_hub import from_pretrained_keras
171
+
172
+ model = from_pretrained_keras("${model.id}")
173
+ `,
174
+ ];
175
+
176
+ const open_clip = (model: ModelData) => [
177
+ `import open_clip
178
+
179
+ model, preprocess_train, preprocess_val = open_clip.create_model_and_transforms('hf-hub:${model.id}')
180
+ tokenizer = open_clip.get_tokenizer('hf-hub:${model.id}')`,
181
+ ];
182
+
183
+ const paddlenlp = (model: ModelData) => {
184
+ if (model.config?.architectures?.[0]) {
185
+ const architecture = model.config.architectures[0];
186
+ return [
187
+ [
188
+ `from paddlenlp.transformers import AutoTokenizer, ${architecture}`,
189
+ "",
190
+ `tokenizer = AutoTokenizer.from_pretrained("${model.id}"${
191
+ model.private ? ", use_auth_token=True" : ""
192
+ }, from_hf_hub=True)`,
193
+ `model = ${architecture}.from_pretrained("${model.id}"${
194
+ model.private ? ", use_auth_token=True" : ""
195
+ }, from_hf_hub=True)`,
196
+ ].join("\n"),
197
+ ];
198
+ } else {
199
+ return [
200
+ [
201
+ `# ⚠️ Type of model unknown`,
202
+ `from paddlenlp.transformers import AutoTokenizer, AutoModel`,
203
+ "",
204
+ `tokenizer = AutoTokenizer.from_pretrained("${model.id}"${
205
+ model.private ? ", use_auth_token=True" : ""
206
+ }, from_hf_hub=True)`,
207
+ `model = AutoModel.from_pretrained("${model.id}"${
208
+ model.private ? ", use_auth_token=True" : ""
209
+ }, from_hf_hub=True)`,
210
+ ].join("\n"),
211
+ ];
212
+ }
213
+ };
214
+
215
+ const pyannote_audio_pipeline = (model: ModelData) => [
216
+ `from pyannote.audio import Pipeline
217
+
218
+ pipeline = Pipeline.from_pretrained("${model.id}")
219
+
220
+ # inference on the whole file
221
+ pipeline("file.wav")
222
+
223
+ # inference on an excerpt
224
+ from pyannote.core import Segment
225
+ excerpt = Segment(start=2.0, end=5.0)
226
+
227
+ from pyannote.audio import Audio
228
+ waveform, sample_rate = Audio().crop("file.wav", excerpt)
229
+ pipeline({"waveform": waveform, "sample_rate": sample_rate})`,
230
+ ];
231
+
232
+ const pyannote_audio_model = (model: ModelData) => [
233
+ `from pyannote.audio import Model, Inference
234
+
235
+ model = Model.from_pretrained("${model.id}")
236
+ inference = Inference(model)
237
+
238
+ # inference on the whole file
239
+ inference("file.wav")
240
+
241
+ # inference on an excerpt
242
+ from pyannote.core import Segment
243
+ excerpt = Segment(start=2.0, end=5.0)
244
+ inference.crop("file.wav", excerpt)`,
245
+ ];
246
+
247
+ const pyannote_audio = (model: ModelData) => {
248
+ if (model.tags?.includes("pyannote-audio-pipeline")) {
249
+ return pyannote_audio_pipeline(model);
250
+ }
251
+ return pyannote_audio_model(model);
252
+ };
253
+
254
+ const tensorflowttsTextToMel = (model: ModelData) => [
255
+ `from tensorflow_tts.inference import AutoProcessor, TFAutoModel
256
+
257
+ processor = AutoProcessor.from_pretrained("${model.id}")
258
+ model = TFAutoModel.from_pretrained("${model.id}")
259
+ `,
260
+ ];
261
+
262
+ const tensorflowttsMelToWav = (model: ModelData) => [
263
+ `from tensorflow_tts.inference import TFAutoModel
264
+
265
+ model = TFAutoModel.from_pretrained("${model.id}")
266
+ audios = model.inference(mels)
267
+ `,
268
+ ];
269
+
270
+ const tensorflowttsUnknown = (model: ModelData) => [
271
+ `from tensorflow_tts.inference import TFAutoModel
272
+
273
+ model = TFAutoModel.from_pretrained("${model.id}")
274
+ `,
275
+ ];
276
+
277
+ const tensorflowtts = (model: ModelData) => {
278
+ if (model.tags?.includes("text-to-mel")) {
279
+ return tensorflowttsTextToMel(model);
280
+ } else if (model.tags?.includes("mel-to-wav")) {
281
+ return tensorflowttsMelToWav(model);
282
+ }
283
+ return tensorflowttsUnknown(model);
284
+ };
285
+
286
+ const timm = (model: ModelData) => [
287
+ `import timm
288
+
289
+ model = timm.create_model("hf_hub:${model.id}", pretrained=True)`,
290
+ ];
291
+
292
+ const skopsPickle = (model: ModelData, modelFile: string) => {
293
+ return [
294
+ `import joblib
295
+ from skops.hub_utils import download
296
+ download("${model.id}", "path_to_folder")
297
+ model = joblib.load(
298
+ "${modelFile}"
299
+ )
300
+ # only load pickle files from sources you trust
301
+ # read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
302
+ ];
303
+ };
304
+
305
+ const skopsFormat = (model: ModelData, modelFile: string) => {
306
+ return [
307
+ `from skops.hub_utils import download
308
+ from skops.io import load
309
+ download("${model.id}", "path_to_folder")
310
+ # make sure model file is in skops format
311
+ # if model is a pickle file, make sure it's from a source you trust
312
+ model = load("path_to_folder/${modelFile}")`,
313
+ ];
314
+ };
315
+
316
+ const skopsJobLib = (model: ModelData) => {
317
+ return [
318
+ `from huggingface_hub import hf_hub_download
319
+ import joblib
320
+ model = joblib.load(
321
+ hf_hub_download("${model.id}", "sklearn_model.joblib")
322
+ )
323
+ # only load pickle files from sources you trust
324
+ # read more about it here https://skops.readthedocs.io/en/stable/persistence.html`,
325
+ ];
326
+ };
327
+
328
+ const sklearn = (model: ModelData) => {
329
+ if (model.tags?.includes("skops")) {
330
+ const skopsmodelFile = model.config?.sklearn?.filename;
331
+ const skopssaveFormat = model.config?.sklearn?.model_format;
332
+ if (!skopsmodelFile) {
333
+ return [`# ⚠️ Model filename not specified in config.json`];
334
+ }
335
+ if (skopssaveFormat === "pickle") {
336
+ return skopsPickle(model, skopsmodelFile);
337
+ } else {
338
+ return skopsFormat(model, skopsmodelFile);
339
+ }
340
+ } else {
341
+ return skopsJobLib(model);
342
+ }
343
+ };
344
+
345
+ const fastai = (model: ModelData) => [
346
+ `from huggingface_hub import from_pretrained_fastai
347
+
348
+ learn = from_pretrained_fastai("${model.id}")`,
349
+ ];
350
+
351
+ const sampleFactory = (model: ModelData) => [
352
+ `python -m sample_factory.huggingface.load_from_hub -r ${model.id} -d ./train_dir`,
353
+ ];
354
+
355
+ const sentenceTransformers = (model: ModelData) => [
356
+ `from sentence_transformers import SentenceTransformer
357
+
358
+ model = SentenceTransformer("${model.id}")`,
359
+ ];
360
+
361
+ const spacy = (model: ModelData) => [
362
+ `!pip install https://huggingface.co/${model.id}/resolve/main/${nameWithoutNamespace(model.id)}-any-py3-none-any.whl
363
+
364
+ # Using spacy.load().
365
+ import spacy
366
+ nlp = spacy.load("${nameWithoutNamespace(model.id)}")
367
+
368
+ # Importing as module.
369
+ import ${nameWithoutNamespace(model.id)}
370
+ nlp = ${nameWithoutNamespace(model.id)}.load()`,
371
+ ];
372
+
373
+ const span_marker = (model: ModelData) => [
374
+ `from span_marker import SpanMarkerModel
375
+
376
+ model = SpanMarkerModel.from_pretrained("${model.id}")`,
377
+ ];
378
+
379
+ const stanza = (model: ModelData) => [
380
+ `import stanza
381
+
382
+ stanza.download("${nameWithoutNamespace(model.id).replace("stanza-", "")}")
383
+ nlp = stanza.Pipeline("${nameWithoutNamespace(model.id).replace("stanza-", "")}")`,
384
+ ];
385
+
386
+ const speechBrainMethod = (speechbrainInterface: string) => {
387
+ switch (speechbrainInterface) {
388
+ case "EncoderClassifier":
389
+ return "classify_file";
390
+ case "EncoderDecoderASR":
391
+ case "EncoderASR":
392
+ return "transcribe_file";
393
+ case "SpectralMaskEnhancement":
394
+ return "enhance_file";
395
+ case "SepformerSeparation":
396
+ return "separate_file";
397
+ default:
398
+ return undefined;
399
+ }
400
+ };
401
+
402
+ const speechbrain = (model: ModelData) => {
403
+ const speechbrainInterface = model.config?.speechbrain?.interface;
404
+ if (speechbrainInterface === undefined) {
405
+ return [`# interface not specified in config.json`];
406
+ }
407
+
408
+ const speechbrainMethod = speechBrainMethod(speechbrainInterface);
409
+ if (speechbrainMethod === undefined) {
410
+ return [`# interface in config.json invalid`];
411
+ }
412
+
413
+ return [
414
+ `from speechbrain.pretrained import ${speechbrainInterface}
415
+ model = ${speechbrainInterface}.from_hparams(
416
+ "${model.id}"
417
+ )
418
+ model.${speechbrainMethod}("file.wav")`,
419
+ ];
420
+ };
421
+
422
+ const transformers = (model: ModelData) => {
423
+ const info = model.transformersInfo;
424
+ if (!info) {
425
+ return [`# ⚠️ Type of model unknown`];
426
+ }
427
+ const remote_code_snippet = info.custom_class ? ", trust_remote_code=True" : "";
428
+
429
+ let autoSnippet: string;
430
+ if (info.processor) {
431
+ const varName =
432
+ info.processor === "AutoTokenizer"
433
+ ? "tokenizer"
434
+ : info.processor === "AutoFeatureExtractor"
435
+ ? "extractor"
436
+ : "processor";
437
+ autoSnippet = [
438
+ "# Load model directly",
439
+ `from transformers import ${info.processor}, ${info.auto_model}`,
440
+ "",
441
+ `${varName} = ${info.processor}.from_pretrained("${model.id}"` + remote_code_snippet + ")",
442
+ `model = ${info.auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ")",
443
+ ].join("\n");
444
+ } else {
445
+ autoSnippet = [
446
+ "# Load model directly",
447
+ `from transformers import ${info.auto_model}`,
448
+ `model = ${info.auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ")",
449
+ ].join("\n");
450
+ }
451
+
452
+ if (model.pipeline_tag) {
453
+ const pipelineSnippet = [
454
+ "# Use a pipeline as a high-level helper",
455
+ "from transformers import pipeline",
456
+ "",
457
+ `pipe = pipeline("${model.pipeline_tag}", model="${model.id}"` + remote_code_snippet + ")",
458
+ ].join("\n");
459
+ return [pipelineSnippet, autoSnippet];
460
+ }
461
+ return [autoSnippet];
462
+ };
463
+
464
+ const transformersJS = (model: ModelData) => {
465
+ if (!model.pipeline_tag) {
466
+ return [`// ⚠️ Unknown pipeline tag`];
467
+ }
468
+
469
+ const libName = "@xenova/transformers";
470
+
471
+ return [
472
+ `// npm i ${libName}
473
+ import { pipeline } from '${libName}';
474
+
475
+ // Allocate pipeline
476
+ const pipe = await pipeline('${model.pipeline_tag}', '${model.id}');`,
477
+ ];
478
+ };
479
+
480
+ const peftTask = (peftTaskType?: string) => {
481
+ switch (peftTaskType) {
482
+ case "CAUSAL_LM":
483
+ return "CausalLM";
484
+ case "SEQ_2_SEQ_LM":
485
+ return "Seq2SeqLM";
486
+ case "TOKEN_CLS":
487
+ return "TokenClassification";
488
+ case "SEQ_CLS":
489
+ return "SequenceClassification";
490
+ default:
491
+ return undefined;
492
+ }
493
+ };
494
+
495
+ const peft = (model: ModelData) => {
496
+ const { base_model_name: peftBaseModel, task_type: peftTaskType } = model.config?.peft ?? {};
497
+ const pefttask = peftTask(peftTaskType);
498
+ if (!pefttask) {
499
+ return [`Task type is invalid.`];
500
+ }
501
+ if (!peftBaseModel) {
502
+ return [`Base model is not found.`];
503
+ }
504
+
505
+ return [
506
+ `from peft import PeftModel, PeftConfig
507
+ from transformers import AutoModelFor${pefttask}
508
+
509
+ config = PeftConfig.from_pretrained("${model.id}")
510
+ model = AutoModelFor${pefttask}.from_pretrained("${peftBaseModel}")
511
+ model = PeftModel.from_pretrained(model, "${model.id}")`,
512
+ ];
513
+ };
514
+
515
+ const fasttext = (model: ModelData) => [
516
+ `from huggingface_hub import hf_hub_download
517
+ import fasttext
518
+
519
+ model = fasttext.load_model(hf_hub_download("${model.id}", "model.bin"))`,
520
+ ];
521
+
522
+ const stableBaselines3 = (model: ModelData) => [
523
+ `from huggingface_sb3 import load_from_hub
524
+ checkpoint = load_from_hub(
525
+ repo_id="${model.id}",
526
+ filename="{MODEL FILENAME}.zip",
527
+ )`,
528
+ ];
529
+
530
+ const nemoDomainResolver = (domain: string, model: ModelData): string[] | undefined => {
531
+ switch (domain) {
532
+ case "ASR":
533
+ return [
534
+ `import nemo.collections.asr as nemo_asr
535
+ asr_model = nemo_asr.models.ASRModel.from_pretrained("${model.id}")
536
+
537
+ transcriptions = asr_model.transcribe(["file.wav"])`,
538
+ ];
539
+ default:
540
+ return undefined;
541
+ }
542
+ };
543
+
544
+ const mlAgents = (model: ModelData) => [`mlagents-load-from-hf --repo-id="${model.id}" --local-dir="./downloads"`];
545
+
546
+ const nemo = (model: ModelData) => {
547
+ let command: string[] | undefined = undefined;
548
+ // Resolve the tag to a nemo domain/sub-domain
549
+ if (model.tags?.includes("automatic-speech-recognition")) {
550
+ command = nemoDomainResolver("ASR", model);
551
+ }
552
+
553
+ return command ?? [`# tag did not correspond to a valid NeMo domain.`];
554
+ };
555
+
556
+ const pythae = (model: ModelData) => [
557
+ `from pythae.models import AutoModel
558
+
559
+ model = AutoModel.load_from_hf_hub("${model.id}")`,
560
+ ];
561
+
562
+ //#endregion
563
+
564
+ export const MODEL_LIBRARIES_UI_ELEMENTS: Partial<Record<ModelLibraryKey, LibraryUiElement>> = {
565
+ "adapter-transformers": {
566
+ btnLabel: "Adapter Transformers",
567
+ repoName: "adapter-transformers",
568
+ repoUrl: "https://github.com/Adapter-Hub/adapter-transformers",
569
+ docsUrl: "https://huggingface.co/docs/hub/adapter-transformers",
570
+ snippets: adapter_transformers,
571
+ },
572
+ allennlp: {
573
+ btnLabel: "AllenNLP",
574
+ repoName: "AllenNLP",
575
+ repoUrl: "https://github.com/allenai/allennlp",
576
+ docsUrl: "https://huggingface.co/docs/hub/allennlp",
577
+ snippets: allennlp,
578
+ },
579
+ asteroid: {
580
+ btnLabel: "Asteroid",
581
+ repoName: "Asteroid",
582
+ repoUrl: "https://github.com/asteroid-team/asteroid",
583
+ docsUrl: "https://huggingface.co/docs/hub/asteroid",
584
+ snippets: asteroid,
585
+ },
586
+ bertopic: {
587
+ btnLabel: "BERTopic",
588
+ repoName: "BERTopic",
589
+ repoUrl: "https://github.com/MaartenGr/BERTopic",
590
+ snippets: bertopic,
591
+ },
592
+ diffusers: {
593
+ btnLabel: "Diffusers",
594
+ repoName: "🤗/diffusers",
595
+ repoUrl: "https://github.com/huggingface/diffusers",
596
+ docsUrl: "https://huggingface.co/docs/hub/diffusers",
597
+ snippets: diffusers,
598
+ },
599
+ espnet: {
600
+ btnLabel: "ESPnet",
601
+ repoName: "ESPnet",
602
+ repoUrl: "https://github.com/espnet/espnet",
603
+ docsUrl: "https://huggingface.co/docs/hub/espnet",
604
+ snippets: espnet,
605
+ },
606
+ fairseq: {
607
+ btnLabel: "Fairseq",
608
+ repoName: "fairseq",
609
+ repoUrl: "https://github.com/pytorch/fairseq",
610
+ snippets: fairseq,
611
+ },
612
+ flair: {
613
+ btnLabel: "Flair",
614
+ repoName: "Flair",
615
+ repoUrl: "https://github.com/flairNLP/flair",
616
+ docsUrl: "https://huggingface.co/docs/hub/flair",
617
+ snippets: flair,
618
+ },
619
+ keras: {
620
+ btnLabel: "Keras",
621
+ repoName: "Keras",
622
+ repoUrl: "https://github.com/keras-team/keras",
623
+ docsUrl: "https://huggingface.co/docs/hub/keras",
624
+ snippets: keras,
625
+ },
626
+ nemo: {
627
+ btnLabel: "NeMo",
628
+ repoName: "NeMo",
629
+ repoUrl: "https://github.com/NVIDIA/NeMo",
630
+ snippets: nemo,
631
+ },
632
+ open_clip: {
633
+ btnLabel: "OpenCLIP",
634
+ repoName: "OpenCLIP",
635
+ repoUrl: "https://github.com/mlfoundations/open_clip",
636
+ snippets: open_clip,
637
+ },
638
+ paddlenlp: {
639
+ btnLabel: "paddlenlp",
640
+ repoName: "PaddleNLP",
641
+ repoUrl: "https://github.com/PaddlePaddle/PaddleNLP",
642
+ docsUrl: "https://huggingface.co/docs/hub/paddlenlp",
643
+ snippets: paddlenlp,
644
+ },
645
+ peft: {
646
+ btnLabel: "PEFT",
647
+ repoName: "PEFT",
648
+ repoUrl: "https://github.com/huggingface/peft",
649
+ snippets: peft,
650
+ },
651
+ "pyannote-audio": {
652
+ btnLabel: "pyannote.audio",
653
+ repoName: "pyannote-audio",
654
+ repoUrl: "https://github.com/pyannote/pyannote-audio",
655
+ snippets: pyannote_audio,
656
+ },
657
+ "sentence-transformers": {
658
+ btnLabel: "sentence-transformers",
659
+ repoName: "sentence-transformers",
660
+ repoUrl: "https://github.com/UKPLab/sentence-transformers",
661
+ docsUrl: "https://huggingface.co/docs/hub/sentence-transformers",
662
+ snippets: sentenceTransformers,
663
+ },
664
+ sklearn: {
665
+ btnLabel: "Scikit-learn",
666
+ repoName: "Scikit-learn",
667
+ repoUrl: "https://github.com/scikit-learn/scikit-learn",
668
+ snippets: sklearn,
669
+ },
670
+ fastai: {
671
+ btnLabel: "fastai",
672
+ repoName: "fastai",
673
+ repoUrl: "https://github.com/fastai/fastai",
674
+ docsUrl: "https://huggingface.co/docs/hub/fastai",
675
+ snippets: fastai,
676
+ },
677
+ spacy: {
678
+ btnLabel: "spaCy",
679
+ repoName: "spaCy",
680
+ repoUrl: "https://github.com/explosion/spaCy",
681
+ docsUrl: "https://huggingface.co/docs/hub/spacy",
682
+ snippets: spacy,
683
+ },
684
+ "span-marker": {
685
+ btnLabel: "SpanMarker",
686
+ repoName: "SpanMarkerNER",
687
+ repoUrl: "https://github.com/tomaarsen/SpanMarkerNER",
688
+ docsUrl: "https://huggingface.co/docs/hub/span_marker",
689
+ snippets: span_marker,
690
+ },
691
+ speechbrain: {
692
+ btnLabel: "speechbrain",
693
+ repoName: "speechbrain",
694
+ repoUrl: "https://github.com/speechbrain/speechbrain",
695
+ docsUrl: "https://huggingface.co/docs/hub/speechbrain",
696
+ snippets: speechbrain,
697
+ },
698
+ stanza: {
699
+ btnLabel: "Stanza",
700
+ repoName: "stanza",
701
+ repoUrl: "https://github.com/stanfordnlp/stanza",
702
+ docsUrl: "https://huggingface.co/docs/hub/stanza",
703
+ snippets: stanza,
704
+ },
705
+ tensorflowtts: {
706
+ btnLabel: "TensorFlowTTS",
707
+ repoName: "TensorFlowTTS",
708
+ repoUrl: "https://github.com/TensorSpeech/TensorFlowTTS",
709
+ snippets: tensorflowtts,
710
+ },
711
+ timm: {
712
+ btnLabel: "timm",
713
+ repoName: "pytorch-image-models",
714
+ repoUrl: "https://github.com/rwightman/pytorch-image-models",
715
+ docsUrl: "https://huggingface.co/docs/hub/timm",
716
+ snippets: timm,
717
+ },
718
+ transformers: {
719
+ btnLabel: "Transformers",
720
+ repoName: "🤗/transformers",
721
+ repoUrl: "https://github.com/huggingface/transformers",
722
+ docsUrl: "https://huggingface.co/docs/hub/transformers",
723
+ snippets: transformers,
724
+ },
725
+ "transformers.js": {
726
+ btnLabel: "Transformers.js",
727
+ repoName: "transformers.js",
728
+ repoUrl: "https://github.com/xenova/transformers.js",
729
+ docsUrl: "https://huggingface.co/docs/hub/transformers-js",
730
+ snippets: transformersJS,
731
+ },
732
+ fasttext: {
733
+ btnLabel: "fastText",
734
+ repoName: "fastText",
735
+ repoUrl: "https://fasttext.cc/",
736
+ snippets: fasttext,
737
+ },
738
+ "sample-factory": {
739
+ btnLabel: "sample-factory",
740
+ repoName: "sample-factory",
741
+ repoUrl: "https://github.com/alex-petrenko/sample-factory",
742
+ docsUrl: "https://huggingface.co/docs/hub/sample-factory",
743
+ snippets: sampleFactory,
744
+ },
745
+ "stable-baselines3": {
746
+ btnLabel: "stable-baselines3",
747
+ repoName: "stable-baselines3",
748
+ repoUrl: "https://github.com/huggingface/huggingface_sb3",
749
+ docsUrl: "https://huggingface.co/docs/hub/stable-baselines3",
750
+ snippets: stableBaselines3,
751
+ },
752
+ "ml-agents": {
753
+ btnLabel: "ml-agents",
754
+ repoName: "ml-agents",
755
+ repoUrl: "https://github.com/huggingface/ml-agents",
756
+ docsUrl: "https://huggingface.co/docs/hub/ml-agents",
757
+ snippets: mlAgents,
758
+ },
759
+ pythae: {
760
+ btnLabel: "pythae",
761
+ repoName: "pythae",
762
+ repoUrl: "https://github.com/clementchadebec/benchmark_VAE",
763
+ snippets: pythae,
764
+ },
765
+ } as const;