Wrappers for including pre-trained transformers in spaCy pipelines
Project description
spaCy-wrap: For Wrapping fine-tuned transformers in spaCy pipelines
spaCy-wrap is a minimal library intended for wrapping fine-tuned transformers from the Huggingface model hub in your spaCy pipeline allowing the inclusion of existing models within SpaCy workflows.
As for as possible it follows a similar API as spacy-transformers.
Installation
Installing spacy-wrap is simple using pip:
pip install spacy_wrap
There is no reason to update from GitHub as the version on PyPI should always be the same as on GitHub.
Example
The following shows a simple example of how you can quickly add a fine-tuned transformer model from the Huggingface model hub. In this example we will use the sentiment model by Barbieri et al. (2020) for classifying whether a tweet is positive, negative or neutral. We will add this model to a blank English pipeline:
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)
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']}
These pipelines can also easily be applied to multiple documents using the nlp.pipe
as one would expect from a spaCy component:
docs = nlp.pipe(
[
"I hate wrapping my own models",
"Isn't there a tool for this?",
"spacy-wrap is great for wrapping models",
]
)
for doc in docs:
print(doc._.sentiment)
# 'negative'
# 'neutral'
# 'positive'
More Examples
It is always nice to have more than one example. Here is another one where we add the Hate speech model for Danish to a blank Danish pipeline:
import spacy
import spacy_wrap
nlp = spacy.blank("da")
config = {
"doc_extension_trf_data": "clf_trf_data", # document extention for the forward pass
"doc_extension_prediction": "hate_speech", # document extention for the prediction
"labels": ["Not hate Speech", "Hate speech"],
"model": {
"name": "DaNLP/da-bert-hatespeech-detection", # the model name or path of huggingface model
},
}
transformer = nlp.add_pipe("classification_transformer", config=config)
doc = nlp("Senile gamle idiot") # old senile idiot
doc._.clf_trf_data
# TransformerData(wordpieces=...
doc._.hate_speech
# "Hate speech"
doc._.hate_speech_prob
# {'prob': array([0.013, 0.987], dtype=float32), 'labels': ['Not hate Speech', 'Hate speech']}
📖 Documentation
Documentation | |
---|---|
🔧 Installation | Installation instructions for spacy-wrap. |
📰 News and changelog | New additions, changes and version history. |
🎛 Documentation | The reference for spacy-wrap's API. |
💬 Where to ask questions
Type | |
---|---|
🚨 FAQ | FAQ |
🚨 Bug Reports | GitHub Issue Tracker |
🎁 Feature Requests & Ideas | GitHub Issue Tracker |
👩💻 Usage Questions | GitHub Discussions |
🗯 General Discussion | GitHub Discussions |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file spacy-wrap-1.1.0.tar.gz
.
File metadata
- Download URL: spacy-wrap-1.1.0.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b706d9269fd2bdafce02307007563a7da84810a9ea0a98b0acf16e16d948583 |
|
MD5 | cbb134597ceb9588c3048fd14ccb3532 |
|
BLAKE2b-256 | 1aaba94289e3225b337a2c75e5256598a92e2e1963c5eb7f992e1611f4e2ab87 |
File details
Details for the file spacy_wrap-1.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: spacy_wrap-1.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ddbccc7090c5dd3f7da280b5908ece3c7173f5e80e21fbe0a885b07ee8bee79 |
|
MD5 | 68eaa60cf592830819582ba84ecc9eb0 |
|
BLAKE2b-256 | f26b32a611f0e4cf381a759cc3a7f1899cfccda1d47144a783d4938f698ccdd7 |