|
| 1 | +--- |
| 2 | +title: "Transcribe audio using Deepgram" |
| 3 | +sidebarTitle: "Deepgram audio transcription" |
| 4 | +description: "This example will show you how to transcribe audio using Deepgram's speech recognition API with Trigger.dev." |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +Transcribe audio using [Deepgram's](https://developers.deepgram.com/docs/introduction) speech recognition API. |
| 10 | + |
| 11 | +## Key Features |
| 12 | + |
| 13 | +- Transcribe audio from a URL |
| 14 | +- Use the Nova 2 model for transcription |
| 15 | + |
| 16 | +## Task code |
| 17 | + |
| 18 | +```ts trigger/deepgramTranscription.ts |
| 19 | +import { createClient } from "@deepgram/sdk"; |
| 20 | +import { logger, task } from "@trigger.dev/sdk/v3"; |
| 21 | + |
| 22 | +// Initialize the Deepgram client, using your Deepgram API key (you can find this in your Deepgram account settings). |
| 23 | +const deepgram = createClient(process.env.DEEPGRAM_SECRET_KEY); |
| 24 | + |
| 25 | +export const deepgramTranscription = task({ |
| 26 | + id: "deepgram-transcribe-audio", |
| 27 | + run: async (payload: { audioUrl: string }) => { |
| 28 | + const { audioUrl } = payload; |
| 29 | + |
| 30 | + logger.log("Transcribing audio from URL", { audioUrl }); |
| 31 | + |
| 32 | + // Transcribe the audio using Deepgram |
| 33 | + const { result, error } = await deepgram.listen.prerecorded.transcribeUrl( |
| 34 | + { |
| 35 | + url: audioUrl, |
| 36 | + }, |
| 37 | + { |
| 38 | + model: "nova-2", // Use the Nova 2 model for the transcription |
| 39 | + smart_format: true, // Automatically format transcriptions to improve readability |
| 40 | + diarize: true, // Recognize speaker changes and assign a speaker to each word in the transcript |
| 41 | + } |
| 42 | + ); |
| 43 | + |
| 44 | + if (error) { |
| 45 | + logger.error("Failed to transcribe audio", { error }); |
| 46 | + throw error; |
| 47 | + } |
| 48 | + |
| 49 | + console.dir(result, { depth: null }); |
| 50 | + |
| 51 | + // Extract the transcription from the result |
| 52 | + const transcription = result.results.channels[0].alternatives[0].paragraphs?.transcript; |
| 53 | + |
| 54 | + logger.log(`Generated transcription: ${transcription}`); |
| 55 | + |
| 56 | + return { |
| 57 | + result, |
| 58 | + }; |
| 59 | + }, |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +## Testing your task |
| 64 | + |
| 65 | +To test this task in the dashboard, you can use the following payload: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "audioUrl": "https://dpgr.am/spacewalk.wav" |
| 70 | +} |
| 71 | +``` |
0 commit comments