11import type { TextToImageInput , TextToImageOutput } from "@huggingface/tasks" ;
22import { InferenceOutputError } from "../../lib/InferenceOutputError" ;
3- import type { BaseArgs , Options } from "../../types" ;
3+ import type { BaseArgs , InferenceProvider , Options } from "../../types" ;
44import { omit } from "../../utils/omit" ;
55import { request } from "../custom/request" ;
66
@@ -15,29 +15,40 @@ interface OutputUrlImageGeneration {
1515 output : string [ ] ;
1616}
1717
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+
1833/**
1934 * This task reads some text input and outputs an image.
2035 * Recommended model: stabilityai/stable-diffusion-2
2136 */
2237export async function textToImage ( args : TextToImageArgs , options ?: Options ) : Promise < Blob > {
2338 const payload =
24- args . provider === "together" ||
25- args . provider === "fal-ai" ||
26- args . provider === "replicate" ||
27- args . provider === "nebius" ||
28- args . provider === "hyperbolic"
29- ? {
39+ ! args . provider || args . provider === "hf-inference" || args . provider === "sambanova"
40+ ? args
41+ : {
3042 ...omit ( args , [ "inputs" , "parameters" ] ) ,
3143 ...args . parameters ,
32- ...( args . provider !== "replicate" ? { response_format : "base64" } : undefined ) ,
33- ...( args . provider === "nebius" ? { response_format : "b64_json" } : undefined ) ,
44+ ...getResponseFormatArg ( args . provider ) ,
3445 prompt : args . inputs ,
35- }
36- : args ;
46+ } ;
3747 const res = await request < TextToImageOutput | Base64ImageGeneration | OutputUrlImageGeneration > ( payload , {
3848 ...options ,
3949 taskHint : "text-to-image" ,
4050 } ) ;
51+
4152 if ( res && typeof res === "object" ) {
4253 if ( args . provider === "fal-ai" && "images" in res && Array . isArray ( res . images ) && res . images [ 0 ] . url ) {
4354 const image = await fetch ( res . images [ 0 ] . url ) ;
0 commit comments