Awesome
bert_sa (bert sentiment analysis tensorflow serving with RESTful API)
based on bert including training, online predicting and serving with REST
Fine tune a sentiment analysis model based on BERT
- Add a
SAProcessor
and include it withinmain
function in run_classifier.py - Prepare train, dev and test files; adapat
_create_examples
method inSAProcessor
based on your own datasets (pandas may not be required) - Specify
BERT_BASE_DIR
,SA_DIR
andoutput_dir
in run_sa.sh and run
Test
- For file based test, change
output_predict_file
in run_classifier.py, specifyTRAINED_CLASSIFIER
andoutput_dir
path, run predict_sa.sh - For online prediction, refer to run_classifier_predict_online (modified based on bert_language_understanding)
Export your model
Refer to sa_predict_saved_model.py
KIND NOTICE: some graph definition and input placeholder is imported from run_classifier_predict_online.py
Serve the model with TensorFlow Serving
- See TensorFlow Serving for details about installing docker and pulling a serving image
- Running a serving image
docker run -p 8501:8501 --name 'bert_sa_serving' --mount type=bind,source=/data/notebooks/xff/bert/output/sa_output/saved_model,target=/models/bert_sa -e MODEL_NAME=bert_sa -t tensorflow/serving:latest-devel-gpu &
docker exec -it bert_sa_serving bash
tensorflow_model_server --port=8500 --rest_api_port=8501 \
--model_name=bert_sa --model_base_path=/models/bert_sa
- Sample request
line=u'建立了完善的质量体系并持续有效运行'
# preprocess is defined in run_classifier_predict_online.py
dict_data = preprocess(line)
resp = requests.post('http://172.17.0.1:8501/v1/models/bert_sa:predict', json=dict_data)
print(resp.json())
Results look like this: {'outputs': {'label_predict': 1, 'possibility': [0.00738544, 0.992615]}}