-
-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathLocalModelFile.cs
More file actions
214 lines (178 loc) · 6.7 KB
/
LocalModelFile.cs
File metadata and controls
214 lines (178 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System.Diagnostics.CodeAnalysis;
using LiteDB;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Models.Api;
namespace StabilityMatrix.Core.Models.Database;
/// <summary>
/// Represents a locally indexed model file.
/// </summary>
public record LocalModelFile
{
private sealed class RelativePathConnectedModelInfoEqualityComparer : IEqualityComparer<LocalModelFile>
{
public bool Equals(LocalModelFile? x, LocalModelFile? y)
{
if (ReferenceEquals(x, y))
return true;
if (ReferenceEquals(x, null))
return false;
if (ReferenceEquals(y, null))
return false;
if (x.GetType() != y.GetType())
return false;
return x.RelativePath == y.RelativePath
&& Equals(x.ConnectedModelInfo, y.ConnectedModelInfo)
&& x.HasUpdate == y.HasUpdate;
}
public int GetHashCode(LocalModelFile obj)
{
return HashCode.Combine(obj.RelativePath, obj.ConnectedModelInfo, obj.HasUpdate);
}
}
public static IEqualityComparer<LocalModelFile> RelativePathConnectedModelInfoComparer { get; } =
new RelativePathConnectedModelInfoEqualityComparer();
public virtual bool Equals(LocalModelFile? other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return RelativePath == other.RelativePath
&& Equals(ConnectedModelInfo, other.ConnectedModelInfo)
&& HasUpdate == other.HasUpdate;
}
public override int GetHashCode()
{
return HashCode.Combine(RelativePath, ConnectedModelInfo, HasUpdate);
}
/// <summary>
/// Relative path to the file from the root model directory.
/// </summary>
[BsonId]
public required string RelativePath { get; init; }
/// <summary>
/// Type of the model file.
/// </summary>
public required SharedFolderType SharedFolderType { get; set; }
/// <summary>
/// Optional connected model information.
/// </summary>
public ConnectedModelInfo? ConnectedModelInfo { get; set; }
/// <summary>
/// Optional preview image relative path.
/// </summary>
public string? PreviewImageRelativePath { get; set; }
/// <summary>
/// Optional preview image full path. Takes priority over <see cref="PreviewImageRelativePath"/>.
/// </summary>
public string? PreviewImageFullPath { get; set; }
/// <summary>
/// Optional full path to the model's configuration (.yaml) file.
/// </summary>
public string? ConfigFullPath { get; set; }
/// <summary>
/// Whether or not an update is available for this model
/// </summary>
public bool HasUpdate { get; set; }
/// <summary>
/// Last time this model was checked for an update
/// </summary>
public DateTimeOffset LastUpdateCheck { get; set; }
/// <summary>
/// The latest CivitModel info
/// </summary>
[BsonRef("CivitModels")]
public CivitModel? LatestModelInfo { get; set; }
/// <summary>
/// File name of the relative path.
/// </summary>
[BsonIgnore]
public string FileName => Path.GetFileName(RelativePath);
/// <summary>
/// File name of the relative path without extension.
/// </summary>
[BsonIgnore]
public string FileNameWithoutExtension => Path.GetFileNameWithoutExtension(RelativePath);
/// <summary>
/// Relative file path from the shared folder type model directory.
/// </summary>
[BsonIgnore]
public string RelativePathFromSharedFolder =>
Path.GetRelativePath(SharedFolderType.GetStringValue(), RelativePath);
/// <summary>
/// Blake3 hash of the file.
/// </summary>
public string? HashBlake3 => ConnectedModelInfo?.Hashes?.BLAKE3;
[BsonIgnore]
public bool IsSafetensorFile => Path.GetExtension(RelativePath) == ".safetensors";
[BsonIgnore]
public string? PreviewImageFullPathGlobal =>
PreviewImageFullPath ?? GetPreviewImageFullPath(GlobalConfig.ModelsDir);
[BsonIgnore]
public Uri? PreviewImageUriGlobal =>
PreviewImageFullPathGlobal == null ? null : new Uri(PreviewImageFullPathGlobal);
[BsonIgnore]
public string DisplayModelName => ConnectedModelInfo?.ModelName ?? FileNameWithoutExtension;
[BsonIgnore]
public string DisplayModelVersion => ConnectedModelInfo?.VersionName ?? string.Empty;
[BsonIgnore]
public string DisplayModelFileName => FileName;
[BsonIgnore]
public string DisplayConfigFileName => Path.GetFileName(ConfigFullPath) ?? string.Empty;
[BsonIgnore]
[MemberNotNullWhen(true, nameof(ConnectedModelInfo))]
public bool HasConnectedModel => ConnectedModelInfo != null;
[BsonIgnore]
[MemberNotNullWhen(true, nameof(ConnectedModelInfo))]
public bool HasCivitMetadata => HasConnectedModel && ConnectedModelInfo.ModelId != null;
public string GetFullPath(string rootModelDirectory)
{
return Path.Combine(rootModelDirectory, RelativePath);
}
public string? GetPreviewImageFullPath(string rootModelDirectory)
{
if (PreviewImageFullPath != null)
return PreviewImageFullPath;
return PreviewImageRelativePath == null
? null
: Path.Combine(rootModelDirectory, PreviewImageRelativePath);
}
public string GetConnectedModelInfoFullPath(string rootModelDirectory)
{
var modelNameNoExt = Path.GetFileNameWithoutExtension(RelativePath);
var modelParentDir = Path.GetDirectoryName(GetFullPath(rootModelDirectory)) ?? "";
return Path.Combine(modelParentDir, $"{modelNameNoExt}.cm-info.json");
}
public IEnumerable<string> GetDeleteFullPaths(string rootModelDirectory)
{
if (GetFullPath(rootModelDirectory) is { } filePath && File.Exists(filePath))
{
yield return filePath;
}
if (
HasConnectedModel
&& GetConnectedModelInfoFullPath(rootModelDirectory) is { } cmInfoPath
&& File.Exists(cmInfoPath)
)
{
yield return cmInfoPath;
}
var previewImagePath = GetPreviewImageFullPath(rootModelDirectory);
if (File.Exists(previewImagePath))
{
yield return previewImagePath;
}
}
public static readonly HashSet<string> SupportedCheckpointExtensions =
[
".safetensors",
".pt",
".ckpt",
".pth",
".bin",
".sft",
".gguf"
];
public static readonly HashSet<string> SupportedImageExtensions = [".png", ".jpg", ".jpeg", ".webp"];
public static readonly HashSet<string> SupportedMetadataExtensions = [".json"];
}