From 52375c27859fa637996dd8aa94292a4de9ba0dd8 Mon Sep 17 00:00:00 2001 From: Pranshu Kumar Agrawal Date: Sat, 29 Jul 2023 13:47:49 +0530 Subject: [PATCH] add function for deepgramLanguageDetection --- kotlin/deepgram_language_detection/README.md | 69 +++++++++++++++ .../deepgram_language_detection/deps.gradle | 3 + .../deepgram_language_detection/src/Index.kt | 86 +++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 kotlin/deepgram_language_detection/README.md create mode 100644 kotlin/deepgram_language_detection/deps.gradle create mode 100644 kotlin/deepgram_language_detection/src/Index.kt diff --git a/kotlin/deepgram_language_detection/README.md b/kotlin/deepgram_language_detection/README.md new file mode 100644 index 00000000..5d6c8edb --- /dev/null +++ b/kotlin/deepgram_language_detection/README.md @@ -0,0 +1,69 @@ +# ๐Ÿงน Deepgram Language Detection + +A Cloud Function for language detection using [Deepgram](https://deepgram.com/) + +_Example input:_ + +```json +{ + "fileUrl": "https://static.deepgram.com/examples/interview_speech-analytics.wav" +} +``` + +_Example output:_ + +```json +{ + "deepgramData": {}, + "success": true +} +``` + +_Example error output:_ + +```json +{ "success": false, "message": "Please provide a valid file URL." } +``` + +## ๐Ÿ“ Environment Variables + +**DEEPGRAM_API_KEY** - Your Deepgram secret API key. +Details are under link: [Deepgram_getting_started](https://developers.deepgram.com/documentation/getting-started/) + +## ๐Ÿš€ Deployment + +1. Clone this repository, and enter this function folder: + +``` +$ git clone https://github.com/open-runtimes/examples.git && cd examples +$ cd kotlin/deepgram_language_detection +``` + +2. Build the code: + +```bash +docker run -e INTERNAL_RUNTIME_ENTRYPOINT=src/Index.kt --rm --interactive --tty --volume $PWD:/usr/code openruntimes/kotlin:v2-1.6 sh /usr/local/src/build.sh +``` + +3. Spin-up open-runtime: + +```bash +docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/kotlin:v2-1.6 sh /usr/local/src/start.sh +``` + +4. Run the curl function to send request. + +```bash +curl --location --request POST 'http://localhost:3000/' \ +--header 'X-Internal-Challenge: secret-key' \ +--header 'Content-Type: application/json' \ +--data-raw '{"payload": "{\"fileUrl\": \"https://static.deepgram.com/examples/interview_speech-analytics.wav\"}", +"variables": {"DEEPGRAM_API_KEY":""}} +' +``` + +Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Kotlin runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/kotlin-1.6). + +## ๐Ÿ“ Notes + +- This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions). diff --git a/kotlin/deepgram_language_detection/deps.gradle b/kotlin/deepgram_language_detection/deps.gradle new file mode 100644 index 00000000..88d25ae1 --- /dev/null +++ b/kotlin/deepgram_language_detection/deps.gradle @@ -0,0 +1,3 @@ +dependencies { + implementation 'com.google.code.gson:gson:2.9.0' +} diff --git a/kotlin/deepgram_language_detection/src/Index.kt b/kotlin/deepgram_language_detection/src/Index.kt new file mode 100644 index 00000000..4e78b5bb --- /dev/null +++ b/kotlin/deepgram_language_detection/src/Index.kt @@ -0,0 +1,86 @@ +import com.google.gson.Gson +import com.google.gson.JsonSyntaxException +import io.openruntimes.kotlin.RuntimeRequest +import io.openruntimes.kotlin.RuntimeResponse +import java.io.BufferedReader +import java.io.IOException +import java.io.InputStreamReader +import java.io.OutputStream +import java.net.HttpURLConnection +import java.net.URL + + +val HTTP_BAD_REQEST = 400 +val DEEPGRAM_LANGUAGE_DETECTION_API_END_POINT = "https://api.deepgram.com/v1/listen?model=general&detect_language=true&punctuate=true" +val gson = Gson() + +fun getErrorResponseWithMessage(res: RuntimeResponse, message: String? = "Some error occurred"): RuntimeResponse { + return res.json( + data = mapOf( + "success" to false, + "message" to message + ), + statusCode = HTTP_BAD_REQEST + ) +} + +@Throws(Exception::class) +suspend fun main(req: RuntimeRequest, res: RuntimeResponse): RuntimeResponse { + + val deepgramApiKey = req.variables["DEEPGRAM_API_KEY"] + if (deepgramApiKey == null || deepgramApiKey.trim().isEmpty()) { + return getErrorResponseWithMessage(res, "Deepgram API key must be set to use this function.") + } + + val payloadMap = Gson().fromJson>( + req.payload.ifBlank { "{}" }, + Map::class.java + ) + val fileUrl = payloadMap["fileUrl"] ?: "" + + if (fileUrl.isEmpty() || fileUrl.trim().isEmpty()) { + return getErrorResponseWithMessage(res, "Payload doesn't contain 'fileUrl'") + } + + val url = URL(DEEPGRAM_LANGUAGE_DETECTION_API_END_POINT) + val conn: HttpURLConnection = url.openConnection() as HttpURLConnection + + conn.requestMethod = "POST" + conn.addRequestProperty("Content-Type", "application/json") + conn.addRequestProperty("Authorization", "Token $deepgramApiKey") + + conn.doOutput = true + + //prepare request body + val requestBody ="{\"url\":\""+fileUrl+"\"}" + + val response = StringBuilder() + + try { + val os: OutputStream = conn.getOutputStream() + val input: ByteArray = requestBody.toByteArray(Charsets.UTF_8) + os.write(input, 0, input.size) + + val br = BufferedReader(InputStreamReader(conn.getInputStream(), "utf-8")) + var responseLine: String? = null + while (br.readLine().also { responseLine = it } != null) { + response.append(responseLine!!.trim { it <= ' ' }) + } + + br.close() + conn.disconnect() + } + catch (e: Exception){ + return getErrorResponseWithMessage(res, e.message) + } + + + val deepgramResponse = response.toString() + return res.json( + data = mapOf( + "success" to true, + "deepgramData" to deepgramResponse + ), + statusCode = 200 + ) +}