[desktop] Remove old ML models when downloading new ones

This commit is contained in:
Manav Rathi
2024-10-11 10:15:13 +05:30
parent b9dd0bc3b7
commit 3512615780

View File

@@ -153,6 +153,8 @@ const modelPathDownloadingIfNeeded = async (
) => {
const modelPath = modelSavePath(modelName);
await cleanupOldModelsIfNeeded();
if (!existsSync(modelPath)) {
log.info("CLIP image model not found, downloading");
await downloadModel(modelPath, modelName);
@@ -169,6 +171,33 @@ const modelPathDownloadingIfNeeded = async (
return modelPath;
};
/**
* Cleanup old models.
*
* This code runs whenever we need to download a new model, which usually
* happens when we update a model, so this is a great time to go through the
* list of previously existent but now unused models, and delete them if they
* exist to clean up the user's disk space.
*/
const cleanupOldModelsIfNeeded = async () => {
const oldModelNames = [
"clip-image-vit-32-float32.onnx",
"clip-text-vit-32-uint8.onnx",
"mobileclip_s2_image.onnx",
"mobileclip_s2_image_opset18_rgba_sim.onnx",
"mobileclip_s2_text_int32.onnx",
"yolov5s_face_640_640_dynamic.onnx",
];
for (const modelName of oldModelNames) {
const modelPath = modelSavePath(modelName);
if (existsSync(modelPath)) {
log.info(`Removing unused ML model at ${modelPath}`);
await fs.rm(modelPath);
}
}
};
/** Return the path where the given {@link modelName} is meant to be saved */
const modelSavePath = (modelName: string) =>
path.join(userDataPath(), "models", modelName);