Juned Ansari
Juned Ansari

Reputation: 5275

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'

Upvotes: 0

Views: 3350

Answers (2)

arranjdavis
arranjdavis

Reputation: 735

I had the same problem (trying to load an old BERT model created in 2020), and I solved it by downgrading both spaCy and spacymoji. I was working on a Google Colab, which is relevant below.

First, I installed spacymoji version 2.0.0. Second, I removed the newer spaCy version that comes pre-installed on Google Colab. Third, I installed the older spaCy version 2.2.2:

pip3 install spacymoji==2.0.0
pip3 uninstall spacy -y
pip3 install spacy==2.2.2

I am sure the accepted solution also works in some contexts, but I will leave this here in case someone else is in my situation!

Upvotes: 1

Ashutosh
Ashutosh

Reputation: 159

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.

Upvotes: 4

Related Questions