-1

I developed a simple model that makes use of the SpaCy model en_core_web_lg. The project structure is as follows:

/model/
    main.py
    app_functions.py
    SpaCy/
          SpaCy_clases.py

At SpaCy_clases.py I have something like this:

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

class Spacy1():
   ...
   ...

class Spacy2():
   ...
   ...

The issue I'm facing is that line:

nlp = spacy.load('en_core_web_lg')  

Since I need to import the model in the same file I define the classes, the same model is used all the time, I want to be able to change that.

I need to have the option to change:

nlp = spacy.load('en_core_web_lg') 

By:

nlp = spacy.load('en_core_web_sm') 

But if I do that with some variable at main.py that does not make any difference at all. How can I load a different model without having to stop the program and edit SpaCy_clases.py

I want to change the model in the complete scope of the project, since it is been used in several steps. But all the classes that make use of Spacy are defined at SpaCy_classes.py all the other files import this the classes from there.

Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181

1 Answers1

1

If you only use nlp in SpaCy_clases, then there is one way.

from SpaCy import SpaCy_clases

# you want to change the nlp attribute of SpaCy_clases 
# if really really want to reload, which is bad practice 
SpaCy_clases.nlp = spacy.load('what_you_want')

really not recommend, but can work. The above way to change nlp in fact is using a global writable variable in many many files, which is very very bad thing.

LiuXiMin
  • 1,225
  • 8
  • 17