Skip to content

Commit

Permalink
Merged refactor/audio2text branch
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrek6641 committed Jun 14, 2023
1 parent e6cea0d commit 6b184ac
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
5 changes: 3 additions & 2 deletions typescript/audio2text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
"Transform"
],
"repository": {
"type":"git",
"url":"https://github.com/scramjetorg/platform-samples/tree/main/typescript/audio2text"
"type": "git",
"url": "https://github.com/scramjetorg/platform-samples/tree/main/typescript/audio2text"
},
"author": "piotrek6641",
"license": "ISC",
"dependencies": {
"@scramjet/obj-logger": "^0.34.3",
"@scramjet/utility": "^0.33.5",
"axios": "^1.3.6",
"date-and-time": "^2.4.3",
Expand Down
7 changes: 5 additions & 2 deletions typescript/audio2text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ const app: ReadableApp<any> = async function(
) {
const assembly = new Assembly(token);

await assembly.processFile();
assembly.logger.outputLogStream.pipe(this.logger.inputLogStream);

return Promise.resolve("finished");
return assembly.processFileFs().then(res => {
this.logger.info("Result", res);
return res;
});
};

export default app;
82 changes: 50 additions & 32 deletions typescript/audio2text/src/utils/assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,42 @@ import axios, { AxiosInstance } from "axios";
import { defer } from "@scramjet/utility";
import { readFile } from "fs";
import path from "path";
import { ObjLogger } from "@scramjet/obj-logger";
import { Readable } from "stream";


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;
logger = new ObjLogger("Assembly");
fileUrl: string = "";
transciptId: 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,
authorization: token,
"transfer-encoding": "chunked",
"content-type" : "application/octet-stream"
},
});
}
private async upload(): Promise<string> {
private async uploadFromFs(): Promise<void> {
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");
this.logger.info("succesfully uploaded");
this.logger.info("upload URL", this.fileUrl);

resolve()
})
.catch((error) => console.error(error));
.catch((error) => this.logger.error("got axios error:", error));
});
});
}
Expand All @@ -46,30 +49,45 @@ export class Assembly {
})
.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);
private async upload(inputStream: Readable): Promise<void> {
return this.client
.post("/upload", inputStream)
.then((res) => {
this.fileUrl = res.data.upload_url;

await defer(3000);
}
});
this.logger.info("succesfully uploaded");
this.logger.info("upload URL", this.fileUrl);
})
.catch((error) => this.logger.error(error));
}
private async transcript(): Promise<string> {
const result = await this.client
.get<{ status?: string, error?: string, text?: string }>(`/transcript/${this.transciptId}`)
.then((res) => {
if (res.data.status === "error") {
this.logger.info("Got error from API", res.data.error);
}

return res.data;
});

if (result.status !== "completed") {
this.logger.info(result.status);
await defer(3000);
return this.transcript();
}

return result.text;
}
async processFileFs(): Promise<string> {
await this.uploadFromFs();
await this.startTranscript();
return this.transcript();
}
async processFile():Promise<void> {
await this.upload();
async processFile(inputStream: Readable): Promise<string> {
await this.upload(inputStream);
await this.startTranscript();
await this.transcript().then(console.log);
return this.transcript();
}
}

0 comments on commit 6b184ac

Please sign in to comment.