0

I have created an NLP model and saved the vectorizer and model in pickle file. I am using these pickle file for predicting the new data. Loading pickle takes around 10 minutes. I want to keep the pickle file loaded in memory and run the prediction when I get the input.

I have a file prediction.py

from sklearn.externals import joblib

count_vectorizer = joblib.load("C:/Count_Vectorizer.pkl")

count_classifier = joblib.load("C:/Count_Classifier.pkl")

X=sys.argv[1]

X_count = count_vectorizer.transform(X)

prediction = count_classifier.predict(X_count )

print(X,prediction)

I am running the python file with input string as an argument.

$ python prediction.py "Hello World"

IN this pickle file is loaded every time I am running the script. Is there anyway to make a program such that the pickle file is already loaded in memory and we run the prediction file and get the result?

Rex Low
  • 2,069
  • 2
  • 18
  • 47
  • Keep the process running and accept data being sent to it? You can run a web server to accept requests, you can take user input via command line, you can make a gui...etc. Just don't kill the process that loads the pickle. – alkasm Jan 31 '19 at 09:52
  • You can also use tkinter to build a small UI for your app and have it running. https://docs.python.org/2/library/tkinter.html – bhathiya-perera Jan 31 '19 at 10:40
  • You could take a look at [flask](http://flask.pocoo.org/) – mickey Jan 31 '19 at 14:40

1 Answers1

0

You would have to change your code structure a little bit.

1. daemon.py

This part is responsible for loading models into memory once and should run all the time getting input from 'front' part

import numpy as np    
from sklearn.externals import joblib

count_vectorizer = joblib.load("C:/Count_Vectorizer.pkl")    
count_classifier = joblib.load("C:/Count_Classifier.pkl")

while True:
    # Load your data from file saved on disk, pass path via input
    # User can pass data, separate script saves it and passes it to daemon
    with open(input("Pass your data here")) as f:
        X_count = count_vectorizer.transform(np.fromfile(f))

        prediction = count_classifier.predict(X_count )
        print(X,prediction)

This is only a sketch as I don't know your exact use case. Basically, there is an infinite loop taking files (or paths to files like here) and outputting predictions.

2. front.py

Using subprocess module you can send path files from the 'front' script to Daemon waiting for paths and returning answers. You have to attach input and output streams of Daemon to pass the file path and get predictions from that process.

subprocess.run or Popen is probably all you need to perform this operation, go through documentation and example use cases (e.g. here, here and so on).

EDIT: @Koalapa answer is another option, as we've said it highly depends on what exactly you wanna do, what is the user load etc.

Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83
  • I need to call a python file with argument all the time I have to do prediction. This python file is called from front end. And I want to use some mechanism that vectorizer and classifier pickle file is not loaded every time. It should be handy so that the result is faster – Ravi Ranjan Prasad Karn Jan 31 '19 at 10:14
  • Start the file once and pass the user files from front-end to daemon, I don't see where is the problem – Szymon Maszke Jan 31 '19 at 10:58
  • Szymon Maszke: Can you please explain a bit more? I have prediction.py as stated in question. Can you please elaborate your answer using some file name. – Ravi Ranjan Prasad Karn Jan 31 '19 at 11:27
  • Edited my answer, though I will not write the whole thing, the idea is put out there. – Szymon Maszke Jan 31 '19 at 12:56