feat: add model status information
This commit is contained in:
parent
76717dd200
commit
435f0f8e1e
|
@ -51,30 +51,41 @@ namespace NoSoliciting.Ml {
|
|||
}
|
||||
|
||||
public static async Task<MlFilter?> Load(Plugin plugin) {
|
||||
plugin.MlStatus = MlFilterStatus.DownloadingManifest;
|
||||
|
||||
var manifest = await DownloadManifest();
|
||||
if (manifest == null) {
|
||||
return null;
|
||||
PluginLog.LogWarning("Could not download manifest. Will attempt to fall back on cached version.");
|
||||
}
|
||||
|
||||
byte[]? data = null;
|
||||
|
||||
var localManifest = LoadCachedManifest(plugin);
|
||||
if (localManifest != null && localManifest.Version == manifest.Item1.Version) {
|
||||
if (localManifest != null && (manifest?.Item1 == null || localManifest.Version == manifest.Item1.Version)) {
|
||||
try {
|
||||
data = File.ReadAllBytes(CachedFilePath(plugin, ModelName));
|
||||
manifest ??= Tuple.Create(localManifest, string.Empty);
|
||||
} catch (IOException) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
data ??= await DownloadModel(manifest.Item1.ModelUrl);
|
||||
if (!string.IsNullOrEmpty(manifest?.Item2)) {
|
||||
plugin.MlStatus = MlFilterStatus.DownloadingModel;
|
||||
data ??= await DownloadModel(manifest!.Item1!.ModelUrl);
|
||||
}
|
||||
|
||||
if (data == null) {
|
||||
plugin.MlStatus = MlFilterStatus.Uninitialised;
|
||||
return null;
|
||||
}
|
||||
|
||||
UpdateCachedFile(plugin, ModelName, data);
|
||||
UpdateCachedFile(plugin, ManifestName, Encoding.UTF8.GetBytes(manifest.Item2));
|
||||
plugin.MlStatus = MlFilterStatus.Initialising;
|
||||
|
||||
if (!string.IsNullOrEmpty(manifest!.Item2)) {
|
||||
UpdateCachedFile(plugin, ModelName, data);
|
||||
UpdateCachedFile(plugin, ManifestName, Encoding.UTF8.GetBytes(manifest.Item2));
|
||||
}
|
||||
|
||||
var pluginFolder = Util.PluginFolder(plugin);
|
||||
|
||||
|
@ -89,8 +100,8 @@ namespace NoSoliciting.Ml {
|
|||
var client = await CreateClassifierClient(data);
|
||||
|
||||
return new MlFilter(
|
||||
manifest.Item1.Version,
|
||||
manifest.Item1.ReportUrl,
|
||||
manifest.Item1!.Version,
|
||||
manifest.Item1!.ReportUrl,
|
||||
process!,
|
||||
client
|
||||
);
|
||||
|
@ -235,4 +246,27 @@ namespace NoSoliciting.Ml {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum MlFilterStatus {
|
||||
Uninitialised,
|
||||
Preparing,
|
||||
DownloadingManifest,
|
||||
DownloadingModel,
|
||||
Initialising,
|
||||
Initialised,
|
||||
}
|
||||
|
||||
public static class MlFilterStatusExt {
|
||||
public static string Description(this MlFilterStatus status) {
|
||||
return status switch {
|
||||
MlFilterStatus.Uninitialised => "Uninitialised",
|
||||
MlFilterStatus.Preparing => "Preparing to update model",
|
||||
MlFilterStatus.DownloadingManifest => "Downloading model manifest",
|
||||
MlFilterStatus.DownloadingModel => "Downloading model",
|
||||
MlFilterStatus.Initialising => "Initialising model and classifier",
|
||||
MlFilterStatus.Initialised => "Initialised",
|
||||
_ => status.ToString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ namespace NoSoliciting {
|
|||
public PluginUi Ui { get; private set; } = null!;
|
||||
public Commands Commands { get; private set; } = null!;
|
||||
public Definitions? Definitions { get; private set; }
|
||||
public MlFilterStatus MlStatus { get; set; } = MlFilterStatus.Uninitialised;
|
||||
public MlFilter? MlFilter { get; set; }
|
||||
public bool MlReady => this.Config.UseMachineLearning && this.MlFilter != null;
|
||||
public bool DefsReady => !this.Config.UseMachineLearning && this.Definitions != null;
|
||||
|
@ -79,9 +80,13 @@ namespace NoSoliciting {
|
|||
|
||||
Task.Run(async () => this.MlFilter = await MlFilter.Load(this))
|
||||
.ContinueWith(e => {
|
||||
if (!e.IsFaulted) {
|
||||
PluginLog.Log("Machine learning model loaded");
|
||||
if (e.IsFaulted) {
|
||||
this.MlStatus = MlFilterStatus.Uninitialised;
|
||||
return;
|
||||
}
|
||||
|
||||
this.MlStatus = MlFilterStatus.Initialised;
|
||||
PluginLog.Log("Machine learning model loaded");
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -226,15 +226,20 @@ namespace NoSoliciting {
|
|||
|
||||
if (ImGui.BeginTabItem("Model")) {
|
||||
ImGui.TextUnformatted($"Version: {this.Plugin.MlFilter?.Version}");
|
||||
ImGui.TextUnformatted($"Model status: {this.Plugin.MlStatus.Description()}");
|
||||
var lastError = MlFilter.LastError;
|
||||
if (lastError != null) {
|
||||
ImGui.TextUnformatted($"Last error: {lastError}");
|
||||
}
|
||||
|
||||
if (ImGui.Button("Update model")) {
|
||||
this.Plugin.MlFilter?.Dispose();
|
||||
this.Plugin.MlFilter = null;
|
||||
this.Plugin.InitialiseMachineLearning();
|
||||
// prevent issues when people spam the button
|
||||
if (this.Plugin.MlStatus == MlFilterStatus.Uninitialised || this.Plugin.MlStatus == MlFilterStatus.Initialised) {
|
||||
this.Plugin.MlFilter?.Dispose();
|
||||
this.Plugin.MlFilter = null;
|
||||
this.Plugin.MlStatus = MlFilterStatus.Uninitialised;
|
||||
this.Plugin.InitialiseMachineLearning();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.EndTabItem();
|
||||
|
|
Loading…
Reference in New Issue