forked from langchain-ai/langchainjs
-
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.
Creating sonix audio transcription document loader integration (langc…
…hain-ai#1917) * Creating sonix audio transcription loader * Rename to match convention, small docs updates --------- Co-authored-by: jacoblee93 <[email protected]>
- Loading branch information
1 parent
c70ff59
commit 981d86f
Showing
10 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...les/indexes/document_loaders/examples/web_loaders/sonix_audio_transcription.mdx
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,33 @@ | ||
--- | ||
hide_table_of_contents: true | ||
sidebar_class_name: node-only | ||
--- | ||
|
||
# Sonix Audio | ||
|
||
:::tip Compatibility | ||
Only available on Node.js. | ||
::: | ||
|
||
This covers how to load document objects from an audio file using the [Sonix](https://sonix.ai/) API. | ||
|
||
## Setup | ||
|
||
To run this loader you will need to create an account on the https://sonix.ai/ and obtain an auth key from the https://my.sonix.ai/api page. | ||
|
||
You'll also need to install the `sonix-speech-recognition` library: | ||
|
||
```bash npm2yarn | ||
npm install sonix-speech-recognition | ||
``` | ||
|
||
## Usage | ||
|
||
Once auth key is configured, you can use the loader to create transcriptions and then convert them into a Document. | ||
In the `request` parameter, you can either specify a local file by setting `audioFilePath` or a remote file using `audioUrl`. | ||
You will also need to specify the audio language. See the list of supported languages [here](https://sonix.ai/docs/api#languages). | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/document_loaders/sonix_audio_transcription.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> |
14 changes: 14 additions & 0 deletions
14
examples/src/document_loaders/sonix_audio_transcription.ts
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,14 @@ | ||
import { SonixAudioTranscriptionLoader } from "langchain/document_loaders/web/sonix_audio_transcription"; | ||
|
||
const loader = new SonixAudioTranscriptionLoader({ | ||
sonixAuthKey: "SONIX_AUTH_KEY", | ||
request: { | ||
audioFilePath: "LOCAL_AUDIO_FILE_PATH", | ||
fileName: "FILE_NAME", | ||
language: "en", | ||
}, | ||
}); | ||
|
||
const docs = await loader.load(); | ||
|
||
console.log(docs); |
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
45 changes: 45 additions & 0 deletions
45
langchain/src/document_loaders/web/sonix_audio_transcription.ts
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,45 @@ | ||
import { SonixSpeechRecognitionService } from "sonix-speech-recognition"; | ||
import { SpeechToTextRequest } from "sonix-speech-recognition/lib/types.js"; | ||
import { Document } from "../../document.js"; | ||
import { BaseDocumentLoader } from "../base.js"; | ||
|
||
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; | ||
} | ||
|
||
async load(): Promise<Document[]> { | ||
const { text, status, error } = | ||
await this.sonixSpeechRecognitionService.speechToText( | ||
this.speechToTextRequest | ||
); | ||
|
||
if (status === "failed") { | ||
console.error("Error:", error); | ||
return []; | ||
} | ||
|
||
const document = new Document({ | ||
pageContent: text, | ||
metadata: { | ||
fileName: this.speechToTextRequest.fileName, | ||
}, | ||
}); | ||
|
||
return [document]; | ||
} | ||
} |
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
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