Skip to content

Add support for image-to-video task type for Replicate #1575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/inference/src/lib/getProviderHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export const PROVIDERS: Record<InferenceProvider, Partial<Record<InferenceTask,
"text-to-speech": new Replicate.ReplicateTextToSpeechTask(),
"text-to-video": new Replicate.ReplicateTextToVideoTask(),
"image-to-image": new Replicate.ReplicateImageToImageTask(),
"image-to-video": new Replicate.ReplicateImageToVideoTask(),
},
sambanova: {
conversational: new Sambanova.SambanovaConversationalTask(),
Expand Down
47 changes: 47 additions & 0 deletions packages/inference/src/providers/replicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,50 @@ export class ReplicateImageToImageTask extends ReplicateTask implements ImageToI
throw new InferenceClientProviderOutputError("Received malformed response from Replicate image-to-image API");
}
}

export class ReplicateImageToVideoTask extends ReplicateTask {
// Synchronous: expects base64 string in args.inputs
override preparePayload(params: BodyParams): Record<string, unknown> {
const { args } = params;
const { inputs, parameters } = args;
return {
input: {
...omit(args, ["inputs", "parameters"]),
...(parameters as Record<string, unknown>),
inputs,
},
version: params.model.includes(":") ? params.model.split(":")[1] : undefined,
};
}

// Asynchronous: handles Blob to base64 conversion
async preparePayloadAsync(args: { inputs: Blob } & Record<string, unknown>): Promise<RequestArgs> {
const { inputs, ...restArgs } = args;
const bytes = new Uint8Array(await inputs.arrayBuffer());
const base64 = base64FromBytes(bytes);
const imageInput = `data:${inputs.type || "image/png"};base64,${base64}`;
return {
...restArgs,
inputs: imageInput,
};
}

// Handle the response from Replicate
override async getResponse(response: ReplicateOutput): Promise<Blob> {
if (
typeof response === "object" &&
!!response &&
"output" in response
) {
if (Array.isArray(response.output) && response.output.length > 0 && typeof response.output[0] === "string") {
const urlResponse = await fetch(response.output[0]);
return await urlResponse.blob();
}
if (typeof response.output === "string" && isUrl(response.output)) {
const urlResponse = await fetch(response.output);
return await urlResponse.blob();
}
}
throw new InferenceClientProviderOutputError("Received malformed response from Replicate image-to-video API");
}
}
12 changes: 12 additions & 0 deletions packages/inference/test/InferenceClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,18 @@ describe.skip("InferenceClient", () => {
});
expect(res).toBeInstanceOf(Blob);
});

it("imageToVideo - MeiGen-AI/MeiGen-MultiTalk", async () => {
const res = await client.imageToVideo({
model: "MeiGen-AI/MeiGen-MultiTalk",
provider: "replicate",
inputs: new Blob([readTestFile("bird_canny.png")], { type: "image/png" }),
parameters: {
prompt: "A bird flying in the sky",
},
});
expect(res).toBeInstanceOf(Blob);
});
},
TIMEOUT
);
Expand Down
Loading