Commit
·
ae5e7b7
1
Parent(s):
db322c2
push jvm
Browse files
app.py
CHANGED
|
@@ -1,209 +1,33 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
import jpype
|
| 5 |
-
import jpype.imports
|
| 6 |
-
from jpype import JClass, getDefaultJVMPath
|
| 7 |
|
| 8 |
-
def
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
2) Any *.jar in ./jars next to this script
|
| 13 |
-
3) Installed pslpython runtime jar
|
| 14 |
"""
|
| 15 |
-
jars: list[str] = []
|
| 16 |
-
|
| 17 |
-
# 2) Local ./jars
|
| 18 |
-
this_dir = Path(__file__).resolve().parent
|
| 19 |
-
jars_dir = this_dir
|
| 20 |
-
jars.extend(glob.glob(str(jars_dir / "*.jar")))
|
| 21 |
-
|
| 22 |
-
# 3) Try the pslpython runtime jar path
|
| 23 |
-
try:
|
| 24 |
-
import pslpython # noqa: F401
|
| 25 |
-
# common install location (pslpython wheels vendor psl-runtime.jar)
|
| 26 |
-
import site
|
| 27 |
-
candidate_sites = site.getsitepackages() + [site.getusersitepackages()]
|
| 28 |
-
for sdir in candidate_sites:
|
| 29 |
-
jar = Path(sdir) / "pslpython" / "psl-runtime.jar"
|
| 30 |
-
if jar.exists():
|
| 31 |
-
jars.append(str(jar))
|
| 32 |
-
except Exception:
|
| 33 |
-
pass
|
| 34 |
-
|
| 35 |
-
# Deduplicate while preserving order
|
| 36 |
-
dedup = []
|
| 37 |
-
seen = set()
|
| 38 |
-
for j in jars:
|
| 39 |
-
if j not in seen and Path(j).is_file():
|
| 40 |
-
seen.add(j)
|
| 41 |
-
dedup.append(j)
|
| 42 |
-
|
| 43 |
-
return dedup
|
| 44 |
-
|
| 45 |
-
def start_psl_jvm(verbose: bool = True) -> list[str]:
|
| 46 |
-
"""
|
| 47 |
-
Start a JVM with a classpath that includes PSL jars.
|
| 48 |
-
Returns the list of jars used.
|
| 49 |
-
"""
|
| 50 |
-
if jpype.isJVMStarted():
|
| 51 |
-
if verbose:
|
| 52 |
-
print("[PSL] JVM already started.")
|
| 53 |
-
return []
|
| 54 |
-
|
| 55 |
-
jars = _find_psl_jars()
|
| 56 |
-
if not jars:
|
| 57 |
-
raise RuntimeError(
|
| 58 |
-
"No PSL jars found. Set PSL_JARS_DIR to a folder with *.jar, "
|
| 59 |
-
"or place jars under ./jars, or ensure pslpython is installed with psl-runtime.jar."
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
classpath = os.pathsep.join(jars)
|
| 63 |
-
|
| 64 |
-
# If getDefaultJVMPath() fails on your platform, set JAVA_HOME first.
|
| 65 |
-
jvm_path = getDefaultJVMPath()
|
| 66 |
-
|
| 67 |
-
# Start JVM
|
| 68 |
-
jpype.startJVM(
|
| 69 |
-
jvm_path,
|
| 70 |
-
f"-Djava.class.path={classpath}",
|
| 71 |
-
# Optional: tighten memory or logging here
|
| 72 |
-
# "-Xms256m", "-Xmx1024m",
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
# Sanity check that PSL classes are visible
|
| 76 |
-
GA = JClass("org.linqs.psl.model.atom.GroundAtom")
|
| 77 |
-
if verbose:
|
| 78 |
-
print(f"[PSL] JVM started with {len(jars)} jars.")
|
| 79 |
-
for j in jars:
|
| 80 |
-
print(f" - {j}")
|
| 81 |
-
print(f"[PSL] Sanity check: loaded {GA}")
|
| 82 |
-
|
| 83 |
-
return jars
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
def print_files():
|
| 87 |
-
print("Printing files")
|
| 88 |
-
here = Path(__file__).resolve().parent # directory of this file
|
| 89 |
-
files = [p.name for p in here.iterdir() if p.is_file()]
|
| 90 |
-
print(files)
|
| 91 |
-
|
| 92 |
-
def ensure_java_env():
|
| 93 |
-
"""
|
| 94 |
-
Set JAVA_HOME/PATH for JPype/PSL on HF Spaces (Debian).
|
| 95 |
-
Tries Temurin 17 (if you unpacked it in postBuild), then Debian OpenJDK 17/11.
|
| 96 |
-
"""
|
| 97 |
-
candidates = (
|
| 98 |
-
"/usr/local/lib/temurin17", # if you installed Temurin 17 in postBuild
|
| 99 |
-
"/usr/lib/jvm/java-17-openjdk-amd64", # Debian OpenJDK 17
|
| 100 |
-
"/usr/lib/jvm/java-11-openjdk-amd64", # Debian OpenJDK 11 (default-jdk on many images)
|
| 101 |
-
)
|
| 102 |
-
for guess in candidates:
|
| 103 |
-
if os.path.isdir(guess):
|
| 104 |
-
os.environ.setdefault("JAVA_HOME", guess)
|
| 105 |
-
os.environ["PATH"] = f"{guess}/bin:" + os.environ["PATH"]
|
| 106 |
-
break
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# --- Actions ----------------------------------------------------------------
|
| 110 |
-
|
| 111 |
-
def psl_health_check():
|
| 112 |
-
ensure_java_env()
|
| 113 |
-
print(f"Running health check")
|
| 114 |
-
out = []
|
| 115 |
-
|
| 116 |
-
# 1) JPype can start a JVM?
|
| 117 |
-
try:
|
| 118 |
-
import jpype
|
| 119 |
-
if not jpype.isJVMStarted():
|
| 120 |
-
jpype.startJVM()
|
| 121 |
-
out.append("JPype: JVM started ✔")
|
| 122 |
-
except Exception as e:
|
| 123 |
-
print(f"Caught error {e}")
|
| 124 |
-
out.append("JPype: FAILED to start JVM ❌")
|
| 125 |
-
out.append(repr(e))
|
| 126 |
-
|
| 127 |
-
# 2) pslpython installed and its runtime jar present?
|
| 128 |
try:
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
find_acquaintances()
|
| 141 |
-
print(f"pslpython ran successfully")
|
| 142 |
except Exception as e:
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
""
|
| 151 |
-
|
| 152 |
-
Tabs are titled by filename; each tab shows file contents.
|
| 153 |
-
"""
|
| 154 |
-
gr.Markdown(f"### Predicate files in `{base_dir}`")
|
| 155 |
-
|
| 156 |
-
if not os.path.isdir(base_dir):
|
| 157 |
-
gr.Markdown(f"⚠️ Directory not found.")
|
| 158 |
-
return
|
| 159 |
-
|
| 160 |
-
# Glob common predicate/text data files; adjust patterns as needed
|
| 161 |
-
patterns = ["*.txt", "*.tsv", "*.data"]
|
| 162 |
-
files = []
|
| 163 |
-
for pat in patterns:
|
| 164 |
-
files.extend(glob.glob(os.path.join(base_dir, pat)))
|
| 165 |
-
files = sorted(set(files))
|
| 166 |
-
|
| 167 |
-
if not files:
|
| 168 |
-
gr.Markdown("*(No matching text files found.)*")
|
| 169 |
-
return
|
| 170 |
-
|
| 171 |
-
with gr.Tabs():
|
| 172 |
-
for path in files:
|
| 173 |
-
name = os.path.basename(path)
|
| 174 |
-
try:
|
| 175 |
-
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
| 176 |
-
content = f.read()
|
| 177 |
-
except Exception as e:
|
| 178 |
-
content = f"<<Failed to read file>>\n{e!r}"
|
| 179 |
-
|
| 180 |
-
with gr.Tab(name):
|
| 181 |
-
# Scrollable code box via elem_classes and CSS below
|
| 182 |
-
gr.Code(
|
| 183 |
-
value=content,
|
| 184 |
-
interactive=False,
|
| 185 |
-
elem_classes=["scrollbox"],
|
| 186 |
-
label=name,
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
# --- Gradio UI ---------------------------------------------------------------
|
| 192 |
-
|
| 193 |
-
with gr.Blocks(css="""
|
| 194 |
-
.scrollbox { max-height: 480px; overflow: auto; }
|
| 195 |
-
""") as demo:
|
| 196 |
-
gr.Markdown("# PSL setup check & toy inference (top-level files)")
|
| 197 |
-
with gr.Row():
|
| 198 |
-
btn_health = gr.Button("Run health check")
|
| 199 |
-
|
| 200 |
-
out = gr.Textbox(lines=32, label="Output")
|
| 201 |
-
|
| 202 |
-
btn_health.click(fn=psl_health_check, outputs=out)
|
| 203 |
-
|
| 204 |
-
gr.Markdown("---")
|
| 205 |
-
# Render the new tabbed predicate browser
|
| 206 |
-
render_predicate_tabs()
|
| 207 |
|
| 208 |
if __name__ == "__main__":
|
| 209 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def _check_jvm():
|
| 5 |
"""
|
| 6 |
+
Try starting the JVM and report status + jars used.
|
| 7 |
+
Safe to call multiple times (your start_psl_jvm handles the already-started case).
|
|
|
|
|
|
|
| 8 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
+
jars = start_psl_jvm(verbose=False) # returns [] if already started
|
| 11 |
+
already = jpype.isJVMStarted() and (len(jars) == 0)
|
| 12 |
+
status = "✅ JVM already running." if already else "✅ JVM started."
|
| 13 |
+
# sanity check load (again) in case caller didn’t run verbose path
|
| 14 |
+
_ = JClass("org.linqs.psl.model.atom.GroundAtom")
|
| 15 |
+
details = {
|
| 16 |
+
"jvm_started": True,
|
| 17 |
+
"already_running": already,
|
| 18 |
+
"jars_loaded": jars,
|
| 19 |
+
}
|
| 20 |
+
return status, details
|
|
|
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
+
tb = traceback.format_exc(limit=2)
|
| 23 |
+
return f"❌ JVM start failed: {e}", {"traceback": tb}
|
| 24 |
+
|
| 25 |
+
with gr.Blocks(title="PSL JVM Sanity Check") as demo:
|
| 26 |
+
gr.Markdown("### PSL JVM loader\nClick the button to start the JVM and verify PSL classes are reachable.")
|
| 27 |
+
btn = gr.Button("Start JVM")
|
| 28 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 29 |
+
info = gr.JSON(label="Details")
|
| 30 |
+
btn.click(fn=_check_jvm, inputs=None, outputs=[status, info])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|
| 33 |
demo.launch()
|