spacy models in django

Advertisements

I have written the following code and placed it in the settings.py script of my Django backend. My App also has an Angular frontend.

import spacy
from spacy import displacy
from spacy.lang.en import English

SUPPORTED_LANGUAGES = ['de', 'en']

LANGUAGE_MODELS = {}

for language in SUPPORTED_LANGUAGES:
    try:
        LANGUAGE_MODELS[language] = spacy.load(language)
    except OSError:
        print('Warning: model {} not found. Run py -m spacy download {} and try again.'.format(language,language))

LANGUAGE_MODELS['en'] = spacy.load('en_core_web_sm')
LANGUAGE_MODELS['de'] = spacy.load('de_core_web_sm')

LANGUAGE_MAPPING = {
        'en': 'English',    
        'de': 'German',
}

I got the following:

Warning: model de not found. Run py -m spacy download de and try again.
Warning: model en not found. Run py -m spacy download en and try again.

When I run the commands, they seem to finish successfully, but then I get the same and also this:

raise IOError(Errors.E050.format(name=name))
OSError: [E050] Can't find model 'de_core_web_sm'. It doesn't seem to be a Python package or a valid path to a data directory.

Is something wrong with my code, or is it an install issue? What am I missing here?


A bit of a background: I am starting to use SpaCy in my project that has Angular as frontend and Django as backend and MongoDB as database. I need to run sentiment analysis on user feedback, which is just text that the user enters in different fields of a web form (Angular). Later on, I will do the same with text retrieved from the database.

>Solution :

you need to download the model, in the terminal run:

python -m spacy download en_core_web_sm

then in python you can run:

import spacy
english_language_model = spacy.load("en_core_web_sm")

Leave a ReplyCancel reply