For serving a text classifier in TensorFlow 2.X, check out my tensorflow-serving-2.0 example.
Restructured and simplified my solution to Kaggle's Toxic Comment Classification Challenge for deployment.
See Accompanying Medium Article for background and more detail.
Install all requirements packages using pip install -r requirements.txt
This repo uses the Kaggle API to fetch the data sets. The Kaggle API requires a bit more work setting up:
- Create a Kaggle account and accept the competition rules
- Generate an API token from
kaggle.com/USERNAME/account
(it will prompt you to download akaggle.json
file which contains the credentials) - Set Kaggle credentials as environment variables:
export KAGGLE_USERNAME = [kaggle username]
export KAGGLE_KEY = [generated key]
Run python profanity_detector/main.py
which fetches the data, trains the model and exports a SavedModel
file into the /model
path. The SavedModel
subdirectory can be used to serve the model using TensorFlow Serving.
Instructions paraphrased from the TensorFlow Serving repo.
- Install Docker
- Fetch the latest version of TensorFlow Serving Docker
docker pull tensorflow/serving:latest
- Specify the directory for export. In the root directory of this repo, run
ModelPath="$(pwd)/model"
To start the server:
docker run -t --rm -p 8501:8501 \
-v "$ModelPath/:/models/toxic_comment_classifier" \
-e MODEL_NAME=toxic_comment_classifier \
tensorflow/serving
curl -d '{"signature_name": "predict","inputs":{"input": "raw text goes here"}}' \
-X POST http://localhost:8501/v1/models/toxic_comment_classifier:predict
Please note limitations of this server:
- preprocessing steps applied to training data set is not applied to input (to be addressed later)