-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
sonix_audio.ts
66 lines (59 loc) · 1.87 KB
/
sonix_audio.ts
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
import { SonixSpeechRecognitionService } from "sonix-speech-recognition";
import { SpeechToTextRequest } from "sonix-speech-recognition/lib/types.js";
import { Document } from "@langchain/core/documents";
import { BaseDocumentLoader } from "../base.js";
/**
* A class that represents a document loader for transcribing audio files
* using the Sonix Speech Recognition service.
* @example
* ```typescript
* const loader = new SonixAudioTranscriptionLoader({
* sonixAuthKey: "SONIX_AUTH_KEY",
* request: {
* audioFilePath: "LOCAL_AUDIO_FILE_PATH",
* fileName: "FILE_NAME",
* language: "en",
* },
* });
* const docs = await loader.load();
* ```
*/
export class SonixAudioTranscriptionLoader extends BaseDocumentLoader {
private readonly sonixSpeechRecognitionService: SonixSpeechRecognitionService;
private readonly speechToTextRequest: SpeechToTextRequest;
constructor({
sonixAuthKey,
request: speechToTextRequest,
}: {
sonixAuthKey: string;
request: SpeechToTextRequest;
}) {
super();
this.sonixSpeechRecognitionService = new SonixSpeechRecognitionService(
sonixAuthKey
);
this.speechToTextRequest = speechToTextRequest;
}
/**
* Performs the speech-to-text transcription using the
* SonixSpeechRecognitionService and returns the transcribed text as a
* Document object.
* @returns An array of Document objects containing the transcribed text.
*/
async load(): Promise<Document[]> {
const { text, status, error } =
await this.sonixSpeechRecognitionService.speechToText(
this.speechToTextRequest
);
if (status === "failed") {
throw new Error(`Failed to transcribe audio file. Error: ${error}`);
}
const document = new Document({
pageContent: text,
metadata: {
fileName: this.speechToTextRequest.fileName,
},
});
return [document];
}
}