BERT as a service
Jayant Kumar
Principal ML Scientist at Adobe | Technical Advisor at Preffect | Multimodal AI | Large language models and Knowledge Graph applications
There are multiple ways of leveraging the open source BERT model for your NLP work, for example, via huggingface transformers or spacy transformers. I recently came across a post about running BERT as a service, and it was quite easy to setup too.
If the pipeline requires efficient extraction of BERT features this easy setup may save your dev/test time.
Create a environment if needed:
conda create --name bert
Install tensorflow (works with cpu or gpu)
pip3 install tensorflow-cpu==1.15 # version must be before 2.0 OR pip3 install tensorflow-gpu==1.15 # version must be before 2.0
Install bert serving server and client
pip3 install -U bert-serving-server bert-serving-client
Start the model serving
bert-serving-start -model_dir /path_to_the_model/ -num_worker=2
For example, I used:
bert-serving-start -model_dir ../BERT_models/wwm_uncased_L-24_H-1024_A-16/ -num_worker=4
In Python 3, start using by setting up a client
from bert_serving.client import BertClient client = BertClient() vector = client.encode(['lovely portrait'])
References:
- https://towardsdatascience.com/word-embedding-using-bert-in-python-dd5a86c00342
- https://github.com/hanxiao/bert-as-service