Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
X/Y plot does not switch between models whose names starts with the same phrase
**Describe the bug**
X/Y plot does not switch between models whose names starts with the same phrase.
I have 3 models named:
    model-wd.ckpt
    model-wd-pruned.ckpt
    model-wd-pruned-model-cn-poster-merged.ckpt
I can manually switch between them in settings, but if I'm using X/Y plot script it uses model with the longest name (model-wd-pruned-model-cn-poster-merged.ckpt)

**To Reproduce**
Steps to reproduce the behavior:
1. You should have several models whose names starts with the same phrase in models folder
2. Start WebUI
3. Select "X/Y Plot" as a script
4. Select "Checkpoint name" as X or Y type and enter the checkpoints names 
5. Press "Generate"
6. See the same picture for all models whose names starts with the same phrase

**Expected behavior**
X/Y Plot script should switch between all models provided in "Checkpoint name" field

**Screenshots**
Settings I use for X/Y Plot:

![image](https://user-images.githubusercontent.com/38957619/192694005-ca457054-0b71-4811-b69d-af3e4f3d1067.png)

For some reason it loads other checkpoints after generating all images:

![image](https://user-images.githubusercontent.com/38957619/192695136-69962a1c-95a2-4102-96c7-bccb1207cf26.png)

Result:

![image](https://user-images.githubusercontent.com/38957619/192694152-4a3f4f10-2790-46a0-95ab-9194cfbb6ad1.png)

Same settings, but switching models manually

model-wd:
![image](https://user-images.githubusercontent.com/38957619/192694470-10692c1c-32c6-4432-8c71-1e1a74f5a893.png)

model-wd-pruned:
![image](https://user-images.githubusercontent.com/38957619/192694502-a9c4e3b1-f6db-44d5-8237-0d550ce2044c.png)

model-wd-pruned-model-cn-poster-merged:
![image](https://user-images.githubusercontent.com/38957619/192694529-9a1ef5ff-81d3-49af-a8a5-12169fb328f9.png)


**Desktop:**
 - OS: Windows
 - Browser: Chrome
 - Commit revision: f2a4a2c3a672e22f088a7455d6039557370dd3f2


Discussion:
I can't yet reproduce this, even with a couple of naming variations:

![image](https://user-images.githubusercontent.com/35278260/192751051-9a51f553-7176-4516-a96a-dcda4a8fb7d1.png)
![image](https://user-images.githubusercontent.com/35278260/192751798-6fb46bd8-186b-4ef8-945d-71da3d2276e2.png)
![image](https://user-images.githubusercontent.com/35278260/192750298-84dfc4aa-7c2e-4671-b6d1-04a5ad9a452b.png)

Batching seems to make no difference either.

Any suggestions?
I renamed the checkpoints and that seems to work

![image](https://user-images.githubusercontent.com/38957619/192780433-a79ff5c8-55d5-4321-92ad-cf3dcbd5d940.png)

I tried to debug, but can't see the reason why it uses the same model for all pictures

Okay, we'll see if we can re-trigger it in that case.

By any chance were any of the models added to the models folder without the app being restarted?
> By any chance were any of the models added to the models folder without the app being restarted?

Webui was restarted many times after adding a models
I have experienced this bug myself multiple times because I have lots of models named similarly 
The issue comes from this line https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/master/scripts/xy_grid.py#L48

I'm not sure if this is a bug, per se, it may also be an user error because of lack of documentation on how we should write the checkpoint name in the field (with or without extension).

For example, in my folders I have two models loaded in this order (according to what `sd_models.checkpoints_list.values()` tells me):
`own-model-v13-mixed.ckpt`
`own-model-v13.ckpt`

When I'm trying to create a chart and I fill out the checkpoint name field like `own-model-v13, own-model-v12` it will use the first match from the checkpoints_list, which is not the expected result.

Another case where this happens is if you have the checkpoint name you're trying to load inside of another checkpoint's name, like
`own-model-wd-mix.ckpt`
`wd.ckpt`

If `own-model-wd-mix` is loaded before `wd`, when trying to load the `wd` checkpoint in the grid, it will instead load `own-model-wd-mix`.




Wonderful thank you!

Resolving patch:
diff --git a/modules/sd_models.py b/modules/sd_models.py
--- a/modules/sd_models.py
+++ b/modules/sd_models.py
@@ -57,6 +57,11 @@ def modeltitle(path, h):
             model_name = title.rsplit(".",1)[0] # remove extension if present
             checkpoints_list[title] = CheckpointInfo(filename, title, h, model_name)
 
+def get_closet_checkpoint_match(searchString):
+    applicable = sorted([info for info in checkpoints_list.values() if searchString in info.title], key = lambda x:len(x.title))
+    if len(applicable)>0:
+        return applicable[0]
+    return None
 
 def model_hash(filename):
     try:
diff --git a/scripts/xy_grid.py b/scripts/xy_grid.py
--- a/scripts/xy_grid.py
+++ b/scripts/xy_grid.py
@@ -45,11 +45,8 @@ def apply_sampler(p, x, xs):
 
 
 def apply_checkpoint(p, x, xs):
-    applicable = [info for info in modules.sd_models.checkpoints_list.values() if x in info.title]
-    assert len(applicable) > 0, f'Checkpoint {x} for found'
-
-    info = applicable[0]
-
+    info = modules.sd_models.get_closet_checkpoint_match(x)
+    assert info is not None, f'Checkpoint for {x} not found'
     modules.sd_models.reload_model_weights(shared.sd_model, info)
 
 


Test patch: