diff --git a/transcribing/file.bal b/transcribing/file.bal new file mode 100644 index 0000000..40de974 --- /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 assemblyaiClient = 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 assemblyaiClient->/upload.post(request); + string uploadUrl = check res?.upload_url; + return uploadUrl; +} + +public function createTranscript(string audio_url) returns Transcription|error { + json result = check assemblyaiClient->/transcript.post({ + audio_url + }); + + string transcriptId = check result?.id; + while true { + json pollingJson = check assemblyaiClient->/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(string `Transcription: "${transcript.text}" with confidence ${transcript.confidence}`); +}