3030import java .util .Arrays ;
3131import java .util .HashMap ;
3232import java .util .List ;
33+ import java .util .concurrent .ExecutionException ;
3334import java .util .function .Consumer ;
3435import java .util .stream .Collectors ;
3536
4142import ai .nets .samj .models .Sam2 ;
4243import io .bioimage .modelrunner .apposed .appose .Mamba ;
4344import 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 ;
4646
4747/*
4848 * 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
266266 return ;
267267 Thread thread = reportProgress (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- INSTALLING SAM2 WEIGHTS (" + modelType + ")" );
268268 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 ();
270270 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 )));
272272 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 );
288282 if (size != file .length ())
289283 throw new IOException ("Model SAM2" + modelType + " was not correctly downloaded" );
290284 } catch (IOException ex ) {
@@ -294,7 +288,10 @@ public void installModelWeigths(boolean force) throws IOException, InterruptedEx
294288 } catch (URISyntaxException e1 ) {
295289 passToConsumer (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- FAILED SAM2 WEIGHTS INSTALLATION" );
296290 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 );
298295 }
299296 thread .interrupt ();
300297 passToConsumer (LocalDateTime .now ().format (DATE_FORMAT ).toString () + " -- SAM2 WEIGHTS INSTALLED" );
@@ -401,7 +398,7 @@ public String getModelEnv() {
401398 */
402399 public String getModelWeigthsName () {
403400 try {
404- return DownloadModel .getFileNameFromURLString (String .format (SAM2_URL , modelType ));
401+ return FileDownloader .getFileNameFromURLString (String .format (SAM2_URL , modelType ));
405402 } catch (MalformedURLException e ) {
406403 return String .format (SAM2_FNAME , modelType );
407404 }
@@ -411,7 +408,7 @@ public String getModelWeigthsName() {
411408 public String getModelWeigthPath () {
412409 File file ;
413410 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 ();
415412 } catch (MalformedURLException e ) {
416413 file = Paths .get (path , "envs" , SAM2_ENV_NAME , SAM2_NAME , "weights" , String .format (SAM2_FNAME , modelType )).toFile ();
417414 }
0 commit comments