Skip to content

[Inference] Add ASR support for Replicate provider #1679

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 5 commits into
base: main
Choose a base branch
from
Open
Changes from 3 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
58 changes: 58 additions & 0 deletions packages/inference/src/providers/replicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import type { BodyParams, HeaderParams, RequestArgs, UrlParams } from "../types.
import { omit } from "../utils/omit.js";
import {
TaskProviderHelper,
type AutomaticSpeechRecognitionTaskHelper,
type ImageToImageTaskHelper,
type TextToImageTaskHelper,
type TextToVideoTaskHelper,
} from "./providerHelper.js";
import type { ImageToImageArgs } from "../tasks/cv/imageToImage.js";
import type { AutomaticSpeechRecognitionArgs } from "../tasks/audio/automaticSpeechRecognition.js";
import type { AutomaticSpeechRecognitionOutput } from "@huggingface/tasks";
import { base64FromBytes } from "../utils/base64FromBytes.js";
export interface ReplicateOutput {
output?: string | string[];
Expand Down Expand Up @@ -163,6 +166,61 @@ export class ReplicateTextToVideoTask extends ReplicateTask implements TextToVid
}
}

export class ReplicateAutomaticSpeechRecognitionTask
extends ReplicateTask
implements AutomaticSpeechRecognitionTaskHelper
{
override preparePayload(params: BodyParams): Record<string, unknown> {
return {
input: {
...omit(params.args, ["inputs", "parameters"]),
...(params.args.parameters as Record<string, unknown>),
audio: params.args.inputs, // This will be processed in preparePayloadAsync
},
version: params.model.includes(":") ? params.model.split(":")[1] : undefined,
};
}

async preparePayloadAsync(args: AutomaticSpeechRecognitionArgs): Promise<RequestArgs> {
const blob = "data" in args && args.data instanceof Blob ? args.data : "inputs" in args ? args.inputs : undefined;

if (!blob || !(blob instanceof Blob)) {
throw new Error("Audio input must be a Blob");
}

// Convert Blob to base64 data URL
const bytes = new Uint8Array(await blob.arrayBuffer());
const base64 = base64FromBytes(bytes);
const audioInput = `data:${blob.type || "audio/wav"};base64,${base64}`;

return {
...("data" in args ? omit(args, "data") : omit(args, "inputs")),
inputs: audioInput,
};
}

override async getResponse(response: ReplicateOutput): Promise<AutomaticSpeechRecognitionOutput> {
if (typeof response === "object" && !!response && "output" in response && typeof response.output === "string") {
return { text: response.output };
}

if (
typeof response === "object" &&
!!response &&
"output" in response &&
Array.isArray(response.output) &&
response.output.length > 0 &&
typeof response.output[0] === "string"
) {
return { text: response.output[0] };
}

throw new InferenceClientProviderOutputError(
"Received malformed response from Replicate automatic-speech-recognition API"
);
}
}

export class ReplicateImageToImageTask extends ReplicateTask implements ImageToImageTaskHelper {
override preparePayload(params: BodyParams<ImageToImageArgs>): Record<string, unknown> {
return {
Expand Down