1
1
import type { TextToImageInput , TextToImageOutput } from "@huggingface/tasks" ;
2
2
import { InferenceOutputError } from "../../lib/InferenceOutputError" ;
3
- import type { BaseArgs , Options } from "../../types" ;
3
+ import type { BaseArgs , InferenceProvider , Options } from "../../types" ;
4
4
import { omit } from "../../utils/omit" ;
5
5
import { request } from "../custom/request" ;
6
6
@@ -15,28 +15,40 @@ interface OutputUrlImageGeneration {
15
15
output : string [ ] ;
16
16
}
17
17
18
+ function getResponseFormatArg ( provider : InferenceProvider ) {
19
+ switch ( provider ) {
20
+ case "fal-ai" :
21
+ return { sync_mode : true } ;
22
+ case "nebius" :
23
+ return { response_format : "b64_json" } ;
24
+ case "replicate" :
25
+ return undefined ;
26
+ case "together" :
27
+ return { response_format : "base64" } ;
28
+ default :
29
+ return undefined ;
30
+ }
31
+ }
32
+
18
33
/**
19
34
* This task reads some text input and outputs an image.
20
35
* Recommended model: stabilityai/stable-diffusion-2
21
36
*/
22
37
export async function textToImage ( args : TextToImageArgs , options ?: Options ) : Promise < Blob > {
23
38
const payload =
24
- args . provider === "together" ||
25
- args . provider === "fal-ai" ||
26
- args . provider === "replicate" ||
27
- args . provider === "nebius"
28
- ? {
29
- ...omit ( args , [ "inputs" , "parameters" ] ) ,
30
- ...args . parameters ,
31
- ...( args . provider !== "replicate" ? { response_format : "base64" } : undefined ) ,
32
- ...( args . provider === "nebius" ? { response_format : "b64_json" } : undefined ) ,
33
- prompt : args . inputs ,
34
- }
35
- : args ;
39
+ ! args . provider || args . provider === "hf-inference" || args . provider === "sambanova"
40
+ ? args
41
+ : {
42
+ ...omit ( args , [ "inputs" , "parameters" ] ) ,
43
+ ...args . parameters ,
44
+ ...getResponseFormatArg ( args . provider ) ,
45
+ prompt : args . inputs ,
46
+ } ;
36
47
const res = await request < TextToImageOutput | Base64ImageGeneration | OutputUrlImageGeneration > ( payload , {
37
48
...options ,
38
49
taskHint : "text-to-image" ,
39
50
} ) ;
51
+
40
52
if ( res && typeof res === "object" ) {
41
53
if ( args . provider === "fal-ai" && "images" in res && Array . isArray ( res . images ) && res . images [ 0 ] . url ) {
42
54
const image = await fetch ( res . images [ 0 ] . url ) ;
0 commit comments