Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
},
"dependencies": {
"@anthropic-ai/sdk": "^0.6.8",
"@deepgram/sdk": "^2.4.0",
"@deepgram/sdk": "^4.11.2",
"@ffmpeg.wasm/core-mt": "0.13.2",
"@ffmpeg.wasm/main": "^0.13.1",
"@leveluptuts/svelte-side-menu": "^1.1.0",
Expand Down
65 changes: 40 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/lib/transcript/Transcript.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

import type { AINoteWithFriends, TranscriptWithUtterances } from '$server/ai/queries';
import type { SlimUtterance } from '$server/transcripts/types';
import type { Utterance } from '@deepgram/sdk/dist/types';
import type { SyncPrerecordedResponse } from '@deepgram/sdk';
import type { Show } from '@prisma/client';

type Utterance = NonNullable<SyncPrerecordedResponse['results']['utterances']>[0];

interface Props {
transcript: TranscriptWithUtterances;
aiShowNote: AINoteWithFriends | null;
Expand Down
69 changes: 27 additions & 42 deletions src/server/transcripts/deepgram.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
// TODO WES BOS Remove
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import DeepgramPkg from '@deepgram/sdk';
const { Deepgram } = DeepgramPkg;
import { createClient } from '@deepgram/sdk';
import { prisma_client as prisma } from '$/server/prisma-client';
import { error } from '@sveltejs/kit';
import { keywords } from './fixes';
import { addFlaggerAudio } from './flagger';
import { save_transcript_to_db } from './transcripts';
import type { PrerecordedTranscriptionResponse } from '@deepgram/sdk/dist/types';
const deepgramApiKey = process.env.DEEPGRAM_SECRET;
if (!deepgramApiKey) {

const deepgram_api_key = process.env.DEEPGRAM_SECRET;
if (!deepgram_api_key) {
console.error('Please set the DEEPGRAM_SECRET environment variable.');
process.exit(1);
}

// Initializes the Deepgram SDK
export const deepgramClient = new Deepgram(deepgramApiKey, 'api.deepgram.com');
export const deepgram_client = createClient(deepgram_api_key);

export async function get_transcript(showNumber: number) {
const show = await prisma.show.findUnique({
Expand All @@ -35,39 +30,29 @@ export async function get_transcript(showNumber: number) {
`Transcript for show #${show.number} already exists. Delete it if you want to re-fetch it.`
);
}
const showBuffer = await addFlaggerAudio(show);
const show_buffer = await addFlaggerAudio(show);
console.log(`Fetching transcript for show #${show.number} - ${show.title}...`);
console.log(showBuffer);
// const filePath = join(process.cwd(), 'temp-transcript.json');
const transcript: PrerecordedTranscriptionResponse = await deepgramClient.transcription
.preRecorded(
{
buffer: showBuffer,
mimetype: 'audio/mpeg'
},
{
punctuate: true,
model: 'nova-2-ea',
language: 'en-US',
detect_entities: true,
diarize: true,
smart_format: true,
paragraphs: true, // Not very good
utterances: true,
detect_topics: false, // not very good
keywords
}
)
.catch((e) => {
console.log(`Error fetching transcript for show #${show.number} - ${show.title}.`);
console.log(e);

try {
const transcript = await deepgram_client.listen.prerecorded.transcribeFile(show_buffer, {
punctuate: true,
model: 'nova-2-ea',
language: 'en-US',
detect_entities: true,
diarize: true,
smart_format: true,
paragraphs: true, // Not very good
utterances: true,
detect_topics: false, // not very good
keywords
});
// Temp: Write to disk as temp-transcript.json
// const transcript = JSON.parse(await readFile(filePath, 'utf-8'));
// await writeFile(filePath, JSON.stringify(transcript, null, 2));
console.log(`Transcript for show #${show.number} - ${show.title} fetched.`);
await save_transcript_to_db(show, transcript.results?.utterances || []);

// await writeFile(filePath, JSON.stringify(transcript, null, 2));
console.log(`Transcript for show #${show.number} - ${show.title} saved.`);
console.log(`Transcript for show #${show.number} - ${show.title} fetched.`);
await save_transcript_to_db(show, transcript.result?.results.utterances || []);

console.log(`Transcript for show #${show.number} - ${show.title} saved.`);
} catch (e) {
console.log(`Error fetching transcript for show #${show.number} - ${show.title}.`);
console.log(e);
}
}
7 changes: 5 additions & 2 deletions src/server/transcripts/transcripts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PrerecordedTranscriptionResponse, Utterance } from '@deepgram/sdk/dist/types';
/* eslint-disable @typescript-eslint/naming-convention */
import type { SyncPrerecordedResponse } from '@deepgram/sdk';
import type { Show } from '@prisma/client';
import { error } from '@sveltejs/kit';
import fs, { readFile } from 'fs/promises';
Expand All @@ -8,6 +9,8 @@ import { detectSpeakerNames, getSlimUtterances } from './utils';
import pMap from 'p-map';
const transcripts_path = path.join(process.cwd(), 'src/assets/transcripts-flagged');

type Utterance = NonNullable<SyncPrerecordedResponse['results']['utterances']>[0];

export async function save_transcript_to_db(show: Show, utterances: Utterance[]) {
// Create Slim Utterances for Speaker Detection
const slim_utterances = getSlimUtterances(utterances, show.number);
Expand Down Expand Up @@ -89,7 +92,7 @@ export async function import_transcripts() {
// Loop over each one and import
const transcript_promises = transcriptFiles.map(async (file) => {
console.log(`Importing ${file}`);
const transcript: PrerecordedTranscriptionResponse = JSON.parse(
const transcript: SyncPrerecordedResponse = JSON.parse(
await readFile(path.join(transcripts_path, file), 'utf-8')
);
const show_number = parseInt(file.split(' - ')[0]);
Expand Down
5 changes: 4 additions & 1 deletion src/server/transcripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { Utterance } from '@deepgram/sdk/dist/types/utterance';
/* eslint-disable @typescript-eslint/naming-convention */
import type { SyncPrerecordedResponse } from '@deepgram/sdk';
import type { PrismaUtterance, SlimUtterance, SpeakerMap } from './types';

type Utterance = NonNullable<SyncPrerecordedResponse['results']['utterances']>[0];

/**
* 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.
* @param utterances the utterances to slim. This can be used on both Prisma Database Utterances and straight from Deepgram.
Expand Down