Extract models to usable location and prefer embedded models

This commit is contained in:
黄仁欢 2026-03-19 15:15:10 +08:00
parent 926fa62798
commit fb838867d8
2 changed files with 47 additions and 4 deletions

View File

@ -306,16 +306,34 @@ public class ResourceExtractor {
private void extractOcrModels() throws IOException { private void extractOcrModels() throws IOException {
Path targetDir = Paths.get(modelsDir); Path targetDir = Paths.get(modelsDir);
if (!Files.exists(targetDir) || isDirEmpty(targetDir)) {
Path altDir = Paths.get(resourceDir, "models");
if (!targetDir.toAbsolutePath().equals(altDir.toAbsolutePath())) {
targetDir = altDir;
}
}
if (Files.exists(targetDir.resolve("pp-ocrv5/det_model/inference.onnx"))) { Path[] essentialModels = {
log.info("[3/4] OCR模型已存在跳过"); targetDir.resolve("pp-ocrv5/PP-OCRv5_server_det_onnx/inference.onnx"),
targetDir.resolve("pp-ocrv5/PP-OCRv5_server_rec_onnx/inference.onnx"),
targetDir.resolve("pp-ocrv5/PP-OCRv5_server_cls_onnx/inference.onnx")
};
boolean allExist = true;
for (Path model : essentialModels) {
if (!Files.exists(model)) {
allExist = false;
break;
}
}
if (allExist) {
log.info("[3/4] OCR????????");
return; return;
} }
log.info("[3/4] 提取OCR模型..."); log.info("[3/4] ??OCR??...");
Files.createDirectories(targetDir); Files.createDirectories(targetDir);
copyDirectoryFromClasspath("models", targetDir); copyDirectoryFromClasspath("models", targetDir);
log.info(" ✓ 完成"); log.info(" ??");
} }
private void copyDirectoryFromClasspath(String resourcePath, Path targetDir) throws IOException { private void copyDirectoryFromClasspath(String resourcePath, Path targetDir) throws IOException {
@ -470,6 +488,16 @@ public class ResourceExtractor {
} }
} }
private boolean isDirEmpty(Path dir) throws IOException {
if (!Files.isDirectory(dir)) {
return true;
}
try (java.nio.file.DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
return !stream.iterator().hasNext();
}
}
private void normalizeVenvDir(Path parentDir, Path expectedDir) throws IOException { private void normalizeVenvDir(Path parentDir, Path expectedDir) throws IOException {
if (Files.isDirectory(expectedDir)) { if (Files.isDirectory(expectedDir)) {
return; return;

View File

@ -171,6 +171,10 @@ public class FlaskProcessManager implements ApplicationListener<ContextRefreshed
// Use bundled models // Use bundled models
Path modelsPath = Paths.get(modelsDir).toAbsolutePath(); Path modelsPath = Paths.get(modelsDir).toAbsolutePath();
if ((!Files.exists(modelsPath) || isDirEmpty(modelsPath)) && Files.exists(Paths.get(resourceDir, "models"))) {
modelsPath = Paths.get(resourceDir, "models").toAbsolutePath();
log.info("Using embedded models from resource dir: {}", modelsPath);
}
env.put("PADDLEOCR_HOME", modelsPath.toString()); env.put("PADDLEOCR_HOME", modelsPath.toString());
env.put("HUB_HOME", modelsPath.toString()); env.put("HUB_HOME", modelsPath.toString());
if (disableModelSourceCheck) { if (disableModelSourceCheck) {
@ -438,6 +442,17 @@ public class FlaskProcessManager implements ApplicationListener<ContextRefreshed
return null; return null;
} }
private boolean isDirEmpty(Path dir) {
if (!Files.isDirectory(dir)) {
return true;
}
try (java.nio.file.DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
return !stream.iterator().hasNext();
} catch (IOException e) {
return true;
}
}
private Path resolveEmbeddedVenvRoot() { private Path resolveEmbeddedVenvRoot() {
Path venv = Paths.get(resourceDir, "python-runtime/venv-offline"); Path venv = Paths.get(resourceDir, "python-runtime/venv-offline");
if (Files.isDirectory(venv)) { if (Files.isDirectory(venv)) {