Skip to content

Instantly share code, notes, and snippets.

View jthomas's full-sized avatar
💻
serverless all the things.

James Thomas jthomas

💻
serverless all the things.
View GitHub Profile
@jthomas
jthomas / package.json
Last active September 24, 2023 21:58
Using TensorFlow.js with MobileNet models for image classification on Node.js
{
"name": "tf-js",
"version": "1.0.0",
"main": "script.js",
"license": "MIT",
"dependencies": {
"@tensorflow-models/mobilenet": "^0.2.2",
"@tensorflow/tfjs": "^0.12.3",
"@tensorflow/tfjs-node": "^0.1.9",
"jpeg-js": "^0.3.4"
@jthomas
jthomas / README.md
Last active February 20, 2023 20:52
OpenWhisk Workshop

OpenWhisk Workshop

Hello 👋.

This workshop will teach you how to develop serverless applications, composed of loosely coupled microservice-like functions, using an open-source serverless platform.

Starting with getting the development environment set up, it'll move onto creating, deploying and invoking serverless functions for multiple runtimes. Once you are comfortable creating serverless functions, the next step is to connect functions to events, like message queues, allowing microservices to fire automatically. Finally, we'll demonstrate how to expose serverless functions as public API endpoints, allowing to build serverless web applications.

Welcome to the future of cloud development, you'll never want to manage another server again 😎.

@jthomas
jthomas / n-queens.js
Last active August 10, 2022 12:49
Solving "N Queens Problem" with JavaScript
var iterations = 0
var print_board = function (columns) {
var n = columns.length, row = 0, col = 0
while (row < n) {
while (col < n) {
process.stdout.write(columns[row] === col ? 'Q ' : '# ')
col++
}
@jthomas
jthomas / primes-with-workers.js
Last active April 10, 2022 13:22
Calculating prime numbers on serverless platforms using Node.js Worker Threads and IBM Cloud Functions (Apache OpenWhisk)
'use strict';
const { Worker } = require('worker_threads');
const os = require('os')
const threadCount = os.cpus().length
const compute_primes = async (start, range) => {
return new Promise((resolve, reject) => {
let primes = []
console.log(`adding worker (${start} => ${start + range})`)
@jthomas
jthomas / worker.js
Created July 24, 2019 14:51
Using IBM Cloud Edge Functions (Cloudflare Workers) to add support for Index and Error documents for Cloud Object Storage static hosting.
const INDEX_DOCUMENT = 'index.html'
const ERROR_DOCUMENT = '404.html'
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* @param {Request} request
*/
@jthomas
jthomas / action.json
Last active January 31, 2021 04:16
IBM Cloud Monitoring service (Grafana) dashboards for monitoring IBM Cloud Functions (OpenWhisk) application metrics
{
"annotations": {
"list": []
},
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"hideControls": false,
"id": 2309,
"links": [],
@jthomas
jthomas / index.js
Created August 10, 2018 11:44
Serverless Machine Learning With TensorFlow.js and IBM Cloud Functions (Apache OpenWhisk)
const tf = require('@tensorflow/tfjs')
const mobilenet = require('@tensorflow-models/mobilenet');
require('@tensorflow/tfjs-node')
const jpeg = require('jpeg-js');
const NUMBER_OF_CHANNELS = 3
const MODEL_PATH = 'mobilenet/model.json'
let mn_model
@jthomas
jthomas / README.md
Created July 25, 2019 16:40
Using WebAssembly Modules from IBM Cloud Functions (Apache OpenWhisk)
$ emcc -s WASM=1 -s SIDE_MODULE=1 -s EXPORTED_FUNCTIONS="['_add']" -O1 add.c -o add.wasm
$ zip action.zip index.js add.wasm package.json
updating: index.js (deflated 52%)
updating: add.wasm (deflated 7%)
updating: package.json (deflated 15%)
$ ibmcloud wsk action create wasm action.zip --kind nodejs:10
ok: created action wasm
$ ibmcloud wsk action invoke wasm -r -p a 2 -p b 2
{
@jthomas
jthomas / action.js
Last active May 14, 2019 09:47
Example showing how to handle intermittant action failures for large number of invocations using Redis
"use strict";
// What percentage of invocations should randomly fail?
const ERROR_RATE = 0.25
// Random delay to results being returned (0 -> 10 seconds)
const DELAY_MS = Math.random() * 10000
function should_fail () {
return Math.random() < ERROR_RATE
@jthomas
jthomas / action.js
Last active May 13, 2019 11:29
Example showing how to handle intermittant action failures for large number of invocations
"use strict";
const ERROR_RATE = 0.25
function should_fail () {
return Math.random() < ERROR_RATE
}
function main(params) {
if (!params.a || !params.b) throw new Error('Missing input parameters (a or b).')