From 817a72ac2eb2334b09d9b4e616be4b09c35a4267 Mon Sep 17 00:00:00 2001 From: Sebabrata Kundu Date: Sat, 6 May 2023 14:57:11 +0530 Subject: [PATCH 1/2] feat: add deepgram php function to detect topic from audio --- php/deepgram_topic_detection/composer.json | 11 +++ php/deepgram_topic_detection/index.php | 78 ++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 php/deepgram_topic_detection/composer.json create mode 100644 php/deepgram_topic_detection/index.php diff --git a/php/deepgram_topic_detection/composer.json b/php/deepgram_topic_detection/composer.json new file mode 100644 index 00000000..4ee12e97 --- /dev/null +++ b/php/deepgram_topic_detection/composer.json @@ -0,0 +1,11 @@ +{ + "name": "opr/deepgram_topic_detection", + "description": "", + "type": "library", + "license": "ISC", + "authors": [], + "require": { + "php": ">=8.0.0", + "guzzlehttp/guzzle": "^7.0" + } +} diff --git a/php/deepgram_topic_detection/index.php b/php/deepgram_topic_detection/index.php new file mode 100644 index 00000000..b9217a50 --- /dev/null +++ b/php/deepgram_topic_detection/index.php @@ -0,0 +1,78 @@ +json([ + 'success' => false, + 'message' => 'Please provide a valid file URL.' + ]); + } + if ( + !isset($req['variables']['DEEPGRAM_API_SECRET_KEY']) || + empty($deepgramSecretApiKey = \trim($req['variables']['DEEPGRAM_API_SECRET_KEY'])) + ) { + $res->json([ + 'success' => false, + 'message' => 'Please provide valid deepgram api secret key under variables.' + ]); + } + + $extension = pathinfo($fileUrl, PATHINFO_EXTENSION); + + if ($extension !== 'wav') { + $res->json([ + 'success' => false, + 'message' => 'Please provide valid file url extension. This should be wav.' + ]); + } + + $client = new Client(); + $headers = [ + 'Authorization' => 'Token ' . $deepgramSecretApiKey, + 'Content-Type' => 'application/json' + ]; + $body = json_encode([ + 'url' => $fileUrl + ]); + $query = [ + 'detect_topics' => 'true', + 'punctuate' => 'true', + ]; + + try { + $response = $client->request('POST', 'https://api.deepgram.com/v1/listen', [ + 'body' => $body, + 'headers' => $headers, + 'query' => $query, + ]); + } catch(\Exception $err) { + $res->json([ + 'success' => false, + 'message' => $err->getMessage() + ]); + } + + if ($response->getStatusCode() === 200) { + $deepgramData = $response->getBody()->getContents(); + $res->json([ + 'success' => true, + 'deepgramData' => \json_decode($deepgramData) + ]); + } else { + $res->json([ + 'success' => false, + 'message' => $response->getBody()->getContents() + ]); + } +}; From c31b342fc9fb22b6500adc085a6ce66f929dd6c6 Mon Sep 17 00:00:00 2001 From: Sebabrata Kundu Date: Sat, 6 May 2023 14:57:45 +0530 Subject: [PATCH 2/2] feat: add readme for deepgram topic detection func --- php/deepgram_topic_detection/README.md | 59 ++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 php/deepgram_topic_detection/README.md diff --git a/php/deepgram_topic_detection/README.md b/php/deepgram_topic_detection/README.md new file mode 100644 index 00000000..fbd9dfff --- /dev/null +++ b/php/deepgram_topic_detection/README.md @@ -0,0 +1,59 @@ +# Deepgram Topic Detection + +A PHP cloud function to detect topic from audio using [Deepgram API](https://developers.deepgram.com/documentation/). + +_Example input:_ + +```json +{ + "payload": { + "fileUrl": "https://static.deepgram.com/examples/interview_speech-analytics.wav" + }, + "variables": { + "DEEPGRAM_API_SECRET_KEY": "YOUR_PERSONAL_DEEPGRAM_API_SECRET_KEY" + } +} +``` + +_Example output:_ + +```json +{ + "success": true, + "deepgramData": "DEEPGRAM_DATA" +} +``` + +## 📝 Variables + +List of variables used by this cloud function: + +**DEEPGRAM_API_SECRET_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 php/deepgram_topic_detection +``` + +2. Enter this function folder and build the code: +``` +docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/php:v2-8.1 sh /usr/local/src/build.sh +``` +As a result, a `code.tar.gz` file will be generated. + +3. Start the Open Runtime: +``` +docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_ENTRYPOINT=index.php --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/php:v2-8.1 sh /usr/local/src/start.sh +``` + +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 PHP runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/php-8.1). + +## 📝 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). +- This example is compatible with PHP 8.1. Other versions may work but are not guaranteed to work as they haven't been tested. \ No newline at end of file