-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add DEEPGRAM and OPENAI keys to env feat: init transcribeAudio with deepgram feat: add openai feat: add summarizeTranscript via OPENAI refactor: use `@env` to access openai key feat: add generateEpisodeContent
- Loading branch information
Showing
8 changed files
with
361 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ NEXT_PUBLIC_SUPABASE_URL= | |
|
||
SUPABASE_SERVICE_ROLE_KEY= | ||
SUPABASE_URL= | ||
|
||
DEEPGRAM_API_KEY= | ||
OPENAI_API_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { Tables } from '@/types/supabase/database'; | ||
|
||
import { | ||
getAccountId, | ||
updateAccountAICredits, | ||
validateAccountAICredits, | ||
} from '../account'; | ||
import { createSupabaseServiceClient } from '../supabase/service'; | ||
import { transcribeAudio } from './deepgram'; | ||
import { summarizeEpisodeTranscript } from './openai'; | ||
|
||
export const generateEpisodeContent = async ({ | ||
episodeId, | ||
}: { | ||
episodeId: Tables<'episode'>['id']; | ||
}) => { | ||
const initialAiCredits = await validateAccountAICredits(); | ||
const updatedAiCredits = await updateAccountAICredits(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 summarizeEpisodeTranscript({ | ||
title: episodeQuery.data.title, | ||
transcript, | ||
}); | ||
|
||
const createEpisodeContentQuery = await supabase | ||
.from('episode_content') | ||
.upsert( | ||
{ | ||
episode: episodeId, | ||
text_summary: summary, | ||
transcription: transcript, | ||
user: await getAccountId(), | ||
}, | ||
{ | ||
onConflict: 'episode', | ||
}, | ||
) | ||
.select('*') | ||
.single(); | ||
|
||
if (createEpisodeContentQuery.error) { | ||
throw new Error(createEpisodeContentQuery.error.message); | ||
} | ||
|
||
return createEpisodeContentQuery.data; | ||
} catch (error) { | ||
await updateAccountAICredits(updatedAiCredits + 1); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.