Skip to content

Commit

Permalink
fix(inference/fal-ai): b64 response format argument (#1149)
Browse files Browse the repository at this point in the history
`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]>
  • Loading branch information
badayvedat and SBrandeis authored Feb 10, 2025
1 parent 18bd1f5 commit f85f5f7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
38 changes: 25 additions & 13 deletions packages/inference/src/tasks/cv/textToImage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TextToImageInput, TextToImageOutput } from "@huggingface/tasks";
import { InferenceOutputError } from "../../lib/InferenceOutputError";
import type { BaseArgs, Options } from "../../types";
import type { BaseArgs, InferenceProvider, Options } from "../../types";
import { omit } from "../../utils/omit";
import { request } from "../custom/request";

Expand All @@ -15,28 +15,40 @@ interface OutputUrlImageGeneration {
output: string[];
}

function getResponseFormatArg(provider: InferenceProvider) {
switch (provider) {
case "fal-ai":
return { sync_mode: true };
case "nebius":
return { response_format: "b64_json" };
case "replicate":
return undefined;
case "together":
return { response_format: "base64" };
default:
return undefined;
}
}

/**
* This task reads some text input and outputs an image.
* Recommended model: stabilityai/stable-diffusion-2
*/
export async function textToImage(args: TextToImageArgs, options?: Options): Promise<Blob> {
const payload =
args.provider === "together" ||
args.provider === "fal-ai" ||
args.provider === "replicate" ||
args.provider === "nebius"
? {
...omit(args, ["inputs", "parameters"]),
...args.parameters,
...(args.provider !== "replicate" ? { response_format: "base64" } : undefined),
...(args.provider === "nebius" ? { response_format: "b64_json" } : undefined),
prompt: args.inputs,
}
: args;
!args.provider || args.provider === "hf-inference" || args.provider === "sambanova"
? args
: {
...omit(args, ["inputs", "parameters"]),
...args.parameters,
...getResponseFormatArg(args.provider),
prompt: args.inputs,
};
const res = await request<TextToImageOutput | Base64ImageGeneration | OutputUrlImageGeneration>(payload, {
...options,
taskHint: "text-to-image",
});

if (res && typeof res === "object") {
if (args.provider === "fal-ai" && "images" in res && Array.isArray(res.images) && res.images[0].url) {
const image = await fetch(res.images[0].url);
Expand Down
32 changes: 32 additions & 0 deletions packages/inference/test/tapes.json

Large diffs are not rendered by default.

0 comments on commit f85f5f7

Please sign in to comment.