@huggingface/inference 2.6.3 → 2.6.5
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 +247 -89
- package/dist/{index.mjs → index.cjs} +58 -19
- package/dist/index.d.ts +47 -139
- package/dist/index.js +6 -72
- package/package.json +13 -15
- package/src/lib/getDefaultTask.ts +1 -1
- package/src/lib/makeRequestOptions.ts +1 -0
- package/src/tasks/custom/request.ts +1 -1
- package/src/tasks/custom/streamingRequest.ts +1 -1
- package/src/tasks/index.ts +0 -1
- package/src/tasks/nlp/textGeneration.ts +5 -57
- package/src/tasks/nlp/textGenerationStream.ts +4 -3
- package/src/tasks/nlp/translation.ts +6 -4
- package/src/types.ts +9 -35
- package/src/tasks/nlp/conversational.ts +0 -81
|
@@ -6,21 +6,23 @@ export type TranslationArgs = BaseArgs & {
|
|
|
6
6
|
/**
|
|
7
7
|
* A string to be translated
|
|
8
8
|
*/
|
|
9
|
-
inputs: string;
|
|
9
|
+
inputs: string | string[];
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
export interface
|
|
12
|
+
export interface TranslationOutputValue {
|
|
13
13
|
/**
|
|
14
14
|
* The string after translation
|
|
15
15
|
*/
|
|
16
16
|
translation_text: string;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
export type TranslationOutput = TranslationOutputValue | TranslationOutputValue[];
|
|
20
|
+
|
|
19
21
|
/**
|
|
20
22
|
* This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en.
|
|
21
23
|
*/
|
|
22
24
|
export async function translation(args: TranslationArgs, options?: Options): Promise<TranslationOutput> {
|
|
23
|
-
const res = await request<
|
|
25
|
+
const res = await request<TranslationOutputValue[]>(args, {
|
|
24
26
|
...options,
|
|
25
27
|
taskHint: "translation",
|
|
26
28
|
});
|
|
@@ -28,5 +30,5 @@ export async function translation(args: TranslationArgs, options?: Options): Pro
|
|
|
28
30
|
if (!isValidOutput) {
|
|
29
31
|
throw new InferenceOutputError("Expected type Array<{translation_text: string}>");
|
|
30
32
|
}
|
|
31
|
-
return res?.[0];
|
|
33
|
+
return res?.length === 1 ? res?.[0] : res;
|
|
32
34
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import type { PipelineType } from "@huggingface/tasks";
|
|
2
|
+
|
|
1
3
|
export interface Options {
|
|
2
4
|
/**
|
|
3
5
|
* (Default: true) Boolean. If a request 503s and wait_for_model is set to false, the request will be retried with the same parameters but with wait_for_model set to true.
|
|
4
6
|
*/
|
|
5
7
|
retry_on_error?: boolean;
|
|
6
8
|
/**
|
|
7
|
-
* (Default: true). Boolean. There is a cache layer on
|
|
9
|
+
* (Default: true). Boolean. There is a cache layer on Inference API (serverless) to speedup requests we have already seen. Most models can use those results as is as models are deterministic (meaning the results will be the same anyway). However if you use a non deterministic model, you can set this parameter to prevent the caching mechanism from being used resulting in a real new query.
|
|
8
10
|
*/
|
|
9
11
|
use_cache?: boolean;
|
|
10
12
|
/**
|
|
@@ -24,6 +26,10 @@ export interface Options {
|
|
|
24
26
|
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
|
|
25
27
|
*/
|
|
26
28
|
fetch?: typeof fetch;
|
|
29
|
+
/**
|
|
30
|
+
* Abort Controller signal to use for request interruption.
|
|
31
|
+
*/
|
|
32
|
+
signal?: AbortSignal;
|
|
27
33
|
|
|
28
34
|
/**
|
|
29
35
|
* (Default: "same-origin"). String | Boolean. Credentials to use for the request. If this is a string, it will be passed straight on. If it's a boolean, true will be "include" and false will not send credentials at all.
|
|
@@ -31,39 +37,7 @@ export interface Options {
|
|
|
31
37
|
includeCredentials?: string | boolean;
|
|
32
38
|
}
|
|
33
39
|
|
|
34
|
-
export type InferenceTask =
|
|
35
|
-
| "audio-classification"
|
|
36
|
-
| "audio-to-audio"
|
|
37
|
-
| "automatic-speech-recognition"
|
|
38
|
-
| "conversational"
|
|
39
|
-
| "depth-estimation"
|
|
40
|
-
| "document-question-answering"
|
|
41
|
-
| "feature-extraction"
|
|
42
|
-
| "fill-mask"
|
|
43
|
-
| "image-classification"
|
|
44
|
-
| "image-segmentation"
|
|
45
|
-
| "image-to-image"
|
|
46
|
-
| "image-to-text"
|
|
47
|
-
| "object-detection"
|
|
48
|
-
| "video-classification"
|
|
49
|
-
| "question-answering"
|
|
50
|
-
| "reinforcement-learning"
|
|
51
|
-
| "sentence-similarity"
|
|
52
|
-
| "summarization"
|
|
53
|
-
| "table-question-answering"
|
|
54
|
-
| "tabular-classification"
|
|
55
|
-
| "tabular-regression"
|
|
56
|
-
| "text-classification"
|
|
57
|
-
| "text-generation"
|
|
58
|
-
| "text-to-image"
|
|
59
|
-
| "text-to-speech"
|
|
60
|
-
| "text-to-video"
|
|
61
|
-
| "token-classification"
|
|
62
|
-
| "translation"
|
|
63
|
-
| "unconditional-image-generation"
|
|
64
|
-
| "visual-question-answering"
|
|
65
|
-
| "zero-shot-classification"
|
|
66
|
-
| "zero-shot-image-classification";
|
|
40
|
+
export type InferenceTask = Exclude<PipelineType, "other">;
|
|
67
41
|
|
|
68
42
|
export interface BaseArgs {
|
|
69
43
|
/**
|
|
@@ -73,7 +47,7 @@ export interface BaseArgs {
|
|
|
73
47
|
*/
|
|
74
48
|
accessToken?: string;
|
|
75
49
|
/**
|
|
76
|
-
* The model to use. Can be a full URL for
|
|
50
|
+
* The model to use. Can be a full URL for a dedicated inference endpoint.
|
|
77
51
|
*
|
|
78
52
|
* If not specified, will call huggingface.co/api/tasks to get the default model for the task.
|
|
79
53
|
*/
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { InferenceOutputError } from "../../lib/InferenceOutputError";
|
|
2
|
-
import type { BaseArgs, Options } from "../../types";
|
|
3
|
-
import { request } from "../custom/request";
|
|
4
|
-
|
|
5
|
-
export type ConversationalArgs = BaseArgs & {
|
|
6
|
-
inputs: {
|
|
7
|
-
/**
|
|
8
|
-
* A list of strings corresponding to the earlier replies from the model.
|
|
9
|
-
*/
|
|
10
|
-
generated_responses?: string[];
|
|
11
|
-
/**
|
|
12
|
-
* A list of strings corresponding to the earlier replies from the user. Should be of the same length of generated_responses.
|
|
13
|
-
*/
|
|
14
|
-
past_user_inputs?: string[];
|
|
15
|
-
/**
|
|
16
|
-
* The last input from the user in the conversation.
|
|
17
|
-
*/
|
|
18
|
-
text: string;
|
|
19
|
-
};
|
|
20
|
-
parameters?: {
|
|
21
|
-
/**
|
|
22
|
-
* (Default: None). Integer to define the maximum length in tokens of the output summary.
|
|
23
|
-
*/
|
|
24
|
-
max_length?: number;
|
|
25
|
-
/**
|
|
26
|
-
* (Default: None). Float (0-120.0). The amount of time in seconds that the query should take maximum. Network can cause some overhead so it will be a soft limit.
|
|
27
|
-
*/
|
|
28
|
-
max_time?: number;
|
|
29
|
-
/**
|
|
30
|
-
* (Default: None). Integer to define the minimum length in tokens of the output summary.
|
|
31
|
-
*/
|
|
32
|
-
min_length?: number;
|
|
33
|
-
/**
|
|
34
|
-
* (Default: None). Float (0.0-100.0). The more a token is used within generation the more it is penalized to not be picked in successive generation passes.
|
|
35
|
-
*/
|
|
36
|
-
repetition_penalty?: number;
|
|
37
|
-
/**
|
|
38
|
-
* (Default: 1.0). Float (0.0-100.0). The temperature of the sampling operation. 1 means regular sampling, 0 means always take the highest score, 100.0 is getting closer to uniform probability.
|
|
39
|
-
*/
|
|
40
|
-
temperature?: number;
|
|
41
|
-
/**
|
|
42
|
-
* (Default: None). Integer to define the top tokens considered within the sample operation to create new text.
|
|
43
|
-
*/
|
|
44
|
-
top_k?: number;
|
|
45
|
-
/**
|
|
46
|
-
* (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.
|
|
47
|
-
*/
|
|
48
|
-
top_p?: number;
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export interface ConversationalOutput {
|
|
53
|
-
conversation: {
|
|
54
|
-
generated_responses: string[];
|
|
55
|
-
past_user_inputs: string[];
|
|
56
|
-
};
|
|
57
|
-
generated_text: string;
|
|
58
|
-
warnings: string[];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* This task corresponds to any chatbot like structure. Models tend to have shorter max_length, so please check with caution when using a given model if you need long range dependency or not. Recommended model: microsoft/DialoGPT-large.
|
|
63
|
-
*
|
|
64
|
-
*/
|
|
65
|
-
export async function conversational(args: ConversationalArgs, options?: Options): Promise<ConversationalOutput> {
|
|
66
|
-
const res = await request<ConversationalOutput>(args, { ...options, taskHint: "conversational" });
|
|
67
|
-
const isValidOutput =
|
|
68
|
-
Array.isArray(res.conversation.generated_responses) &&
|
|
69
|
-
res.conversation.generated_responses.every((x) => typeof x === "string") &&
|
|
70
|
-
Array.isArray(res.conversation.past_user_inputs) &&
|
|
71
|
-
res.conversation.past_user_inputs.every((x) => typeof x === "string") &&
|
|
72
|
-
typeof res.generated_text === "string" &&
|
|
73
|
-
(typeof res.warnings === "undefined" ||
|
|
74
|
-
(Array.isArray(res.warnings) && res.warnings.every((x) => typeof x === "string")));
|
|
75
|
-
if (!isValidOutput) {
|
|
76
|
-
throw new InferenceOutputError(
|
|
77
|
-
"Expected {conversation: {generated_responses: string[], past_user_inputs: string[]}, generated_text: string, warnings: string[]}"
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
return res;
|
|
81
|
-
}
|