@huggingface/inference 4.0.1 → 4.0.3
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/package.d.ts +1 -1
- package/dist/commonjs/package.js +1 -1
- package/dist/commonjs/snippets/getInferenceSnippets.d.ts +1 -0
- package/dist/commonjs/snippets/getInferenceSnippets.d.ts.map +1 -1
- package/dist/commonjs/snippets/getInferenceSnippets.js +28 -16
- package/dist/commonjs/snippets/templates.exported.js +1 -1
- package/dist/esm/package.d.ts +1 -1
- package/dist/esm/package.js +1 -1
- package/dist/esm/snippets/getInferenceSnippets.d.ts +1 -0
- package/dist/esm/snippets/getInferenceSnippets.d.ts.map +1 -1
- package/dist/esm/snippets/getInferenceSnippets.js +28 -16
- package/dist/esm/snippets/templates.exported.js +1 -1
- package/package.json +2 -2
- package/src/package.ts +1 -1
- package/src/snippets/getInferenceSnippets.ts +43 -21
- package/src/snippets/templates.exported.ts +1 -1
package/dist/commonjs/package.js
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PACKAGE_NAME = exports.PACKAGE_VERSION = void 0;
|
|
4
4
|
// Generated file from package.json. Issues importing JSON directly when publishing on commonjs/ESM - see https://github.com/microsoft/TypeScript/issues/51783
|
|
5
|
-
exports.PACKAGE_VERSION = "4.0.
|
|
5
|
+
exports.PACKAGE_VERSION = "4.0.3";
|
|
6
6
|
exports.PACKAGE_NAME = "@huggingface/inference";
|
|
@@ -5,6 +5,7 @@ export type InferenceSnippetOptions = {
|
|
|
5
5
|
streaming?: boolean;
|
|
6
6
|
billTo?: string;
|
|
7
7
|
accessToken?: string;
|
|
8
|
+
directRequest?: boolean;
|
|
8
9
|
} & Record<string, unknown>;
|
|
9
10
|
export declare function getInferenceSnippets(model: ModelDataMinimal, provider: InferenceProviderOrPolicy, inferenceProviderMapping?: InferenceProviderModelMapping, opts?: Record<string, unknown>): InferenceSnippet[];
|
|
10
11
|
//# sourceMappingURL=getInferenceSnippets.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,uCAAuC,CAAC;AAG3F,OAAO,KAAK,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAGzF,MAAM,MAAM,uBAAuB,GAAG;
|
|
1
|
+
{"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,uCAAuC,CAAC;AAG3F,OAAO,KAAK,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAGzF,MAAM,MAAM,uBAAuB,GAAG;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAiV5B,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,yBAAyB,EACnC,wBAAwB,CAAC,EAAE,6BAA6B,EACxD,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,gBAAgB,EAAE,CAIpB"}
|
|
@@ -76,7 +76,10 @@ const HF_JS_METHODS = {
|
|
|
76
76
|
"text-to-speech": "textToSpeech",
|
|
77
77
|
translation: "translation",
|
|
78
78
|
};
|
|
79
|
-
|
|
79
|
+
// Placeholders to replace with env variable in snippets
|
|
80
|
+
// little hack to support both direct requests and routing => routed requests should start with "hf_"
|
|
81
|
+
const ACCESS_TOKEN_ROUTING_PLACEHOLDER = "hf_token_placeholder";
|
|
82
|
+
const ACCESS_TOKEN_DIRECT_REQUEST_PLACEHOLDER = "not_hf_token_placeholder";
|
|
80
83
|
// Snippet generators
|
|
81
84
|
const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
82
85
|
return (model, provider, inferenceProviderMapping, opts) => {
|
|
@@ -98,7 +101,10 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
98
101
|
console.error(`Failed to get provider helper for ${provider} (${task})`, e);
|
|
99
102
|
return [];
|
|
100
103
|
}
|
|
101
|
-
const
|
|
104
|
+
const placeholder = opts?.directRequest
|
|
105
|
+
? ACCESS_TOKEN_DIRECT_REQUEST_PLACEHOLDER
|
|
106
|
+
: ACCESS_TOKEN_ROUTING_PLACEHOLDER;
|
|
107
|
+
const accessTokenOrPlaceholder = opts?.accessToken ?? placeholder;
|
|
102
108
|
/// Prepare inputs + make request
|
|
103
109
|
const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: (0, tasks_1.getModelInputSnippet)(model) };
|
|
104
110
|
const request = (0, makeRequestOptions_js_1.makeRequestOptionsFromResolvedModel)(providerModelId, providerHelper, {
|
|
@@ -188,8 +194,8 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
188
194
|
snippet = `${importSection}\n\n${snippet}`;
|
|
189
195
|
}
|
|
190
196
|
/// Replace access token placeholder
|
|
191
|
-
if (snippet.includes(
|
|
192
|
-
snippet = replaceAccessTokenPlaceholder(snippet, language, provider);
|
|
197
|
+
if (snippet.includes(placeholder)) {
|
|
198
|
+
snippet = replaceAccessTokenPlaceholder(opts?.directRequest, placeholder, snippet, language, provider);
|
|
193
199
|
}
|
|
194
200
|
/// Snippet is ready!
|
|
195
201
|
return { language, client: client, content: snippet };
|
|
@@ -316,35 +322,41 @@ function indentString(str) {
|
|
|
316
322
|
function removeSuffix(str, suffix) {
|
|
317
323
|
return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
|
|
318
324
|
}
|
|
319
|
-
function replaceAccessTokenPlaceholder(snippet, language, provider) {
|
|
325
|
+
function replaceAccessTokenPlaceholder(directRequest, placeholder, snippet, language, provider) {
|
|
320
326
|
// If "opts.accessToken" is not set, the snippets are generated with a placeholder.
|
|
321
327
|
// Once snippets are rendered, we replace the placeholder with code to fetch the access token from an environment variable.
|
|
322
328
|
// Determine if HF_TOKEN or specific provider token should be used
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
329
|
+
const useHfToken = provider == "hf-inference" || // hf-inference provider => use $HF_TOKEN
|
|
330
|
+
(!directRequest && // if explicit directRequest => use provider-specific token
|
|
331
|
+
(!snippet.includes("https://") || // no URL provided => using a client => use $HF_TOKEN
|
|
332
|
+
snippet.includes("https://router.huggingface.co"))); // explicit routed request => use $HF_TOKEN
|
|
333
|
+
const accessTokenEnvVar = useHfToken
|
|
334
|
+
? "HF_TOKEN" // e.g. routed request or hf-inference
|
|
327
335
|
: provider.toUpperCase().replace("-", "_") + "_API_KEY"; // e.g. "REPLICATE_API_KEY"
|
|
328
336
|
// Replace the placeholder with the env variable
|
|
329
337
|
if (language === "sh") {
|
|
330
|
-
snippet = snippet.replace(`'Authorization: Bearer ${
|
|
338
|
+
snippet = snippet.replace(`'Authorization: Bearer ${placeholder}'`, `"Authorization: Bearer $${accessTokenEnvVar}"` // e.g. "Authorization: Bearer $HF_TOKEN"
|
|
331
339
|
);
|
|
332
340
|
}
|
|
333
341
|
else if (language === "python") {
|
|
334
342
|
snippet = "import os\n" + snippet;
|
|
335
|
-
snippet = snippet.replace(`"${
|
|
343
|
+
snippet = snippet.replace(`"${placeholder}"`, `os.environ["${accessTokenEnvVar}"]` // e.g. os.environ["HF_TOKEN")
|
|
336
344
|
);
|
|
337
|
-
snippet = snippet.replace(`"Bearer ${
|
|
345
|
+
snippet = snippet.replace(`"Bearer ${placeholder}"`, `f"Bearer {os.environ['${accessTokenEnvVar}']}"` // e.g. f"Bearer {os.environ['HF_TOKEN']}"
|
|
338
346
|
);
|
|
339
|
-
snippet = snippet.replace(`"Key ${
|
|
347
|
+
snippet = snippet.replace(`"Key ${placeholder}"`, `f"Key {os.environ['${accessTokenEnvVar}']}"` // e.g. f"Key {os.environ['FAL_AI_API_KEY']}"
|
|
348
|
+
);
|
|
349
|
+
snippet = snippet.replace(`"X-Key ${placeholder}"`, `f"X-Key {os.environ['${accessTokenEnvVar}']}"` // e.g. f"X-Key {os.environ['BLACK_FOREST_LABS_API_KEY']}"
|
|
340
350
|
);
|
|
341
351
|
}
|
|
342
352
|
else if (language === "js") {
|
|
343
|
-
snippet = snippet.replace(`"${
|
|
353
|
+
snippet = snippet.replace(`"${placeholder}"`, `process.env.${accessTokenEnvVar}` // e.g. process.env.HF_TOKEN
|
|
354
|
+
);
|
|
355
|
+
snippet = snippet.replace(`Authorization: "Bearer ${placeholder}",`, `Authorization: \`Bearer $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `Bearer ${process.env.HF_TOKEN}`,
|
|
344
356
|
);
|
|
345
|
-
snippet = snippet.replace(`Authorization: "
|
|
357
|
+
snippet = snippet.replace(`Authorization: "Key ${placeholder}",`, `Authorization: \`Key $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `Key ${process.env.FAL_AI_API_KEY}`,
|
|
346
358
|
);
|
|
347
|
-
snippet = snippet.replace(`Authorization: "Key ${
|
|
359
|
+
snippet = snippet.replace(`Authorization: "X-Key ${placeholder}",`, `Authorization: \`X-Key $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `X-Key ${process.env.BLACK_FOREST_LABS_AI_API_KEY}`,
|
|
348
360
|
);
|
|
349
361
|
}
|
|
350
362
|
return snippet;
|
|
@@ -42,7 +42,7 @@ exports.templates = {
|
|
|
42
42
|
"imageToImage": "# output is a PIL.Image object\nimage = client.image_to_image(\n \"{{ inputs.asObj.inputs }}\",\n prompt=\"{{ inputs.asObj.parameters.prompt }}\",\n model=\"{{ model.id }}\",\n) ",
|
|
43
43
|
"importInferenceClient": "from huggingface_hub import InferenceClient\n\nclient = InferenceClient(\n provider=\"{{ provider }}\",\n api_key=\"{{ accessToken }}\",\n{% if billTo %}\n bill_to=\"{{ billTo }}\",\n{% endif %}\n)",
|
|
44
44
|
"questionAnswering": "answer = client.question_answering(\n question=\"{{ inputs.asObj.question }}\",\n context=\"{{ inputs.asObj.context }}\",\n model=\"{{ model.id }}\",\n) ",
|
|
45
|
-
"tableQuestionAnswering": "answer = client.
|
|
45
|
+
"tableQuestionAnswering": "answer = client.table_question_answering(\n query=\"{{ inputs.asObj.query }}\",\n table={{ inputs.asObj.table }},\n model=\"{{ model.id }}\",\n) ",
|
|
46
46
|
"textToImage": "# output is a PIL.Image object\nimage = client.text_to_image(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) ",
|
|
47
47
|
"textToSpeech": "# audio is returned as bytes\naudio = client.text_to_speech(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) \n",
|
|
48
48
|
"textToVideo": "video = client.text_to_video(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) "
|
package/dist/esm/package.d.ts
CHANGED
package/dist/esm/package.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// Generated file from package.json. Issues importing JSON directly when publishing on commonjs/ESM - see https://github.com/microsoft/TypeScript/issues/51783
|
|
2
|
-
export const PACKAGE_VERSION = "4.0.
|
|
2
|
+
export const PACKAGE_VERSION = "4.0.3";
|
|
3
3
|
export const PACKAGE_NAME = "@huggingface/inference";
|
|
@@ -5,6 +5,7 @@ export type InferenceSnippetOptions = {
|
|
|
5
5
|
streaming?: boolean;
|
|
6
6
|
billTo?: string;
|
|
7
7
|
accessToken?: string;
|
|
8
|
+
directRequest?: boolean;
|
|
8
9
|
} & Record<string, unknown>;
|
|
9
10
|
export declare function getInferenceSnippets(model: ModelDataMinimal, provider: InferenceProviderOrPolicy, inferenceProviderMapping?: InferenceProviderModelMapping, opts?: Record<string, unknown>): InferenceSnippet[];
|
|
10
11
|
//# sourceMappingURL=getInferenceSnippets.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,uCAAuC,CAAC;AAG3F,OAAO,KAAK,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAGzF,MAAM,MAAM,uBAAuB,GAAG;
|
|
1
|
+
{"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,uCAAuC,CAAC;AAG3F,OAAO,KAAK,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAGzF,MAAM,MAAM,uBAAuB,GAAG;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAiV5B,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,yBAAyB,EACnC,wBAAwB,CAAC,EAAE,6BAA6B,EACxD,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,gBAAgB,EAAE,CAIpB"}
|
|
@@ -73,7 +73,10 @@ const HF_JS_METHODS = {
|
|
|
73
73
|
"text-to-speech": "textToSpeech",
|
|
74
74
|
translation: "translation",
|
|
75
75
|
};
|
|
76
|
-
|
|
76
|
+
// Placeholders to replace with env variable in snippets
|
|
77
|
+
// little hack to support both direct requests and routing => routed requests should start with "hf_"
|
|
78
|
+
const ACCESS_TOKEN_ROUTING_PLACEHOLDER = "hf_token_placeholder";
|
|
79
|
+
const ACCESS_TOKEN_DIRECT_REQUEST_PLACEHOLDER = "not_hf_token_placeholder";
|
|
77
80
|
// Snippet generators
|
|
78
81
|
const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
79
82
|
return (model, provider, inferenceProviderMapping, opts) => {
|
|
@@ -95,7 +98,10 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
95
98
|
console.error(`Failed to get provider helper for ${provider} (${task})`, e);
|
|
96
99
|
return [];
|
|
97
100
|
}
|
|
98
|
-
const
|
|
101
|
+
const placeholder = opts?.directRequest
|
|
102
|
+
? ACCESS_TOKEN_DIRECT_REQUEST_PLACEHOLDER
|
|
103
|
+
: ACCESS_TOKEN_ROUTING_PLACEHOLDER;
|
|
104
|
+
const accessTokenOrPlaceholder = opts?.accessToken ?? placeholder;
|
|
99
105
|
/// Prepare inputs + make request
|
|
100
106
|
const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: getModelInputSnippet(model) };
|
|
101
107
|
const request = makeRequestOptionsFromResolvedModel(providerModelId, providerHelper, {
|
|
@@ -185,8 +191,8 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
185
191
|
snippet = `${importSection}\n\n${snippet}`;
|
|
186
192
|
}
|
|
187
193
|
/// Replace access token placeholder
|
|
188
|
-
if (snippet.includes(
|
|
189
|
-
snippet = replaceAccessTokenPlaceholder(snippet, language, provider);
|
|
194
|
+
if (snippet.includes(placeholder)) {
|
|
195
|
+
snippet = replaceAccessTokenPlaceholder(opts?.directRequest, placeholder, snippet, language, provider);
|
|
190
196
|
}
|
|
191
197
|
/// Snippet is ready!
|
|
192
198
|
return { language, client: client, content: snippet };
|
|
@@ -313,35 +319,41 @@ function indentString(str) {
|
|
|
313
319
|
function removeSuffix(str, suffix) {
|
|
314
320
|
return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
|
|
315
321
|
}
|
|
316
|
-
function replaceAccessTokenPlaceholder(snippet, language, provider) {
|
|
322
|
+
function replaceAccessTokenPlaceholder(directRequest, placeholder, snippet, language, provider) {
|
|
317
323
|
// If "opts.accessToken" is not set, the snippets are generated with a placeholder.
|
|
318
324
|
// Once snippets are rendered, we replace the placeholder with code to fetch the access token from an environment variable.
|
|
319
325
|
// Determine if HF_TOKEN or specific provider token should be used
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
326
|
+
const useHfToken = provider == "hf-inference" || // hf-inference provider => use $HF_TOKEN
|
|
327
|
+
(!directRequest && // if explicit directRequest => use provider-specific token
|
|
328
|
+
(!snippet.includes("https://") || // no URL provided => using a client => use $HF_TOKEN
|
|
329
|
+
snippet.includes("https://router.huggingface.co"))); // explicit routed request => use $HF_TOKEN
|
|
330
|
+
const accessTokenEnvVar = useHfToken
|
|
331
|
+
? "HF_TOKEN" // e.g. routed request or hf-inference
|
|
324
332
|
: provider.toUpperCase().replace("-", "_") + "_API_KEY"; // e.g. "REPLICATE_API_KEY"
|
|
325
333
|
// Replace the placeholder with the env variable
|
|
326
334
|
if (language === "sh") {
|
|
327
|
-
snippet = snippet.replace(`'Authorization: Bearer ${
|
|
335
|
+
snippet = snippet.replace(`'Authorization: Bearer ${placeholder}'`, `"Authorization: Bearer $${accessTokenEnvVar}"` // e.g. "Authorization: Bearer $HF_TOKEN"
|
|
328
336
|
);
|
|
329
337
|
}
|
|
330
338
|
else if (language === "python") {
|
|
331
339
|
snippet = "import os\n" + snippet;
|
|
332
|
-
snippet = snippet.replace(`"${
|
|
340
|
+
snippet = snippet.replace(`"${placeholder}"`, `os.environ["${accessTokenEnvVar}"]` // e.g. os.environ["HF_TOKEN")
|
|
333
341
|
);
|
|
334
|
-
snippet = snippet.replace(`"Bearer ${
|
|
342
|
+
snippet = snippet.replace(`"Bearer ${placeholder}"`, `f"Bearer {os.environ['${accessTokenEnvVar}']}"` // e.g. f"Bearer {os.environ['HF_TOKEN']}"
|
|
335
343
|
);
|
|
336
|
-
snippet = snippet.replace(`"Key ${
|
|
344
|
+
snippet = snippet.replace(`"Key ${placeholder}"`, `f"Key {os.environ['${accessTokenEnvVar}']}"` // e.g. f"Key {os.environ['FAL_AI_API_KEY']}"
|
|
345
|
+
);
|
|
346
|
+
snippet = snippet.replace(`"X-Key ${placeholder}"`, `f"X-Key {os.environ['${accessTokenEnvVar}']}"` // e.g. f"X-Key {os.environ['BLACK_FOREST_LABS_API_KEY']}"
|
|
337
347
|
);
|
|
338
348
|
}
|
|
339
349
|
else if (language === "js") {
|
|
340
|
-
snippet = snippet.replace(`"${
|
|
350
|
+
snippet = snippet.replace(`"${placeholder}"`, `process.env.${accessTokenEnvVar}` // e.g. process.env.HF_TOKEN
|
|
351
|
+
);
|
|
352
|
+
snippet = snippet.replace(`Authorization: "Bearer ${placeholder}",`, `Authorization: \`Bearer $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `Bearer ${process.env.HF_TOKEN}`,
|
|
341
353
|
);
|
|
342
|
-
snippet = snippet.replace(`Authorization: "
|
|
354
|
+
snippet = snippet.replace(`Authorization: "Key ${placeholder}",`, `Authorization: \`Key $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `Key ${process.env.FAL_AI_API_KEY}`,
|
|
343
355
|
);
|
|
344
|
-
snippet = snippet.replace(`Authorization: "Key ${
|
|
356
|
+
snippet = snippet.replace(`Authorization: "X-Key ${placeholder}",`, `Authorization: \`X-Key $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `X-Key ${process.env.BLACK_FOREST_LABS_AI_API_KEY}`,
|
|
345
357
|
);
|
|
346
358
|
}
|
|
347
359
|
return snippet;
|
|
@@ -39,7 +39,7 @@ export const templates = {
|
|
|
39
39
|
"imageToImage": "# output is a PIL.Image object\nimage = client.image_to_image(\n \"{{ inputs.asObj.inputs }}\",\n prompt=\"{{ inputs.asObj.parameters.prompt }}\",\n model=\"{{ model.id }}\",\n) ",
|
|
40
40
|
"importInferenceClient": "from huggingface_hub import InferenceClient\n\nclient = InferenceClient(\n provider=\"{{ provider }}\",\n api_key=\"{{ accessToken }}\",\n{% if billTo %}\n bill_to=\"{{ billTo }}\",\n{% endif %}\n)",
|
|
41
41
|
"questionAnswering": "answer = client.question_answering(\n question=\"{{ inputs.asObj.question }}\",\n context=\"{{ inputs.asObj.context }}\",\n model=\"{{ model.id }}\",\n) ",
|
|
42
|
-
"tableQuestionAnswering": "answer = client.
|
|
42
|
+
"tableQuestionAnswering": "answer = client.table_question_answering(\n query=\"{{ inputs.asObj.query }}\",\n table={{ inputs.asObj.table }},\n model=\"{{ model.id }}\",\n) ",
|
|
43
43
|
"textToImage": "# output is a PIL.Image object\nimage = client.text_to_image(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) ",
|
|
44
44
|
"textToSpeech": "# audio is returned as bytes\naudio = client.text_to_speech(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) \n",
|
|
45
45
|
"textToVideo": "video = client.text_to_video(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) "
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huggingface/inference",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Hugging Face and Tim Mikeladze <tim.mikeladze@gmail.com>",
|
|
6
6
|
"description": "Typescript client for the Hugging Face Inference Providers and Inference Endpoints",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"type": "module",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@huggingface/tasks": "^0.19.
|
|
43
|
+
"@huggingface/tasks": "^0.19.12",
|
|
44
44
|
"@huggingface/jinja": "^0.5.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
package/src/package.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// Generated file from package.json. Issues importing JSON directly when publishing on commonjs/ESM - see https://github.com/microsoft/TypeScript/issues/51783
|
|
2
|
-
export const PACKAGE_VERSION = "4.0.
|
|
2
|
+
export const PACKAGE_VERSION = "4.0.3";
|
|
3
3
|
export const PACKAGE_NAME = "@huggingface/inference";
|
|
@@ -14,10 +14,12 @@ import { makeRequestOptionsFromResolvedModel } from "../lib/makeRequestOptions.j
|
|
|
14
14
|
import type { InferenceProviderOrPolicy, InferenceTask, RequestArgs } from "../types.js";
|
|
15
15
|
import { templates } from "./templates.exported.js";
|
|
16
16
|
|
|
17
|
-
export type InferenceSnippetOptions = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
export type InferenceSnippetOptions = {
|
|
18
|
+
streaming?: boolean;
|
|
19
|
+
billTo?: string;
|
|
20
|
+
accessToken?: string;
|
|
21
|
+
directRequest?: boolean;
|
|
22
|
+
} & Record<string, unknown>;
|
|
21
23
|
|
|
22
24
|
const PYTHON_CLIENTS = ["huggingface_hub", "fal_client", "requests", "openai"] as const;
|
|
23
25
|
const JS_CLIENTS = ["fetch", "huggingface.js", "openai"] as const;
|
|
@@ -124,7 +126,10 @@ const HF_JS_METHODS: Partial<Record<WidgetType, string>> = {
|
|
|
124
126
|
translation: "translation",
|
|
125
127
|
};
|
|
126
128
|
|
|
127
|
-
|
|
129
|
+
// Placeholders to replace with env variable in snippets
|
|
130
|
+
// little hack to support both direct requests and routing => routed requests should start with "hf_"
|
|
131
|
+
const ACCESS_TOKEN_ROUTING_PLACEHOLDER = "hf_token_placeholder";
|
|
132
|
+
const ACCESS_TOKEN_DIRECT_REQUEST_PLACEHOLDER = "not_hf_token_placeholder";
|
|
128
133
|
|
|
129
134
|
// Snippet generators
|
|
130
135
|
const snippetGenerator = (templateName: string, inputPreparationFn?: InputPreparationFn) => {
|
|
@@ -153,7 +158,11 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
|
|
|
153
158
|
console.error(`Failed to get provider helper for ${provider} (${task})`, e);
|
|
154
159
|
return [];
|
|
155
160
|
}
|
|
156
|
-
|
|
161
|
+
|
|
162
|
+
const placeholder = opts?.directRequest
|
|
163
|
+
? ACCESS_TOKEN_DIRECT_REQUEST_PLACEHOLDER
|
|
164
|
+
: ACCESS_TOKEN_ROUTING_PLACEHOLDER;
|
|
165
|
+
const accessTokenOrPlaceholder = opts?.accessToken ?? placeholder;
|
|
157
166
|
|
|
158
167
|
/// Prepare inputs + make request
|
|
159
168
|
const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: getModelInputSnippet(model) };
|
|
@@ -255,8 +264,8 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
|
|
|
255
264
|
}
|
|
256
265
|
|
|
257
266
|
/// Replace access token placeholder
|
|
258
|
-
if (snippet.includes(
|
|
259
|
-
snippet = replaceAccessTokenPlaceholder(snippet, language, provider);
|
|
267
|
+
if (snippet.includes(placeholder)) {
|
|
268
|
+
snippet = replaceAccessTokenPlaceholder(opts?.directRequest, placeholder, snippet, language, provider);
|
|
260
269
|
}
|
|
261
270
|
|
|
262
271
|
/// Snippet is ready!
|
|
@@ -431,6 +440,8 @@ function removeSuffix(str: string, suffix: string) {
|
|
|
431
440
|
}
|
|
432
441
|
|
|
433
442
|
function replaceAccessTokenPlaceholder(
|
|
443
|
+
directRequest: boolean | undefined,
|
|
444
|
+
placeholder: string,
|
|
434
445
|
snippet: string,
|
|
435
446
|
language: InferenceSnippetLanguage,
|
|
436
447
|
provider: InferenceProviderOrPolicy
|
|
@@ -439,46 +450,57 @@ function replaceAccessTokenPlaceholder(
|
|
|
439
450
|
// Once snippets are rendered, we replace the placeholder with code to fetch the access token from an environment variable.
|
|
440
451
|
|
|
441
452
|
// Determine if HF_TOKEN or specific provider token should be used
|
|
442
|
-
const
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
453
|
+
const useHfToken =
|
|
454
|
+
provider == "hf-inference" || // hf-inference provider => use $HF_TOKEN
|
|
455
|
+
(!directRequest && // if explicit directRequest => use provider-specific token
|
|
456
|
+
(!snippet.includes("https://") || // no URL provided => using a client => use $HF_TOKEN
|
|
457
|
+
snippet.includes("https://router.huggingface.co"))); // explicit routed request => use $HF_TOKEN
|
|
458
|
+
|
|
459
|
+
const accessTokenEnvVar = useHfToken
|
|
460
|
+
? "HF_TOKEN" // e.g. routed request or hf-inference
|
|
461
|
+
: provider.toUpperCase().replace("-", "_") + "_API_KEY"; // e.g. "REPLICATE_API_KEY"
|
|
448
462
|
|
|
449
463
|
// Replace the placeholder with the env variable
|
|
450
464
|
if (language === "sh") {
|
|
451
465
|
snippet = snippet.replace(
|
|
452
|
-
`'Authorization: Bearer ${
|
|
466
|
+
`'Authorization: Bearer ${placeholder}'`,
|
|
453
467
|
`"Authorization: Bearer $${accessTokenEnvVar}"` // e.g. "Authorization: Bearer $HF_TOKEN"
|
|
454
468
|
);
|
|
455
469
|
} else if (language === "python") {
|
|
456
470
|
snippet = "import os\n" + snippet;
|
|
457
471
|
snippet = snippet.replace(
|
|
458
|
-
`"${
|
|
472
|
+
`"${placeholder}"`,
|
|
459
473
|
`os.environ["${accessTokenEnvVar}"]` // e.g. os.environ["HF_TOKEN")
|
|
460
474
|
);
|
|
461
475
|
snippet = snippet.replace(
|
|
462
|
-
`"Bearer ${
|
|
476
|
+
`"Bearer ${placeholder}"`,
|
|
463
477
|
`f"Bearer {os.environ['${accessTokenEnvVar}']}"` // e.g. f"Bearer {os.environ['HF_TOKEN']}"
|
|
464
478
|
);
|
|
465
479
|
snippet = snippet.replace(
|
|
466
|
-
`"Key ${
|
|
480
|
+
`"Key ${placeholder}"`,
|
|
467
481
|
`f"Key {os.environ['${accessTokenEnvVar}']}"` // e.g. f"Key {os.environ['FAL_AI_API_KEY']}"
|
|
468
482
|
);
|
|
483
|
+
snippet = snippet.replace(
|
|
484
|
+
`"X-Key ${placeholder}"`,
|
|
485
|
+
`f"X-Key {os.environ['${accessTokenEnvVar}']}"` // e.g. f"X-Key {os.environ['BLACK_FOREST_LABS_API_KEY']}"
|
|
486
|
+
);
|
|
469
487
|
} else if (language === "js") {
|
|
470
488
|
snippet = snippet.replace(
|
|
471
|
-
`"${
|
|
489
|
+
`"${placeholder}"`,
|
|
472
490
|
`process.env.${accessTokenEnvVar}` // e.g. process.env.HF_TOKEN
|
|
473
491
|
);
|
|
474
492
|
snippet = snippet.replace(
|
|
475
|
-
`Authorization: "Bearer ${
|
|
493
|
+
`Authorization: "Bearer ${placeholder}",`,
|
|
476
494
|
`Authorization: \`Bearer $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `Bearer ${process.env.HF_TOKEN}`,
|
|
477
495
|
);
|
|
478
496
|
snippet = snippet.replace(
|
|
479
|
-
`Authorization: "Key ${
|
|
497
|
+
`Authorization: "Key ${placeholder}",`,
|
|
480
498
|
`Authorization: \`Key $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `Key ${process.env.FAL_AI_API_KEY}`,
|
|
481
499
|
);
|
|
500
|
+
snippet = snippet.replace(
|
|
501
|
+
`Authorization: "X-Key ${placeholder}",`,
|
|
502
|
+
`Authorization: \`X-Key $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `X-Key ${process.env.BLACK_FOREST_LABS_AI_API_KEY}`,
|
|
503
|
+
);
|
|
482
504
|
}
|
|
483
505
|
return snippet;
|
|
484
506
|
}
|
|
@@ -39,7 +39,7 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
|
|
|
39
39
|
"imageToImage": "# output is a PIL.Image object\nimage = client.image_to_image(\n \"{{ inputs.asObj.inputs }}\",\n prompt=\"{{ inputs.asObj.parameters.prompt }}\",\n model=\"{{ model.id }}\",\n) ",
|
|
40
40
|
"importInferenceClient": "from huggingface_hub import InferenceClient\n\nclient = InferenceClient(\n provider=\"{{ provider }}\",\n api_key=\"{{ accessToken }}\",\n{% if billTo %}\n bill_to=\"{{ billTo }}\",\n{% endif %}\n)",
|
|
41
41
|
"questionAnswering": "answer = client.question_answering(\n question=\"{{ inputs.asObj.question }}\",\n context=\"{{ inputs.asObj.context }}\",\n model=\"{{ model.id }}\",\n) ",
|
|
42
|
-
"tableQuestionAnswering": "answer = client.
|
|
42
|
+
"tableQuestionAnswering": "answer = client.table_question_answering(\n query=\"{{ inputs.asObj.query }}\",\n table={{ inputs.asObj.table }},\n model=\"{{ model.id }}\",\n) ",
|
|
43
43
|
"textToImage": "# output is a PIL.Image object\nimage = client.text_to_image(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) ",
|
|
44
44
|
"textToSpeech": "# audio is returned as bytes\naudio = client.text_to_speech(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) \n",
|
|
45
45
|
"textToVideo": "video = client.text_to_video(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) "
|