This repository creates a docker image that uses R and the keras and plumber R packages to create a neural network powered RESTful API. The package keras provides the ability to create neural networks, while plumber allows it to run R as a web service.
This is an introductory repository to help R users learn about keras, plumber, and Docker. A more featureful version of this code can be found at the T-Mobile open-source r-tensorflow-api project.
The Docker container is currently being hosted on the Nolis, LLC website, and can be queried by visiting http://pets.nolisllc.com/names.
This code was used in talks by Jacqueline Nolis and Heather Nolis at the New York R Conference in May of 2019.
This repository has four different parts to it:
- Training a neural network on pet names - Running the R script
train_model.R
will:- load up the pet names csv,
- format them into X and Y matrices that a neural network can be trained on,
- defines the keras neural network model,
- trains the neural network on the data,
- saves the trained model to
model.h5
.
- Defines a function to generate new pet names - Running the R script
runtime_functions.R
loads the neural network model and parameters, then creates functions to generate the pet names. There are two functions in the code:generate_name
- Uses the model to generate a single pet namegenerate_many_names
- calls the previous function a specified number of times to get a vector of names.
- Create a RESTful API around the pet name generator function - The R scripts
rest_controller.R
andmain.R
together allow you to run the generate as an API, so that you can get pet names via HTTP requests. Runningmain.R
starts the web service. - Building a Docker image for the API - The file
Dockerfile
specifies how the Docker image should be built. To build and run the Docker container, download and install Docker, then in a terminal rundocker build -t pet-names .
anddocker run --rm -p 80:80 pet-names
- T-Mobile, since much of this code comes from a T-Mobile open-source project
- 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.
- The City of Seattle for making the pet license data available for public use.
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