-
Notifications
You must be signed in to change notification settings - Fork 492
[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
Merged
+62
−5
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ceaf141
Add ASR support for Replicate provider
lucataco af7a21e
Fix lint
lucataco ba1755a
Merge branch 'main' into main
lucataco 5299c1c
Add ReplicateASR mapping
lucataco de2778d
fix get response
hanouticelina 3dae24c
Merge branch 'main' into main
coyotte508 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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[]; | ||
|
@@ -163,6 +166,64 @@ 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?.output === "string") return { text: response.output }; | ||
if (Array.isArray(response?.output) && typeof response.output[0] === "string") return { text: response.output[0] }; | ||
|
||
const out = response?.output as | ||
| undefined | ||
| { | ||
transcription?: string; | ||
translation?: string; | ||
txt_file?: string; | ||
}; | ||
Comment on lines
+206
to
+212
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. following the schema defined in https://replicate.com/openai/whisper/api/schema#output-schema |
||
if (out && typeof out === "object") { | ||
if (typeof out.transcription === "string") return { text: out.transcription }; | ||
if (typeof out.translation === "string") return { text: out.translation }; | ||
if (typeof out.txt_file === "string") { | ||
const r = await fetch(out.txt_file); | ||
return { text: await r.text() }; | ||
} | ||
} | ||
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 { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message 'Audio input must be a Blob' is not descriptive enough. Consider providing more context about expected input formats and how to convert them to Blob.
Copilot uses AI. Check for mistakes.