From a50ce21d92ce49c9459783c288b123ed44860111 Mon Sep 17 00:00:00 2001 From: Chiran Fernando Date: Thu, 3 Aug 2023 01:11:25 +0530 Subject: [PATCH 1/2] Create file.bal Add ballerina(https://ballerina.io/) support --- transcribing/file.bal | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 transcribing/file.bal diff --git a/transcribing/file.bal b/transcribing/file.bal new file mode 100644 index 0000000..b191da8 --- /dev/null +++ b/transcribing/file.bal @@ -0,0 +1,51 @@ +import ballerina/io; +import ballerina/http; +import ballerina/lang.runtime; + +configurable string API_TOKEN = ?; + +http:Client albumClient = check new ("https://api.assemblyai.com/v2", auth = { + token: API_TOKEN +}); + +public function uploadFile(string path) returns string|error { + http:Request request = new; + // Sets the file as the request payload. + request.setFileAsPayload(path); + + //Sends the request to the receiver service with the file content. + json res = check albumClient->/upload.post(request); + string uploadUrl = check res?.upload_url; + return uploadUrl; +} + +public function createTranscript(string audio_url) returns Transcription|error { + json result = check albumClient->/transcript.post({ + audio_url + }); + + string transcriptId = check result?.id; + while true { + json pollingJson = check albumClient->/transcript/[transcriptId].get(); + string status = check pollingJson?.status; + if status == "completed" { + return pollingJson.cloneWithType(); + } else if status == "error" { + string cause = check pollingJson?.'error; + panic error(string `Transcription failed due to ${cause}`); + } else { + runtime:sleep(5); + } + } +} + +type Transcription record { + string text; + float confidence; +}; + +public function main(string filePath) returns error? { + string uploadUrl = check uploadFile(filePath); + Transcription transcript = check createTranscript(uploadUrl); + io:println(transcript); +} From da400e021128279caf11cb36a955b1e42fe95195 Mon Sep 17 00:00:00 2001 From: Chiran Fernando Date: Thu, 3 Aug 2023 01:14:44 +0530 Subject: [PATCH 2/2] Update file.bal --- transcribing/file.bal | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/transcribing/file.bal b/transcribing/file.bal index b191da8..40de974 100644 --- a/transcribing/file.bal +++ b/transcribing/file.bal @@ -4,7 +4,7 @@ import ballerina/lang.runtime; configurable string API_TOKEN = ?; -http:Client albumClient = check new ("https://api.assemblyai.com/v2", auth = { +http:Client assemblyaiClient = check new ("https://api.assemblyai.com/v2", auth = { token: API_TOKEN }); @@ -14,19 +14,19 @@ public function uploadFile(string path) returns string|error { request.setFileAsPayload(path); //Sends the request to the receiver service with the file content. - json res = check albumClient->/upload.post(request); + json res = check assemblyaiClient->/upload.post(request); string uploadUrl = check res?.upload_url; return uploadUrl; } public function createTranscript(string audio_url) returns Transcription|error { - json result = check albumClient->/transcript.post({ + json result = check assemblyaiClient->/transcript.post({ audio_url }); string transcriptId = check result?.id; while true { - json pollingJson = check albumClient->/transcript/[transcriptId].get(); + json pollingJson = check assemblyaiClient->/transcript/[transcriptId].get(); string status = check pollingJson?.status; if status == "completed" { return pollingJson.cloneWithType(); @@ -47,5 +47,5 @@ type Transcription record { public function main(string filePath) returns error? { string uploadUrl = check uploadFile(filePath); Transcription transcript = check createTranscript(uploadUrl); - io:println(transcript); + io:println(string `Transcription: "${transcript.text}" with confidence ${transcript.confidence}`); }