@huggingface/inference 2.1.3 → 2.2.1

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/README.md CHANGED
@@ -188,6 +188,22 @@ await hf.documentQuestionAnswering({
188
188
  }
189
189
  })
190
190
 
191
+ // Tabular
192
+
193
+ await hf.tabularRegression({
194
+ model: "scikit-learn/Fish-Weight",
195
+ inputs: {
196
+ data: {
197
+ "Height":["11.52", "12.48", "12.3778"],
198
+ "Length1":["23.2", "24", "23.9"],
199
+ "Length2":["25.4", "26.3", "26.5"],
200
+ "Length3":["30", "31.2", "31.1"],
201
+ "Species":["Bream", "Bream", "Bream"],
202
+ "Width":["4.02", "4.3056", "4.6961"]
203
+ },
204
+ },
205
+ })
206
+
191
207
  // Custom call, for models with custom parameters / outputs
192
208
  await hf.request({
193
209
  model: 'my-custom-model',
@@ -222,7 +238,7 @@ const { generated_text } = await gpt2.textGeneration({inputs: 'The answer to the
222
238
  - [x] Question answering
223
239
  - [x] Table question answering
224
240
  - [x] Text classification
225
- - [x] Text generation
241
+ - [x] Text generation - [demo](https://huggingface.co/spaces/huggingfacejs/streaming-text-generation)
226
242
  - [x] Text2Text generation
227
243
  - [x] Token classification
228
244
  - [x] Named entity recognition
@@ -243,11 +259,14 @@ const { generated_text } = await gpt2.textGeneration({inputs: 'The answer to the
243
259
  - [x] Object detection
244
260
  - [x] Image segmentation
245
261
  - [x] Text to image
246
- - [x] Image to text
262
+ - [x] Image to text - [demo](https://huggingface.co/spaces/huggingfacejs/image-to-text)
247
263
 
248
264
  ### Multimodal
249
- - [x] Document question answering
250
- - [x] Visual question answering
265
+ - [x] Document question answering - [demo](https://huggingface.co/spaces/huggingfacejs/doc-vis-qa)
266
+ - [x] Visual question answering - [demo](https://huggingface.co/spaces/huggingfacejs/doc-vis-qa)
267
+
268
+ ### Tabular
269
+ - [x] Tabular regression
251
270
 
252
271
  ## Tree-shaking
253
272
 
package/dist/index.js CHANGED
@@ -38,10 +38,12 @@ __export(src_exports, {
38
38
  streamingRequest: () => streamingRequest,
39
39
  summarization: () => summarization,
40
40
  tableQuestionAnswering: () => tableQuestionAnswering,
41
+ tabularRegression: () => tabularRegression,
41
42
  textClassification: () => textClassification,
42
43
  textGeneration: () => textGeneration,
43
44
  textGenerationStream: () => textGenerationStream,
44
45
  textToImage: () => textToImage,
46
+ textToSpeech: () => textToSpeech,
45
47
  tokenClassification: () => tokenClassification,
46
48
  translation: () => translation,
47
49
  visualQuestionAnswering: () => visualQuestionAnswering,
@@ -68,10 +70,12 @@ __export(tasks_exports, {
68
70
  streamingRequest: () => streamingRequest,
69
71
  summarization: () => summarization,
70
72
  tableQuestionAnswering: () => tableQuestionAnswering,
73
+ tabularRegression: () => tabularRegression,
71
74
  textClassification: () => textClassification,
72
75
  textGeneration: () => textGeneration,
73
76
  textGenerationStream: () => textGenerationStream,
74
77
  textToImage: () => textToImage,
78
+ textToSpeech: () => textToSpeech,
75
79
  tokenClassification: () => tokenClassification,
76
80
  translation: () => translation,
77
81
  visualQuestionAnswering: () => visualQuestionAnswering,
@@ -286,7 +290,11 @@ async function* streamingRequest(args, options) {
286
290
  onChunk(value);
287
291
  for (const event of events) {
288
292
  if (event.data.length > 0) {
289
- yield JSON.parse(event.data);
293
+ const data = JSON.parse(event.data);
294
+ if (typeof data === "object" && data !== null && "error" in data) {
295
+ throw new Error(data.error);
296
+ }
297
+ yield data;
290
298
  }
291
299
  }
292
300
  events = [];
@@ -326,6 +334,16 @@ async function automaticSpeechRecognition(args, options) {
326
334
  return res;
327
335
  }
328
336
 
337
+ // src/tasks/audio/textToSpeech.ts
338
+ async function textToSpeech(args, options) {
339
+ const res = await request(args, options);
340
+ const isValidOutput = res && res instanceof Blob;
341
+ if (!isValidOutput) {
342
+ throw new InferenceOutputError("Expected Blob");
343
+ }
344
+ return res;
345
+ }
346
+
329
347
  // src/tasks/cv/imageClassification.ts
330
348
  async function imageClassification(args, options) {
331
349
  const res = await request(args, options);
@@ -543,7 +561,7 @@ async function zeroShotClassification(args, options) {
543
561
  return res;
544
562
  }
545
563
 
546
- // src/utils/base64FromBytes.ts
564
+ // ../shared/src/base64FromBytes.ts
547
565
  function base64FromBytes(arr) {
548
566
  if (globalThis.Buffer) {
549
567
  return globalThis.Buffer.from(arr).toString("base64");
@@ -556,6 +574,10 @@ function base64FromBytes(arr) {
556
574
  }
557
575
  }
558
576
 
577
+ // ../shared/src/isBackend.ts
578
+ var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
579
+ var isWebWorker = typeof self === "object" && self.constructor && self.constructor.name === "DedicatedWorkerGlobalScope";
580
+
559
581
  // src/tasks/multimodal/documentQuestionAnswering.ts
560
582
  async function documentQuestionAnswering(args, options) {
561
583
  const reqArgs = {
@@ -594,6 +616,16 @@ async function visualQuestionAnswering(args, options) {
594
616
  return res;
595
617
  }
596
618
 
619
+ // src/tasks/tabular/tabularRegression.ts
620
+ async function tabularRegression(args, options) {
621
+ const res = await request(args, options);
622
+ const isValidOutput = Array.isArray(res) && res.every((x) => typeof x === "number");
623
+ if (!isValidOutput) {
624
+ throw new InferenceOutputError("Expected number[]");
625
+ }
626
+ return res;
627
+ }
628
+
597
629
  // src/HfInference.ts
598
630
  var HfInference = class {
599
631
  accessToken;
@@ -653,10 +685,12 @@ var HfInferenceEndpoint = class {
653
685
  streamingRequest,
654
686
  summarization,
655
687
  tableQuestionAnswering,
688
+ tabularRegression,
656
689
  textClassification,
657
690
  textGeneration,
658
691
  textGenerationStream,
659
692
  textToImage,
693
+ textToSpeech,
660
694
  tokenClassification,
661
695
  translation,
662
696
  visualQuestionAnswering,
package/dist/index.mjs CHANGED
@@ -23,10 +23,12 @@ __export(tasks_exports, {
23
23
  streamingRequest: () => streamingRequest,
24
24
  summarization: () => summarization,
25
25
  tableQuestionAnswering: () => tableQuestionAnswering,
26
+ tabularRegression: () => tabularRegression,
26
27
  textClassification: () => textClassification,
27
28
  textGeneration: () => textGeneration,
28
29
  textGenerationStream: () => textGenerationStream,
29
30
  textToImage: () => textToImage,
31
+ textToSpeech: () => textToSpeech,
30
32
  tokenClassification: () => tokenClassification,
31
33
  translation: () => translation,
32
34
  visualQuestionAnswering: () => visualQuestionAnswering,
@@ -241,7 +243,11 @@ async function* streamingRequest(args, options) {
241
243
  onChunk(value);
242
244
  for (const event of events) {
243
245
  if (event.data.length > 0) {
244
- yield JSON.parse(event.data);
246
+ const data = JSON.parse(event.data);
247
+ if (typeof data === "object" && data !== null && "error" in data) {
248
+ throw new Error(data.error);
249
+ }
250
+ yield data;
245
251
  }
246
252
  }
247
253
  events = [];
@@ -281,6 +287,16 @@ async function automaticSpeechRecognition(args, options) {
281
287
  return res;
282
288
  }
283
289
 
290
+ // src/tasks/audio/textToSpeech.ts
291
+ async function textToSpeech(args, options) {
292
+ const res = await request(args, options);
293
+ const isValidOutput = res && res instanceof Blob;
294
+ if (!isValidOutput) {
295
+ throw new InferenceOutputError("Expected Blob");
296
+ }
297
+ return res;
298
+ }
299
+
284
300
  // src/tasks/cv/imageClassification.ts
285
301
  async function imageClassification(args, options) {
286
302
  const res = await request(args, options);
@@ -498,7 +514,7 @@ async function zeroShotClassification(args, options) {
498
514
  return res;
499
515
  }
500
516
 
501
- // src/utils/base64FromBytes.ts
517
+ // ../shared/src/base64FromBytes.ts
502
518
  function base64FromBytes(arr) {
503
519
  if (globalThis.Buffer) {
504
520
  return globalThis.Buffer.from(arr).toString("base64");
@@ -511,6 +527,10 @@ function base64FromBytes(arr) {
511
527
  }
512
528
  }
513
529
 
530
+ // ../shared/src/isBackend.ts
531
+ var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
532
+ var isWebWorker = typeof self === "object" && self.constructor && self.constructor.name === "DedicatedWorkerGlobalScope";
533
+
514
534
  // src/tasks/multimodal/documentQuestionAnswering.ts
515
535
  async function documentQuestionAnswering(args, options) {
516
536
  const reqArgs = {
@@ -549,6 +569,16 @@ async function visualQuestionAnswering(args, options) {
549
569
  return res;
550
570
  }
551
571
 
572
+ // src/tasks/tabular/tabularRegression.ts
573
+ async function tabularRegression(args, options) {
574
+ const res = await request(args, options);
575
+ const isValidOutput = Array.isArray(res) && res.every((x) => typeof x === "number");
576
+ if (!isValidOutput) {
577
+ throw new InferenceOutputError("Expected number[]");
578
+ }
579
+ return res;
580
+ }
581
+
552
582
  // src/HfInference.ts
553
583
  var HfInference = class {
554
584
  accessToken;
@@ -607,10 +637,12 @@ export {
607
637
  streamingRequest,
608
638
  summarization,
609
639
  tableQuestionAnswering,
640
+ tabularRegression,
610
641
  textClassification,
611
642
  textGeneration,
612
643
  textGenerationStream,
613
644
  textToImage,
645
+ textToSpeech,
614
646
  tokenClassification,
615
647
  translation,
616
648
  visualQuestionAnswering,
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "@huggingface/shared",
3
+ "version": "1.0.0",
4
+ "private": true,
5
+ "main": "src/index.ts",
6
+ "source": "src/index.ts",
7
+ "types": "./src/index.ts"
8
+ }
@@ -0,0 +1,3 @@
1
+ export * from './base64FromBytes';
2
+ export * from './isBackend';
3
+ export * from './isFrontend';
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "esModuleInterop": true,
4
+ "noEmit": false,
5
+ "module": "ESNext",
6
+ "target": "ESNext",
7
+ "moduleResolution": "Node",
8
+ "noImplicitAny": true,
9
+ "strict": true,
10
+ "strictNullChecks": true,
11
+ "skipLibCheck": true,
12
+ "composite": true
13
+ }
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huggingface/inference",
3
- "version": "2.1.3",
3
+ "version": "2.2.1",
4
4
  "license": "MIT",
5
5
  "author": "Tim Mikeladze <tim.mikeladze@gmail.com>",
6
6
  "description": "Typescript wrapper for the Hugging Face Inference API",
@@ -40,8 +40,12 @@
40
40
  "@types/node": "18.13.0",
41
41
  "typescript": "^5.0.4",
42
42
  "vite": "^4.1.4",
43
- "vitest": "^0.29.8"
43
+ "vitest": "^0.29.8",
44
+ "@huggingface/shared": "1.0.0"
44
45
  },
46
+ "bundledDependencies": [
47
+ "@huggingface/shared"
48
+ ],
45
49
  "resolutions": {},
46
50
  "scripts": {
47
51
  "build": "tsup src/index.ts --format cjs,esm --clean",
@@ -49,6 +53,7 @@
49
53
  "lint:check": "eslint --ext .cjs,.ts .",
50
54
  "format": "prettier --write .",
51
55
  "format:check": "prettier --check .",
56
+ "preversion": "pnpm --filter doc-internal run fix-cdn-versions && git add ../../README.md",
52
57
  "test": "vitest run --config vitest.config.ts",
53
58
  "test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest.config.ts",
54
59
  "type-check": "tsc"
@@ -0,0 +1,25 @@
1
+ import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import type { BaseArgs, Options } from "../../types";
3
+ import { request } from "../custom/request";
4
+
5
+ export type TextToSpeechArgs = BaseArgs & {
6
+ /**
7
+ * The text to generate an audio from
8
+ */
9
+ inputs: string;
10
+ };
11
+
12
+ export type TextToSpeechOutput = Blob;
13
+
14
+ /**
15
+ * This task synthesize an audio of a voice pronouncing a given text.
16
+ * Recommended model: espnet/kan-bayashi_ljspeech_vits
17
+ */
18
+ export async function textToSpeech(args: TextToSpeechArgs, options?: Options): Promise<TextToSpeechOutput> {
19
+ const res = await request<TextToSpeechOutput>(args, options);
20
+ const isValidOutput = res && res instanceof Blob;
21
+ if (!isValidOutput) {
22
+ throw new InferenceOutputError("Expected Blob");
23
+ }
24
+ return res;
25
+ }
@@ -65,7 +65,11 @@ export async function* streamingRequest<T>(
65
65
  onChunk(value);
66
66
  for (const event of events) {
67
67
  if (event.data.length > 0) {
68
- yield JSON.parse(event.data) as T;
68
+ const data = JSON.parse(event.data);
69
+ if (typeof data === "object" && data !== null && "error" in data) {
70
+ throw new Error(data.error);
71
+ }
72
+ yield data as T;
69
73
  }
70
74
  }
71
75
  events = [];
@@ -5,8 +5,9 @@ export * from "./custom/streamingRequest";
5
5
  // Audio tasks
6
6
  export * from "./audio/audioClassification";
7
7
  export * from "./audio/automaticSpeechRecognition";
8
+ export * from "./audio/textToSpeech";
8
9
 
9
- // Commputer Vision tasks
10
+ // Computer Vision tasks
10
11
  export * from "./cv/imageClassification";
11
12
  export * from "./cv/imageSegmentation";
12
13
  export * from "./cv/imageToText";
@@ -31,3 +32,6 @@ export * from "./nlp/zeroShotClassification";
31
32
  // Multimodal tasks
32
33
  export * from "./multimodal/documentQuestionAnswering";
33
34
  export * from "./multimodal/visualQuestionAnswering";
35
+
36
+ // Tabular tasks
37
+ export * from "./tabular/tabularRegression";
@@ -2,7 +2,7 @@ import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
2
  import type { BaseArgs, Options } from "../../types";
3
3
  import { request } from "../custom/request";
4
4
  import type { RequestArgs } from "../../types";
5
- import { base64FromBytes } from "../../utils/base64FromBytes";
5
+ import { base64FromBytes } from "@huggingface/shared";
6
6
  import { toArray } from "../../utils/toArray";
7
7
 
8
8
  export type DocumentQuestionAnsweringArgs = BaseArgs & {
@@ -1,7 +1,7 @@
1
1
  import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
2
  import type { BaseArgs, Options, RequestArgs } from "../../types";
3
3
  import { request } from "../custom/request";
4
- import { base64FromBytes } from "../../utils/base64FromBytes";
4
+ import { base64FromBytes } from "@huggingface/shared";
5
5
 
6
6
  export type VisualQuestionAnsweringArgs = BaseArgs & {
7
7
  inputs: {
@@ -44,6 +44,10 @@ export type TextGenerationArgs = BaseArgs & {
44
44
  * (Default: None). Float to define the tokens that are within the sample operation of text generation. Add tokens in the sample for more probable to least probable until the sum of the probabilities is greater than top_p.
45
45
  */
46
46
  top_p?: number;
47
+ /**
48
+ * (Default: None). Integer. The maximum number of tokens from the input.
49
+ */
50
+ truncate?: number;
47
51
  };
48
52
  };
49
53
 
@@ -0,0 +1,34 @@
1
+ import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import type { BaseArgs, Options } from "../../types";
3
+ import { request } from "../custom/request";
4
+
5
+ export type TabularRegressionArgs = BaseArgs & {
6
+ inputs: {
7
+ /**
8
+ * A table of data represented as a dict of list where entries are headers and the lists are all the values, all lists must have the same size.
9
+ */
10
+ data: Record<string, string[]>;
11
+ };
12
+ };
13
+
14
+ /**
15
+ * a list of predicted values for each row
16
+ */
17
+ export type TabularRegressionOutput = number[];
18
+
19
+ /**
20
+ * Predicts target value for a given set of features in tabular form.
21
+ * Typically, you will want to train a regression model on your training data and use it with your new data of the same format.
22
+ * Example model: scikit-learn/Fish-Weight
23
+ */
24
+ export async function tabularRegression(
25
+ args: TabularRegressionArgs,
26
+ options?: Options
27
+ ): Promise<TabularRegressionOutput> {
28
+ const res = await request<TabularRegressionOutput>(args, options);
29
+ const isValidOutput = Array.isArray(res) && res.every((x) => typeof x === "number");
30
+ if (!isValidOutput) {
31
+ throw new InferenceOutputError("Expected number[]");
32
+ }
33
+ return res;
34
+ }