-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscribe.ts
More file actions
87 lines (77 loc) · 1.94 KB
/
Copy pathtranscribe.ts
File metadata and controls
87 lines (77 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import fs from 'fs';
import { createClient } from '@deepgram/sdk';
interface Sentence {
text: string;
start: number;
end: number;
}
interface Word {
word: string;
start: number;
end: number;
punctuated_word?: string;
}
export interface TranscriptionResult {
sentences: Sentence[];
words: Word[];
}
function getDeepgramClient() {
return createClient(process.env.DEEPGRAM_API_KEY!);
}
export async function transcribeDummy(
audioPath: string
): Promise<TranscriptionResult> {
console.log(
`[transcribeDummy] Using static transcription file instead of transcribing ${audioPath}`
);
const transcriptFile = await fs.promises.readFile('transcript.json', 'utf8');
const transcription = JSON.parse(transcriptFile) as TranscriptionResult;
return transcription;
}
export async function transcribe(
audioPath: string
): Promise<TranscriptionResult> {
const deepgramClient = getDeepgramClient();
const { result, error } =
await deepgramClient.listen.prerecorded.transcribeFile(
fs.createReadStream(audioPath),
{
model: 'nova-3',
smart_format: true,
punctuate: true,
dictation: true,
utterances: true,
diarize: true,
language: 'fr',
filler_words: true,
}
);
if (error) {
throw error;
}
// get the sentences and words from the result
const sentences: Sentence[] = [];
for (const paragraph of result.results.channels[0].alternatives[0].paragraphs
?.paragraphs ?? []) {
for (const sentence of paragraph.sentences) {
sentences.push({
text: sentence.text,
start: sentence.start,
end: sentence.end,
});
}
}
const words: Word[] = [];
for (const word of result.results.channels[0].alternatives[0].words) {
words.push({
word: word.word,
start: word.start,
end: word.end,
punctuated_word: word.punctuated_word,
});
}
return {
sentences,
words,
};
}