-
Notifications
You must be signed in to change notification settings - Fork 475
feat(api): add batch STT transcription endpoint #2146
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
332efc8
feat(api): add batch STT transcription endpoint
devin-ai-integration[bot] b979016
fix(api): add params.model support to AssemblyAI batch handler
devin-ai-integration[bot] c17d552
fix(api): add requireSupabaseAuth middleware to /transcribe endpoint
devin-ai-integration[bot] 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import { env } from "../env"; | ||
| import type { | ||
| BatchAlternatives, | ||
| BatchChannel, | ||
| BatchParams, | ||
| BatchResponse, | ||
| BatchResults, | ||
| BatchWord, | ||
| } from "./batch-types"; | ||
|
|
||
| const ASSEMBLYAI_API_URL = "https://api.assemblyai.com/v2"; | ||
| const POLL_INTERVAL_MS = 3000; | ||
| const MAX_POLL_ATTEMPTS = 200; | ||
|
|
||
| type AssemblyAIWord = { | ||
| text: string; | ||
| start: number; | ||
| end: number; | ||
| confidence: number; | ||
| speaker?: string; | ||
| }; | ||
|
|
||
| type AssemblyAITranscriptResponse = { | ||
| id: string; | ||
| status: string; | ||
| text?: string; | ||
| words?: AssemblyAIWord[]; | ||
| confidence?: number; | ||
| audio_duration?: number; | ||
| error?: string; | ||
| }; | ||
|
|
||
| const uploadAudio = async (audioData: ArrayBuffer): Promise<string> => { | ||
| const response = await fetch(`${ASSEMBLYAI_API_URL}/upload`, { | ||
| method: "POST", | ||
| headers: { | ||
| Authorization: env.ASSEMBLYAI_API_KEY, | ||
| "Content-Type": "application/octet-stream", | ||
| }, | ||
| body: audioData, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
| throw new Error( | ||
| `AssemblyAI upload failed: ${response.status} - ${errorText}`, | ||
| ); | ||
| } | ||
|
|
||
| const result = (await response.json()) as { upload_url: string }; | ||
| return result.upload_url; | ||
| }; | ||
|
|
||
| const createTranscript = async ( | ||
| audioUrl: string, | ||
| params: BatchParams, | ||
| ): Promise<string> => { | ||
| const languageCode = | ||
| params.languages && params.languages.length === 1 | ||
| ? params.languages[0] | ||
| : undefined; | ||
| const languageDetection = | ||
| !params.languages || | ||
| params.languages.length === 0 || | ||
| params.languages.length > 1; | ||
|
|
||
| const requestBody: Record<string, unknown> = { | ||
| audio_url: audioUrl, | ||
| speaker_labels: true, | ||
| }; | ||
|
|
||
| if (languageCode) { | ||
| requestBody.language_code = languageCode; | ||
| } | ||
| if (languageDetection) { | ||
| requestBody.language_detection = true; | ||
| } | ||
| if (params.keywords && params.keywords.length > 0) { | ||
| requestBody.keyterms_prompt = params.keywords; | ||
| } | ||
| if (params.model) { | ||
| requestBody.speech_model = params.model; | ||
| } | ||
|
|
||
| const response = await fetch(`${ASSEMBLYAI_API_URL}/transcript`, { | ||
| method: "POST", | ||
| headers: { | ||
| Authorization: env.ASSEMBLYAI_API_KEY, | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(requestBody), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
| throw new Error( | ||
| `AssemblyAI transcript creation failed: ${response.status} - ${errorText}`, | ||
| ); | ||
| } | ||
|
|
||
| const result = (await response.json()) as { id: string }; | ||
| return result.id; | ||
| }; | ||
|
|
||
| const pollTranscript = async ( | ||
| transcriptId: string, | ||
| ): Promise<AssemblyAITranscriptResponse> => { | ||
| for (let attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt++) { | ||
| const response = await fetch( | ||
| `${ASSEMBLYAI_API_URL}/transcript/${transcriptId}`, | ||
| { | ||
| headers: { | ||
| Authorization: env.ASSEMBLYAI_API_KEY, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
| throw new Error( | ||
| `AssemblyAI poll failed: ${response.status} - ${errorText}`, | ||
| ); | ||
| } | ||
|
|
||
| const result = (await response.json()) as AssemblyAITranscriptResponse; | ||
|
|
||
| if (result.status === "completed") { | ||
| return result; | ||
| } | ||
|
|
||
| if (result.status === "error") { | ||
| throw new Error( | ||
| `AssemblyAI transcription failed: ${result.error ?? "unknown error"}`, | ||
| ); | ||
| } | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS)); | ||
| } | ||
|
|
||
| throw new Error("AssemblyAI transcription timed out"); | ||
| }; | ||
|
|
||
| const convertToResponse = ( | ||
| result: AssemblyAITranscriptResponse, | ||
| ): BatchResponse => { | ||
| const words: BatchWord[] = (result.words ?? []).map((w) => { | ||
| const speaker = w.speaker | ||
| ? parseInt(w.speaker.replace(/\D/g, ""), 10) | ||
| : undefined; | ||
|
|
||
| return { | ||
| word: w.text, | ||
| start: w.start / 1000, | ||
| end: w.end / 1000, | ||
| confidence: w.confidence, | ||
| speaker: Number.isNaN(speaker) ? undefined : speaker, | ||
| punctuated_word: w.text, | ||
| }; | ||
| }); | ||
|
|
||
| const alternatives: BatchAlternatives = { | ||
| transcript: result.text ?? "", | ||
| confidence: result.confidence ?? 1.0, | ||
| words, | ||
| }; | ||
|
|
||
| const channel: BatchChannel = { | ||
| alternatives: [alternatives], | ||
| }; | ||
|
|
||
| const results: BatchResults = { | ||
| channels: [channel], | ||
| }; | ||
|
|
||
| return { | ||
| metadata: { | ||
| audio_duration: result.audio_duration, | ||
| }, | ||
| results, | ||
| }; | ||
| }; | ||
|
|
||
| export const transcribeWithAssemblyAI = async ( | ||
| audioData: ArrayBuffer, | ||
| _contentType: string, | ||
| params: BatchParams, | ||
| ): Promise<BatchResponse> => { | ||
| const uploadUrl = await uploadAudio(audioData); | ||
| const transcriptId = await createTranscript(uploadUrl, params); | ||
| const result = await pollTranscript(transcriptId); | ||
| return convertToResponse(result); | ||
| }; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.