@huggingface/inference 3.3.4 → 3.3.6
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
CHANGED
|
@@ -134,7 +134,7 @@ function isUrl(modelOrUrl) {
|
|
|
134
134
|
|
|
135
135
|
// package.json
|
|
136
136
|
var name = "@huggingface/inference";
|
|
137
|
-
var version = "3.3.
|
|
137
|
+
var version = "3.3.6";
|
|
138
138
|
|
|
139
139
|
// src/providers/consts.ts
|
|
140
140
|
var HARDCODED_MODEL_ID_MAPPING = {
|
|
@@ -840,24 +840,35 @@ async function textToImage(args, options) {
|
|
|
840
840
|
});
|
|
841
841
|
if (res && typeof res === "object") {
|
|
842
842
|
if (args.provider === "black-forest-labs" && "polling_url" in res && typeof res.polling_url === "string") {
|
|
843
|
-
return await pollBflResponse(res.polling_url);
|
|
843
|
+
return await pollBflResponse(res.polling_url, options?.outputType);
|
|
844
844
|
}
|
|
845
845
|
if (args.provider === "fal-ai" && "images" in res && Array.isArray(res.images) && res.images[0].url) {
|
|
846
|
-
|
|
847
|
-
|
|
846
|
+
if (options?.outputType === "url") {
|
|
847
|
+
return res.images[0].url;
|
|
848
|
+
} else {
|
|
849
|
+
const image = await fetch(res.images[0].url);
|
|
850
|
+
return await image.blob();
|
|
851
|
+
}
|
|
848
852
|
}
|
|
849
853
|
if (args.provider === "hyperbolic" && "images" in res && Array.isArray(res.images) && res.images[0] && typeof res.images[0].image === "string") {
|
|
854
|
+
if (options?.outputType === "url") {
|
|
855
|
+
return `data:image/jpeg;base64,${res.images[0].image}`;
|
|
856
|
+
}
|
|
850
857
|
const base64Response = await fetch(`data:image/jpeg;base64,${res.images[0].image}`);
|
|
851
|
-
|
|
852
|
-
return blob;
|
|
858
|
+
return await base64Response.blob();
|
|
853
859
|
}
|
|
854
860
|
if ("data" in res && Array.isArray(res.data) && res.data[0].b64_json) {
|
|
855
861
|
const base64Data = res.data[0].b64_json;
|
|
862
|
+
if (options?.outputType === "url") {
|
|
863
|
+
return `data:image/jpeg;base64,${base64Data}`;
|
|
864
|
+
}
|
|
856
865
|
const base64Response = await fetch(`data:image/jpeg;base64,${base64Data}`);
|
|
857
|
-
|
|
858
|
-
return blob;
|
|
866
|
+
return await base64Response.blob();
|
|
859
867
|
}
|
|
860
868
|
if ("output" in res && Array.isArray(res.output)) {
|
|
869
|
+
if (options?.outputType === "url") {
|
|
870
|
+
return res.output[0];
|
|
871
|
+
}
|
|
861
872
|
const urlResponse = await fetch(res.output[0]);
|
|
862
873
|
const blob = await urlResponse.blob();
|
|
863
874
|
return blob;
|
|
@@ -867,9 +878,13 @@ async function textToImage(args, options) {
|
|
|
867
878
|
if (!isValidOutput) {
|
|
868
879
|
throw new InferenceOutputError("Expected Blob");
|
|
869
880
|
}
|
|
881
|
+
if (options?.outputType === "url") {
|
|
882
|
+
const b64 = await res.arrayBuffer().then((buf) => Buffer.from(buf).toString("base64"));
|
|
883
|
+
return `data:image/jpeg;base64,${b64}`;
|
|
884
|
+
}
|
|
870
885
|
return res;
|
|
871
886
|
}
|
|
872
|
-
async function pollBflResponse(url) {
|
|
887
|
+
async function pollBflResponse(url, outputType) {
|
|
873
888
|
const urlObj = new URL(url);
|
|
874
889
|
for (let step = 0; step < 5; step++) {
|
|
875
890
|
await delay(1e3);
|
|
@@ -881,6 +896,9 @@ async function pollBflResponse(url) {
|
|
|
881
896
|
}
|
|
882
897
|
const payload = await resp.json();
|
|
883
898
|
if (typeof payload === "object" && payload && "status" in payload && typeof payload.status === "string" && payload.status === "Ready" && "result" in payload && typeof payload.result === "object" && payload.result && "sample" in payload.result && typeof payload.result.sample === "string") {
|
|
899
|
+
if (outputType === "url") {
|
|
900
|
+
return payload.result.sample;
|
|
901
|
+
}
|
|
884
902
|
const image = await fetch(payload.result.sample);
|
|
885
903
|
return await image.blob();
|
|
886
904
|
}
|
package/dist/index.js
CHANGED
|
@@ -79,7 +79,7 @@ function isUrl(modelOrUrl) {
|
|
|
79
79
|
|
|
80
80
|
// package.json
|
|
81
81
|
var name = "@huggingface/inference";
|
|
82
|
-
var version = "3.3.
|
|
82
|
+
var version = "3.3.6";
|
|
83
83
|
|
|
84
84
|
// src/providers/consts.ts
|
|
85
85
|
var HARDCODED_MODEL_ID_MAPPING = {
|
|
@@ -785,24 +785,35 @@ async function textToImage(args, options) {
|
|
|
785
785
|
});
|
|
786
786
|
if (res && typeof res === "object") {
|
|
787
787
|
if (args.provider === "black-forest-labs" && "polling_url" in res && typeof res.polling_url === "string") {
|
|
788
|
-
return await pollBflResponse(res.polling_url);
|
|
788
|
+
return await pollBflResponse(res.polling_url, options?.outputType);
|
|
789
789
|
}
|
|
790
790
|
if (args.provider === "fal-ai" && "images" in res && Array.isArray(res.images) && res.images[0].url) {
|
|
791
|
-
|
|
792
|
-
|
|
791
|
+
if (options?.outputType === "url") {
|
|
792
|
+
return res.images[0].url;
|
|
793
|
+
} else {
|
|
794
|
+
const image = await fetch(res.images[0].url);
|
|
795
|
+
return await image.blob();
|
|
796
|
+
}
|
|
793
797
|
}
|
|
794
798
|
if (args.provider === "hyperbolic" && "images" in res && Array.isArray(res.images) && res.images[0] && typeof res.images[0].image === "string") {
|
|
799
|
+
if (options?.outputType === "url") {
|
|
800
|
+
return `data:image/jpeg;base64,${res.images[0].image}`;
|
|
801
|
+
}
|
|
795
802
|
const base64Response = await fetch(`data:image/jpeg;base64,${res.images[0].image}`);
|
|
796
|
-
|
|
797
|
-
return blob;
|
|
803
|
+
return await base64Response.blob();
|
|
798
804
|
}
|
|
799
805
|
if ("data" in res && Array.isArray(res.data) && res.data[0].b64_json) {
|
|
800
806
|
const base64Data = res.data[0].b64_json;
|
|
807
|
+
if (options?.outputType === "url") {
|
|
808
|
+
return `data:image/jpeg;base64,${base64Data}`;
|
|
809
|
+
}
|
|
801
810
|
const base64Response = await fetch(`data:image/jpeg;base64,${base64Data}`);
|
|
802
|
-
|
|
803
|
-
return blob;
|
|
811
|
+
return await base64Response.blob();
|
|
804
812
|
}
|
|
805
813
|
if ("output" in res && Array.isArray(res.output)) {
|
|
814
|
+
if (options?.outputType === "url") {
|
|
815
|
+
return res.output[0];
|
|
816
|
+
}
|
|
806
817
|
const urlResponse = await fetch(res.output[0]);
|
|
807
818
|
const blob = await urlResponse.blob();
|
|
808
819
|
return blob;
|
|
@@ -812,9 +823,13 @@ async function textToImage(args, options) {
|
|
|
812
823
|
if (!isValidOutput) {
|
|
813
824
|
throw new InferenceOutputError("Expected Blob");
|
|
814
825
|
}
|
|
826
|
+
if (options?.outputType === "url") {
|
|
827
|
+
const b64 = await res.arrayBuffer().then((buf) => Buffer.from(buf).toString("base64"));
|
|
828
|
+
return `data:image/jpeg;base64,${b64}`;
|
|
829
|
+
}
|
|
815
830
|
return res;
|
|
816
831
|
}
|
|
817
|
-
async function pollBflResponse(url) {
|
|
832
|
+
async function pollBflResponse(url, outputType) {
|
|
818
833
|
const urlObj = new URL(url);
|
|
819
834
|
for (let step = 0; step < 5; step++) {
|
|
820
835
|
await delay(1e3);
|
|
@@ -826,6 +841,9 @@ async function pollBflResponse(url) {
|
|
|
826
841
|
}
|
|
827
842
|
const payload = await resp.json();
|
|
828
843
|
if (typeof payload === "object" && payload && "status" in payload && typeof payload.status === "string" && payload.status === "Ready" && "result" in payload && typeof payload.result === "object" && payload.result && "sample" in payload.result && typeof payload.result.sample === "string") {
|
|
844
|
+
if (outputType === "url") {
|
|
845
|
+
return payload.result.sample;
|
|
846
|
+
}
|
|
829
847
|
const image = await fetch(payload.result.sample);
|
|
830
848
|
return await image.blob();
|
|
831
849
|
}
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import type { TextToImageInput } from "@huggingface/tasks";
|
|
2
2
|
import type { BaseArgs, Options } from "../../types";
|
|
3
3
|
export type TextToImageArgs = BaseArgs & TextToImageInput;
|
|
4
|
+
interface TextToImageOptions extends Options {
|
|
5
|
+
outputType?: "url" | "blob";
|
|
6
|
+
}
|
|
4
7
|
/**
|
|
5
8
|
* This task reads some text input and outputs an image.
|
|
6
9
|
* Recommended model: stabilityai/stable-diffusion-2
|
|
7
10
|
*/
|
|
8
|
-
export declare function textToImage(args: TextToImageArgs, options?:
|
|
11
|
+
export declare function textToImage(args: TextToImageArgs, options?: TextToImageOptions & {
|
|
12
|
+
outputType: "url";
|
|
13
|
+
}): Promise<string>;
|
|
14
|
+
export declare function textToImage(args: TextToImageArgs, options?: TextToImageOptions & {
|
|
15
|
+
outputType?: undefined | "blob";
|
|
16
|
+
}): Promise<Blob>;
|
|
17
|
+
export {};
|
|
9
18
|
//# sourceMappingURL=textToImage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"textToImage.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToImage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAqB,MAAM,oBAAoB,CAAC;AAE9E,OAAO,KAAK,EAAE,QAAQ,EAAqB,OAAO,EAAE,MAAM,aAAa,CAAC;AAKxE,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"textToImage.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToImage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAqB,MAAM,oBAAoB,CAAC;AAE9E,OAAO,KAAK,EAAE,QAAQ,EAAqB,OAAO,EAAE,MAAM,aAAa,CAAC;AAKxE,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,gBAAgB,CAAC;AAmB1D,UAAU,kBAAmB,SAAQ,OAAO;IAC3C,UAAU,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;CAC5B;AAiBD;;;GAGG;AACH,wBAAsB,WAAW,CAChC,IAAI,EAAE,eAAe,EACrB,OAAO,CAAC,EAAE,kBAAkB,GAAG;IAAE,UAAU,EAAE,KAAK,CAAA;CAAE,GAClD,OAAO,CAAC,MAAM,CAAC,CAAC;AACnB,wBAAsB,WAAW,CAChC,IAAI,EAAE,eAAe,EACrB,OAAO,CAAC,EAAE,kBAAkB,GAAG;IAAE,UAAU,CAAC,EAAE,SAAS,GAAG,MAAM,CAAA;CAAE,GAChE,OAAO,CAAC,IAAI,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huggingface/inference",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.6",
|
|
4
4
|
"packageManager": "pnpm@8.10.5",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Tim Mikeladze <tim.mikeladze@gmail.com>",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"type": "module",
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@huggingface/tasks": "^0.
|
|
42
|
+
"@huggingface/tasks": "^0.16.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/node": "18.13.0"
|
|
@@ -24,6 +24,10 @@ interface BlackForestLabsResponse {
|
|
|
24
24
|
polling_url: string;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
interface TextToImageOptions extends Options {
|
|
28
|
+
outputType?: "url" | "blob";
|
|
29
|
+
}
|
|
30
|
+
|
|
27
31
|
function getResponseFormatArg(provider: InferenceProvider) {
|
|
28
32
|
switch (provider) {
|
|
29
33
|
case "fal-ai":
|
|
@@ -43,7 +47,15 @@ function getResponseFormatArg(provider: InferenceProvider) {
|
|
|
43
47
|
* This task reads some text input and outputs an image.
|
|
44
48
|
* Recommended model: stabilityai/stable-diffusion-2
|
|
45
49
|
*/
|
|
46
|
-
export async function textToImage(
|
|
50
|
+
export async function textToImage(
|
|
51
|
+
args: TextToImageArgs,
|
|
52
|
+
options?: TextToImageOptions & { outputType: "url" }
|
|
53
|
+
): Promise<string>;
|
|
54
|
+
export async function textToImage(
|
|
55
|
+
args: TextToImageArgs,
|
|
56
|
+
options?: TextToImageOptions & { outputType?: undefined | "blob" }
|
|
57
|
+
): Promise<Blob>;
|
|
58
|
+
export async function textToImage(args: TextToImageArgs, options?: TextToImageOptions): Promise<Blob | string> {
|
|
47
59
|
const payload =
|
|
48
60
|
!args.provider || args.provider === "hf-inference" || args.provider === "sambanova"
|
|
49
61
|
? args
|
|
@@ -66,11 +78,15 @@ export async function textToImage(args: TextToImageArgs, options?: Options): Pro
|
|
|
66
78
|
|
|
67
79
|
if (res && typeof res === "object") {
|
|
68
80
|
if (args.provider === "black-forest-labs" && "polling_url" in res && typeof res.polling_url === "string") {
|
|
69
|
-
return await pollBflResponse(res.polling_url);
|
|
81
|
+
return await pollBflResponse(res.polling_url, options?.outputType);
|
|
70
82
|
}
|
|
71
83
|
if (args.provider === "fal-ai" && "images" in res && Array.isArray(res.images) && res.images[0].url) {
|
|
72
|
-
|
|
73
|
-
|
|
84
|
+
if (options?.outputType === "url") {
|
|
85
|
+
return res.images[0].url;
|
|
86
|
+
} else {
|
|
87
|
+
const image = await fetch(res.images[0].url);
|
|
88
|
+
return await image.blob();
|
|
89
|
+
}
|
|
74
90
|
}
|
|
75
91
|
if (
|
|
76
92
|
args.provider === "hyperbolic" &&
|
|
@@ -79,17 +95,24 @@ export async function textToImage(args: TextToImageArgs, options?: Options): Pro
|
|
|
79
95
|
res.images[0] &&
|
|
80
96
|
typeof res.images[0].image === "string"
|
|
81
97
|
) {
|
|
98
|
+
if (options?.outputType === "url") {
|
|
99
|
+
return `data:image/jpeg;base64,${res.images[0].image}`;
|
|
100
|
+
}
|
|
82
101
|
const base64Response = await fetch(`data:image/jpeg;base64,${res.images[0].image}`);
|
|
83
|
-
|
|
84
|
-
return blob;
|
|
102
|
+
return await base64Response.blob();
|
|
85
103
|
}
|
|
86
104
|
if ("data" in res && Array.isArray(res.data) && res.data[0].b64_json) {
|
|
87
105
|
const base64Data = res.data[0].b64_json;
|
|
106
|
+
if (options?.outputType === "url") {
|
|
107
|
+
return `data:image/jpeg;base64,${base64Data}`;
|
|
108
|
+
}
|
|
88
109
|
const base64Response = await fetch(`data:image/jpeg;base64,${base64Data}`);
|
|
89
|
-
|
|
90
|
-
return blob;
|
|
110
|
+
return await base64Response.blob();
|
|
91
111
|
}
|
|
92
112
|
if ("output" in res && Array.isArray(res.output)) {
|
|
113
|
+
if (options?.outputType === "url") {
|
|
114
|
+
return res.output[0];
|
|
115
|
+
}
|
|
93
116
|
const urlResponse = await fetch(res.output[0]);
|
|
94
117
|
const blob = await urlResponse.blob();
|
|
95
118
|
return blob;
|
|
@@ -99,10 +122,14 @@ export async function textToImage(args: TextToImageArgs, options?: Options): Pro
|
|
|
99
122
|
if (!isValidOutput) {
|
|
100
123
|
throw new InferenceOutputError("Expected Blob");
|
|
101
124
|
}
|
|
125
|
+
if (options?.outputType === "url") {
|
|
126
|
+
const b64 = await res.arrayBuffer().then((buf) => Buffer.from(buf).toString("base64"));
|
|
127
|
+
return `data:image/jpeg;base64,${b64}`;
|
|
128
|
+
}
|
|
102
129
|
return res;
|
|
103
130
|
}
|
|
104
131
|
|
|
105
|
-
async function pollBflResponse(url: string): Promise<Blob> {
|
|
132
|
+
async function pollBflResponse(url: string, outputType?: "url" | "blob"): Promise<Blob> {
|
|
106
133
|
const urlObj = new URL(url);
|
|
107
134
|
for (let step = 0; step < 5; step++) {
|
|
108
135
|
await delay(1000);
|
|
@@ -125,6 +152,9 @@ async function pollBflResponse(url: string): Promise<Blob> {
|
|
|
125
152
|
"sample" in payload.result &&
|
|
126
153
|
typeof payload.result.sample === "string"
|
|
127
154
|
) {
|
|
155
|
+
if (outputType === "url") {
|
|
156
|
+
return payload.result.sample;
|
|
157
|
+
}
|
|
128
158
|
const image = await fetch(payload.result.sample);
|
|
129
159
|
return await image.blob();
|
|
130
160
|
}
|