Ionite
1 year ago
25 changed files with 382 additions and 157 deletions
@ -0,0 +1,10 @@
|
||||
using System.Text.Json.Nodes; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Models; |
||||
|
||||
public interface IJsonLoadableState |
||||
{ |
||||
void LoadStateFromJsonObject(JsonObject state); |
||||
|
||||
JsonObject SaveStateToJsonObject(); |
||||
} |
@ -1,14 +1,13 @@
|
||||
using System.Text.Json.Serialization; |
||||
using System.Text.Json.Nodes; |
||||
using System.Text.Json.Serialization; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Models.Inference; |
||||
|
||||
[JsonSerializable(typeof(InferenceTextToImageModel))] |
||||
public class InferenceTextToImageModel |
||||
{ |
||||
public string? Prompt { get; init; } |
||||
public string? NegativePrompt { get; init; } |
||||
public string? SelectedModelName { get; init; } |
||||
public SeedCardModel? SeedCardState { get; init; } |
||||
public SamplerCardModel? SamplerCardState { get; init; } |
||||
public PromptCardModel? PromptCardState { get; init; } |
||||
public JsonObject? SeedCardState { get; init; } |
||||
public JsonObject? PromptCardState { get; init; } |
||||
public JsonObject? StackCardState { get; init; } |
||||
} |
||||
|
@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic; |
||||
using System.Text.Json.Nodes; |
||||
using System.Text.Json.Serialization; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Models.Inference; |
||||
|
||||
[JsonSerializable(typeof(StackCardModel))] |
||||
public class StackCardModel |
||||
{ |
||||
public List<JsonObject>? Cards { get; init; } |
||||
} |
@ -0,0 +1,11 @@
|
||||
using System.Text.Json.Serialization; |
||||
using StabilityMatrix.Avalonia.ViewModels.Inference; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Models.Inference; |
||||
|
||||
[JsonSerializable(typeof(StackExpanderModel))] |
||||
public class StackExpanderModel : StackCardModel |
||||
{ |
||||
public string? Title { get; set; } |
||||
public bool IsEnabled { get; set; } |
||||
} |
@ -0,0 +1,11 @@
|
||||
using System.Text.Json.Serialization; |
||||
using StabilityMatrix.Core.Models.Api.Comfy; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Models.Inference; |
||||
|
||||
[JsonSerializable(typeof(UpscalerCardModel))] |
||||
public class UpscalerCardModel |
||||
{ |
||||
public double Scale { get; init; } = 1; |
||||
public ComfyUpscaler? SelectedUpscaler { get; init; } |
||||
} |
@ -1,31 +1,33 @@
|
||||
using AvaloniaEdit.Document; |
||||
using System.Text.Json.Nodes; |
||||
using AvaloniaEdit.Document; |
||||
using StabilityMatrix.Avalonia.Controls; |
||||
using StabilityMatrix.Avalonia.Models; |
||||
using StabilityMatrix.Avalonia.Models.Inference; |
||||
using StabilityMatrix.Core.Attributes; |
||||
|
||||
namespace StabilityMatrix.Avalonia.ViewModels.Inference; |
||||
|
||||
[View(typeof(PromptCard))] |
||||
public class PromptCardViewModel : ViewModelBase, ILoadableState<PromptCardModel> |
||||
public class PromptCardViewModel : LoadableViewModelBase |
||||
{ |
||||
public TextDocument PromptDocument { get; } = new(); |
||||
public TextDocument NegativePromptDocument { get; } = new(); |
||||
|
||||
/// <inheritdoc /> |
||||
public void LoadState(PromptCardModel state) |
||||
public override void LoadStateFromJsonObject(JsonObject state) |
||||
{ |
||||
PromptDocument.Text = state.Prompt ?? ""; |
||||
NegativePromptDocument.Text = state.NegativePrompt ?? ""; |
||||
var model = DeserializeModel<PromptCardModel>(state); |
||||
|
||||
PromptDocument.Text = model.Prompt ?? ""; |
||||
NegativePromptDocument.Text = model.NegativePrompt ?? ""; |
||||
} |
||||
|
||||
/// <inheritdoc /> |
||||
public PromptCardModel SaveState() |
||||
public override JsonObject SaveStateToJsonObject() |
||||
{ |
||||
return new PromptCardModel |
||||
return SerializeModel(new PromptCardModel |
||||
{ |
||||
Prompt = PromptDocument.Text, |
||||
NegativePrompt = NegativePromptDocument.Text |
||||
}; |
||||
}); |
||||
} |
||||
} |
||||
|
@ -1,46 +1,37 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using Avalonia.Collections; |
||||
using System.Linq; |
||||
using System.Text.Json.Nodes; |
||||
using StabilityMatrix.Avalonia.Controls; |
||||
using StabilityMatrix.Avalonia.Models.Inference; |
||||
using StabilityMatrix.Core.Attributes; |
||||
using StabilityMatrix.Core.Extensions; |
||||
|
||||
namespace StabilityMatrix.Avalonia.ViewModels.Inference; |
||||
|
||||
[View(typeof(StackCard))] |
||||
public class StackCardViewModel : ViewModelBase |
||||
public class StackCardViewModel : StackViewModelBase |
||||
{ |
||||
private readonly Dictionary<Type, List<ViewModelBase>> viewModelManager = new(); |
||||
|
||||
public AvaloniaList<ViewModelBase> ConfigCards { get; } = new(); |
||||
|
||||
/// <summary> |
||||
/// Register new cards |
||||
/// </summary> |
||||
public void AddCards(IEnumerable<ViewModelBase> cards) |
||||
/// <inheritdoc /> |
||||
public override void LoadStateFromJsonObject(JsonObject state) |
||||
{ |
||||
foreach (var card in cards) |
||||
var model = DeserializeModel<StackCardModel>(state); |
||||
|
||||
if (model.Cards is null) return; |
||||
|
||||
foreach (var (i, card) in model.Cards.Enumerate()) |
||||
{ |
||||
var list = viewModelManager.GetOrAdd(card.GetType()); |
||||
list.Add(card); |
||||
ConfigCards.Add(card); |
||||
// Ignore if more than cards than we have |
||||
if (i > Cards.Count - 1) break; |
||||
|
||||
Cards[i].LoadStateFromJsonObject(card); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Registers new cards and returns self |
||||
/// </summary> |
||||
public StackCardViewModel WithCards(IEnumerable<ViewModelBase> cards) |
||||
{ |
||||
AddCards(cards); |
||||
return this; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets a card by type at specified index |
||||
/// </summary> |
||||
public T GetCard<T>(int index = 0) where T : ViewModelBase |
||||
|
||||
/// <inheritdoc /> |
||||
public override JsonObject SaveStateToJsonObject() |
||||
{ |
||||
return (T) viewModelManager[typeof(T)][index]; |
||||
return SerializeModel(new StackCardModel |
||||
{ |
||||
Cards = Cards.Select(x => x.SaveStateToJsonObject()).ToList() |
||||
}); |
||||
} |
||||
} |
||||
|
@ -1,12 +1,45 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel; |
||||
using System.Linq; |
||||
using System.Text.Json.Nodes; |
||||
using CommunityToolkit.Mvvm.ComponentModel; |
||||
using StabilityMatrix.Avalonia.Controls; |
||||
using StabilityMatrix.Avalonia.Models.Inference; |
||||
using StabilityMatrix.Core.Attributes; |
||||
using StabilityMatrix.Core.Extensions; |
||||
|
||||
namespace StabilityMatrix.Avalonia.ViewModels.Inference; |
||||
|
||||
[View(typeof(StackExpander))] |
||||
public partial class StackExpanderViewModel : StackCardViewModel |
||||
public partial class StackExpanderViewModel : StackViewModelBase |
||||
{ |
||||
[ObservableProperty] private string? title; |
||||
[ObservableProperty] private bool isEnabled; |
||||
|
||||
/// <inheritdoc /> |
||||
public override void LoadStateFromJsonObject(JsonObject state) |
||||
{ |
||||
var model = DeserializeModel<StackExpanderModel>(state); |
||||
Title = model.Title; |
||||
IsEnabled = model.IsEnabled; |
||||
|
||||
if (model.Cards is null) return; |
||||
|
||||
foreach (var (i, card) in model.Cards.Enumerate()) |
||||
{ |
||||
// Ignore if more than cards than we have |
||||
if (i > Cards.Count - 1) break; |
||||
|
||||
Cards[i].LoadStateFromJsonObject(card); |
||||
} |
||||
} |
||||
|
||||
/// <inheritdoc /> |
||||
public override JsonObject SaveStateToJsonObject() |
||||
{ |
||||
return SerializeModel(new StackExpanderModel |
||||
{ |
||||
Title = Title, |
||||
IsEnabled = IsEnabled, |
||||
Cards = Cards.Select(x => x.SaveStateToJsonObject()).ToList() |
||||
}); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,43 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using Avalonia.Collections; |
||||
using StabilityMatrix.Core.Extensions; |
||||
|
||||
namespace StabilityMatrix.Avalonia.ViewModels.Inference; |
||||
|
||||
public abstract class StackViewModelBase : LoadableViewModelBase |
||||
{ |
||||
private readonly Dictionary<Type, List<LoadableViewModelBase>> viewModelManager = new(); |
||||
|
||||
public AvaloniaList<LoadableViewModelBase> Cards { get; } = new(); |
||||
|
||||
/// <summary> |
||||
/// Register new cards |
||||
/// </summary> |
||||
public void AddCards(IEnumerable<LoadableViewModelBase> cards) |
||||
{ |
||||
foreach (var card in cards) |
||||
{ |
||||
var list = viewModelManager.GetOrAdd(card.GetType()); |
||||
list.Add(card); |
||||
Cards.Add(card); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Registers new cards and returns self |
||||
/// </summary> |
||||
public StackViewModelBase WithCards(IEnumerable<LoadableViewModelBase> cards) |
||||
{ |
||||
AddCards(cards); |
||||
return this; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets a card by type at specified index |
||||
/// </summary> |
||||
public T GetCard<T>(int index = 0) where T : LoadableViewModelBase |
||||
{ |
||||
return (T) viewModelManager[typeof(T)][index]; |
||||
} |
||||
} |
@ -1,11 +1,43 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel; |
||||
using System.Text.Json.Nodes; |
||||
using CommunityToolkit.Mvvm.ComponentModel; |
||||
using StabilityMatrix.Avalonia.Controls; |
||||
using StabilityMatrix.Avalonia.Models.Inference; |
||||
using StabilityMatrix.Avalonia.Services; |
||||
using StabilityMatrix.Core.Attributes; |
||||
using StabilityMatrix.Core.Models.Api.Comfy; |
||||
|
||||
namespace StabilityMatrix.Avalonia.ViewModels.Inference; |
||||
|
||||
[View(typeof(UpscalerCard))] |
||||
public partial class UpscalerCardViewModel : ViewModelBase |
||||
public partial class UpscalerCardViewModel : LoadableViewModelBase |
||||
{ |
||||
[ObservableProperty] private double scale = 1; |
||||
|
||||
[ObservableProperty] private ComfyUpscaler? selectedUpscaler; |
||||
|
||||
public IInferenceClientManager ClientManager { get; } |
||||
|
||||
public UpscalerCardViewModel(IInferenceClientManager clientManager) |
||||
{ |
||||
ClientManager = clientManager; |
||||
} |
||||
|
||||
/// <inheritdoc /> |
||||
public override void LoadStateFromJsonObject(JsonObject state) |
||||
{ |
||||
var model = DeserializeModel<UpscalerCardModel>(state); |
||||
|
||||
Scale = model.Scale; |
||||
SelectedUpscaler = model.SelectedUpscaler; |
||||
} |
||||
|
||||
/// <inheritdoc /> |
||||
public override JsonObject SaveStateToJsonObject() |
||||
{ |
||||
return SerializeModel(new UpscalerCardModel |
||||
{ |
||||
Scale = Scale, |
||||
SelectedUpscaler = SelectedUpscaler |
||||
}); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,34 @@
|
||||
using System; |
||||
using System.Text.Json; |
||||
using System.Text.Json.Nodes; |
||||
using StabilityMatrix.Avalonia.Models; |
||||
|
||||
namespace StabilityMatrix.Avalonia.ViewModels; |
||||
|
||||
public abstract class LoadableViewModelBase : ViewModelBase, IJsonLoadableState |
||||
{ |
||||
/// <inheritdoc /> |
||||
public abstract void LoadStateFromJsonObject(JsonObject state); |
||||
|
||||
/// <inheritdoc /> |
||||
public abstract JsonObject SaveStateToJsonObject(); |
||||
|
||||
/// <summary> |
||||
/// Serialize a model to a JSON object. |
||||
/// </summary> |
||||
protected static JsonObject SerializeModel<T>(T model) |
||||
{ |
||||
var node = JsonSerializer.SerializeToNode(model); |
||||
return node?.AsObject() ?? throw new |
||||
NullReferenceException("Failed to serialize state to JSON object."); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Deserialize a model from a JSON object. |
||||
/// </summary> |
||||
protected static T DeserializeModel<T>(JsonObject state) |
||||
{ |
||||
return state.Deserialize<T>() ?? throw new |
||||
NullReferenceException("Failed to deserialize state from JSON object."); |
||||
} |
||||
} |
Loading…
Reference in new issue