Attribute Error using NeuralCoref in Colab
Asked Answered
N

4

6

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)
Nabonidus answered 17/4, 2020 at 10:56 Comment(0)
I
5

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

Impropriate answered 17/4, 2020 at 11:43 Comment(7)
quick follow up question - are you able to run the example on the website? I just tried and it crashes my session with an unknown error?Nabonidus
Yes, I took that screenshot from Colab. Wait I'll tryImpropriate
okay so for me it crashs as soon as I add the following lines: doc1 = nlp('My sister has a dog. She loves him.') print(doc1._.coref_clusters) is that the same for you?Nabonidus
ahh okay thanks - I just posted a new question, but will change it around now, thank you :-)Nabonidus
I had to add a % in front of the cd command in my colab for the installation to work, but then got a new error message where it can't find a model 'en'... I think I will have to make a new question for this! Thank you though!Nabonidus
That update solved that issue. Try that line: !pip install -U spacy !python -m spacy download enImpropriate
yeah I tried the exact code, but that gives me an index error on the cd command? adding the % lets the code run but I get the error message on not being able to load 'en'...Nabonidus
W
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.

Woodham answered 8/4, 2021 at 15:3 Comment(0)
A
1

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)
Afghanistan answered 20/3, 2022 at 11:6 Comment(0)
P
0

Neuralcoref just works with spacy Version

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

see requirements.txt

Pack answered 7/7, 2020 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.