-
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.
- Loading branch information
1 parent
dbbea91
commit e35ba7f
Showing
6 changed files
with
170 additions
and
0 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 |
---|---|---|
@@ -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 - | ||
|
||
``` |
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 @@ | ||
{ | ||
"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" | ||
} | ||
} |
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,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; |
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,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 not shown.
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,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": [ "ESNext" ], | ||
"target": "ESNext", | ||
"module": "CommonJS", | ||
"outDir": "./dist", | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"resolveJsonModule": true, | ||
} | ||
|
||
} |