Skip to content

Commit

Permalink
Fix NovelAI metadata scanning error
Browse files Browse the repository at this point in the history
Add HasError flag to Image
Add Rescan Context menu option to thumbnails
Add Show in Explorer to folders
  • Loading branch information
RupertAvery committed Sep 21, 2024
1 parent 525c110 commit c8a7aa0
Show file tree
Hide file tree
Showing 31 changed files with 556 additions and 93 deletions.
2 changes: 1 addition & 1 deletion Diffusion.Database/DataStore.Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public IEnumerable<ImageView> Search(string? prompt, int pageSize, int offset, s
const string columns = "FolderId, FileName, Prompt, NegativePrompt, Steps, Sampler, " +
"CFGScale, Seed, Width, Height, ModelHash, Model, BatchSize, BatchPos, CreatedDate, ModifiedDate, " +
"CustomTags, Rating, Favorite, ForDeletion, NSFW, " +
"AestheticScore, HyperNetwork, HyperNetworkStrength, ClipSkip, ENSD, FileSize, NoMetadata";
"AestheticScore, HyperNetwork, HyperNetworkStrength, ClipSkip, ENSD, FileSize, NoMetadata, HasError";


public IEnumerable<ImageView> Search(Filter filter, int pageSize, int offset, string sortBy, string sortDirection)
Expand Down
2 changes: 2 additions & 0 deletions Diffusion.Database/DataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ await Task.Run(() =>
db.CreateIndex<Image>(image => image.NegativePrompt);
db.CreateIndex<Image>(image => image.Workflow);
db.CreateIndex<Image>(image => image.WorkflowId);
db.CreateIndex<Image>(image => image.HasError);

db.CreateIndex("Image", new[] { "HasError", "CreatedDate" });
db.CreateIndex("Image", new[] { "ForDeletion", "CreatedDate" });
db.CreateIndex("Image", new[] { "NSFW", "CreatedDate" });
db.CreateIndex("Image", new[] { "Unavailable", "CreatedDate" });
Expand Down
1 change: 1 addition & 0 deletions Diffusion.Database/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Image
public bool NoMetadata { get; set; }
public string? Workflow { get; set; }
public string? WorkflowId { get; set; }
public bool HasError { get; set; }
}


Expand Down
5 changes: 3 additions & 2 deletions Diffusion.Database/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3077,9 +3077,10 @@ public int ExecuteNonQuery ()
}

throw SQLiteException.New (r, SQLite3.GetErrmsg (_conn.Handle));
}

public IEnumerable<T> ExecuteDeferredQuery<T> ()
}

public IEnumerable<T> ExecuteDeferredQuery<T> ()
{
return ExecuteDeferredQuery<T> (_conn.GetMapping (typeof (T)));
}
Expand Down
25 changes: 17 additions & 8 deletions Diffusion.Scanner/ComfyUI.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
Expand All @@ -24,26 +25,30 @@ public override int GetHashCode()
{
hash = (hash * 397) ^ input.Name.GetHashCode();
}

return hash;
}
}

public class Input
{
public string WorkflowId { get; set; }
public string Path { get; set; }
public string Name { get; set; }
public object Value { get; set; }

public Input(string name, object value)
public Input(string workflowId, string path, string name, object value)
{
WorkflowId = workflowId;
Path = path;
Name = name;
Value = value;
}
}

