0

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 Answer 1

1
  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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.