Skip to content

Commit 1196abc

Browse files
committed
adapt to new downloaders
1 parent 2afa860 commit 1196abc

File tree

3 files changed

+36
-84
lines changed

3 files changed

+36
-84
lines changed

src/main/java/ai/nets/samj/install/EfficientViTSamEnvManager.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.HashMap;
3434
import java.util.List;
3535
import java.util.Map;
36+
import java.util.concurrent.ExecutionException;
3637
import java.util.function.Consumer;
3738
import java.util.stream.Collectors;
3839
import java.util.zip.ZipEntry;
@@ -46,8 +47,7 @@
4647
import ai.nets.samj.models.EfficientViTSamJ;
4748
import io.bioimage.modelrunner.apposed.appose.Mamba;
4849
import io.bioimage.modelrunner.apposed.appose.MambaInstallException;
49-
import io.bioimage.modelrunner.apposed.appose.MambaInstallerUtils;
50-
import io.bioimage.modelrunner.bioimageio.download.DownloadModel;
50+
import io.bioimage.modelrunner.download.FileDownloader;
5151

5252
/*
5353
* Class that is manages the installation of SAM and EfficientSAM together with Python, their corresponding environments
@@ -272,25 +272,19 @@ public void installModelWeigths(boolean force) throws IOException, InterruptedEx
272272
return;
273273
Thread thread = reportProgress(LocalDateTime.now().format(DATE_FORMAT).toString() + " -- INSTALLING EFFICIENTVITSAM WEIGHTS (" + modelType + ")");
274274
try {
275-
File file = Paths.get(path, "envs", EVITSAM_ENV_NAME, EVITSAM_NAME, "weights", DownloadModel.getFileNameFromURLString(String.format(EVITSAM_URL, modelType))).toFile();
275+
File file = Paths.get(path, "envs", EVITSAM_ENV_NAME, EVITSAM_NAME, "weights", FileDownloader.getFileNameFromURLString(String.format(EVITSAM_URL, modelType))).toFile();
276276
file.getParentFile().mkdirs();
277-
URL url = MambaInstallerUtils.redirectedURL(new URL(String.format(EVITSAM_URL, modelType)));
277+
URL url = FileDownloader.redirectedURL(new URL(String.format(EVITSAM_URL, modelType)));
278278
Thread parentThread = Thread.currentThread();
279-
Thread downloadThread = new Thread(() -> {
280-
try {
281-
downloadFile(url.toString(), file, parentThread);
282-
} catch (IOException | URISyntaxException | InterruptedException e) {
283-
e.printStackTrace();
284-
}
285-
});
286-
downloadThread.start();
287-
long size = DownloadModel.getFileSize(url);
288-
while (downloadThread.isAlive()) {
289-
try {Thread.sleep(280);} catch (InterruptedException e) {break;}
290-
double progress = Math.round( (double) 100 * file.length() / size );
291-
if (progress < 0 || progress > 100) passToConsumer(LocalDateTime.now().format(DATE_FORMAT).toString() + " -- EFFICIENTVITSAM WEIGHTS DOWNLOAD: UNKNOWN%");
292-
else passToConsumer(LocalDateTime.now().format(DATE_FORMAT).toString() + " -- EFFICIENTVITSAM WEIGHTS DOWNLOAD: " + progress + "%");
293-
}
279+
FileDownloader fd = new FileDownloader(url.toString(), file, false);
280+
long size = fd.getOnlineFileSize();
281+
Consumer<Double> dConsumer = (d) -> {
282+
d = (double) (Math.round(d * 1000) / 10);
283+
if (d < 0 || d > 100) passToConsumer(LocalDateTime.now().format(DATE_FORMAT).toString() + " -- EFFICIENTVITSAM WEIGHTS DOWNLOAD: UNKNOWN%");
284+
else passToConsumer(LocalDateTime.now().format(DATE_FORMAT).toString() + " -- EFFICIENTVITSAM WEIGHTS DOWNLOAD: " + d + "%");
285+
};
286+
fd.setPartialProgressConsumer(dConsumer);
287+
fd.download(parentThread);
294288
if (size != file.length())
295289
throw new IOException("Model EfficientViTSAM-" + modelType + " was not correctly downloaded");
296290
} catch (IOException ex) {
@@ -300,7 +294,10 @@ public void installModelWeigths(boolean force) throws IOException, InterruptedEx
300294
} catch (URISyntaxException e1) {
301295
passToConsumer(LocalDateTime.now().format(DATE_FORMAT).toString() + " -- FAILED EFFICIENTVITSAM WEIGHTS INSTALLATION");
302296
throw new IOException("Unable to find the download URL for EfficientViTSAM " + modelType + ": " + String.format(EVITSAM_URL, modelType));
303-
297+
} catch (ExecutionException e) {
298+
thread.interrupt();
299+
passToConsumer(LocalDateTime.now().format(DATE_FORMAT).toString() + " -- FAILED EFFICIENTVITSAM WEIGHTS INSTALLATION");
300+
throw new RuntimeException(e);
304301
}
305302
thread.interrupt();
306303
passToConsumer(LocalDateTime.now().format(DATE_FORMAT).toString() + " -- EFFICIENTVITSAM WEIGHTS INSTALLED");

src/main/java/ai/nets/samj/install/Sam2EnvManager.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Arrays;
3131
import java.util.HashMap;
3232
import java.util.List;
33+
import java.util.concurrent.ExecutionException;
3334
import java.util.function.Consumer;
3435
import java.util.stream.Collectors;
3536

@@ -41,8 +42,7 @@
4142
import ai.nets.samj.models.Sam2;
4243
import io.bioimage.modelrunner.apposed.appose.Mamba;
4344
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;
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
}

src/main/java/ai/nets/samj/install/SamEnvManagerAbstract.java

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
2626
import java.io.FileOutputStream;
27-
import java.net.HttpURLConnection;
2827
import java.net.URISyntaxException;
29-
import java.net.URL;
30-
import java.nio.channels.Channels;
31-
import java.nio.channels.ReadableByteChannel;
3228
import java.nio.file.Paths;
3329
import java.time.LocalDateTime;
3430
import java.time.format.DateTimeFormatter;
3531
import java.util.function.Consumer;
3632
import java.util.zip.ZipEntry;
3733
import java.util.zip.ZipInputStream;
3834

39-
import io.bioimage.modelrunner.engine.installation.FileDownloader;
4035
import io.bioimage.modelrunner.system.PlatformDetection;
41-
import io.bioimage.modelrunner.utils.CommonUtils;
4236

4337
import org.apache.commons.compress.archivers.ArchiveException;
4438

@@ -229,42 +223,6 @@ public String getEnvsPath() {
229223
return Paths.get(path, "envs").toFile().getAbsolutePath();
230224
}
231225

232-
/**
233-
* Method that downloads a file
234-
* @param downloadURL
235-
* url of the file to be downloaded
236-
* @param targetFile
237-
* file where the file from the url will be downloaded too
238-
* @param parentThread
239-
* main thread that launched the current one where the download is happening
240-
* @throws IOException if there si any error downloading the file
241-
* @throws URISyntaxException if there is any error in the URL syntax
242-
* @throws InterruptedException if the parent thread is stopped and the download stopped
243-
*/
244-
public void downloadFile(String downloadURL, File targetFile, Thread parentThread)
245-
throws IOException, URISyntaxException, InterruptedException {
246-
FileOutputStream fos = null;
247-
ReadableByteChannel rbc = null;
248-
try {
249-
URL website = new URL(downloadURL);
250-
HttpURLConnection conn = (HttpURLConnection) website.openConnection();
251-
conn.setRequestMethod("GET");
252-
conn.setRequestProperty("User-Agent", CommonUtils.getJDLLUserAgent());
253-
rbc = Channels.newChannel(conn.getInputStream());
254-
// TODO rbc = Channels.newChannel(website.openStream());
255-
// Create the new model file as a zip
256-
fos = new FileOutputStream(targetFile);
257-
// Send the correct parameters to the progress screen
258-
FileDownloader downloader = new FileDownloader(rbc, fos);
259-
downloader.call(parentThread);
260-
} finally {
261-
if (fos != null)
262-
fos.close();
263-
if (rbc != null)
264-
rbc.close();
265-
}
266-
}
267-
268226
/**
269227
* For a fresh, installation, SAMJ might need to download first micromamba. In that case, this method
270228
* returns the progress made for its download.

0 commit comments

Comments
 (0)