Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save Models in baseModel subfolder #230

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ public class FileFormatData : AutoConfiguration
[ConfigComment("If true, folders will be discarded from starred image paths.")]
public bool StarNoFolders = false;

[ConfigComment("Whether to automatically use the base model type as part of the model path when downloading using the 'Model Download' tool")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3x minor doc nits:

  • . at end of the sentence
  • also it's the Model Downloader utility
  • Also doc here should ideally be a bit more clean what it does - something like \nFor example, SDXL models will be placed automatically into an SDXL/` directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it

public bool GroupDownloadedModelsByBaseType = false;

public class ThemesImpl : SettingsOptionsAttribute.AbstractImpl
{
public override string[] GetOptions => [.. Program.Web.RegisteredThemes.Keys];
Expand Down
35 changes: 30 additions & 5 deletions src/WebAPI/ModelsAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,23 @@ public static async Task<JObject> DoModelDownloadWS(Session session, WebSocket w
}
try
{
string outPath = $"{handler.FolderPaths[0]}/{name}.safetensors";
if (File.Exists(outPath))
string baseModel = "";
if (!string.IsNullOrWhiteSpace(metadata))
{
JObject metadataObj = JObject.Parse(metadata);
baseModel = metadataObj["modelspec.baseModel"]?.ToString() ?? "";
}
string modelOutPath;
if (!string.IsNullOrWhiteSpace(baseModel) && session.User.Settings.GroupDownloadedModelsByBaseType)
{
modelOutPath = $"{handler.FolderPaths[0]}/{baseModel}/{name}.safetensors";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is just editing the folder path, it should be implemented in the utiltab.js rather than the C# here.
On top of avoiding cleaning/handling/API stability issues, that also gives the perk that you can just emit the folder prefix into the existing folder dropdown field, ie allowing users to edit the path on a case-by-case basis whenever they want

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I get access to user settings from the js side? On the C# side I could just reach for the user session pretty easily. I know the JS side must have a helper method for that as well, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/wwwroot/js/genpage/settings_editor.js getUserSetting maybe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah getUserSetting

}
else
{
modelOutPath = $"{handler.FolderPaths[0]}/{name}.safetensors";
}
modelOutPath = Utilities.StrictFilenameClean(modelOutPath);
if (File.Exists(modelOutPath))
{
await ws.SendJson(new JObject() { ["error"] = "Model at that save path already exists." }, API.WebsocketTimeout);
return null;
Expand All @@ -516,7 +531,7 @@ public static async Task<JObject> DoModelDownloadWS(Session session, WebSocket w
{
File.Delete(tempPath);
}
Directory.CreateDirectory(Path.GetDirectoryName(outPath));
Directory.CreateDirectory(Path.GetDirectoryName(modelOutPath));
using CancellationTokenSource canceller = new();
Task downloading = Utilities.DownloadFile(url, tempPath, (progress, total, perSec) =>
{
Expand Down Expand Up @@ -556,10 +571,20 @@ public static async Task<JObject> DoModelDownloadWS(Session session, WebSocket w
}
});
await downloading;
File.Move(tempPath, outPath);
File.Move(tempPath, modelOutPath);
if (!string.IsNullOrWhiteSpace(metadata))
{
File.WriteAllText($"{handler.FolderPaths[0]}/{name}.json", metadata);
string metadataOutPath;
if (!string.IsNullOrWhiteSpace(baseModel) && session.User.Settings.GroupDownloadedModelsByBaseType)
{
metadataOutPath = $"{handler.FolderPaths[0]}/{baseModel}/{name}.json";
}
else
{
metadataOutPath = $"{handler.FolderPaths[0]}/{name}.json";
}
metadataOutPath = Utilities.StrictFilenameClean(metadataOutPath);
File.WriteAllText(metadataOutPath, metadata);
}
await ws.SendJson(new JObject() { ["success"] = true }, API.WebsocketTimeout);
}
Expand Down
1 change: 1 addition & 0 deletions src/wwwroot/js/genpage/utiltab.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class ModelDownloaderUtil {
'modelspec.author': rawData.creator.username,
'modelspec.description': `From <a href="${url}">${url}</a>\n${rawVersion.description || ''}\n${rawData.description}\n`,
'modelspec.date': rawVersion.createdAt,
'modelspec.baseModel': rawVersion.baseModel,
};
if (rawVersion.trainedWords) {
metadata['modelspec.trigger_phrase'] = rawVersion.trainedWords.join(", ");
Expand Down