From 14346574573e6a8ed90df644d6b0d373ff799386 Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Thu, 14 Jan 2021 15:28:31 +0100 Subject: [PATCH] Added button to delete cached models --- Code/Forms/SettingsForm.Designer.cs | 64 +++++++++++++++++++++-------- Code/Forms/SettingsForm.cs | 21 ++++++++++ Code/IO/ModelDownloader.cs | 40 +++++++++++++++--- 3 files changed, 103 insertions(+), 22 deletions(-) diff --git a/Code/Forms/SettingsForm.Designer.cs b/Code/Forms/SettingsForm.Designer.cs index 253b2a8..7e6441a 100644 --- a/Code/Forms/SettingsForm.Designer.cs +++ b/Code/Forms/SettingsForm.Designer.cs @@ -32,6 +32,8 @@ System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SettingsForm)); this.settingsTabList = new Cyotek.Windows.Forms.TabList(); this.generalTab = new Cyotek.Windows.Forms.TabListPage(); + this.modelSuffix = new System.Windows.Forms.CheckBox(); + this.label63 = new System.Windows.Forms.Label(); this.label62 = new System.Windows.Forms.Label(); this.clearLogOnInput = new System.Windows.Forms.CheckBox(); this.label61 = new System.Windows.Forms.Label(); @@ -157,8 +159,8 @@ this.cmdDebugMode = new System.Windows.Forms.ComboBox(); this.titleLabel = new System.Windows.Forms.Label(); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); - this.label63 = new System.Windows.Forms.Label(); - this.modelSuffix = new System.Windows.Forms.CheckBox(); + this.label64 = new System.Windows.Forms.Label(); + this.clearModelCacheBtn = new HTAlt.WinForms.HTButton(); this.settingsTabList.SuspendLayout(); this.generalTab.SuspendLayout(); this.tabListPage2.SuspendLayout(); @@ -186,6 +188,8 @@ // generalTab // this.generalTab.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(48)))), ((int)(((byte)(48))))); + this.generalTab.Controls.Add(this.clearModelCacheBtn); + this.generalTab.Controls.Add(this.label64); this.generalTab.Controls.Add(this.modelSuffix); this.generalTab.Controls.Add(this.label63); this.generalTab.Controls.Add(this.label62); @@ -209,6 +213,25 @@ this.generalTab.Size = new System.Drawing.Size(762, 419); this.generalTab.Text = "General"; // + // modelSuffix + // + this.modelSuffix.AutoSize = true; + this.modelSuffix.Location = new System.Drawing.Point(280, 191); + this.modelSuffix.Name = "modelSuffix"; + this.modelSuffix.Size = new System.Drawing.Size(15, 14); + this.modelSuffix.TabIndex = 77; + this.modelSuffix.UseVisualStyleBackColor = true; + // + // label63 + // + this.label63.AutoSize = true; + this.label63.Location = new System.Drawing.Point(10, 191); + this.label63.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7); + this.label63.Name = "label63"; + this.label63.Size = new System.Drawing.Size(210, 13); + this.label63.TabIndex = 76; + this.label63.Text = "Include AI Model Name In Output Filename"; + // // label62 // this.label62.AutoSize = true; @@ -1752,24 +1775,29 @@ this.titleLabel.TabIndex = 1; this.titleLabel.Text = "Settings"; // - // label63 + // label64 // - this.label63.AutoSize = true; - this.label63.Location = new System.Drawing.Point(10, 191); - this.label63.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7); - this.label63.Name = "label63"; - this.label63.Size = new System.Drawing.Size(210, 13); - this.label63.TabIndex = 76; - this.label63.Text = "Include AI Model Name In Output Filename"; + this.label64.AutoSize = true; + this.label64.Location = new System.Drawing.Point(10, 221); + this.label64.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7); + this.label64.Name = "label64"; + this.label64.Size = new System.Drawing.Size(157, 13); + this.label64.TabIndex = 78; + this.label64.Text = "Delete Downloaded Model Files"; // - // modelSuffix + // clearModelCacheBtn // - this.modelSuffix.AutoSize = true; - this.modelSuffix.Location = new System.Drawing.Point(280, 191); - this.modelSuffix.Name = "modelSuffix"; - this.modelSuffix.Size = new System.Drawing.Size(15, 14); - this.modelSuffix.TabIndex = 77; - this.modelSuffix.UseVisualStyleBackColor = true; + this.clearModelCacheBtn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); + this.clearModelCacheBtn.FlatAppearance.BorderSize = 0; + this.clearModelCacheBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.clearModelCacheBtn.ForeColor = System.Drawing.Color.White; + this.clearModelCacheBtn.Location = new System.Drawing.Point(280, 216); + this.clearModelCacheBtn.Name = "clearModelCacheBtn"; + this.clearModelCacheBtn.Size = new System.Drawing.Size(206, 23); + this.clearModelCacheBtn.TabIndex = 79; + this.clearModelCacheBtn.Text = "Clear Model Cache"; + this.clearModelCacheBtn.UseVisualStyleBackColor = false; + this.clearModelCacheBtn.Click += new System.EventHandler(this.clearModelCacheBtn_Click); // // SettingsForm // @@ -1938,5 +1966,7 @@ private System.Windows.Forms.Label label61; private System.Windows.Forms.CheckBox modelSuffix; private System.Windows.Forms.Label label63; + private System.Windows.Forms.Label label64; + private HTAlt.WinForms.HTButton clearModelCacheBtn; } } \ No newline at end of file diff --git a/Code/Forms/SettingsForm.cs b/Code/Forms/SettingsForm.cs index b9c2e40..ae814a7 100644 --- a/Code/Forms/SettingsForm.cs +++ b/Code/Forms/SettingsForm.cs @@ -1,4 +1,5 @@ using Flowframes.IO; +using Flowframes.MiscUtils; using Microsoft.WindowsAPICodePack.Dialogs; using System; using System.Collections.Generic; @@ -25,6 +26,20 @@ namespace Flowframes.Forms { LoadSettings(); initialized = true; + LazyLoadingStuff(); + } + + public async Task LazyLoadingStuff () + { + long modelFoldersBytes = 0; + + foreach (string modelFolder in ModelDownloader.GetAllModelFolders()) + modelFoldersBytes += IOUtils.GetDirSize(modelFolder, true); + + if (modelFoldersBytes > 1024 * 1024) + clearModelCacheBtn.Text = $"Clear Model Cache ({FormatUtils.Bytes(modelFoldersBytes)})"; + else + clearModelCacheBtn.Enabled = false; } private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e) @@ -175,5 +190,11 @@ namespace Flowframes.Forms magickDedupePanel.Visible = dedupMode.SelectedIndex == 1; mpDedupePanel.Visible = dedupMode.SelectedIndex == 2; } + + private void clearModelCacheBtn_Click(object sender, EventArgs e) + { + ModelDownloader.DeleteAllModels(); + clearModelCacheBtn.Text = "Clear Model Cache"; + } } } diff --git a/Code/IO/ModelDownloader.cs b/Code/IO/ModelDownloader.cs index 14b1474..75fcb6a 100644 --- a/Code/IO/ModelDownloader.cs +++ b/Code/IO/ModelDownloader.cs @@ -1,4 +1,6 @@ -using System; +using Flowframes.Data; +using Flowframes.MiscUtils; +using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -100,14 +102,10 @@ namespace Flowframes.IO Logger.Log($"Downloading '{model}' model files..."); Directory.CreateDirectory(mdlDir); await DownloadTo(GetMdlFileUrl(ai, model, "md5.txt"), mdlDir); - Dictionary fileList = await GetFilelist(ai, model); foreach (KeyValuePair modelFile in fileList) - { - //Logger.Log($"Downloading '{model}' model files: '{modelFile.Key}'...", false, true); await DownloadTo(GetMdlFileUrl(ai, model, modelFile.Key), mdlDir); - } Logger.Log($"Downloaded \"{model}\" model files.", false, true); } @@ -118,6 +116,38 @@ namespace Flowframes.IO } } + public static void DeleteAllModels () + { + foreach(string modelFolder in GetAllModelFolders()) + { + string size = FormatUtils.Bytes(IOUtils.GetDirSize(modelFolder, true)); + if (IOUtils.TryDeleteIfExists(modelFolder)) + Logger.Log($"Deleted cached model '{Path.GetFileName(modelFolder.GetParentDir())}/{Path.GetFileName(modelFolder)}' ({size})"); + } + } + + public static List GetAllModelFolders() + { + List modelPaths = new List(); + + foreach (AI ai in Networks.networks) + { + string aiPkgFolder = PkgUtils.GetPkgFolder(ai.pkg); + string modelsFile = Path.Combine(aiPkgFolder, "models.txt"); + if (!File.Exists(modelsFile)) continue; + + foreach (string mdl in IOUtils.ReadLines(modelsFile)) + { + string modelName = mdl.Split('-')[0].Remove(" ").Remove("."); + string mdlFolder = Path.Combine(aiPkgFolder, modelName); + if (!Directory.Exists(mdlFolder)) continue; + modelPaths.Add(mdlFolder); + } + } + + return modelPaths; + } + public static bool AreFilesValid (string ai, string model) { string mdlDir = GetLocalPath(ai, model);