@@ -26,7 +26,7 @@ public class JPyRustBridge {
2626 System .out .println ("[JPyRustBridge] Native library loaded successfully via NativeLoader" );
2727 } catch (Throwable e ) {
2828 System .out .println ("DEBUG: Failed to load native library!" );
29- e .printStackTrace (System .out ); // Print to stdout to ensure visibility
29+ e .printStackTrace (System .out );
3030 throw new RuntimeException ("Fatal: Native library load failed" , e );
3131 }
3232 }
@@ -35,14 +35,12 @@ public class JPyRustBridge {
3535 private static Path pythonExe ;
3636
3737 public synchronized static void initialize () {
38- // Default to user home directory
3938 String userHome = System .getProperty ("user.home" );
4039 Path defaultWorkDir = Paths .get (userHome , ".jpyrust" );
4140 initialize (defaultWorkDir .toString (), null );
4241 }
4342
4443 public synchronized static void initialize (String workDirectory , String ignoredSourceScript ) {
45- // Generate a unique session key for Shared Memory
4644 String memoryKey = "JPyRust_" + java .util .UUID .randomUUID ().toString ();
4745 System .out .println ("[JPyRust] Generated Session Key (Default): " + memoryKey );
4846 initialize (workDirectory , ignoredSourceScript , memoryKey );
@@ -53,11 +51,7 @@ private static void setupEmbeddedPython(Path targetDir) throws Exception {
5351 Path markerFile = pythonDistDir .resolve (".installed" );
5452
5553 if (!Files .exists (markerFile )) {
56- // Robustness: Check if it's actually installed but marker is missing (e.g.
57- // crash during last boot)
58- // We check for a key package like 'ultralytics' or 'torch'
59- Path sitePackages = pythonDistDir .resolve ("Lib/site-packages" ); // Standard for embedded layout usually, or
60- // just Lib/site-packages
54+ Path sitePackages = pythonDistDir .resolve ("Lib/site-packages" );
6155 if (Files .exists (pythonDistDir .resolve ("python.exe" )) &&
6256 (Files .exists (sitePackages .resolve ("ultralytics" ))
6357 || Files .exists (sitePackages .resolve ("torch" )))) {
@@ -69,48 +63,25 @@ private static void setupEmbeddedPython(Path targetDir) throws Exception {
6963 System .out .println ("[Init] Extracting user embedded python to: " + pythonDistDir );
7064 NativeLoader .extractZip ("/python_dist.zip" , pythonDistDir );
7165
72- // --- [Patched] Auto-fix for pip support ---
7366 Path pthFile = pythonDistDir .resolve ("python311._pth" );
7467 if (Files .exists (pthFile )) {
7568 System .out .println ("[Init] Patching python311._pth to enable 'import site'..." );
7669 Files .write (pthFile , "python311.zip\n .\n import site" .getBytes ());
7770 }
7871
79- // --- [Patched] Manual Dependency Installation ---
8072 System .out .println ("[Init] Installing dependencies via pip..." );
8173 Path pyExe = pythonDistDir .resolve ("python.exe" );
8274 Path requirements = targetDir .resolve ("requirements.txt" );
8375
84- // Generate requirements.txt if missing (fallback)
85- if (!Files .exists (requirements )) {
86- // Copy from classpath or creating a minimal one if needed,
87- // but for now we assume it exists or the user provided it.
88- // Actually, let's copy the one from project root if we can or skip.
89- // Better: JPyRustBridge is usually run where requirements.txt is available or
90- // provided.
91- }
92-
93- // We need to assume requirements.txt is in the targetDir or provided.
94- // The previous logic didn't explicitly move it, but our manual steps did.
95- // Let's ensure requirements.txt is present.
96- // Since we can't easily access the project root from here dynamically in all
97- // cases,
98- // we will rely on the fact that if it exists, we install.
99-
10076 if (Files .exists (requirements )) {
101- Path wheelsDir = pythonDistDir .resolve ("../wheels" ); // Based on our manual copy structure
102- // Adjust logic: The wheels were in build/python_staging/wheels.
103- // We need to be careful. If wheels are not there, try online install or skip?
104- // For now, let's just try to run pip.
77+ Path wheelsDir = pythonDistDir .resolve ("../wheels" );
10578
10679 ProcessBuilder pipPb = new ProcessBuilder (
10780 pyExe .toString (),
10881 "-m" , "pip" , "install" ,
10982 "--no-index" ,
110- "--find-links=wheels" , // Relative to working dir? No, let's use absolute if possible or
111- // relative
83+ "--find-links=wheels" ,
11284 "-r" , requirements .toString ());
113- // We assume wheels are in targetDir/wheels
11485 pipPb .directory (targetDir .toFile ());
11586 pipPb .redirectErrorStream (true );
11687 Process pipProc = pipPb .start ();
@@ -124,7 +95,6 @@ private static void setupEmbeddedPython(Path targetDir) throws Exception {
12495 pipProc .waitFor ();
12596 }
12697
127- // Create marker
12898 Files .createFile (markerFile );
12999
130100 } else {
@@ -135,40 +105,28 @@ private static void setupEmbeddedPython(Path targetDir) throws Exception {
135105 pythonExe = pythonDistDir .resolve ("python.exe" );
136106 }
137107
138- /**
139- * [Native Callback] Rust 프로세스에서 발생하는 로그를 Java로 전달받는 메서드입니다.
140- * 주의: Native 코드(Rust)에서 이 메서드 시그니처를 참조하므로 절대 삭제하거나 변경하면 안 됩니다.
141- *
142- * @param level 로그 레벨 (INFO, ERROR, DEBUG 등)
143- * @param msg 로그 메시지 내용
144- */
145108 public static void log (String level , String msg ) {
146- // 기본적으로 System.out으로 출력 (추후 SLF4J 등으로 확장 가능)
147109 System .out .println ("[JPyRust-Native] [" + level + "] " + msg );
148110 }
149111
150112 private static native void initNative (String workDir , String sourceScriptDir , String modelPath , float confidence ,
151113 String memoryKey );
152114
153- // 4-param initialize overload for AIImageController compatibility
154115 public static void initialize (String workDirectory , String sourceScript , String modelPath , float confidence ) {
155116 System .out .println ("[JPyRust] Init with model: " + modelPath + ", confidence: " + confidence );
156117
157- // Generate a unique session key for Shared Memory
158118 String memoryKey = "JPyRust_" + java .util .UUID .randomUUID ().toString ();
159119 System .out .println ("[JPyRust] Generated Session Key: " + memoryKey );
160120
161121 initialize (workDirectory , sourceScript , memoryKey );
162122 }
163123
164- // Internal initialize with key
165124 private synchronized static void initialize (String workDirectory , String ignoredSourceScript , String memoryKey ) {
166125 if (initialized ) {
167126 return ;
168127 }
169128
170129 workDir = workDirectory ;
171- // sourceScriptDir is no longer needed as we use the embedded one
172130
173131 System .out .println ("=== JPyRust IPC Initialization ===" );
174132 System .out .println ("[Init] Work Directory: " + workDir );
@@ -179,10 +137,8 @@ private synchronized static void initialize(String workDirectory, String ignored
179137 Files .createDirectories (workPath );
180138 }
181139
182- // 1. Setup Embedded Python
183140 setupEmbeddedPython (workPath );
184141
185- // 2. Initialize Native
186142 initNative (workDir , workDir , "yolov8n.pt" , 0.5f , memoryKey );
187143
188144 System .out .println ("=== Initialization Complete ===" );
@@ -194,18 +150,15 @@ private synchronized static void initialize(String workDirectory, String ignored
194150 }
195151 }
196152
197- // Native executeTask declaration matching Rust signature
198153 private native byte [] executeTask (String workDir , String taskType , String requestId , String metadata ,
199154 ByteBuffer data , int length );
200155
201156 public byte [] processImage (String workDirectory , ByteBuffer data , int length , int width , int height , int channels ) {
202157 String requestId = java .util .UUID .randomUUID ().toString ();
203- // Construct metadata directly as simple string "width height channels"
204158 String metadata = width + " " + height + " " + channels ;
205159 return executeTask (workDirectory , "YOLO" , requestId , metadata , data , length );
206160 }
207161
208- // 7-param processImage overload for AIImageController compatibility
209162 public byte [] processImage (String workDirectory , ByteBuffer data , int length , int width , int height , int channels ,
210163 String requestId ) {
211164 System .out .println ("[JPyRust] Processing request: " + requestId );
@@ -217,7 +170,6 @@ public byte[] processImage(ByteBuffer data, int length, int width, int height, i
217170 return processImage (workDir , data , length , width , height , channels );
218171 }
219172
220- // Edge detection implementation
221173 public byte [] processEdgeDetection (byte [] imageData , int width , int height , int channels ) {
222174 System .out .println ("[JPyRust] Edge detection called (Native)" );
223175 try {
@@ -236,7 +188,6 @@ public byte[] processEdgeDetection(byte[] imageData, int width, int height, int
236188 }
237189 }
238190
239- // NLP processing implementation
240191 public String processNlp (String text ) {
241192 System .out .println ("[JPyRust] NLP processing: " + text );
242193 try {
@@ -246,7 +197,6 @@ public String processNlp(String text) {
246197 directBuffer .flip ();
247198
248199 String requestId = java .util .UUID .randomUUID ().toString ();
249- // Metadata empty for NLP, or specialized if needed
250200 String metadata = "TEXT" ;
251201
252202 byte [] resultBytes = executeTask (workDir , "NLP_TEXTBLOB" , requestId , metadata , directBuffer ,
@@ -262,7 +212,6 @@ public String processNlp(String text) {
262212 }
263213 }
264214
265- // Regression processing implementation
266215 public String processRegression (String jsonPoints ) {
267216 System .out .println ("[JPyRust] Regression processing: " + jsonPoints );
268217 try {
@@ -287,13 +236,6 @@ public String processRegression(String jsonPoints) {
287236 }
288237 }
289238
290- // Legacy runPythonProcess declaration removed/replaced by executeTask usage
291- /*
292- * private native byte[] runPythonProcess(String workDir, ByteBuffer data, int
293- * length, int width, int height,
294- * int channels, String requestId);
295- */
296-
297239 public String runPythonRaw (ByteBuffer data , int length , int width , int height , int channels ) {
298240 String inputFilePath = workDir + "/input_image.dat" ;
299241 String outputFilePath = workDir + "/output_image.dat" ;
0 commit comments