• Stars
    star
    261
  • Rank 156,630 (Top 4 %)
  • Language
    R
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A small Docker container for using R and TensorFlow as an enterprise API

r-tensorflow-api

License

(version 1.1.1)

This repository creates a production-ready docker image that uses R and the keras and plumber R packages to create a neural network powered REST API. The package keras provides the ability to create neural networks, while plumber allows it to run R as a web service. The docker container is designed to be:

  • R first - designed so that R users can comfortably create their own neural network powered services without having to learn other languages.
  • Production ready - as small of an image size as possible while still being maintainable and understandable. At our last measure, it was 1.86gb, with around 800mb from Python and the keras backend and 900mb from R and R libraries.
  • TensorFlow compatible - the container works with models created using the R keras package without any additional Python configuration. Note: this uses the cpu version of Tensorflow since it is designed for running models (not training them).
  • HTTPS enabled - unfortunately, the R plumber library does not support SSL encrypted traffic. Since encryption is likely required for enterprise use, we have an optional dockerfile which uses an Apache 2 server. The server redirects HTTPS traffic as HTTP to plumber, and then take the plumber response and re-encrypts it back to HTTPS.

Check out our blog post on the T-Mobile open source website.

How to use this repository

To use this repository, first download and install docker. Once docker is installed and the repository is downloaded, go to the repository folder and open a terminal, then run the following commands:

docker build -t r-tensorflow-api .
docker run --rm -it -p 80:80 r-tensorflow-api

The first command builds the docker image (which may take up to 30 minutes to compile the R libraries), and the second runs the container.

If the image successfully built, you should be able to go to http://127.0.0.1/names in your browser and see a random 20 pet names generated from a neural network. The names are based on pet licenses from the City of Seattle.

Creating your own API

To make your own API, you only need to modify the R code in the src folder. The code in there is split into several files:

  1. main.R [required] - this is what gets run by the API, and only contains code to start plumber. You should not need to modify this.
  2. rest_controller.R [required] - this file specifies the endpoints for your web service. You should modify this heavily as you create your own endpoints.
  3. setup.R [required] - this script is run when building the image to install the R libraries. If any libraries fail to install, the build aborts. You should modify this to include the libraries you need. If you want to install packages from GitHub, you can install devtools and then call it from within this script.
  4. runtime_functions.R - this file contains any additional functions that are needed when you run the service. It's a good place to store functions to keep the rest_controller.R file clean.
  5. parameters.R - this file contains parameters needed for the model. It's a good place to put values that are need for both building the model and during runtime.
  6. train_model.R - this script trains the model and saves it. It is not run as part of the container, it's only here as a reference. The repository already includes the output of the script (the model as an hdf5 object), but if you wanted to rebuild them you only need to run this file.

dockerfile details

