Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example deepgram topic detection cloud function using PHP #148

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions php/deepgram_topic_detection/README.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions php/deepgram_topic_detection/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "opr/deepgram_topic_detection",
"description": "",
"type": "library",
"license": "ISC",
"authors": [],
"require": {
"php": ">=8.0.0",
"guzzlehttp/guzzle": "^7.0"
}
}
78 changes: 78 additions & 0 deletions php/deepgram_topic_detection/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

require 'vendor/autoload.php';

use GuzzleHttp\Client;

return function($req, $res) {
try {
$payload = \json_decode($req['payload'], true);
} catch(\Exception $err) {
\var_dump($err);
throw new \Exception('Payload is invalid.');
}

if (!isset($payload['fileUrl']) || empty($fileUrl = \trim($payload['fileUrl']))) {
$res->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()
]);
}
};