@@ -8,6 +8,12 @@ export type TextToImageArgs = BaseArgs & {
88 */
99 inputs : string ;
1010
11+ /**
12+ * Same param but for external providers like Together
13+ */
14+ prompt ?: string ;
15+ response_format ?: "base64" ;
16+
1117 parameters ?: {
1218 /**
1319 * An optional negative prompt for the image generation
@@ -32,17 +38,35 @@ export type TextToImageArgs = BaseArgs & {
3238 } ;
3339} ;
3440
41+ interface Base64ImageGeneration {
42+ id : string ;
43+ model : string ;
44+ data : Array < {
45+ b64_json : string ;
46+ } > ;
47+ }
3548export type TextToImageOutput = Blob ;
3649
3750/**
3851 * This task reads some text input and outputs an image.
3952 * Recommended model: stabilityai/stable-diffusion-2
4053 */
4154export async function textToImage ( args : TextToImageArgs , options ?: Options ) : Promise < TextToImageOutput > {
42- const res = await request < TextToImageOutput > ( args , {
55+ if ( args . provider === "together" ) {
56+ args . prompt = args . inputs ;
57+ args . inputs = "" ;
58+ args . response_format = "base64" ;
59+ }
60+ const res = await request < TextToImageOutput | Base64ImageGeneration > ( args , {
4361 ...options ,
4462 taskHint : "text-to-image" ,
4563 } ) ;
64+ if ( res && typeof res === "object" && Array . isArray ( res . data ) && res . data [ 0 ] . b64_json ) {
65+ const base64Data = res . data [ 0 ] . b64_json ;
66+ const base64Response = await fetch ( `data:image/jpeg;base64,${ base64Data } ` ) ;
67+ const blob = await base64Response . blob ( ) ;
68+ return blob ;
69+ }
4670 const isValidOutput = res && res instanceof Blob ;
4771 if ( ! isValidOutput ) {
4872 throw new InferenceOutputError ( "Expected Blob" ) ;
0 commit comments