This repo is a small setup to quickly refactor your R machine learning script in production ready format via REST API service.
The purpose of this meta project is to create REST API for your machine learning project in R and expose your model
to the end user. The project contains ready-made script to create the below APIs in R:
preprocessingtrainingprediction
The user only needs to plug his original preprocessing, training and prediction logic as function. And then call the respective modules from the respective API functions. Currently to simulate the above three tasks, dummy code snippet is used, which the user will replace with his appropriate code.
The code also implements basic logging facility for debugging purpose.
- package
plumber: for creating api - package
futile.logger: for capturing the log - package
future: for creating asynchronous function call for preprocessing and training
- The
prediction_API.Rholds the actual code for prediciton module for the/predictapi.run_prediction_API.Ris a wrapper script to start theprediction_API.R
- The
training_model_API.pyholds the code for preprocessing and training module for the apis/preprocessand/trainrun_training_API.Ris a wrapper script to start thetraining_model_API.R
- The
dummyAPI.Runder directory/testdirholds the dummy scipt for/notifyapi for implementing callback facility - The
start.shscripts can be used to start all the server at one go - You may need to start the dummy api separately.
Rscript run_prediction_API.R <prediction port number>OR- use the
start.shfile, run it as follows: ./start.sh- Remember to update the
start.shfor port changing
- use the
- following curl command can be used:
curl http://localhost:<prediction_server_port>/predict --data '{"input": [{"UID":"1","AGE":"15"},{"UID":"2","AGE":"25"},{"UID":"3","AGE":"45"},{"UID":"4","AGE":"55"}]}' -H "Content-Type: application/json"/predictis the prediction API end point- You may need to update the
port numberin the above curl command
Rscript run_training_API.R <train port number>OR- use the
start.shfile and run./start.sh - As training and preprocessing can take several minutes, they should be called asynchronously using python package
futurefollowed by a post call to thecallback APIfor sending the status of preprocessing and training.
- curl for preprocessing api
/preprocesscurl http://localhost:<train_server_port>/prepro --data '{"callbackURL":"http://localhost:<notify_server_port>/notify"}' -H "Content-Type: application/json"
- curl for training api
/traincurl http://localhost:<train_server_port>/train --data '{"callbackURL":"http://localhost:<notify_server_port>/notify"}' -H "Content-Type: application/json"
- The above
callbackURLis a dummy api for testing. The dummy api script is in/testdirfolder. Run the dummy callback APIpython dummy_API.py
Rscript notifyAPI.R <notify port number>
- Currently, the asynchronous call by the
futurepackage is not sending the response to client immediately. - ...
- Nested file source may lose the argument value. So use sourcing as follows
source(filename, local=TRUE) - Rememeber sourcing a file
outsidea function andinsidea function are two different scenario.- It changes the scope of the variable to the sourced scripts.
- global variebles are not available to a sourced script if it's sourced inside a function.