diff --git a/api/src/main/java/ai/djl/repository/RemoteRepository.java b/api/src/main/java/ai/djl/repository/RemoteRepository.java index 6b01ce14ef8..52f87afbf82 100644 --- a/api/src/main/java/ai/djl/repository/RemoteRepository.java +++ b/api/src/main/java/ai/djl/repository/RemoteRepository.java @@ -75,7 +75,7 @@ public Metadata locate(MRL mrl) throws IOException { Metadata metadata = JsonUtils.GSON_PRETTY.fromJson(reader, Metadata.class); metadata.init(arguments); Date lastUpdated = metadata.getLastUpdated(); - if (Boolean.getBoolean("offline") + if (Utils.isOfflineMode() || System.currentTimeMillis() - lastUpdated.getTime() < ONE_DAY) { metadata.setRepositoryUri(mrlUri); return metadata; diff --git a/api/src/main/java/ai/djl/util/Ec2Utils.java b/api/src/main/java/ai/djl/util/Ec2Utils.java index 178c3d7efe7..5408182964f 100644 --- a/api/src/main/java/ai/djl/util/Ec2Utils.java +++ b/api/src/main/java/ai/djl/util/Ec2Utils.java @@ -97,7 +97,7 @@ public static String readMetadata(String key) { * @param engine the default engine name */ public static void callHome(String engine) { - if (Boolean.getBoolean("offline") + if (Utils.isOfflineMode() || Boolean.parseBoolean(Utils.getEnvOrSystemProperty("OPT_OUT_TRACKING")) || System.currentTimeMillis() - lastCheckIn < ONE_DAY) { return; diff --git a/api/src/main/java/ai/djl/util/Utils.java b/api/src/main/java/ai/djl/util/Utils.java index c8e1bd514ac..270958d5b40 100644 --- a/api/src/main/java/ai/djl/util/Utils.java +++ b/api/src/main/java/ai/djl/util/Utils.java @@ -357,6 +357,20 @@ public static Path getCacheDir() { return Paths.get(cacheDir); } + /** + * Returns if offline mode is enabled. + * + * @return true if offline mode is enabled + */ + public static boolean isOfflineMode() { + String mode = getenv("DJL_OFFLINE", System.getProperty("ai.djl.offline")); + if (mode != null) { + return Boolean.parseBoolean(mode); + } + // backward compatible + return Boolean.getBoolean("offline"); + } + /** * Returns nested model directory if the directory contains only one subdirectory. * @@ -481,7 +495,7 @@ public static InputStream openUrl(String url) throws IOException { */ public static InputStream openUrl(URL url) throws IOException { String protocol = url.getProtocol(); - if (Boolean.getBoolean("offline") + if (isOfflineMode() && ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol))) { throw new IOException("Offline model is enabled."); } diff --git a/build.gradle b/build.gradle index f98b86c4e51..4b8b20cd516 100644 --- a/build.gradle +++ b/build.gradle @@ -88,7 +88,7 @@ configure(javaProjects()) { systemProperty "disableProgressBar", "true" systemProperty "nightly", System.getProperty("nightly", "false") if (gradle.startParameter.offline) { - systemProperty "offline", "true" + systemProperty "ai.djl.offline", "true" } // This is used to avoid overriding on default engine for modules: // mxnet-engine, mxnet-model-zoo, api (MockEngine), basicdataset, fasttext, etc diff --git a/docs/telemetry.md b/docs/telemetry.md index d6ff9b20bc1..256adf00a49 100644 --- a/docs/telemetry.md +++ b/docs/telemetry.md @@ -20,5 +20,5 @@ System.setProperty("OPT_OUT_TRACKING", "true") Usage tracking is also disable in `offline` mode: ```java -System.setProperty("offline", "true") +System.setProperty("ai.djl.offline", "true") ``` diff --git a/examples/src/test/java/ai/djl/testing/TestRequirements.java b/examples/src/test/java/ai/djl/testing/TestRequirements.java index e8c9bd4bdda..01eef756201 100644 --- a/examples/src/test/java/ai/djl/testing/TestRequirements.java +++ b/examples/src/test/java/ai/djl/testing/TestRequirements.java @@ -14,6 +14,7 @@ import ai.djl.engine.Engine; import ai.djl.engine.EngineException; +import ai.djl.util.Utils; import org.testng.SkipException; @@ -45,7 +46,7 @@ public static void weekly() { /** Requires a test not be run in offline mode. */ public static void notOffline() { - if (Boolean.getBoolean("offline")) { + if (Utils.isOfflineMode()) { throw new SkipException("This test can not run while offline"); } } diff --git a/extensions/tokenizers/src/main/java/ai/djl/huggingface/zoo/HfModelZoo.java b/extensions/tokenizers/src/main/java/ai/djl/huggingface/zoo/HfModelZoo.java index 9ee8fc19cf8..a0099073c97 100644 --- a/extensions/tokenizers/src/main/java/ai/djl/huggingface/zoo/HfModelZoo.java +++ b/extensions/tokenizers/src/main/java/ai/djl/huggingface/zoo/HfModelZoo.java @@ -131,8 +131,7 @@ private Map> listModels(Application app) { Path file = dir.resolve("models.json"); if (Files.exists(file)) { long lastModified = Files.getLastModifiedTime(file).toMillis(); - if (Boolean.getBoolean("offline") - || System.currentTimeMillis() - lastModified < ONE_DAY) { + if (Utils.isOfflineMode() || System.currentTimeMillis() - lastModified < ONE_DAY) { try (Reader reader = Files.newBufferedReader(file)) { return JsonUtils.GSON.fromJson(reader, type); } diff --git a/extensions/tokenizers/src/test/java/ai/djl/huggingface/zoo/ModelZooTest.java b/extensions/tokenizers/src/test/java/ai/djl/huggingface/zoo/ModelZooTest.java index e585a219f17..5c3ed6c3ed0 100644 --- a/extensions/tokenizers/src/test/java/ai/djl/huggingface/zoo/ModelZooTest.java +++ b/extensions/tokenizers/src/test/java/ai/djl/huggingface/zoo/ModelZooTest.java @@ -103,21 +103,21 @@ public void testFutureVersion() throws IOException { @Test public void testOffLine() throws IOException { System.setProperty("DJL_CACHE_DIR", "build/cache"); - System.setProperty("offline", "true"); + System.setProperty("ai.djl.offline", "true"); try { Utils.deleteQuietly(Paths.get("build/cache")); // static variables cannot not be initialized properly if directly use new HfModelZoo() ModelZoo.getModelZoo("ai.djl.huggingface.pytorch"); ModelZoo zoo = new HfModelZoo(); - Assert.assertTrue(zoo.getModelLoaders().size() > 0); + Assert.assertFalse(zoo.getModelLoaders().isEmpty()); Set engines = zoo.getSupportedEngines(); Assert.assertEquals(engines.size(), 1); Assert.assertEquals(engines.iterator().next(), "PyTorch"); } finally { System.clearProperty("DJL_CACHE_DIR"); - System.clearProperty("offline"); + System.clearProperty("ai.djl.offline"); } } } diff --git a/testing/src/main/java/ai/djl/testing/TestRequirements.java b/testing/src/main/java/ai/djl/testing/TestRequirements.java index bf57d64bd7c..32f242589b9 100644 --- a/testing/src/main/java/ai/djl/testing/TestRequirements.java +++ b/testing/src/main/java/ai/djl/testing/TestRequirements.java @@ -13,6 +13,7 @@ package ai.djl.testing; import ai.djl.engine.Engine; +import ai.djl.util.Utils; import org.testng.SkipException; @@ -45,7 +46,7 @@ public static void weekly() { /** Requires a test not be run in offline mode. */ public static void notOffline() { - if (Boolean.getBoolean("offline")) { + if (Utils.isOfflineMode()) { throw new SkipException("This test can not run while offline"); } }