@huggingface/tasks 0.12.22 → 0.12.24
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/index.cjs +503 -132
- package/dist/index.js +503 -132
- package/dist/src/hardware.d.ts +20 -0
- package/dist/src/hardware.d.ts.map +1 -1
- package/dist/src/model-libraries-snippets.d.ts +1 -0
- package/dist/src/model-libraries-snippets.d.ts.map +1 -1
- package/dist/src/model-libraries.d.ts +9 -2
- package/dist/src/model-libraries.d.ts.map +1 -1
- package/dist/src/snippets/common.d.ts +20 -0
- package/dist/src/snippets/common.d.ts.map +1 -0
- package/dist/src/snippets/curl.d.ts +15 -8
- package/dist/src/snippets/curl.d.ts.map +1 -1
- package/dist/src/snippets/js.d.ts +17 -10
- package/dist/src/snippets/js.d.ts.map +1 -1
- package/dist/src/snippets/python.d.ts +20 -13
- package/dist/src/snippets/python.d.ts.map +1 -1
- package/dist/src/snippets/types.d.ts +4 -0
- package/dist/src/snippets/types.d.ts.map +1 -1
- package/dist/src/tasks/depth-estimation/data.d.ts.map +1 -1
- package/dist/src/tasks/image-segmentation/data.d.ts.map +1 -1
- package/dist/src/tasks/image-text-to-text/data.d.ts.map +1 -1
- package/dist/src/tasks/object-detection/data.d.ts.map +1 -1
- package/dist/src/tasks/text-to-speech/data.d.ts.map +1 -1
- package/dist/src/tasks/token-classification/inference.d.ts +8 -5
- package/dist/src/tasks/token-classification/inference.d.ts.map +1 -1
- package/dist/src/tasks/video-text-to-text/data.d.ts.map +1 -1
- package/dist/src/tasks/visual-question-answering/inference.d.ts +0 -1
- package/dist/src/tasks/visual-question-answering/inference.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/hardware.ts +20 -0
- package/src/model-libraries-snippets.ts +28 -3
- package/src/model-libraries.ts +8 -1
- package/src/snippets/common.ts +63 -0
- package/src/snippets/curl.ts +71 -26
- package/src/snippets/js.ts +165 -40
- package/src/snippets/python.ts +186 -48
- package/src/snippets/types.ts +5 -0
- package/src/tasks/depth-estimation/data.ts +15 -7
- package/src/tasks/image-segmentation/about.md +1 -1
- package/src/tasks/image-segmentation/data.ts +10 -9
- package/src/tasks/image-text-to-text/data.ts +17 -9
- package/src/tasks/keypoint-detection/data.ts +1 -1
- package/src/tasks/object-detection/data.ts +5 -4
- package/src/tasks/text-generation/data.ts +7 -7
- package/src/tasks/text-to-image/data.ts +2 -2
- package/src/tasks/text-to-speech/data.ts +5 -1
- package/src/tasks/text-to-video/data.ts +10 -10
- package/src/tasks/token-classification/inference.ts +8 -5
- package/src/tasks/token-classification/spec/output.json +6 -2
- package/src/tasks/video-text-to-text/data.ts +8 -0
- package/src/tasks/visual-question-answering/inference.ts +0 -1
- package/src/tasks/visual-question-answering/spec/output.json +1 -1
package/src/snippets/curl.ts
CHANGED
|
@@ -1,36 +1,73 @@
|
|
|
1
1
|
import type { PipelineType } from "../pipelines.js";
|
|
2
|
+
import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks/index.js";
|
|
3
|
+
import { stringifyGenerationConfig, stringifyMessages } from "./common.js";
|
|
2
4
|
import { getModelInputSnippet } from "./inputs.js";
|
|
3
|
-
import type { ModelDataMinimal } from "./types.js";
|
|
5
|
+
import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
|
|
4
6
|
|
|
5
|
-
export const snippetBasic = (model: ModelDataMinimal, accessToken: string):
|
|
6
|
-
`curl https://api-inference.huggingface.co/models/${model.id} \\
|
|
7
|
+
export const snippetBasic = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => ({
|
|
8
|
+
content: `curl https://api-inference.huggingface.co/models/${model.id} \\
|
|
7
9
|
-X POST \\
|
|
8
10
|
-d '{"inputs": ${getModelInputSnippet(model, true)}}' \\
|
|
9
11
|
-H 'Content-Type: application/json' \\
|
|
10
|
-
-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"
|
|
12
|
+
-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"`,
|
|
13
|
+
});
|
|
11
14
|
|
|
12
|
-
export const snippetTextGeneration = (
|
|
15
|
+
export const snippetTextGeneration = (
|
|
16
|
+
model: ModelDataMinimal,
|
|
17
|
+
accessToken: string,
|
|
18
|
+
opts?: {
|
|
19
|
+
streaming?: boolean;
|
|
20
|
+
messages?: ChatCompletionInputMessage[];
|
|
21
|
+
temperature?: GenerationParameters["temperature"];
|
|
22
|
+
max_tokens?: GenerationParameters["max_tokens"];
|
|
23
|
+
top_p?: GenerationParameters["top_p"];
|
|
24
|
+
}
|
|
25
|
+
): InferenceSnippet => {
|
|
13
26
|
if (model.tags.includes("conversational")) {
|
|
14
27
|
// Conversational model detected, so we display a code snippet that features the Messages API
|
|
15
|
-
|
|
28
|
+
const streaming = opts?.streaming ?? true;
|
|
29
|
+
const messages: ChatCompletionInputMessage[] = opts?.messages ?? [
|
|
30
|
+
{ role: "user", content: "What is the capital of France?" },
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
const config = {
|
|
34
|
+
...(opts?.temperature ? { temperature: opts.temperature } : undefined),
|
|
35
|
+
max_tokens: opts?.max_tokens ?? 500,
|
|
36
|
+
...(opts?.top_p ? { top_p: opts.top_p } : undefined),
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
content: `curl 'https://api-inference.huggingface.co/models/${model.id}/v1/chat/completions' \\
|
|
16
40
|
-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}" \\
|
|
17
41
|
-H 'Content-Type: application/json' \\
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
42
|
+
--data '{
|
|
43
|
+
"model": "${model.id}",
|
|
44
|
+
"messages": ${stringifyMessages(messages, {
|
|
45
|
+
sep: ",\n\t\t",
|
|
46
|
+
start: `[\n\t\t`,
|
|
47
|
+
end: `\n\t]`,
|
|
48
|
+
attributeKeyQuotes: true,
|
|
49
|
+
customContentEscaper: (str) => str.replace(/'/g, "'\\''"),
|
|
50
|
+
})},
|
|
51
|
+
${stringifyGenerationConfig(config, {
|
|
52
|
+
sep: ",\n ",
|
|
53
|
+
start: "",
|
|
54
|
+
end: "",
|
|
55
|
+
attributeKeyQuotes: true,
|
|
56
|
+
attributeValueConnector: ": ",
|
|
57
|
+
})},
|
|
58
|
+
"stream": ${!!streaming}
|
|
59
|
+
}'`,
|
|
60
|
+
};
|
|
25
61
|
} else {
|
|
26
62
|
return snippetBasic(model, accessToken);
|
|
27
63
|
}
|
|
28
64
|
};
|
|
29
65
|
|
|
30
|
-
export const snippetImageTextToTextGeneration = (model: ModelDataMinimal, accessToken: string):
|
|
66
|
+
export const snippetImageTextToTextGeneration = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => {
|
|
31
67
|
if (model.tags.includes("conversational")) {
|
|
32
68
|
// Conversational model detected, so we display a code snippet that features the Messages API
|
|
33
|
-
return
|
|
69
|
+
return {
|
|
70
|
+
content: `curl 'https://api-inference.huggingface.co/models/${model.id}/v1/chat/completions' \\
|
|
34
71
|
-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}" \\
|
|
35
72
|
-H 'Content-Type: application/json' \\
|
|
36
73
|
-d '{
|
|
@@ -47,26 +84,34 @@ export const snippetImageTextToTextGeneration = (model: ModelDataMinimal, access
|
|
|
47
84
|
"max_tokens": 500,
|
|
48
85
|
"stream": false
|
|
49
86
|
}'
|
|
50
|
-
|
|
87
|
+
`,
|
|
88
|
+
};
|
|
51
89
|
} else {
|
|
52
90
|
return snippetBasic(model, accessToken);
|
|
53
91
|
}
|
|
54
92
|
};
|
|
55
93
|
|
|
56
|
-
export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string):
|
|
57
|
-
`curl https://api-inference.huggingface.co/models/${model.id} \\
|
|
94
|
+
export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => ({
|
|
95
|
+
content: `curl https://api-inference.huggingface.co/models/${model.id} \\
|
|
58
96
|
-X POST \\
|
|
59
97
|
-d '{"inputs": ${getModelInputSnippet(model, true)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}' \\
|
|
60
98
|
-H 'Content-Type: application/json' \\
|
|
61
|
-
-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"
|
|
99
|
+
-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"`,
|
|
100
|
+
});
|
|
62
101
|
|
|
63
|
-
export const snippetFile = (model: ModelDataMinimal, accessToken: string):
|
|
64
|
-
`curl https://api-inference.huggingface.co/models/${model.id} \\
|
|
102
|
+
export const snippetFile = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => ({
|
|
103
|
+
content: `curl https://api-inference.huggingface.co/models/${model.id} \\
|
|
65
104
|
-X POST \\
|
|
66
105
|
--data-binary '@${getModelInputSnippet(model, true, true)}' \\
|
|
67
|
-
-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"
|
|
106
|
+
-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"`,
|
|
107
|
+
});
|
|
68
108
|
|
|
69
|
-
export const curlSnippets: Partial<
|
|
109
|
+
export const curlSnippets: Partial<
|
|
110
|
+
Record<
|
|
111
|
+
PipelineType,
|
|
112
|
+
(model: ModelDataMinimal, accessToken: string, opts?: Record<string, unknown>) => InferenceSnippet
|
|
113
|
+
>
|
|
114
|
+
> = {
|
|
70
115
|
// Same order as in js/src/lib/interfaces/Types.ts
|
|
71
116
|
"text-classification": snippetBasic,
|
|
72
117
|
"token-classification": snippetBasic,
|
|
@@ -93,10 +138,10 @@ export const curlSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal
|
|
|
93
138
|
"image-segmentation": snippetFile,
|
|
94
139
|
};
|
|
95
140
|
|
|
96
|
-
export function getCurlInferenceSnippet(model: ModelDataMinimal, accessToken: string):
|
|
141
|
+
export function getCurlInferenceSnippet(model: ModelDataMinimal, accessToken: string): InferenceSnippet {
|
|
97
142
|
return model.pipeline_tag && model.pipeline_tag in curlSnippets
|
|
98
|
-
? curlSnippets[model.pipeline_tag]?.(model, accessToken) ?? ""
|
|
99
|
-
: "";
|
|
143
|
+
? curlSnippets[model.pipeline_tag]?.(model, accessToken) ?? { content: "" }
|
|
144
|
+
: { content: "" };
|
|
100
145
|
}
|
|
101
146
|
|
|
102
147
|
export function hasCurlInferenceSnippet(model: Pick<ModelDataMinimal, "pipeline_tag">): boolean {
|
package/src/snippets/js.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { PipelineType } from "../pipelines.js";
|
|
2
|
+
import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks/index.js";
|
|
3
|
+
import { stringifyGenerationConfig, stringifyMessages } from "./common.js";
|
|
2
4
|
import { getModelInputSnippet } from "./inputs.js";
|
|
3
|
-
import type { ModelDataMinimal } from "./types.js";
|
|
5
|
+
import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
|
|
4
6
|
|
|
5
|
-
export const snippetBasic = (model: ModelDataMinimal, accessToken: string):
|
|
6
|
-
`async function query(data) {
|
|
7
|
+
export const snippetBasic = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => ({
|
|
8
|
+
content: `async function query(data) {
|
|
7
9
|
const response = await fetch(
|
|
8
10
|
"https://api-inference.huggingface.co/models/${model.id}",
|
|
9
11
|
{
|
|
@@ -21,31 +23,136 @@ export const snippetBasic = (model: ModelDataMinimal, accessToken: string): stri
|
|
|
21
23
|
|
|
22
24
|
query({"inputs": ${getModelInputSnippet(model)}}).then((response) => {
|
|
23
25
|
console.log(JSON.stringify(response));
|
|
24
|
-
})
|
|
26
|
+
});`,
|
|
27
|
+
});
|
|
25
28
|
|
|
26
|
-
export const snippetTextGeneration = (
|
|
29
|
+
export const snippetTextGeneration = (
|
|
30
|
+
model: ModelDataMinimal,
|
|
31
|
+
accessToken: string,
|
|
32
|
+
opts?: {
|
|
33
|
+
streaming?: boolean;
|
|
34
|
+
messages?: ChatCompletionInputMessage[];
|
|
35
|
+
temperature?: GenerationParameters["temperature"];
|
|
36
|
+
max_tokens?: GenerationParameters["max_tokens"];
|
|
37
|
+
top_p?: GenerationParameters["top_p"];
|
|
38
|
+
}
|
|
39
|
+
): InferenceSnippet | InferenceSnippet[] => {
|
|
27
40
|
if (model.tags.includes("conversational")) {
|
|
28
41
|
// Conversational model detected, so we display a code snippet that features the Messages API
|
|
29
|
-
|
|
42
|
+
const streaming = opts?.streaming ?? true;
|
|
43
|
+
const messages: ChatCompletionInputMessage[] = opts?.messages ?? [
|
|
44
|
+
{ role: "user", content: "What is the capital of France?" },
|
|
45
|
+
];
|
|
46
|
+
const messagesStr = stringifyMessages(messages, { sep: ",\n\t\t", start: "[\n\t\t", end: "\n\t]" });
|
|
30
47
|
|
|
31
|
-
const
|
|
48
|
+
const config = {
|
|
49
|
+
...(opts?.temperature ? { temperature: opts.temperature } : undefined),
|
|
50
|
+
max_tokens: opts?.max_tokens ?? 500,
|
|
51
|
+
...(opts?.top_p ? { top_p: opts.top_p } : undefined),
|
|
52
|
+
};
|
|
53
|
+
const configStr = stringifyGenerationConfig(config, {
|
|
54
|
+
sep: ",\n\t",
|
|
55
|
+
start: "",
|
|
56
|
+
end: "",
|
|
57
|
+
attributeValueConnector: ": ",
|
|
58
|
+
});
|
|
32
59
|
|
|
33
|
-
|
|
60
|
+
if (streaming) {
|
|
61
|
+
return [
|
|
62
|
+
{
|
|
63
|
+
client: "huggingface_hub",
|
|
64
|
+
content: `import { HfInference } from "@huggingface/inference"
|
|
65
|
+
|
|
66
|
+
const client = new HfInference("${accessToken || `{API_TOKEN}`}")
|
|
67
|
+
|
|
68
|
+
let out = "";
|
|
69
|
+
|
|
70
|
+
const stream = client.chatCompletionStream({
|
|
34
71
|
model: "${model.id}",
|
|
35
|
-
messages:
|
|
36
|
-
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
|
|
72
|
+
messages: ${messagesStr},
|
|
73
|
+
${configStr}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
for await (const chunk of stream) {
|
|
77
|
+
if (chunk.choices && chunk.choices.length > 0) {
|
|
78
|
+
const newContent = chunk.choices[0].delta.content;
|
|
79
|
+
out += newContent;
|
|
80
|
+
console.log(newContent);
|
|
81
|
+
}
|
|
82
|
+
}`,
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
client: "openai",
|
|
86
|
+
content: `import { OpenAI } from "openai"
|
|
87
|
+
|
|
88
|
+
const client = new OpenAI({
|
|
89
|
+
baseURL: "https://api-inference.huggingface.co/v1/",
|
|
90
|
+
apiKey: "${accessToken || `{API_TOKEN}`}"
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
let out = "";
|
|
94
|
+
|
|
95
|
+
const stream = await client.chat.completions.create({
|
|
96
|
+
model: "${model.id}",
|
|
97
|
+
messages: ${messagesStr},
|
|
98
|
+
${configStr},
|
|
99
|
+
stream: true,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
for await (const chunk of stream) {
|
|
103
|
+
if (chunk.choices && chunk.choices.length > 0) {
|
|
104
|
+
const newContent = chunk.choices[0].delta.content;
|
|
105
|
+
out += newContent;
|
|
106
|
+
console.log(newContent);
|
|
107
|
+
}
|
|
108
|
+
}`,
|
|
109
|
+
},
|
|
110
|
+
];
|
|
111
|
+
} else {
|
|
112
|
+
return [
|
|
113
|
+
{
|
|
114
|
+
client: "huggingface_hub",
|
|
115
|
+
content: `import { HfInference } from '@huggingface/inference'
|
|
116
|
+
|
|
117
|
+
const client = new HfInference("${accessToken || `{API_TOKEN}`}")
|
|
118
|
+
|
|
119
|
+
const chatCompletion = await client.chatCompletion({
|
|
120
|
+
model: "${model.id}",
|
|
121
|
+
messages: ${messagesStr},
|
|
122
|
+
${configStr}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
console.log(chatCompletion.choices[0].message);`,
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
client: "openai",
|
|
129
|
+
content: `import { OpenAI } from "openai"
|
|
130
|
+
|
|
131
|
+
const client = new OpenAI({
|
|
132
|
+
baseURL: "https://api-inference.huggingface.co/v1/",
|
|
133
|
+
apiKey: "${accessToken || `{API_TOKEN}`}"
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
const chatCompletion = await client.chat.completions.create({
|
|
137
|
+
model: "${model.id}",
|
|
138
|
+
messages: ${messagesStr},
|
|
139
|
+
${configStr}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
console.log(chatCompletion.choices[0].message);`,
|
|
143
|
+
},
|
|
144
|
+
];
|
|
145
|
+
}
|
|
40
146
|
} else {
|
|
41
147
|
return snippetBasic(model, accessToken);
|
|
42
148
|
}
|
|
43
149
|
};
|
|
44
150
|
|
|
45
|
-
export const snippetImageTextToTextGeneration = (model: ModelDataMinimal, accessToken: string):
|
|
151
|
+
export const snippetImageTextToTextGeneration = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => {
|
|
46
152
|
if (model.tags.includes("conversational")) {
|
|
47
153
|
// Conversational model detected, so we display a code snippet that features the Messages API
|
|
48
|
-
return
|
|
154
|
+
return {
|
|
155
|
+
content: `import { HfInference } from "@huggingface/inference";
|
|
49
156
|
|
|
50
157
|
const inference = new HfInference("${accessToken || `{API_TOKEN}`}");
|
|
51
158
|
const imageUrl = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg";
|
|
@@ -64,14 +171,15 @@ for await (const chunk of inference.chatCompletionStream({
|
|
|
64
171
|
max_tokens: 500,
|
|
65
172
|
})) {
|
|
66
173
|
process.stdout.write(chunk.choices[0]?.delta?.content || "");
|
|
67
|
-
}
|
|
174
|
+
}`,
|
|
175
|
+
};
|
|
68
176
|
} else {
|
|
69
177
|
return snippetBasic(model, accessToken);
|
|
70
178
|
}
|
|
71
179
|
};
|
|
72
180
|
|
|
73
|
-
export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string):
|
|
74
|
-
`async function query(data) {
|
|
181
|
+
export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => ({
|
|
182
|
+
content: `async function query(data) {
|
|
75
183
|
const response = await fetch(
|
|
76
184
|
"https://api-inference.huggingface.co/models/${model.id}",
|
|
77
185
|
{
|
|
@@ -91,10 +199,11 @@ query({"inputs": ${getModelInputSnippet(
|
|
|
91
199
|
model
|
|
92
200
|
)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}).then((response) => {
|
|
93
201
|
console.log(JSON.stringify(response));
|
|
94
|
-
})
|
|
202
|
+
});`,
|
|
203
|
+
});
|
|
95
204
|
|
|
96
|
-
export const snippetTextToImage = (model: ModelDataMinimal, accessToken: string):
|
|
97
|
-
`async function query(data) {
|
|
205
|
+
export const snippetTextToImage = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => ({
|
|
206
|
+
content: `async function query(data) {
|
|
98
207
|
const response = await fetch(
|
|
99
208
|
"https://api-inference.huggingface.co/models/${model.id}",
|
|
100
209
|
{
|
|
@@ -111,9 +220,10 @@ export const snippetTextToImage = (model: ModelDataMinimal, accessToken: string)
|
|
|
111
220
|
}
|
|
112
221
|
query({"inputs": ${getModelInputSnippet(model)}}).then((response) => {
|
|
113
222
|
// Use image
|
|
114
|
-
})
|
|
223
|
+
});`,
|
|
224
|
+
});
|
|
115
225
|
|
|
116
|
-
export const snippetTextToAudio = (model: ModelDataMinimal, accessToken: string):
|
|
226
|
+
export const snippetTextToAudio = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => {
|
|
117
227
|
const commonSnippet = `async function query(data) {
|
|
118
228
|
const response = await fetch(
|
|
119
229
|
"https://api-inference.huggingface.co/models/${model.id}",
|
|
@@ -127,33 +237,35 @@ export const snippetTextToAudio = (model: ModelDataMinimal, accessToken: string)
|
|
|
127
237
|
}
|
|
128
238
|
);`;
|
|
129
239
|
if (model.library_name === "transformers") {
|
|
130
|
-
return
|
|
131
|
-
|
|
132
|
-
|
|
240
|
+
return {
|
|
241
|
+
content:
|
|
242
|
+
commonSnippet +
|
|
243
|
+
`
|
|
133
244
|
const result = await response.blob();
|
|
134
245
|
return result;
|
|
135
246
|
}
|
|
136
247
|
query({"inputs": ${getModelInputSnippet(model)}}).then((response) => {
|
|
137
248
|
// Returns a byte object of the Audio wavform. Use it directly!
|
|
138
|
-
})
|
|
139
|
-
|
|
249
|
+
});`,
|
|
250
|
+
};
|
|
140
251
|
} else {
|
|
141
|
-
return
|
|
142
|
-
|
|
143
|
-
|
|
252
|
+
return {
|
|
253
|
+
content:
|
|
254
|
+
commonSnippet +
|
|
255
|
+
`
|
|
144
256
|
const result = await response.json();
|
|
145
257
|
return result;
|
|
146
258
|
}
|
|
147
259
|
|
|
148
260
|
query({"inputs": ${getModelInputSnippet(model)}}).then((response) => {
|
|
149
261
|
console.log(JSON.stringify(response));
|
|
150
|
-
})
|
|
151
|
-
|
|
262
|
+
});`,
|
|
263
|
+
};
|
|
152
264
|
}
|
|
153
265
|
};
|
|
154
266
|
|
|
155
|
-
export const snippetFile = (model: ModelDataMinimal, accessToken: string):
|
|
156
|
-
`async function query(filename) {
|
|
267
|
+
export const snippetFile = (model: ModelDataMinimal, accessToken: string): InferenceSnippet => ({
|
|
268
|
+
content: `async function query(filename) {
|
|
157
269
|
const data = fs.readFileSync(filename);
|
|
158
270
|
const response = await fetch(
|
|
159
271
|
"https://api-inference.huggingface.co/models/${model.id}",
|
|
@@ -172,9 +284,19 @@ export const snippetFile = (model: ModelDataMinimal, accessToken: string): strin
|
|
|
172
284
|
|
|
173
285
|
query(${getModelInputSnippet(model)}).then((response) => {
|
|
174
286
|
console.log(JSON.stringify(response));
|
|
175
|
-
})
|
|
287
|
+
});`,
|
|
288
|
+
});
|
|
176
289
|
|
|
177
|
-
export const jsSnippets: Partial<
|
|
290
|
+
export const jsSnippets: Partial<
|
|
291
|
+
Record<
|
|
292
|
+
PipelineType,
|
|
293
|
+
(
|
|
294
|
+
model: ModelDataMinimal,
|
|
295
|
+
accessToken: string,
|
|
296
|
+
opts?: Record<string, unknown>
|
|
297
|
+
) => InferenceSnippet | InferenceSnippet[]
|
|
298
|
+
>
|
|
299
|
+
> = {
|
|
178
300
|
// Same order as in js/src/lib/interfaces/Types.ts
|
|
179
301
|
"text-classification": snippetBasic,
|
|
180
302
|
"token-classification": snippetBasic,
|
|
@@ -201,10 +323,13 @@ export const jsSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal,
|
|
|
201
323
|
"image-segmentation": snippetFile,
|
|
202
324
|
};
|
|
203
325
|
|
|
204
|
-
export function getJsInferenceSnippet(
|
|
326
|
+
export function getJsInferenceSnippet(
|
|
327
|
+
model: ModelDataMinimal,
|
|
328
|
+
accessToken: string
|
|
329
|
+
): InferenceSnippet | InferenceSnippet[] {
|
|
205
330
|
return model.pipeline_tag && model.pipeline_tag in jsSnippets
|
|
206
|
-
? jsSnippets[model.pipeline_tag]?.(model, accessToken) ?? ""
|
|
207
|
-
: "";
|
|
331
|
+
? jsSnippets[model.pipeline_tag]?.(model, accessToken) ?? { content: "" }
|
|
332
|
+
: { content: "" };
|
|
208
333
|
}
|
|
209
334
|
|
|
210
335
|
export function hasJsInferenceSnippet(model: ModelDataMinimal): boolean {
|