Include Python Classifier Model in Java

27 Views Asked by At

I have developed an ML Classifier Model in Python and I want to consume it from java. I have looked up some options like Jython etc. Can anyone help me, how could I achieve this use case?

I tried to use Jython and i did not found any good documentation for this use case

1

There are 1 best solutions below

0
Ajinkya Kamat On
  1. Create a python interpreter inside Java using Jython.
  2. Execute your python code using the interpreter.
  3. Get a handle to your python function
  4. Invoke it
class Classifier:
    def classify(i: int):
        return i + 1

def main():
    classifier = Classifier()
import org.python.core.PyException;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class Main {
    /**
     * @param args the command line arguments
     */
     public static void main(String[] args) throws PyException {
        PythonInterpreter interp = new PythonInterpreter();
        interp.execfile();
        PyObject classifer = interp.get("classifier");
        PyObject x = foo.invoke("classify", new PyInteger);
        System.out.println("x: " + x);
    }
}