Skip to content

Commit

Permalink
refactor: move stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Dec 12, 2023
1 parent ad8b64d commit 47fb9cd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 56 deletions.
25 changes: 25 additions & 0 deletions lib/services/ai/deepgram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { env } from '@/env.mjs';
import { createClient as createDeepgramClient } from '@deepgram/sdk';

const deepgram = createDeepgramClient(env.DEEPGRAM_API_KEY);

export const transcribeAudio = async ({ fileURL }: { fileURL: string }) => {
const { result } = await deepgram.listen.prerecorded.transcribeUrl(
{
url: fileURL,
},
{
diarize: true,
model: 'nova-2',
smart_format: true,
},
);

const transcript = result?.results.channels[0]?.alternatives[0]?.transcript;

if (!transcript) {
throw new Error('No transcript found');
}

return transcript;
};
Original file line number Diff line number Diff line change
@@ -1,64 +1,13 @@
import type { Tables } from '@/types/supabase/database';

import { env } from '@/env.mjs';
import { createClient as createDeepgramClient } from '@deepgram/sdk';
import { OpenAI } from 'openai';

import {
getAccountId,
updateAccountAICredits,
validateAccountAICredits,
} from './account';
import { createSupabaseServiceClient } from './supabase/service';

const transcribeAudio = async ({ fileURL }: { fileURL: string }) => {
const deepgram = createDeepgramClient(env.DEEPGRAM_API_KEY);

const { result } = await deepgram.listen.prerecorded.transcribeUrl(
{
url: fileURL,
},
{
diarize: true,
model: 'nova-2',
smart_format: true,
},
);

const transcript = result?.results.channels[0]?.alternatives[0]?.transcript;

if (!transcript) {
throw new Error('No transcript found');
}

return transcript;
};

const summarizeTranscript = async ({
title,
transcript,
}: {
title: string;
transcript: string;
}) => {
const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY });

const response = await openai.chat.completions.create({
messages: [
{
content: `Summarize the following transcript from a podcast episode titled as ${title}`,
role: 'system',
},
{
content: transcript,
role: 'system',
},
],
model: 'gpt-3.5-turbo-1106',
});

return response.choices[0].message.content;
};
} from '../account';
import { createSupabaseServiceClient } from '../supabase/service';
import { transcribeAudio } from './deepgram';
import { summarizeEpisodeTranscript } from './openai';

export const generateEpisodeContent = async ({
episodeId,
Expand All @@ -85,7 +34,7 @@ export const generateEpisodeContent = async ({
fileURL: episodeQuery.data.audio_url,
});

const summary = await summarizeTranscript({
const summary = await summarizeEpisodeTranscript({
title: episodeQuery.data.title,
transcript,
});
Expand Down
28 changes: 28 additions & 0 deletions lib/services/ai/openai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { env } from '@/env.mjs';
import { OpenAI } from 'openai';

const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY });

export const summarizeEpisodeTranscript = async ({
title,
transcript,
}: {
title: string;
transcript: string;
}) => {
const response = await openai.chat.completions.create({
messages: [
{
content: `Summarize the following transcript from a podcast episode titled as ${title}`,
role: 'system',
},
{
content: transcript,
role: 'system',
},
],
model: 'gpt-3.5-turbo-1106',
});

return response.choices[0].message.content;
};

0 comments on commit 47fb9cd

Please sign in to comment.