public class ComfyUIParser
{
public IReadOnlyCollection<Node> Parse(string workflow)
public IReadOnlyCollection<Node> Parse(string workflowId, string workflow)
{
if (workflow == null) return null;
var root = System.Text.Json.JsonDocument.Parse(workflow);
Expand All @@ -58,12 +63,16 @@ public IReadOnlyCollection<Node> Parse(string workflow)

foreach (var props in element.Value.EnumerateObject())
{

if (props.Name == "inputs")
{
node.Inputs = new List<Input>();


foreach (var prop2 in props.Value.EnumerateObject())
{
var name = prop2.Name.Replace("_", "__");
string path = $"[{node.Id}].inputs[\"{prop2.Name}\"]";

switch (prop2.Value.ValueKind)
{
case JsonValueKind.Undefined:
Expand All @@ -73,16 +82,16 @@ public IReadOnlyCollection<Node> Parse(string workflow)
case JsonValueKind.Array:
break;
case JsonValueKind.String:
node.Inputs.Add(new Input(prop2.Name.Replace("_", "__"), prop2.Value.GetString()));
node.Inputs.Add(new Input(workflowId, path, name, prop2.Value.GetString()));
break;
case JsonValueKind.Number:
node.Inputs.Add(new Input(prop2.Name.Replace("_", "__"), prop2.Value.GetDouble()));
node.Inputs.Add(new Input(workflowId, path, name, prop2.Value.GetDouble()));
break;
case JsonValueKind.True:
node.Inputs.Add(new Input(prop2.Name.Replace("_", "__"), prop2.Value.GetBoolean()));
node.Inputs.Add(new Input(workflowId, path, name, prop2.Value.GetBoolean()));
break;
case JsonValueKind.False:
node.Inputs.Add(new Input(prop2.Name.Replace("_", "__"), prop2.Value.GetBoolean()));
node.Inputs.Add(new Input(workflowId, path, name, prop2.Value.GetBoolean()));
break;
case JsonValueKind.Null:
break;
Expand Down
4 changes: 4 additions & 0 deletions Diffusion.Scanner/FileParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public class FileParameters
public bool NoMetadata { get; set; }
public string? Workflow { get; set; }
public string? WorkflowId { get; set; }
public bool HasError { get; set; }


public string ErrorMessage { get; set; }
}
42 changes: 33 additions & 9 deletions Diffusion.Scanner/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ private static FileType GetFileType(Stream stream)
{
FileParameters? fileParameters = null;

try
{
fileParameters = Metadata.ReadFromFileInternal(file);
}
catch (Exception e)
{
Logger.Log($"An error occurred while reading {file}: {e.Message}\r\n\r\n{e.StackTrace}");
fileParameters ??= new FileParameters();
fileParameters.HasError = true;
fileParameters.ErrorMessage = $"{e.Message}\r\n\r\n{e.StackTrace}";
}
finally
{
fileParameters ??= new FileParameters()
{
NoMetadata = true
};

FileInfo fileInfo = new FileInfo(file);
fileParameters.Path = file;
fileParameters.FileSize = fileInfo.Length;
}

return fileParameters;
}

public static FileParameters? ReadFromFileInternal(string file)
{
FileParameters? fileParameters = null;

var ext = Path.GetExtension(file).ToLowerInvariant();

using var stream = File.OpenRead(file);
Expand Down Expand Up @@ -124,9 +154,11 @@ private static FileType GetFileType(Stream stream)
}
else if (tag.Description.StartsWith("Comment:"))
{

if (fileParameters == null)
{
if (directory.Tags.Any(t => t.Description == "Software: NovelAI"))
//if (directory.Tags.Any(t => t.Description == "Software: NovelAI"))
if (directory.Tags.Any(t => t.Description == "Software: NovelAI") || directories.Any(d => d.Tags.Any(t => t.Description == "Software: NovelAI")))
{
format = MetaFormat.NovelAI;
fileParameters = ReadNovelAIParameters(file, directories);
Expand Down Expand Up @@ -381,14 +413,6 @@ private static FileType GetFileType(Stream stream)
}
}

fileParameters ??= new FileParameters()
{
NoMetadata = true
};

FileInfo fileInfo = new FileInfo(file);
fileParameters.Path = file;
fileParameters.FileSize = fileInfo.Length;

return fileParameters;
}
Expand Down
11 changes: 3 additions & 8 deletions Diffusion.Scanner/MetadataScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,15 @@ public static IEnumerable<string> GetFiles(string path, string extensions, HashS
return files;
}



public static IEnumerable<FileParameters> Scan(IEnumerable<string> files)
{
foreach (var file in files)
{
FileParameters? fp = null;

try
{
fp = Metadata.ReadFromFile(file);
}
catch (Exception e)
{
Logger.Log($"An error occurred while reading {file}: {e.Message}\r\n\r\n{e.StackTrace}");
}
fp = Metadata.ReadFromFile(file);

if (fp != null)
{
Expand Down
22 changes: 21 additions & 1 deletion Diffusion.Toolkit/Controls/ComfyNode.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,19 @@ public void BuildNode(Node node)
MaxHeight = 200,
Text = input.Value?.ToString()
};
var button = new Button()
{
DataContext = input,
FontSize = 12,
Width = 16,
Height = 16,
Content = "...",
};
label.SetBinding(Label.ContentProperty, "Name");
textBox.SetValue(Grid.ColumnProperty, 1);
textBox.SetBinding(TextBox.TextProperty, "Value");
button.SetValue(Grid.ColumnProperty, 2);
button.Click += ButtonOnClick;

var grid = new Grid()
{
Expand All @@ -61,11 +71,16 @@ public void BuildNode(Node node)
{
Width = new GridLength(100)
},
new ColumnDefinition(),
new ColumnDefinition()
{
Width = new GridLength(16)
},
},
Children = {
label,
textBox
textBox,
button
}
};

Expand All @@ -77,6 +92,11 @@ public void BuildNode(Node node)
Id = node.GetHashCode();
}

private void ButtonOnClick(object sender, RoutedEventArgs e)
{
//throw new System.NotImplementedException();
}

private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is Node node)
Expand Down
43 changes: 28 additions & 15 deletions Diffusion.Toolkit/Controls/MetadataPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
xmlns:local="clr-namespace:Diffusion.Toolkit.Controls"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
xmlns:converters="clr-namespace:Diffusion.Toolkit.Converters"
xmlns:common="clr-namespace:Diffusion.Toolkit.Common"
mc:Ignorable="d"
d:DesignHeight="776.386" d:DesignWidth="464.94">
<UserControl.Resources>
<converters:SizeConverter x:Key="SizeConverter"></converters:SizeConverter>
<converters:ZeroConverter x:Key="ZeroConverter"></converters:ZeroConverter>
<converters:BoolToVisibilityConverter x:Key="boolToVis"></converters:BoolToVisibilityConverter>
</UserControl.Resources>
<Grid Background="Transparent"
<Grid x:Name="Root" Background="Transparent"
DataContext="{Binding CurrentImage, RelativeSource={RelativeSource AncestorLevel=1, Mode=FindAncestor, AncestorType=UserControl}}">
<Grid.ContextMenu>
<ContextMenu>
Expand Down Expand Up @@ -188,13 +190,24 @@
HeaderBackground="#80000000"
State="{Binding DataContext.MetadataSection.AlbumState, ElementName=PreviewPaneGrid, Mode=TwoWay}" Header="{lex:Loc Metadata.Albums}">
<ItemsControl ItemsSource="{Binding Albums}">
<ItemsControl.Resources>
<common:BindingProxy x:Key="Proxy" Data="{Binding .}" />
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="{StaticResource SecondaryBrush}" CornerRadius="5" Margin="5">
<TextBox FontSize="14" BorderThickness="0"
IsReadOnly="True" Padding="5" Background="Transparent"
TextWrapping="WrapWithOverflow" Text="{Binding Name}"
PreviewMouseDown="UIElement_OnMouseDown" Cursor="Hand"/>
<Border BorderThickness="1" BorderBrush="{StaticResource SecondaryBrush}" CornerRadius="5" Margin="5"
Tag="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}}"
>
<StackPanel Orientation="Horizontal">
<TextBox FontSize="14" BorderThickness="0"
IsReadOnly="True" Padding="5" Background="Transparent"
TextWrapping="WrapWithOverflow" Text="{Binding Name}"
PreviewMouseDown="AlbumName_OnMouseDown" Cursor="Hand"/>
<Button Style="{StaticResource BorderlessButton}"
Command="{Binding Source={StaticResource Proxy}, Path=Data.RemoveFromAlbumCommand}"
CommandParameter="{Binding .}"
Margin="0,5,5,5" Padding="0,5,5,5">X</Button>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
Expand All @@ -206,15 +219,6 @@
</ItemsControl>
</local:AccordionControl>

<local:AccordionControl x:Name="WorkflowMetadata"
Margin="0,0,0,5"
HeaderBackground="#80000000"
State="{Binding DataContext.MetadataSection.WorkflowState, ElementName=PreviewPaneGrid, Mode=TwoWay}" Header="{lex:Loc Metadata.Workflow}">

</local:AccordionControl>



</StackPanel>
</ScrollViewer>
</TabItem>
Expand All @@ -223,6 +227,15 @@
<local:ComfyNodeStack DataContext="{Binding Nodes}" Background="Transparent"></local:ComfyNodeStack>
</ScrollViewer>
</TabItem>
<TabItem Visibility="{Binding HasError, Converter={StaticResource boolToVis}}" Header="Errors" Background="Transparent" GotFocus="UIElement_OnGotFocus" HorizontalContentAlignment="Stretch">
<Grid>
<ScrollViewer Grid.Row="0" Background="Transparent"
VerticalAlignment="Stretch"
VerticalScrollBarVisibility="Auto">
<TextBox TextWrapping="Wrap" Margin="5" Text="{Binding ErrorMessage}" Foreground="{StaticResource ForegroundBrush}"></TextBox>
</ScrollViewer>
</Grid>
</TabItem>
</TabControl>


Expand Down
11 changes: 2 additions & 9 deletions Diffusion.Toolkit/Controls/MetadataPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,10 @@ private void SetMetadataState(AccordionState state)
DateMetadata.State = state;
}

private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
private void AlbumName_OnMouseDown(object sender, MouseButtonEventArgs e)
{
var album = ((Album)((TextBox)sender).DataContext);

var albumModel = new AlbumModel()
{
Id = album.Id,
Name = album.Name,
};

CurrentImage.OpenAlbumCommand?.Execute(albumModel);
CurrentImage.OpenAlbumCommand?.Execute(album);
}

private void UIElement_OnGotFocus(object sender, RoutedEventArgs e)
Expand Down
Loading

0 comments on commit c8a7aa0

Please sign in to comment.