Skip to content

Instantly share code, notes, and snippets.

@Galina-Blokh
Galina-Blokh / runPython39Jep.java
Last active July 10, 2022 16:07
How to run python3.9 functions from Java Jep
//import .py doc with to run
subInterp.eval("import python_functions as p");
// run each function from the .py doc I
subInterp.eval("res_spacy = p.run_spacy_nlp('Apple is looking at buying U.K. startup for $1 billion')");
System.out.println(subInterp.getValue("res_spacy"));
//II
subInterp.eval("res_c = p.get_c_path('.idea','*.xml')");
System.out.println(subInterp.getValue("res_c"));
@Galina-Blokh
Galina-Blokh / setJepPathConfigInerpreter.java
Last active July 11, 2022 08:59
Set path for jep and create the SubInterpreter for executing python3.9
// set path for jep executing python3.9
MainInterpreter.setJepLibraryPath(jepPath);
// set path for python docs with python script to run
jep.JepConfig jepConf = new JepConfig();
jepConf.addIncludePaths(System.getProperty("user.dir")+"/src/main/java/");
//create the interpreter for python executing
Interpreter subInterp = jepConf.createSubInterpreter();
@Galina-Blokh
Galina-Blokh / runPythonJep.java
Created July 10, 2022 15:21
Run function from the python_functions.py document python 3.8
// run function from the python_functions.py document
subInterp.eval("import python_functions as p");
subInterp.eval("res_spacy = p.run_spacy_nlp('Apple is looking at buying U.K. startup for $1 billion')");
ArrayList result = (ArrayList) subInterp.getValue("res_spacy");
for (Object item:result) {
System.out.println(item.toString());
}
@Galina-Blokh
Galina-Blokh / python_functions.py
Created July 10, 2022 14:55
Example of simple Python functions for Jep check
import glob
import os
import spacy
def get_c_path(pathlib, extension):
path_list = glob.glob(f'{pathlib}/' + f'{extension}')
return path_list
@Galina-Blokh
Galina-Blokh / setUpJepConfig.java
Created July 10, 2022 14:43
Example of how to set up the Jep configurations and to create a SharedInterpreter for Python execution
jep.JepConfig jepConf = new JepConfig();
jepConf.addIncludePaths("/Users/galinablokh/Documents/POCNerClassificationSDK/src/main/java");
jepConf.addIncludePaths(pythonFolder);
SharedInterpreter.setConfig(jepConf);
SharedInterpreter subInterp = new SharedInterpreter();
@Galina-Blokh
Galina-Blokh / setJepPath.java
Last active July 10, 2022 15:12
Example how to set up a Python Path for Jep library in Java
import jep.*;
import jep.MainInterpreter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
String pythonFolder = System.getenv("DYLD_LIBRARY_PATH");
@Galina-Blokh
Galina-Blokh / pom.xml
Created July 10, 2022 14:36
The pom.xml dependency for running Python from Java
<dependency>
<groupId>black.ninia</groupId>
<artifactId>jep</artifactId>
<version>4.0.3</version>
<scope>compile</scope>
</dependency>
import pickle
import pandas as pd
from pattern.text import Sentence
from pattern.text.en import sentiment, parse, modality
import torch
from transformers import AutoTokenizer, AutoModel
from flask import render_template, request, Flask
MODEL_PATH = 'data/model_for_prediction.pkl'
file = open(MODEL_PATH, 'rb')
body{
font:15px/1.5 Arial, Helvetica,sans-serif;
padding: 0;
background-color: #f4f3f3;
}
.container{
text-align:center;
width:100%;
<!DOCTYPE html>
<html lang="en-us">
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="../static/style.css">
</head>
<body>
<header>
<div class="container">
<div id="brandname">