forked from open-runtimes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
225 changed files
with
11,887 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# 🌐 Compress any Image | ||
|
||
A Dart Cloud Function that perform a lossless compression of an image. Input image is in base64 format. | ||
|
||
_Example input 1:_ | ||
|
||
```json | ||
{ | ||
"provider":"tinypng", | ||
"image":"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAf0lEQVR4nO2Wuw2AMAxEbw1gpMwDDMBcGQpooDKydGVAoXCK6J7k6qyc83MCCFGP/Yz+CkDF4KHmjgowbQF0CKFrCDUiwztqxabHCL0/xwcNhoI2UdsjC8g0mQvaSs1zwkg0uQAsAEaGm9/UPCeU7eMj6loTEpf6ZOQWMxd98gAhZnS6XEZcNQAAAABJRU5ErkJggg==" | ||
} | ||
``` | ||
|
||
_Example output 1:_ | ||
|
||
```json | ||
{ | ||
"success":true, | ||
"image":"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAAG1BMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACUUeIgAAAACHRSTlMA8712Sxr5g97cFtUAAAA9SURBVCjPY6Aa6AADfAIcDSA8KoBTgLGVgSFCAEmAqZmBwUIBSYClzTQ4wwE52Cs6OtpR4oFFUciBerEKAP58HnyLtZsYAAAAAElFTkSuQmCC" | ||
} | ||
``` | ||
|
||
_Example input 2:_ | ||
|
||
```json | ||
{ | ||
"provider": "tinypng", | ||
"image": "wrong-image" | ||
} | ||
``` | ||
|
||
_Example output 2:_ | ||
|
||
```json | ||
{ | ||
"success": false, | ||
"message": "Payload is invalid" | ||
} | ||
``` | ||
|
||
## 📝 Environment Variables | ||
|
||
List of environment variables used by this cloud function: | ||
|
||
* **TINYPNG_API_KEY** - API Key for [tinyPNG](https://tinypng.com/developers) | ||
<!-- * **TINYURL_API_KEY** - API Key for TinyUrl --> | ||
|
||
## 🚀 Deployment | ||
|
||
1. Clone this repository, and enter this function folder: | ||
|
||
```shell | ||
git clone https://github.com/open-runtimes/examples.git && cd examples | ||
cd dart/compress_image | ||
``` | ||
|
||
2. Enter this function folder and build the code: | ||
|
||
```shell | ||
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=lib/main.dart --rm --interactive --tty --volume $PWD:/usr/code openruntimes/dart:v2-2.16 sh /usr/local/src/build.sh | ||
``` | ||
|
||
As a result, a `code.tar.gz` file will be generated. | ||
|
||
3. Start the Open Runtime: | ||
|
||
```shell | ||
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/dart:v2-2.16 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 Dart runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/dart-2.16). | ||
|
||
4. Execute function: | ||
|
||
```shell | ||
curl http:https://localhost:3000/ -d '{"variables":{"TINYPNG_API_KEY":"YOUR_TINYPNG_API_KEY"},"payload":"{\"provider\":\"tinypng\",\"image\":\"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAf0lEQVR4nO2Wuw2AMAxEbw1gpMwDDMBcGQpooDKydGVAoXCK6J7k6qyc83MCCFGP/Yz+CkDF4KHmjgowbQF0CKFrCDUiwztqxabHCL0/xwcNhoI2UdsjC8g0mQvaSs1zwkg0uQAsAEaGm9/UPCeU7eMj6loTEpf6ZOQWMxd98gAhZnS6XEZcNQAAAABJRU5ErkJggg==\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" | ||
``` | ||
|
||
## 📝 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 Dart 2.16. Other versions may work but are not guaranteed to work as they haven't been tested. Versions below Dart 2.14 will not work, because Apwrite SDK requires Dart 2.14, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
import 'package:http/http.dart' as http; | ||
|
||
Future<void> _tinypng( | ||
final res, final Uint8List binaryImage, final String token) async { | ||
final reqUrl = Uri.parse("https://api.tinify.com/shrink"); | ||
final headers = { | ||
"Authorization": "Basic ${base64.encode(utf8.encode('api:$token'))}", | ||
}; | ||
|
||
try { | ||
var tinypngRes = | ||
await http.post(reqUrl, headers: headers, body: binaryImage); | ||
|
||
if (tinypngRes.statusCode == 200 || tinypngRes.statusCode == 201) { | ||
final output = jsonDecode(tinypngRes.body)['output']; | ||
final compressedImageURL = output['url']; | ||
final compressedImageBytes = | ||
await http.get(Uri.parse(compressedImageURL)); | ||
markSuccess(res, base64Encode(compressedImageBytes.bodyBytes)); | ||
return; | ||
} else { | ||
throw Exception(tinypngRes.reasonPhrase); | ||
} | ||
} catch (e) { | ||
print("Failure in tinpng $e"); | ||
markFailure(res, e.toString()); | ||
return; | ||
} | ||
} | ||
|
||
void markSuccess(final res, final String image) { | ||
res.json({ | ||
'success': true, | ||
'image': image, | ||
}); | ||
} | ||
|
||
void markFailure(final res, final String message) { | ||
res.json({ | ||
'success': false, | ||
'message': message, | ||
}); | ||
} | ||
|
||
Future<void> start(final request, final resonse) async { | ||
late final payload; | ||
late final String provider, imageBase64, apiKey; | ||
late final Uint8List image; | ||
try { | ||
payload = jsonDecode(request.payload); | ||
provider = payload['provider'].toLowerCase(); | ||
imageBase64 = payload['image']; | ||
} catch (err) { | ||
markFailure(resonse, "Payload is invalid"); | ||
return; | ||
} | ||
|
||
try { | ||
image = base64Decode(imageBase64); | ||
} catch (err) { | ||
markFailure(resonse, imageBase64); | ||
return; | ||
} | ||
|
||
switch (provider) { | ||
case "tinypng": | ||
try { | ||
apiKey = request.variables["TINYPNG_API_KEY"]; | ||
} catch (err) { | ||
markFailure(resonse, "TINYPNG_API_KEY is not provided"); | ||
return; | ||
} | ||
await _tinypng(resonse, image, apiKey); | ||
break; | ||
default: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name: compress_image | ||
description: "" | ||
version: 1.0.0 | ||
|
||
environment: | ||
sdk: ">=2.14.0 <3.0.0" | ||
|
||
dependencies: | ||
http: ^0.13.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# 📝 Topic detection using Deepgram | ||
|
||
A Dart Cloud Function that detects topics from pre-recorded audio using [Deepgram](https://developers.deepgram.com/). | ||
|
||
_Example input 1:_ | ||
|
||
```json | ||
{ | ||
"fileUrl": "https://static.deepgram.com/examples/interview_speech-analytics.wav" | ||
} | ||
``` | ||
|
||
_Example output 1:_ | ||
|
||
```json | ||
{ | ||
"success": true, | ||
"deepgramData": { } | ||
} | ||
``` | ||
|
||
_Example input 2:_ | ||
|
||
```json | ||
{ | ||
"fileUrl": "https://static.deepgram.com/examples/interview_speech-analytics.wav" | ||
} | ||
``` | ||
|
||
_Example output 2:_ | ||
|
||
```json | ||
{ | ||
"success": false, | ||
"message": "Please provide a valid file URL." | ||
} | ||
``` | ||
|
||
_Example input 3:_ | ||
|
||
```json | ||
{ } | ||
``` | ||
|
||
_Example output 3:_ | ||
|
||
```json | ||
{ | ||
"success": false, | ||
"message": "Please provide a valid file URL." | ||
} | ||
``` | ||
|
||
If the API Key is not provided, you would get: | ||
|
||
```json | ||
{ | ||
"success": false, | ||
"message": "Please provide a valid deepgram API key" | ||
} | ||
``` | ||
|
||
## 📝 Variables | ||
|
||
List of variables used by this cloud function: | ||
|
||
- **DEEPGRAM_API_KEY** - API Key for Deepgram | ||
|
||
## 🚀 Deployment | ||
|
||
1. Clone this repository, and enter this function folder: | ||
|
||
``` | ||
$ git clone https://github.com/open-runtimes/examples.git && cd examples | ||
$ cd dart/deepgram_Topic_Detection | ||
``` | ||
|
||
2. Enter this function folder and build the code: | ||
``` | ||
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=lib/main.dart --rm --interactive --tty --volume $PWD:/usr/code openruntimes/dart:v2-2.16 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 --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/dart:v2-2.16 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 Dart runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/dart-2.16). | ||
|
||
## 📝 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 Dart 2.16. Other versions may work but are not guaranteed to work as they haven't been tested. Versions below Dart 2.14 will not work, because Apwrite SDK requires Dart 2.14, |
Oops, something went wrong.