Search code examples
nlpspacybert-language-model

AttributeError: type object 'Language' has no attribute 'factory'


I am getting error while using "spacy_sentence_bert"

Code:

import spacy_sentence_bert
# load one of the models listed at https://github.com/MartinoMensio/spacy-sentence-bert/
nlp = spacy_sentence_bert.load_model('en_roberta_large_nli_stsb_mean_tokens')
# get two documents
doc_1 = nlp('Hi there, how are you?')
doc_2 = nlp('Hello there, how are you doing today?')
# use the similarity method that is based on the vectors, on Doc, Span or Token
print(doc_1.similarity(doc_2[0:7]))

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-42-d86d8866c1ca> in <module>()
      1 ##### using BERT
----> 2 import spacy_sentence_bert
      3 # load one of the models listed at https://github.com/MartinoMensio/spacy-sentence-bert/
      4 nlp = spacy_sentence_bert.load_model('en_roberta_large_nli_stsb_mean_tokens')
      5 # get two documents

1 frames
/usr/local/lib/python3.7/dist-packages/spacy_sentence_bert/language.py in <module>()
     24 
     25 # the pipeline stage factory
---> 26 @Language.factory('sentence_bert', default_config={
     27     'model_name': None,
     28     'debug': True

AttributeError: type object 'Language' has no attribute 'factory'

Solution

  • The issue seems to be with your environment.

    AttributeError: type object 'Language' has no attribute 'factory' message

    is a spacy 2.x error.

    Please try to run it with spacy 3.x. If you have already installed 3.x, then please verify if your virtual environment is pointing to correct python.