Skip to content

Commit

Permalink
feat: add generateEpisodeContent
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Dec 11, 2023
1 parent 102c834 commit 48d5f5c
Showing 1 changed file with 72 additions and 6 deletions.
78 changes: 72 additions & 6 deletions lib/services/episode-content-generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +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';

const transcribeAudio = async (fileURL: string) => {
import { 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(
Expand All @@ -25,15 +30,19 @@ const transcribeAudio = async (fileURL: string) => {
return transcript;
};

const summarizeTranscript = async (transcript: string) => {
const openai = new OpenAI({
apiKey: env.OPENAI_API_KEY,
});
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:',
content: `Summarize the following transcript from a podcast episode titled as ${title}`,
role: 'system',
},
{
Expand All @@ -46,3 +55,60 @@ const summarizeTranscript = async (transcript: string) => {

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

export const generateEpisodeContent = async ({
accountId,
episodeId,
}: {
accountId: Tables<'account'>['id'];
episodeId: Tables<'episode'>['id'];
}) => {
const initialAiCredits = await validateAccountAICredits(accountId);
const updatedAiCredits = await updateAccountAICredits(
accountId,
initialAiCredits - 1,
);

try {
const supabase = createSupabaseServiceClient();

const episodeQuery = await supabase
.from('episode')
.select('title, audio_url')
.eq('id', episodeId)
.single();

if (episodeQuery.error) {
throw new Error(episodeQuery.error.message);
}

const transcript = await transcribeAudio({
fileURL: episodeQuery.data.audio_url,
});

const summary = await summarizeTranscript({
title: episodeQuery.data.title,
transcript,
});

const createEpisodeContentQuery = await supabase
.from('episode_content')
.insert({
episode: episodeId,
text_summary: summary,
transcript,
user: accountId,
})
.select('*')
.single();

if (createEpisodeContentQuery.error) {
throw new Error(createEpisodeContentQuery.error.message);
}

return createEpisodeContentQuery.data;
} catch (error) {
await updateAccountAICredits(accountId, updatedAiCredits + 1);
throw error;
}
};

0 comments on commit 48d5f5c

Please sign in to comment.