30
30
import java .util .Arrays ;
31
31
import java .util .HashMap ;
32
32
import java .util .List ;
33
+ import java .util .concurrent .ExecutionException ;
33
34
import java .util .function .Consumer ;
34
35
import java .util .stream .Collectors ;
35
36
41
42
import ai .nets .samj .models .Sam2 ;
42
43
import io .bioimage .modelrunner .apposed .appose .Mamba ;
43
44
import io .bioimage .modelrunner .apposed .appose .MambaInstallException ;
44
- import io .bioimage .modelrunner .apposed .appose .MambaInstallerUtils ;
45
- import io .bioimage .modelrunner .bioimageio .download .DownloadModel ;
45
+ import io .bioimage .modelrunner .download .FileDownloader ;
46
46
47
47
/*
48
48
* Class that is manages the installation of SAM and EfficientSAM together with Python, their corresponding environments
@@ -266,25 +266,19 @@ public void installModelWeigths(boolean force) throws IOException, InterruptedEx
266
266
return ;
267
267
Thread thread = reportProgress (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- INSTALLING SAM2 WEIGHTS (" + modelType + ")" );
268
268
try {
269
- File file = Paths .get (path , "envs" , SAM2_ENV_NAME , SAM2_NAME , "weights" , DownloadModel .getFileNameFromURLString (String .format (SAM2_URL , modelType ))).toFile ();
269
+ File file = Paths .get (path , "envs" , SAM2_ENV_NAME , SAM2_NAME , "weights" , FileDownloader .getFileNameFromURLString (String .format (SAM2_URL , modelType ))).toFile ();
270
270
file .getParentFile ().mkdirs ();
271
- URL url = MambaInstallerUtils .redirectedURL (new URL (String .format (SAM2_URL , modelType )));
271
+ URL url = FileDownloader .redirectedURL (new URL (String .format (SAM2_URL , modelType )));
272
272
Thread parentThread = Thread .currentThread ();
273
- Thread downloadThread = new Thread (() -> {
274
- try {
275
- downloadFile (url .toString (), file , parentThread );
276
- } catch (IOException | URISyntaxException | InterruptedException e ) {
277
- e .printStackTrace ();
278
- }
279
- });
280
- downloadThread .start ();
281
- long size = DownloadModel .getFileSize (url );
282
- while (downloadThread .isAlive ()) {
283
- try {Thread .sleep (280 );} catch (InterruptedException e ) {break ;}
284
- double progress = Math .round ( (double ) 100 * file .length () / size );
285
- if (progress < 0 || progress > 100 ) passToConsumer (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- SAM2 WEIGHTS DOWNLOAD: UNKNOWN%" );
286
- else passToConsumer (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- SAM2 WEIGHTS DOWNLOAD: " + progress + "%" );
287
- }
273
+ FileDownloader fd = new FileDownloader (url .toString (), file , false );
274
+ long size = fd .getOnlineFileSize ();
275
+ Consumer <Double > dConsumer = (d ) -> {
276
+ d = (double ) (Math .round (d * 1000 ) / 10 );
277
+ if (d < 0 || d > 100 ) passToConsumer (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- EFFICIENTVITSAM WEIGHTS DOWNLOAD: UNKNOWN%" );
278
+ else passToConsumer (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- EFFICIENTVITSAM WEIGHTS DOWNLOAD: " + d + "%" );
279
+ };
280
+ fd .setPartialProgressConsumer (dConsumer );
281
+ fd .download (parentThread );
288
282
if (size != file .length ())
289
283
throw new IOException ("Model SAM2" + modelType + " was not correctly downloaded" );
290
284
} catch (IOException ex ) {
@@ -294,7 +288,10 @@ public void installModelWeigths(boolean force) throws IOException, InterruptedEx
294
288
} catch (URISyntaxException e1 ) {
295
289
passToConsumer (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- FAILED SAM2 WEIGHTS INSTALLATION" );
296
290
throw new IOException ("Unable to find the download URL for SAM2 " + modelType + ": " + String .format (SAM2_URL , modelType ));
297
-
291
+ } catch (ExecutionException e ) {
292
+ thread .interrupt ();
293
+ passToConsumer (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- FAILED SAM2 WEIGHTS INSTALLATION" );
294
+ throw new RuntimeException (e );
298
295
}
299
296
thread .interrupt ();
300
297
passToConsumer (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- SAM2 WEIGHTS INSTALLED" );
@@ -401,7 +398,7 @@ public String getModelEnv() {
401
398
*/
402
399
public String getModelWeigthsName () {
403
400
try {
404
- return DownloadModel .getFileNameFromURLString (String .format (SAM2_URL , modelType ));
401
+ return FileDownloader .getFileNameFromURLString (String .format (SAM2_URL , modelType ));
405
402
} catch (MalformedURLException e ) {
406
403
return String .format (SAM2_FNAME , modelType );
407
404
}
@@ -411,7 +408,7 @@ public String getModelWeigthsName() {
411
408
public String getModelWeigthPath () {
412
409
File file ;
413
410
try {
414
- file = Paths .get (path , "envs" , SAM2_ENV_NAME , SAM2_NAME , "weights" , DownloadModel .getFileNameFromURLString (String .format (SAM2_URL , modelType ))).toFile ();
411
+ file = Paths .get (path , "envs" , SAM2_ENV_NAME , SAM2_NAME , "weights" , FileDownloader .getFileNameFromURLString (String .format (SAM2_URL , modelType ))).toFile ();
415
412
} catch (MalformedURLException e ) {
416
413
file = Paths .get (path , "envs" , SAM2_ENV_NAME , SAM2_NAME , "weights" , String .format (SAM2_FNAME , modelType )).toFile ();
417
414
}
0 commit comments