Skip to content

Commit

Permalink
add option to skip interrogate categories
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmandic authored Jan 23, 2023
1 parent efa7287 commit 04a561c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
32 changes: 18 additions & 14 deletions modules/interrogate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import traceback
from collections import namedtuple
from pathlib import Path
import re

import torch
Expand All @@ -20,12 +21,16 @@

re_topn = re.compile(r"\.top(\d+)\.")

category_types = ["artists", "flavors", "mediums", "movements"]
def category_types():
return [f.stem for f in Path(shared.interrogator.content_dir).glob('*.txt')]


def download_default_clip_interrogate_categories(content_dir):
print("Downloading CLIP categories...")

tmpdir = content_dir + "_tmp"
category_types = ["artists", "flavors", "mediums", "movements"]

try:
os.makedirs(tmpdir)
for category_type in category_types:
Expand All @@ -48,33 +53,32 @@ class InterrogateModels:

def __init__(self, content_dir):
self.loaded_categories = None
self.selected_categories = []
self.skip_categories = []
self.content_dir = content_dir
self.running_on_cpu = devices.device_interrogate == torch.device("cpu")

def categories(self):
if self.loaded_categories is not None and self.selected_categories == shared.opts.interrogate_clip_categories:
if not os.path.exists(self.content_dir):
download_default_clip_interrogate_categories(self.content_dir)

if self.loaded_categories is not None and self.skip_categories == shared.opts.interrogate_clip_skip_categories:
return self.loaded_categories

self.loaded_categories = []

if not os.path.exists(self.content_dir):
download_default_clip_interrogate_categories(self.content_dir)

if os.path.exists(self.content_dir):
self.selected_categories = shared.opts.interrogate_clip_categories
for category_type in category_types:
if 'all' not in self.selected_categories and category_type not in self.selected_categories:
continue
filename = os.path.join(self.content_dir, f"{category_type}.txt")
if not os.path.isfile(filename):
self.skip_categories = shared.opts.interrogate_clip_skip_categories
category_types = []
for filename in Path(self.content_dir).glob('*.txt'):
category_types.append(filename.stem)
if filename.stem in self.skip_categories:
continue
m = re_topn.search(filename)
m = re_topn.search(filename.stem)
topn = 1 if m is None else int(m.group(1))
with open(filename, "r", encoding="utf8") as file:
lines = [x.strip() for x in file.readlines()]

self.loaded_categories.append(Category(name=category_type, topn=topn, items=lines))
self.loaded_categories.append(Category(name=filename.stem, topn=topn, items=lines))

return self.loaded_categories

Expand Down
2 changes: 1 addition & 1 deletion modules/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def list_samplers():
"interrogate_clip_min_length": OptionInfo(24, "Interrogate: minimum description length (excluding artists, etc..)", gr.Slider, {"minimum": 1, "maximum": 128, "step": 1}),
"interrogate_clip_max_length": OptionInfo(48, "Interrogate: maximum description length", gr.Slider, {"minimum": 1, "maximum": 256, "step": 1}),
"interrogate_clip_dict_limit": OptionInfo(1500, "CLIP: maximum number of lines in text file (0 = No limit)"),
"interrogate_clip_categories": OptionInfo(modules.interrogate.category_types, "CLIP: select which categories to inquire", gr.CheckboxGroup, lambda: {"choices": modules.interrogate.category_types}),
"interrogate_clip_skip_categories": OptionInfo([], "CLIP: skip inquire categories", gr.CheckboxGroup, lambda: {"choices": modules.interrogate.category_types()}, refresh=modules.interrogate.category_types),
"interrogate_deepbooru_score_threshold": OptionInfo(0.5, "Interrogate: deepbooru score threshold", gr.Slider, {"minimum": 0, "maximum": 1, "step": 0.01}),
"deepbooru_sort_alpha": OptionInfo(True, "Interrogate: deepbooru sort alphabetically"),
"deepbooru_use_spaces": OptionInfo(False, "use spaces for tags in deepbooru"),
Expand Down

0 comments on commit 04a561c

Please sign in to comment.