amirali1985 commited on
Commit
efc78fc
·
1 Parent(s): ae5e7b7

more small fixes.

Browse files
Files changed (1) hide show
  1. app.py +73 -1
app.py CHANGED
@@ -1,6 +1,78 @@
1
- import gradio as gr
 
 
2
  import traceback
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def _check_jvm():
5
  """
6
  Try starting the JVM and report status + jars used.
 
1
+ import os
2
+ import glob
3
+ from pathlib import Path
4
  import traceback
5
 
6
+ import gradio as gr
7
+ import jpype
8
+ import jpype.imports
9
+ from jpype import JClass, getDefaultJVMPath
10
+
11
+ def _find_psl_jars() -> list[str]:
12
+ """
13
+ Priority:
14
+ 1) Any *.jar inside PSL_JARS_DIR (if set)
15
+ 2) Any *.jar in ./jars next to this script
16
+ 3) Installed pslpython runtime jar
17
+ """
18
+ jars: list[str] = []
19
+
20
+ # 2) Local ./jars
21
+ this_dir = Path(__file__).resolve().parent
22
+ jars_dir = this_dir
23
+ jars.extend(glob.glob(str(jars_dir / "*.jar")))
24
+
25
+ # Deduplicate while preserving order
26
+ dedup = []
27
+ seen = set()
28
+ for j in jars:
29
+ if j not in seen and Path(j).is_file():
30
+ seen.add(j)
31
+ dedup.append(j)
32
+
33
+ return dedup
34
+
35
+ def start_psl_jvm(verbose: bool = True) -> list[str]:
36
+ """
37
+ Start a JVM with a classpath that includes PSL jars.
38
+ Returns the list of jars used.
39
+ """
40
+ if jpype.isJVMStarted():
41
+ if verbose:
42
+ print("[PSL] JVM already started.")
43
+ return []
44
+
45
+ jars = _find_psl_jars()
46
+ if not jars:
47
+ raise RuntimeError(
48
+ "No PSL jars found. Set PSL_JARS_DIR to a folder with *.jar, "
49
+ "or place jars under ./jars, or ensure pslpython is installed with psl-runtime.jar."
50
+ )
51
+
52
+ classpath = os.pathsep.join(jars)
53
+
54
+ # If getDefaultJVMPath() fails on your platform, set JAVA_HOME first.
55
+ jvm_path = getDefaultJVMPath()
56
+
57
+ # Start JVM
58
+ jpype.startJVM(
59
+ jvm_path,
60
+ f"-Djava.class.path={classpath}",
61
+ # Optional: tighten memory or logging here
62
+ # "-Xms256m", "-Xmx1024m",
63
+ )
64
+
65
+ # Sanity check that PSL classes are visible
66
+ GA = JClass("org.linqs.psl.model.atom.GroundAtom")
67
+ if verbose:
68
+ print(f"[PSL] JVM started with {len(jars)} jars.")
69
+ for j in jars:
70
+ print(f" - {j}")
71
+ print(f"[PSL] Sanity check: loaded {GA}")
72
+
73
+ return jars
74
+
75
+
76
  def _check_jvm():
77
  """
78
  Try starting the JVM and report status + jars used.