28

I have a django form, which is collecting user response. I also have a tensorflow sentences classification model. What is the best/standard way to put these two together. Details:

  1. tensorflow model was trained on the Movie Review data from Rotten Tomatoes.
  2. Everytime a new row is made in my response model , i want the tensorflow code to classify it( + or - ).
  3. Basically I have a django project directory and two .py files for classification. Before going ahead myself , i wanted to know what is the standard way to implement machine learning algorithms to a web app.

It'd be awesome if you could suggest a tutorial or a repo. Thank you !

bakkal
  • 54,350
  • 12
  • 131
  • 107
Subrat
  • 980
  • 2
  • 11
  • 17
  • why is it different from anything else happening on the server end? you get input, you return output. and why is it tagged as a node.js question? – user2717954 May 22 '16 at 12:56
  • At the moment I do feature extraction and append it to a numpy array , this takes a lot of memory and i intend to keep retraining my CNN with the new responses. Also , I think it will be easier to traverse the responses stored in mongoDB, idk. – Subrat May 22 '16 at 13:34
  • How did you init your TF session? Each time a request came in? Or did you init it somewhere and reuse it? – shellbye Aug 10 '17 at 11:52
  • rn its initialized once and reused, running on a different machine with an API on top of it. – Subrat Aug 10 '17 at 12:56
  • https://stackoverflow.com/questions/47295025/valueerror-at-image-tensor-tensoractivation-5-softmax0-shape-4-dtyp any suggestions? – Dexter Nov 15 '17 at 07:40
  • There is a tutorial how to deploy machine learning models with Django: https://deploymachinelearning.com with code available at github: https://github.com/pplonski/my_ml_service – pplonski Nov 13 '19 at 10:50

3 Answers3

32

Asynchronous processing

If you don't need the classification result from the ML code to pass immediately to the user (e.g. as a response to the same POST request that submtted), then you can always queue the classification job to be ran in the background or even a different server with more CPU/memory resources (e.g. with django-background-tasks or Celery)

A queued task would be for example to populate the field UserResponse.class_name (positive, negative) on the database rows that have that field blank (not yet classified)

Real time notification

If the ML code is slow and want to return that result to the user as soon as it is available, you can use the asynchronous approach described above, and pair with the real time notification (e.g. socket.io to the browser (this can be triggered from the queued task)

This becomes necessary if ML execution time is so long that it might time-out the HTTP request in the synchronous approach described below.

Synchronous processing, if ML code is not CPU intensive (fast enough)

If you need that classification result returned immediately, and the ML classification is fast enough *, you can do so within the HTTP request-response cycle (the POST request returns after the ML code is done, synchronously)

*Fast enough here means it wouldn't time-out the HTTP request/response, and the user wouldn't lose patience.

Community
  • 1
  • 1
bakkal
  • 54,350
  • 12
  • 131
  • 107
10

Well, I had to develop the same solution myself. In my case, I used Theano. If you are using tensorflow or theano, you are able to save the model you have built. So first, train the model with your training dataset, then save the model using the library you have chosen. You need to deploy into your django web application only the part of your code that handles the prediction. So using a simple POST, you would give to the user the predicted class of your sentence quickly enough. Also, if you think is needed, you can run a job periodically to train your model again with the new input patterns and save it once more.

  • could it be possible to automate the process of retraining the model once every month or week using the data collected? – Dev_Man Oct 17 '17 at 05:21
  • https://stackoverflow.com/questions/47295025/valueerror-at-image-tensor-tensoractivation-5-softmax0-shape-4-dtyp any suggestions – Dexter Nov 15 '17 at 07:40
  • @Dev_Man You can use Celery to run a task like this periodically – Raymond Chen Dec 10 '18 at 21:23
2

I would suggest not to use Django since it will add execution time to the solution.

Instead, you could use node to serve a Reactjs frontend that interacts with the TensorFlow rest API that functions as a standalone server.

As the answer above this post suggests, it will be better to use WebSockets, you could use a react WebSocket module so it will refresh your components once the state of the component changes.

Hope this helps.