'Attribute Error using NeuralCoref in Colab

I'm trying to use the following spacy module in colab:

https://spacy.io/universe/project/neuralcoref

I install the following packages:

!pip install spacy
import spacy 
!pip show spacy

!git clone https://github.com/huggingface/neuralcoref.git
import neuralcoref

I get the following output after installing:

Name: spacy
Version: 2.2.4
Summary: Industrial-strength Natural Language Processing (NLP) in Python
Home-page: https://spacy.io
Author: Explosion
Author-email: [email protected]
License: MIT
Location: /usr/local/lib/python3.6/dist-packages
Requires: thinc, murmurhash, preshed, blis, srsly, cymem, setuptools, plac, requests, tqdm, numpy, wasabi, catalogue
Required-by: fastai, en-core-web-sm
Cloning into 'neuralcoref'...
remote: Enumerating objects: 48, done.
remote: Counting objects: 100% (48/48), done.
remote: Compressing objects: 100% (44/44), done.
remote: Total 739 (delta 14), reused 10 (delta 1), pack-reused 691
Receiving objects: 100% (739/739), 67.86 MiB | 30.25 MiB/s, done.
Resolving deltas: 100% (368/368), done.

I then follow the instructions on the website:

nlp = spacy.load('en')
neuralcoref.add_to_pipe(nlp)

However, I get the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-fe99e1a1a10f> in <module>()
      1 nlp = spacy.load('en')
----> 2 neuralcoref.add_to_pipe(nlp)
      3 #coref = neuralcoref.NeuralCoref(nlp.vocab)
      4 #nlp.add_pipe(coref, name='neuralcoref')

AttributeError: module 'neuralcoref' has no attribute 'add_to_pipe'

Does anybody know how to fix this?

EDIT

After (successfully) using the suggestion below, colab crashed on me when I tried to run the example provided (see details below).

Here is the code used:

from google.colab import drive
drive.mount('/content/gdrive')

!pip install neuralcoref

import spacy
import neuralcoref

nlp = spacy.load('en') # this is the line where it crashes
neuralcoref.add_to_pipe(nlp)

doc1 = nlp('My sister has a dog. She loves him.')
print(doc1._.coref_clusters)

I've attached a screenshot with the original error message at the bottom left.

enter image description here

EDIT 2

I got the code to work on colab when changing the order of installing the modules (not sure why).

The following has worked for me now:

from google.colab import drive
drive.mount('/content/gdrive')

!git clone https://github.com/huggingface/neuralcoref.git
!pip install -U spacy
!python -m spacy download en

import spacy
nlp = spacy.load('en')

%cd neuralcoref

!pip install -r requirements.txt
!pip install -e .

import neuralcoref
neuralcoref.add_to_pipe(nlp)

doc1 = nlp('My sister has a dog. She loves him.')
print(doc1._.coref_clusters)


Solution 1:[1]

Update:

Since the previous helped solving the first problem but created another problem, I have updated the answer.

According to neuralcoref page, for our version of Spacy, we need to manually install it from the source.

Also, try each of the following blocks in new cell in Colab, and Restart Runtime after installation.

mkdir temp

cd temp

!git clone https://github.com/huggingface/neuralcoref.git
!pip install -U spacy
!python -m spacy download en

cd neuralcoref

!pip install -r requirements.txt
!pip install -e .


import neuralcoref
import spacy

nlp = spacy.load('en')
neuralcoref.add_to_pipe(nlp)

doc1 = nlp('My sister has a dog. She loves him.')
print(doc1._.coref_clusters)


Solved

Solution 2:[2]

I had the similar problem. After a lot of debugging here is what I did to get it to work on google colab:

  1. Install the right version of Spacy. My google colab had 2.2.4, but i reinstalled it like this:
pip install -U spacy==2.1.0 

python -m spacy download en
  1. Google colab has the right version of Cython, but double check by running: !pip show cython. If not, install it with:
pip install Cython --install-option="--no-cython-compile"

The version needs to be >=0.25

  1. Install neuralcoref like this. Make sure you are using GPU in google colab:
pip uninstall -y neuralcoref 

pip install neuralcoref --no-binary neuralcoref

That should fix it. Most likely the Spacy installation would do it for you, if not, follow all the steps.

Solution 3:[3]

Neuralcoref just works with spacy Version

spacy>=2.1.0,<2.2.0 cython>=0.25 pytest

see requirements.txt

Solution 4:[4]

This works for me. Tested on 20 March 2022. Also, make sure that you factory reset the runtime so that it removes all previous installations of spacy and other dependencies.

from google.colab import drive
drive.mount('/content/drive')

!git clone https://github.com/huggingface/neuralcoref.git

%cd neuralcoref

!pip install -r requirements.txt
!pip install -e .

import spacy
import neuralcoref

nlp = spacy.load('en')
neuralcoref.add_to_pipe(nlp)
doc1 = nlp('My sister has a dog. She loves him.')
print(doc1._.coref_clusters)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 DaveL17
Solution 3 Sofia
Solution 4 abhigyanghosh30