The dockerfile does the following steps:

  1. Load from the rocker/r-ver docker image - This image was chosen as a balance of ease of maintenance (compared to a pure debian or alpine base) and size of the image (compared to rocker/tidyverse which also includes RStudio). Unfortunately this means many R packages have to be manually installed. If you are finding the images builds too slowly for your tastes, switch to using rocker/tidyverse as the base image.
  2. Install the necessary linux libraries. These libraries should be sufficient for most tidyverse libraries, however if you need more check out the libraries loaded by the rocker/tidyverse image.
  3. Install miniconda and the appropriate Python libraries for keras. This image uses a Miniconda version based on Python 3.6. Miniconda Python was chosen instead of Anaconda Python to decrease the size of the image by 3gb. An environmental variable is also set so R knows to use the correct Python version.
  4. Install the necessary R packages. Any package that you would want to install to run your service (that you'd normally use install.packages() for) you need to list in the setup.R file. Instead of using an R script to install the libraries, you could do it directly from within the dockerfile using install2.r. We chose to split this out so that data scientists have fewer reasons to touch the dockerfile.
  5. Copy the R code over.
  6. At runtime, use Rscript to start the main.R script, which has the plumber web server code.

HTTPS details

To run the HTTPS version, use these commands:

docker build -f Dockerfile.https -t r-tensorflow-api-https .
docker run --rm -it -p 443:443 r-tensorflow-api-https

If you try to test it by going to https://127.0.0.1/names you'll get a warning that the certificates are invalid. You'll want to replace the https/server.cert and https/server.key file with your valid certificates before deploying.

The dockerfile has several differences from the HTTP one:

  1. It sets up an Apache 2 server to reroute HTTPS traffic (port 443) to HTTP traffic (port 80) which gets received by plumber. This includes transferring a config file (000-default.conf) and setting Apache 2 to only listen to port 443 and not port 80.
  2. It transfers the certificate and key file to use for the HTTPS encryption.
  3. It has a run-r-and-redirect.sh script which is executed when the image is run. This script first starts the apache2 server then runs the main.R R script. Thus, the docker container has two programs running simultaneously: R and Apache 2.

Thanks to

  • The Rocker project for maintaining the R docker images these build from.
  • The plumber maintainers for creating a way to use R as a web service.
  • The RStudio developers for creating the keras interface to python keras.
  • Rbloggers author gluc, for writing the blog post that informed our solution for https redirecting.
  • The City of Seattle for making the pet license data available for public use.

Terms and conditions

From the City of Seattle on the pet license data:

The data made available here has been modified for use from its original source, which is the City of Seattle. Neither the City of Seattle nor the Office of the Chief Technology Officer (OCTO) makes any claims as to the completeness, timeliness, accuracy or content of any data contained in this application; makes any representation of any kind, including, but not limited to, warranty of the accuracy or fitness for a particular use; nor are any such warranties to be implied or inferred with respect to the information or data furnished herein. The data is subject to change as modifications and updates are complete. It is understood that the information contained in the web feed is being used at one's own risk.

From RStudio for using some of the R Keras example code as a framework for our model:

the keras library is copyright 2017: RStudio, Inc; Google, Inc; Franรงois Chollet; Yuan Tang

More Repositories

1

pacbot

PacBot (Policy as Code Bot)
Java
1,288
star
2

t-vault

Simplified secrets management solution
Java
364
star
3

jazz

Platform to develop and manage serverless applications at an enterprise scale!
JavaScript
298
star
4

kardio

Service Health Dashboard for Kubernetes, Containers and more...
Java
222
star
5

magtape

MagTape Policy-as-Code for Kubernetes
Python
145
star
6

loadtest

an R package that automates performance testing of ML models and summarizes the results in a dashboard w/ rad visualizations
R
93
star
7

magentaA11y

Magenta A11y is a tool built to simplify the process of accessibility testing.
HTML
60
star
8

POET-pipeline-library

POET pipeline framework automation code.
Groovy
46
star
9

hyperdirectory

Blockchain-based, highly auditable access management solution (directory service)
TypeScript
34
star
10

monarch

App-level Chaos Engineering
Python
28
star
11

tmus-geofeed

26
star
12

jazz-installer

Installer for Jazz Serverless Developer Platform!
Python
25
star
13

casquatch

Casquatch: an open source Java abstraction layer for Cassandra databases
Java
21
star
14

faas-java-templates

Java templates for OpenFaas, a serverless functions as a service platform built on Docker
Java
19
star
15

themes-platform-vendor-tmobile-apps-ThemeChooser

Java
18
star
16

DevEdge-IoTDevKit-ZephyrSDK

ZephyrSDK (TMO_shell) is a Zephyr application built by T-Mobile and comes shipped on the DevEdge - IoT Developer Kit
C
18
star
17

codeless

ETP codeless project allows you to quickly write tests using basic yaml and spreadsheet files without having to have in-depth understanding of writing Java automation tests.
Java
18
star
18

themes-platform-vendor-tmobile-themes-Androidian

13
star
19

DevEdge-IoTDevKit-ZephyrRTOS

T-Mobile Zephyr OS is a fork of zephyrproject-rtos/zephyr that is shipped on the DevKit and used for contributing upstream.
C
13
star
20

developer-puzzle

T-Mobile Developer Candidate Puzzles. Do not Fork, Do not submit a PR. Simply clone to your local and create a new repo in your public account. Solve the puzzle and send us the address of your repo. DO NOT FORK THIS REPO OR SUBMIT PULL REQUEST (your submission will be deleted!)
TypeScript
13
star
21

jest-jsdom-browser-compatibility

This is a matrix of issues and risks of using Jest with JSDOM to test browser applications. This will include several sub projects with example tests to demonstrate the failures.
TypeScript
12
star
22

passport-tmobileid

T-Mobile ID enabled authentication strategy for Passport and Node.js
JavaScript
12
star
23

percy-cake

Percival: A Configuration As Kode Editor to simplify managing distributed applications and services.
TypeScript
12
star
24

docinator

Build a website from your code's documentation with zero configuration.
JavaScript
11
star
25

pi-alarm

Raspberry Pi Alarm
Python
11
star
26

chaostoolkit-turbulence

Tools and resources to support Chaos Engineering
Python
11
star
27

themes-platform-vendor-tmobile-libs-com.tmobile.themes

Java
11
star
28

springboot-restapi-generator

Custom yoman generator for scaffolding REST API projects based on springboot
JavaScript
10
star
29

keybiner

Entitlements compression and validation library
Java
9
star
30

themes-platform-manifest

8
star
31

themes-platform-vendor-tmobile-providers-ThemeManager

Java
8
star
32

developer-kata

Some coding exercises for developers. There is not sample code here, only descriptions of problems to solve.
8
star
33

stf_ios_mirrorfeed

Stream IOS mirroring via USB to websocket
Go
7
star
34

themes-platform-frameworks-base

Java
7
star
35

opensource

t-mobile's open source microsite
CSS
7
star
36

cf-smoke-tests

Python
7
star
37

qapi

Query API
Java
5
star
38

orchestration-desk

Node module to connect to orchestration services like marathon to get details about applications, containers and more.
TypeScript
5
star
39

ducklett

Ducklett: managing all the little nodes of a Conducktor cluster
Go
5
star
40

themes-platform-vendor-tmobile-products-themes

Shell
5
star
41

parallelizer

A go library for building work pools.
Go
4
star
42

t-rover

Java
4
star
43

node-red-contrib-sms-send

JavaScript
4
star
44

DevEdge-IoTDevKit-Binaries

Zephyr DevKit Supporting Binaries
Makefile
4
star
45

themes-platform-packages-apps-Settings

Java
3
star
46

themes-platform-packages-apps-Contacts

Java
3
star
47

themes-platform-vendor-tmobile-libs-com.tmobile.themehelper

Java
3
star
48

DevEdge-IoTDevKit-SiLabs-WiseConnect

WiSeConnect Wi-Fi and Bluetooth Software used as a supporting module to DevEdge-IoTDevKit-ZephyrRTOS
C
3
star
49

node-red-contrib-yolo-object-detection

Node-RED contrib module that uses YOLO3 object detection to identify items in images.
Python
3
star
50

themes-platform-frameworks-policies-base

Java
3
star
51

node-red-contrib-object-to-array

Given a JavaScript object, when this node is executed it will transform the object key/value properties to an array of object properties with specified property names..
TypeScript
3
star
52

DevEdge-IoTDevKit-Sony-cxd5605

Zephyr module supporting Sony CXD5605 GNSS
C
3
star
53

node-red-contrib-rpi-adeept-motor

HTML
2
star
54

node-red-contrib-summarizer

Node-RED node to summarize arrays of data
HTML
2
star
55

themes-platform-packages-apps-Launcher2

Java
2
star
56

themes-platform-packages-apps-Email

Java
2
star
57

depaginator

Go
2
star
58

common-platform-vendor-tmobile-build-common

Shell
2
star
59

node-red-contrib-differences

A Node-RED node to detect differences between two sets of data
TypeScript
2
star
60

jazz-content

Static content for Jazz - https://github.com/tmobile/jazz
Dockerfile
2
star
61

node-red-contrib-array-iterator

Given an array input, when this node is executed it will pass the โ€œnextโ€ value of the array to the succeeding connected node(s).
TypeScript
2
star
62

qlkube-client

JS library to make interacting with qlkube easier
JavaScript
2
star
63

themes-platform-packages-apps-Music

Java
2
star
64

themes-platform-packages-apps-Mms

Java
2
star
65

emerald-platform-vendor-tmobile-themes-Androidian

Java
1
star
66

t-racer-legacy

consolidate t-racer projects into one repo
C++
1
star
67

gatsby-starter-gitlab

A Gatsby starter to generate a website from a list of Gitlab groups and/or projects.
TypeScript
1
star
68

node-red-contrib-tm-mjpg-server

Node red module to start a python mjpg server
Python
1
star
69

gatsby-source-gitlab

A Gatsby starter to generate a website from a list of Gitlab groups and/or projects.
TypeScript
1
star