Search code examples
pythondjangoexceptiondoc2vec

How to load a trained model in django


I'm working on a django project where I have to use Doc2Vec model to predict most similar articles based on the user input. I have trained a model with the help of articles in our database and when I test that model using a python file .py by right clicking in the file and selecting run from the context menu its working. The problem is Now I'm moving that working code to a django function to load model and predict article based on user-given abstract text But I'm getting FileNotFoundError.
I have searched how to load model in django and it seems the way is already OK. here is the complete exception:

FileNotFoundError at /searchresult
[Errno 2] No such file or directory: 'd2vmodel.model'
Request Method: GET
Request URL: http://127.0.0.1:8000/searchresult
Django Version: 3.1.5
Exception Type: FileNotFoundError
Exception Value:
[Errno 2] No such file or directory: 'd2vmodel.model'
Exception Location: C:\Users\INZIMAM_TARIQ\AppData\Roaming\Python\Python37\site-packages\smart_open\smart_open_lib.py, line 346, in _shortcut_open
Python Executable: C:\Program Files\Python37\python.exe
Python Version: 3.7.9
Python Path:
['D:\Web Work\doc2vec final submission',
'C:\Program Files\Python37\python37.zip',
'C:\Program Files\Python37\DLLs',
'C:\Program Files\Python37\lib',
'C:\Program Files\Python37',
'C:\Users\INZIMAM_TARIQ\AppData\Roaming\Python\Python37\site-packages',
'C:\Program Files\Python37\lib\site-packages']
Server time: Mon, 24 May 2021 12:44:47 +0000
D:\Web Work\doc2vec final submission\App\views.py, line 171, in searchresult
model = Doc2Vec.load("d2vmodel.model")

Here is my django function where I'm loading Doc2Vec model.

def searchresult(request):
    articles = []

    all_articles = Article.objects.all()

    model = Doc2Vec.load("d2vmodel.model")

    def output_sentences(most_similr):
        print('\n')
        for label, index in [('MOST Similar', 0), ('Second-Most Similar', 1), ('Third Most Similar', 3),
                             ('Fourth Most Similar', 4)]:
            print(u'%s %s: %s\n' % (label, most_similar[index][1], all_articles[int(most_similar[index][0])]))
            articles.append(all_articles[int(most_similar[index][0])])
        print('=====================\n')

    # to find the vector of a document which is not in training data
    seed_text = 'Test from frontend dashboard'
    tokens = seed_text.split()
    vector = model.infer_vector(tokens)
    most_similar = model.dv.most_similar([vector])

    output_sentences(most_similar)

    article = Article.objects.filter(abstract=articles)
    art_dict = {
        'articles': article
    }

    return render(request, 'searchresult.html', art_dict)

I have d2vmodel.model file and views.py in the same directory/folder. enter image description here

Please let me know what I have to do to load that doc2vec model here in django.
I'm using Python 3, PyCharm IDE and installed the libs with pip commands

Thanks & Regards


Solution

  • Move the models from App to root directory of your project, I think it is 'doc2vec final submission'

    Or create a folder inside 'doc2vec final submission' named 'models'

    Change this

    model = Doc2Vec.load("d2vmodel.model")
    

    To

    model = Doc2Vec.load("models/d2vmodel.model")