For Wrapping fine-tuned transformers in spaCy pipelines

spaCy-wrap is a wrapper library for spaCy for including fine-tuned transformers from Huggingface in your spaCy pipeline allowing inclusion of existing models within existing workflows.


import spacy import spacy_wrap nlp = spacy.blank('en') config = { 'doc_extension_trf_data': 'clf_trf_data', # document extention for the forward pass 'doc_extension_prediction': 'sentiment', # document extention for the prediction 'labels': ['negative', 'neutral', 'positive'], 'model': { 'name': 'cardiffnlp/twitter-roberta-base-sentiment', # the model name or path of huggingface model }, } transformer = nlp.add_pipe('classification_transformer', config=config) transformer.model.initialize() doc = nlp('spaCy is a wonderful tool') print(doc._.clf_trf_data) # TransformerData(wordpieces=... print(doc._.sentiment) # 'positive' print(doc._.sentiment_prob) # {'prob': array([0.004, 0.028, 0.969], dtype=float32), 'labels': ['negative', 'neutral', 'positive']}
Author info

Kenneth Enevoldsen


Categories pipeline models training

Found a mistake or something isn't working?

If you've come across a universe project that isn't working or is incompatible with the reported spaCy version, let us know by opening a discussion thread.

Submit your project

If you have a project that you want the spaCy community to make use of, you can suggest it by submitting a pull request to the spaCy website repository. The Universe database is open-source and collected in a simple JSON file. For more details on the formats and available fields, see the documentation. Looking for inspiration your own spaCy plugin or extension? Check out the project idea label on the issue tracker.

Read the docsJSON source