@@ -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 ( args : TextToImageArgs , options ?: Options ) : Promise < Blob > {
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,18 @@ 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" ) {
81
+ if ( options ?. outputType === "url" ) {
82
+ return res . polling_url ;
83
+ }
69
84
return await pollBflResponse ( res . polling_url ) ;
70
85
}
71
86
if ( args . provider === "fal-ai" && "images" in res && Array . isArray ( res . images ) && res . images [ 0 ] . url ) {
72
- const image = await fetch ( res . images [ 0 ] . url ) ;
73
- return await image . blob ( ) ;
87
+ if ( options ?. outputType === "url" ) {
88
+ return res . images [ 0 ] . url ;
89
+ } else {
90
+ const image = await fetch ( res . images [ 0 ] . url ) ;
91
+ return await image . blob ( ) ;
92
+ }
74
93
}
75
94
if (
76
95
args . provider === "hyperbolic" &&
@@ -79,17 +98,24 @@ export async function textToImage(args: TextToImageArgs, options?: Options): Pro
79
98
res . images [ 0 ] &&
80
99
typeof res . images [ 0 ] . image === "string"
81
100
) {
101
+ if ( options ?. outputType === "url" ) {
102
+ return `data:image/jpeg;base64,${ res . images [ 0 ] . image } ` ;
103
+ }
82
104
const base64Response = await fetch ( `data:image/jpeg;base64,${ res . images [ 0 ] . image } ` ) ;
83
- const blob = await base64Response . blob ( ) ;
84
- return blob ;
105
+ return await base64Response . blob ( ) ;
85
106
}
86
107
if ( "data" in res && Array . isArray ( res . data ) && res . data [ 0 ] . b64_json ) {
87
108
const base64Data = res . data [ 0 ] . b64_json ;
109
+ if ( options ?. outputType === "url" ) {
110
+ return `data:image/jpeg;base64,${ base64Data } ` ;
111
+ }
88
112
const base64Response = await fetch ( `data:image/jpeg;base64,${ base64Data } ` ) ;
89
- const blob = await base64Response . blob ( ) ;
90
- return blob ;
113
+ return await base64Response . blob ( ) ;
91
114
}
92
115
if ( "output" in res && Array . isArray ( res . output ) ) {
116
+ if ( options ?. outputType === "url" ) {
117
+ return res . output [ 0 ] ;
118
+ }
93
119
const urlResponse = await fetch ( res . output [ 0 ] ) ;
94
120
const blob = await urlResponse . blob ( ) ;
95
121
return blob ;
@@ -99,6 +125,10 @@ export async function textToImage(args: TextToImageArgs, options?: Options): Pro
99
125
if ( ! isValidOutput ) {
100
126
throw new InferenceOutputError ( "Expected Blob" ) ;
101
127
}
128
+ if ( options ?. outputType === "url" ) {
129
+ const b64 = await res . arrayBuffer ( ) . then ( ( buf ) => Buffer . from ( buf ) . toString ( "base64" ) ) ;
130
+ return `data:image/jpeg;base64,${ b64 } ` ;
131
+ }
102
132
return res ;
103
133
}
104
134
0 commit comments