Skip to content

Commit f85f5f7

Browse files
fix(inference/fal-ai): b64 response format argument (#1149)
`fal-ai` endpoints are using `sync_mode: true` argument to receive the result as a **base64 data uri** here is an example input/output with the parameter: https://fal.ai/models/fal-ai/flux/dev?share=c1793354-3994-44ef-a50b-31ef2ee9c90f --------- Co-authored-by: SBrandeis <[email protected]>
1 parent 18bd1f5 commit f85f5f7

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

packages/inference/src/tasks/cv/textToImage.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TextToImageInput, TextToImageOutput } from "@huggingface/tasks";
22
import { InferenceOutputError } from "../../lib/InferenceOutputError";
3-
import type { BaseArgs, Options } from "../../types";
3+
import type { BaseArgs, InferenceProvider, Options } from "../../types";
44
import { omit } from "../../utils/omit";
55
import { request } from "../custom/request";
66

@@ -15,28 +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
*/
2237
export 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-
? {
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+
};
3647
const res = await request<TextToImageOutput | Base64ImageGeneration | OutputUrlImageGeneration>(payload, {
3748
...options,
3849
taskHint: "text-to-image",
3950
});
51+
4052
if (res && typeof res === "object") {
4153
if (args.provider === "fal-ai" && "images" in res && Array.isArray(res.images) && res.images[0].url) {
4254
const image = await fetch(res.images[0].url);

packages/inference/test/tapes.json

Lines changed: 32 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)