Search code examples
pythondjangodjango-rest-frameworkdeep-learningmodel

Keep Deep Learning Model Loaded in Memory in Django Project


I have a deep learning model and I have added it in my django web REST API. I want model to be always loaded in memory. Curretly when user request , model loads in memory and then released after some time. I want it to constantly loaded in ram so that the response time of my api got better. I have deployed API on Windows Server 2019.


Solution

  • You can load it from your apps.py file.

    In your django app (Lets' call your app myapp), you may have a file called apps.py in the same level as your models.py, views.py, etc

    You override the ready() method in there.

    Example in myapp/apps.py

    from django.apps import AppConfig
    
    loaded_model = None
    
    class MyAppConfig(AppConfig):
        default_auto_field = 'django.db.models.BigAutoField'
        name = 'myapp'
    
        def ready(self):
            global loaded_model
            loaded_model = <load your model here>  
    

    Update in your app in INSTALLED_APPS settings in settings.py like this 'myapp.apps.MyAppConfig'

    You can then use this in your views.py like this

    from .apps import loaded_model
    
    def my_view(request):
        result = loaded_model.<your_model_method>