Skip to content

Commit 15cf1c2

Browse files
authored
🐛 fix transcript fetch (#2080)
1 parent ca4c7f0 commit 15cf1c2

File tree

6 files changed

+80
-72
lines changed

6 files changed

+80
-72
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
},
7777
"dependencies": {
7878
"@anthropic-ai/sdk": "^0.6.8",
79-
"@deepgram/sdk": "^2.4.0",
79+
"@deepgram/sdk": "^4.11.2",
8080
"@ffmpeg.wasm/core-mt": "0.13.2",
8181
"@ffmpeg.wasm/main": "^0.13.1",
8282
"@leveluptuts/svelte-side-menu": "^1.1.0",

pnpm-lock.yaml

Lines changed: 40 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/transcript/Transcript.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
1010
import type { AINoteWithFriends, TranscriptWithUtterances } from '$server/ai/queries';
1111
import type { SlimUtterance } from '$server/transcripts/types';
12-
import type { Utterance } from '@deepgram/sdk/dist/types';
12+
import type { SyncPrerecordedResponse } from '@deepgram/sdk';
1313
import type { Show } from '@prisma/client';
1414
15+
type Utterance = NonNullable<SyncPrerecordedResponse['results']['utterances']>[0];
16+
1517
interface Props {
1618
transcript: TranscriptWithUtterances;
1719
aiShowNote: AINoteWithFriends | null;

src/server/transcripts/deepgram.ts

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
// TODO WES BOS Remove
2-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3-
// @ts-nocheck
4-
import DeepgramPkg from '@deepgram/sdk';
5-
const { Deepgram } = DeepgramPkg;
1+
import { createClient } from '@deepgram/sdk';
62
import { prisma_client as prisma } from '$/server/prisma-client';
73
import { error } from '@sveltejs/kit';
84
import { keywords } from './fixes';
95
import { addFlaggerAudio } from './flagger';
106
import { save_transcript_to_db } from './transcripts';
11-
import type { PrerecordedTranscriptionResponse } from '@deepgram/sdk/dist/types';
12-
const deepgramApiKey = process.env.DEEPGRAM_SECRET;
13-
if (!deepgramApiKey) {
7+
8+
const deepgram_api_key = process.env.DEEPGRAM_SECRET;
9+
if (!deepgram_api_key) {
1410
console.error('Please set the DEEPGRAM_SECRET environment variable.');
1511
process.exit(1);
1612
}
1713

18-
// Initializes the Deepgram SDK
19-
export const deepgramClient = new Deepgram(deepgramApiKey, 'api.deepgram.com');
14+
export const deepgram_client = createClient(deepgram_api_key);
2015

2116
export async function get_transcript(showNumber: number) {
2217
const show = await prisma.show.findUnique({
@@ -35,39 +30,29 @@ export async function get_transcript(showNumber: number) {
3530
`Transcript for show #${show.number} already exists. Delete it if you want to re-fetch it.`
3631
);
3732
}
38-
const showBuffer = await addFlaggerAudio(show);
33+
const show_buffer = await addFlaggerAudio(show);
3934
console.log(`Fetching transcript for show #${show.number} - ${show.title}...`);
40-
console.log(showBuffer);
41-
// const filePath = join(process.cwd(), 'temp-transcript.json');
42-
const transcript: PrerecordedTranscriptionResponse = await deepgramClient.transcription
43-
.preRecorded(
44-
{
45-
buffer: showBuffer,
46-
mimetype: 'audio/mpeg'
47-
},
48-
{
49-
punctuate: true,
50-
model: 'nova-2-ea',
51-
language: 'en-US',
52-
detect_entities: true,
53-
diarize: true,
54-
smart_format: true,
55-
paragraphs: true, // Not very good
56-
utterances: true,
57-
detect_topics: false, // not very good
58-
keywords
59-
}
60-
)
61-
.catch((e) => {
62-
console.log(`Error fetching transcript for show #${show.number} - ${show.title}.`);
63-
console.log(e);
35+
36+
try {
37+
const transcript = await deepgram_client.listen.prerecorded.transcribeFile(show_buffer, {
38+
punctuate: true,
39+
model: 'nova-2-ea',
40+
language: 'en-US',
41+
detect_entities: true,
42+
diarize: true,
43+
smart_format: true,
44+
paragraphs: true, // Not very good
45+
utterances: true,
46+
detect_topics: false, // not very good
47+
keywords
6448
});
65-
// Temp: Write to disk as temp-transcript.json
66-
// const transcript = JSON.parse(await readFile(filePath, 'utf-8'));
67-
// await writeFile(filePath, JSON.stringify(transcript, null, 2));
68-
console.log(`Transcript for show #${show.number} - ${show.title} fetched.`);
69-
await save_transcript_to_db(show, transcript.results?.utterances || []);
7049

71-
// await writeFile(filePath, JSON.stringify(transcript, null, 2));
72-
console.log(`Transcript for show #${show.number} - ${show.title} saved.`);
50+
console.log(`Transcript for show #${show.number} - ${show.title} fetched.`);
51+
await save_transcript_to_db(show, transcript.result?.results.utterances || []);
52+
53+
console.log(`Transcript for show #${show.number} - ${show.title} saved.`);
54+
} catch (e) {
55+
console.log(`Error fetching transcript for show #${show.number} - ${show.title}.`);
56+
console.log(e);
57+
}
7358
}

src/server/transcripts/transcripts.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { PrerecordedTranscriptionResponse, Utterance } from '@deepgram/sdk/dist/types';
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
import type { SyncPrerecordedResponse } from '@deepgram/sdk';
23
import type { Show } from '@prisma/client';
34
import { error } from '@sveltejs/kit';
45
import fs, { readFile } from 'fs/promises';
@@ -8,6 +9,8 @@ import { detectSpeakerNames, getSlimUtterances } from './utils';
89
import pMap from 'p-map';
910
const transcripts_path = path.join(process.cwd(), 'src/assets/transcripts-flagged');
1011

12+
type Utterance = NonNullable<SyncPrerecordedResponse['results']['utterances']>[0];
13+
1114
export async function save_transcript_to_db(show: Show, utterances: Utterance[]) {
1215
// Create Slim Utterances for Speaker Detection
1316
const slim_utterances = getSlimUtterances(utterances, show.number);
@@ -89,7 +92,7 @@ export async function import_transcripts() {
8992
// Loop over each one and import
9093
const transcript_promises = transcriptFiles.map(async (file) => {
9194
console.log(`Importing ${file}`);
92-
const transcript: PrerecordedTranscriptionResponse = JSON.parse(
95+
const transcript: SyncPrerecordedResponse = JSON.parse(
9396
await readFile(path.join(transcripts_path, file), 'utf-8')
9497
);
9598
const show_number = parseInt(file.split(' - ')[0]);

src/server/transcripts/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import type { Utterance } from '@deepgram/sdk/dist/types/utterance';
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
import type { SyncPrerecordedResponse } from '@deepgram/sdk';
23
import type { PrismaUtterance, SlimUtterance, SpeakerMap } from './types';
34

5+
type Utterance = NonNullable<SyncPrerecordedResponse['results']['utterances']>[0];
6+
47
/**
58
* Get the slim version of the utterances. This is used for the transcript and embedding functions. It groups together utterances that have the same speaker.
69
* @param utterances the utterances to slim. This can be used on both Prisma Database Utterances and straight from Deepgram.

0 commit comments

Comments
 (0)