Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

357
Visualizações
Django: Machine learning model in server side?

I have a Word2Vec model(One of Machine learning model) and could get this pre-trained model by filename :

model = Word2Vec.load(fname)

So, I can get some prediction by using this model:

prediction = model.predict(X)

What I'm trying to do is to get request(including query word) from user and query this data to my pre-trained model and get prediction so that server could send response with this prediction data. This process should occur every time user send queries, so this pre-trained model should always be in memory.

To implement this, I think I have to use Redis, Celery kinda thing, but as I know of, Celery is working asynchronously with Django web application, so it would not be suitable for what I want to do...

How can I implement this function in my Django application?

Thanks.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You don't actually need Redis or celery for this.

Before I post the solution using Django, I should mention that if you only need a web interface for your ML project, that is, you don't need Django's fancy ORM, admin, etc, you should go with Flask. It's perfect for your use case.


Solution using Flask:

It's very easy to store your trained model in memory using Flask:

# ...
# your Flask application code
# ...
# ...

if __name__ == '__main__':
    model = Word2Vec.load(fname)
    app.run()

If you're interested, the complete example is here.


Solution using Django:

You can utilise Django's cache framework to store your model. First, activate the local memory cache backend. Instructions are here.

Now, you'll need to store your model in the cache.

from django.core.cache import cache

model_cache_key = 'model_cache' 
# this key is used to `set` and `get` 
# your trained model from the cache

model = cache.get(model_cache_key) # get model from cache

if model is None:
    # your model isn't in the cache
    # so `set` it
    model = Word2Vec.load(fname) # load model
    cache.set(model_cache_key, model, None) # save in the cache
    # in above line, None is the timeout parameter. It means cache forever

# now predict
prediction = model.predict(...)

You can keep the above code in your views, but I'd rather you create a separate file for that and then import this file in you views.

You can find the full example is on this blog.

about 4 years ago · Santiago Trujillo Relatório

0

Seconded that you don't need a Redis and celery for this. The most simple solution is outlined in this question by Daniel Roseman.

This approach is also practical for if you have an object which cannot be serialized/ pickled (as is necessary to take advantage of django's cache).

All you need to do is instantiate the model object before your views in views.py, that way model is called one time (when views.py is imported) and the class/function based views will be able to access the model.

This would look like this in your question:

# views.py
model = Word2Vec.load(fname)

class WordView(View):
  def post(self, request, *args, **kwargs):

    # make a prediction based on request
    prediction = model.predict(self.request.something)

    return prediction
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda