Skip to content

Commit

Permalink
Add Model name to metadata and search
Browse files Browse the repository at this point in the history
  • Loading branch information
RupertAvery committed Apr 21, 2023
1 parent 0e78713 commit fa71bd5
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 9 deletions.
1 change: 1 addition & 0 deletions Diffusion.Database/DataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void Create()
db.CreateIndex<Image>(image => image.FolderId);
db.CreateIndex<Image>(image => image.Path);
db.CreateIndex<Image>(image => image.ModelHash);
db.CreateIndex<Image>(image => image.Model);
db.CreateIndex<Image>(image => image.Seed);
db.CreateIndex<Image>(image => image.Sampler);
db.CreateIndex<Image>(image => image.Height);
Expand Down
1 change: 1 addition & 0 deletions Diffusion.Database/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Image
public int Width { get; set; }
public int Height { get; set; }
public string ModelHash { get; set; }
public string? Model { get; set; }
public int BatchSize { get; set; }
public int BatchPos { get; set; }
//public string OtherParameters { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions Diffusion.Database/QueryBuilder.Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private static void FilterModelName(Filter filter, List<KeyValuePair<string, obj
{
foreach (var model in _models)
{
if (model.Filename.Contains(name))
if (model.Filename.Contains(name, StringComparison.InvariantCultureIgnoreCase))
{
orConditions.Add(new KeyValuePair<string, object>("(ModelHash = ?)", model.Hash));
if (!string.IsNullOrEmpty(model.SHA256))
Expand All @@ -327,7 +327,7 @@ private static void FilterModelName(Filter filter, List<KeyValuePair<string, obj
}
}

//orConditions.Add(new KeyValuePair<string, object>("(ModelName = ?)", names[i]));
orConditions.Add(new KeyValuePair<string, object>("(Model LIKE ?)", name.Replace("*", "%")));
}

if (orConditions.Any())
Expand Down
43 changes: 43 additions & 0 deletions Diffusion.Database/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static partial class QueryBuilder

private static readonly Regex SamplerRegex = new Regex("sampler:", RegexOptions.Compiled | RegexOptions.IgnoreCase);

private static readonly Regex ModelNameRegex = new Regex("\\bmodel:\\s*(?:\"(?<value>[^\"]+)\"|(?<value>\\S+))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex HashRegex = new Regex("\\b(?:model_hash|model hash):\\s*([0-9a-f]+)(?:\\s*\\|\\s*([0-9a-f]+))*\\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex CfgRegex = new Regex("\\b(?:cfg|cfg_scale|cfg scale):\\s*(\\d+(?:\\.\\d+)?)(?:\\s*\\|\\s*(\\d+(?:\\.\\d+)?))*\\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex SizeRegex = new Regex("\\bsize:\\s*((?:(?<width>\\d+|\\?)\\s*x\\s*(?<height>\\d+|\\?))|(?:(?<width>\\d+|\\?)\\s*:\\s*(?<height>\\d+|\\?)))[\\b]?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Expand Down Expand Up @@ -63,6 +64,7 @@ public static (string WhereClause, IEnumerable<object> Bindings, IEnumerable<obj
ParseSteps(ref prompt, conditions);
ParseSampler(ref prompt, conditions);
ParseHash(ref prompt, conditions);
ParseModelName(ref prompt, conditions);
ParseCFG(ref prompt, conditions);
ParseSize(ref prompt, conditions);
ParseAestheticScore(ref prompt, conditions);
Expand Down Expand Up @@ -434,6 +436,47 @@ private static void ParseHash(ref string prompt, List<KeyValuePair<string, objec
}
}

private static void ParseModelName(ref string prompt, List<KeyValuePair<string, object>> conditions)
{
var match = ModelNameRegex.Match(prompt);
if (match.Success)
{
prompt = ModelNameRegex.Replace(prompt, String.Empty);
var orConditions = new List<KeyValuePair<string, object>>();

var names = match.Groups[1].Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

foreach (var name in names)
{
foreach (var model in _models)
{
if (model.Filename.Contains(name, StringComparison.InvariantCultureIgnoreCase))
{
orConditions.Add(new KeyValuePair<string, object>("(ModelHash = ?)", model.Hash));
if (!string.IsNullOrEmpty(model.SHA256))
{
orConditions.Add(new KeyValuePair<string, object>("(ModelHash = ?)", model.SHA256.Substring(0, 10)));
}
}
}

orConditions.Add(new KeyValuePair<string, object>("(Model LIKE ?)", name.Replace("*", "%")));
}

if (orConditions.Any())
{
var keys = string.Join(" OR ", orConditions.Select(c => c.Key));
var values = orConditions.Select(c => c.Value);

conditions.Add(new KeyValuePair<string, object>($"({keys})", values));
}
else
{
conditions.Add(new KeyValuePair<string, object>($"(0 == 1)", null));
}
}
}

private static void ParseSampler(ref string prompt, List<KeyValuePair<string, object>> conditions)
{
var match = SamplerRegex.Match(prompt);
Expand Down
1 change: 1 addition & 0 deletions Diffusion.Scanner/FileParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class FileParameters
public int Width { get; set; }
public int Height { get; set; }
public string ModelHash { get; set; }
public string? Model { get; set; }
public int BatchSize { get; set; }
public int BatchPos { get; set; }
public string OtherParameters { get; set; }
Expand Down
3 changes: 3 additions & 0 deletions Diffusion.Scanner/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ private static FileParameters ReadA111Parameters(string data)
case "Model hash":
fileParameters.ModelHash = kvp[1].Trim();
break;
case "Model":
fileParameters.Model = kvp[1].Trim();
break;
case "Batch size":
fileParameters.BatchSize = int.Parse(kvp[1].Trim(), CultureInfo.InvariantCulture);
break;
Expand Down
4 changes: 2 additions & 2 deletions Diffusion.Toolkit/Controls/MessagePopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
<RowDefinition Height="36"/>
</Grid.RowDefinitions>
<Label FontWeight="Bold" Grid.Row="0" Content="{Binding Title}" HorizontalAlignment="Center" FontSize="16"></Label>
<StackPanel>
<TextBlock TextWrapping="WrapWithOverflow" Grid.Row="1" Text="{Binding Message}" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
<StackPanel Grid.Row="1" >
<TextBlock TextWrapping="WrapWithOverflow" Text="{Binding Message}" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
<TextBox Visibility="{Binding ShowInput, Converter={StaticResource boolToVisCol}}" Text="{Binding Input}"></TextBox>
</StackPanel>
<Grid Grid.Row="2">
Expand Down
7 changes: 6 additions & 1 deletion Diffusion.Toolkit/Controls/PreviewPane.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<converters:InverseBoolToVisibilityMultiConverter x:Key="invBoolToVisMulti"></converters:InverseBoolToVisibilityMultiConverter>
<converters:BoolToOpacityConverter x:Key="BoolToOpacity"></converters:BoolToOpacityConverter>
<converters:ScrollBarVisibilityConverter x:Key="scrollBarVisibility"></converters:ScrollBarVisibilityConverter>
<converters:StringToVisibilityConverter x:Key="strToVis"></converters:StringToVisibilityConverter>
<converters:StretchConverter x:Key="stretch"></converters:StretchConverter>

<Style x:Key="ShowOnHover" TargetType="Grid">
Expand Down Expand Up @@ -165,7 +166,7 @@
<Grid Margin="-20" Background="Transparent"></Grid>

<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel Background="#5F000000">
<StackPanel Background="#AF000000">

<Grid Background="Transparent" >
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -353,6 +354,10 @@
</Image.Effect>
</Image>

<Border Height="40" Width="40" CornerRadius="5" Background="#90000000" Visibility="{Binding Path=AestheticScore, Converter={StaticResource strToVis}, ConverterParameter=10}">
<Label FontSize="18" FontWeight="Bold" Content="{Binding AestheticScore}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"></Label>
</Border>

</StackPanel>
</Grid>
</Grid>
Expand Down
4 changes: 2 additions & 2 deletions Diffusion.Toolkit/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
</MenuItem>
<MenuItem Style="{DynamicResource MenuItemStyle1}" Header="_Tools">
<MenuItem Header="_Query">
<MenuItem Header="Add all matching files to Album" Command="{Binding AddMatchingToAlbum}"/>
<Separator></Separator>
<!--<MenuItem Header="Add all matching files to Album" Command="{Binding AddMatchingToAlbum}"/>
<Separator></Separator>-->
<MenuItem Header="_Mark all matching files for Deletion" Command="{Binding MarkAllForDeletion}"/>
<MenuItem Header="_Unmark all matching files for Deletion" Command="{Binding UnmarkAllForDeletion}"/>
<Separator></Separator>
Expand Down
1 change: 1 addition & 0 deletions Diffusion.Toolkit/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,7 @@ await Dispatcher.Invoke(async () =>
Width = file.Width,
Height = file.Height,
ModelHash = file.ModelHash,
Model = file.Model,
Steps = file.Steps,
Sampler = file.Sampler,
CFGScale = file.CFGScale,
Expand Down
7 changes: 7 additions & 0 deletions Diffusion.Toolkit/Models/ImageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class ImageViewModel : BaseNotify
private long _seed;
private string _modelHash;
private ICommand _toggleParameters;
private string? _aestheticScore;

public int Id { get; set; }

Expand Down Expand Up @@ -183,4 +184,10 @@ public string ModelHash
get => _modelHash;
set => SetField(ref _modelHash, value);
}

public string? AestheticScore
{
get => _aestheticScore;
set => SetField(ref _aestheticScore, value);
}
}
7 changes: 7 additions & 0 deletions Diffusion.Toolkit/Models/MessagePopupModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class MessagePopupModel : BaseNotify
private int _width;
private UIElement _placementTarget;
private string _input;
private bool _showInput;


public MessagePopupModel()
Expand Down Expand Up @@ -142,4 +143,10 @@ public string Input
get => _input;
set => SetField(ref _input, value);
}

public bool ShowInput
{
get => _showInput;
set => SetField(ref _showInput, value);
}
}
1 change: 1 addition & 0 deletions Diffusion.Toolkit/Pages/Search.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ public void LoadPreviewImage(string path, ImageEntry? image = null)

_model.CurrentImage.ModelHash = parameters.ModelHash;
_model.CurrentImage.Seed = parameters.Seed;
_model.CurrentImage.AestheticScore = $"{parameters.AestheticScore}";

if (_modelLookup != null)
{
Expand Down
6 changes: 6 additions & 0 deletions Diffusion.Toolkit/Tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ You can query `seed`, with a number, a range, or wildcards.

* `model_hash: <hash>`

## Model Name

Wildcards (`?`, `*`) are supported

* `model: <term>`

## Aesthetic Score

Aesthetic score is a tag added by the [Aesthetic Image Scorer Extension](https://github.com/tsngo/stable-diffusion-webui-aesthetic-image-scorer) for AUTOMATIC111 Web UI.
Expand Down
11 changes: 9 additions & 2 deletions What's New v1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ How to get it:
# What's New in v1.0

* Less files! All .NET assemblies are now packaged into a single executable. If you're coming from an older version, you may want to start anew and remove all the old files, as the updater doesn't remove existing files.
* The Diffusion view will immediate display all images (no search query) upon loading.
* **Watch Folders** with A1111 image generation has been fixed.
* Portable mode. Diffusion Toolkit can run using settings and database in your executable folder.
* Check the **Portable mode** checkbox in settings to move your `config.json` and `diffusion-toolkit.db` to your executable folder. You can also do it manually.
* You can now **sort** images by
* Date Created
* Aesthetic Score
* Rating
* Ascending / Descending
* **Aesthetic Score** is now displayed in the thumbnail view and Preview Pane.
* **Folders** feature has been added.
* Browse from your Diffusion folders
* Only indexed images will be displayed
Expand All @@ -54,8 +57,12 @@ How to get it:
* Pressing **CTRL+C** with one or more images selected in the thumbnail view will copy the files to the clipboard.
* Copy parameter context menu items (Copy seed, Copy prompt, etc,) have been moved to **Right-click > Copy Parameters**. This allows you to press **Right-click C, S** to copy the Seed, for example.
* Path queries no longer uses GLOB as it is case-sensitive.
* A new Folder query now lets you search in a specific folder, not including subfolders.
* Seed can now be queried with wildcards.
* A new **Folder** query now lets you search in a specific folder, not including subfolders.
* **Model** metadata is now tracked. **Rebuild Images to update the Model metadata in your existing images.**
* **Model** name (checkpoint name) can now be queried. Wild cards are supported.
* `model: realistic*` - match images with model name starting with "realistic".
* Will use both A111 cache information and Model name metadata (A111 metadata only for now) to search.
* **Seed** can now be queried with wildcards.
* `seed: 123*` will show all images have a seed that starts with `123`
* `seed: 123456???000` will show all images have a seed that starts with `123456`, matches any 3 digits, and ends with `000`
* Support for **ComfyUI** metadata
Expand Down

0 comments on commit fa71bd5

Please sign in to comment.