Skip to content

Commit

Permalink
audio to text
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrek6641 committed May 24, 2023
1 parent dbbea91 commit e35ba7f
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
40 changes: 40 additions & 0 deletions typescript/audio2text/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Audio to text

Sequence used to transcript audio file to a text using AssemblyAi api.

For now this sequence lets transcript only file that is in a dist folder

## Prerequirements

To run this sequence it is required to have own api key from [AssemblyAi](https://www.assemblyai.com)


## Running

> ❗ Remember to [setup transform-hub locally](https://docs.scramjet.org/platform/self-hosted-installation) or use the [platform's environment](https://docs.scramjet.org/platform/quick-start) for the sequence deployment.
Open the terminal and run the following commands:

```bash
# install dependencies
npm install

# transpile TS->JS to dist/
npm run build

# make a compressed package with Sequence
si seq pack dist

# send Sequence to transform hub, this will output Sequence ID
si seq send dist.tar.gz

# start a Sequence, provide AssemblyAi-key as an argument parameter
si seq start - --args [\"AssemblyAi-key\"]
```

To get a output:

```bash
si inst stdout -

```
28 changes: 28 additions & 0 deletions typescript/audio2text/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "audio2text",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc -p tsconfig.json",
"postbuild": "cp -r package.json dist/ && (cd dist && npm i --omit=dev) && cp src/utils/song.wav dist/utils ",
"pack": "si seq pack dist/",
"clean": "rm -rf ./dist ./*.tar.gz"
},
"keywords": [
"API",
"Tranform"
],
"author": "piotrek6641",
"license": "ISC",
"dependencies": {
"@scramjet/utility": "^0.33.5",
"axios": "^1.3.6",
"date-and-time": "^2.4.3",
"typescript": "^5.0.4"
},
"devDependencies": {
"@scramjet/types": "0.33.5",
"@types/node": "^20.2.3"
}
}
15 changes: 15 additions & 0 deletions typescript/audio2text/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ReadableApp } from "@scramjet/types";
import { Assembly } from "./utils/assembly";

const app: ReadableApp<any> = async function(
_stream,
token:string
) {
const assembly = new Assembly(token);

await assembly.processFile();

return Promise.resolve("finished");
};

export default app;
75 changes: 75 additions & 0 deletions typescript/audio2text/src/utils/assembly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-disable no-console */
import axios, { AxiosInstance } from "axios";
import { defer } from "@scramjet/utility";
import { readFile } from "fs";
import path from "path";


export class Assembly {
//path to file change song.wav to name of your file
//file has to be in a same directory
file = path.join(__dirname, "song.wav");
fileUrl:string = "";
status:string = "";
transciptId:string = "";
token:string;
client:AxiosInstance;
constructor(token:string) {
this.token = token;
this.client = axios.create({
baseURL: "https://api.assemblyai.com/v2",
headers: {
authorization: this.token, //conf.apiKey,
"transfer-encoding": "chunked",
},
});
}
private async upload(): Promise<string> {
return new Promise((resolve, reject) => {
readFile(this.file, async (err, data) => {
if (err) reject(err);

await this.client
.post("/upload", data)
.then((res) => {
this.fileUrl = res.data.upload_url;
resolve("succesfully uploaded");
})
.catch((error) => console.error(error));
});
});
}
private async startTranscript():Promise<void> {
await this.client
.post("/transcript", {
audio_url: this.fileUrl
})
.then((res) => { this.transciptId = res.data.id; });
}
private async transcript():Promise<string> {
return new Promise(async (resolve, _reject) => {
while (this.status !== "completed") {
this.client
.get(`/transcript/${this.transciptId}`)
// eslint-disable-next-line no-loop-func
.then((res) => {
if (res.data.status === "completed") {
resolve(res.data.text);
} else {
console.log(res.data.status + "...");
this.status = res.data.status;
}
})
.catch(console.error);

await defer(3000);
}
});
}
async processFile():Promise<void> {
await this.upload();
await this.startTranscript();
await this.transcript().then(console.log);
}
}

Binary file added typescript/audio2text/src/utils/song.wav
Binary file not shown.
12 changes: 12 additions & 0 deletions typescript/audio2text/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"lib": [ "ESNext" ],
"target": "ESNext",
"module": "CommonJS",
"outDir": "./dist",
"esModuleInterop": true,
"skipLibCheck": true,
"resolveJsonModule": true,
}

}

0 comments on commit e35ba7f

Please sign in to comment.