Last used & min count settings

Also some performance improvements
This commit is contained in:
DominikDoom
2023-11-29 17:45:51 +01:00
parent 15478e73b5
commit a156214a48
4 changed files with 41 additions and 25 deletions

View File

@@ -421,6 +421,8 @@ def on_ui_settings():
"tac_modelSortOrder": shared.OptionInfo("Name", "Model sort order", gr.Dropdown, lambda: {"choices": list(sort_criteria.keys())}).info("Order for extra network models and wildcards in dropdown"),
"tac_frequencySort": shared.OptionInfo(True, "Locally record tag usage and sort frequent tags higher").info("Will also work for extra networks, keeping the specified base order"),
"tac_frequencyFunction": shared.OptionInfo("Logarithmic (weak)", "Function to use for frequency sorting", gr.Dropdown, lambda: {"choices": list(frequency_sort_functions.keys())}).info("; ".join([f'<b>{key}</b>: {val}' for key, val in frequency_sort_functions.items()])),
"tac_frequencyMinCount": shared.OptionInfo(3, "Minimum number of uses for a tag to be considered frequent").info("Tags with less uses than this will not be sorted higher, even if the sorting function would normally result in a higher position."),
"tac_frequencyMaxAge": shared.OptionInfo(30, "Maximum days since last use for a tag to be considered frequent").info("Similar to the above, tags that haven't been used in this many days will not be sorted higher."),
# Insertion related settings
"tac_replaceUnderscores": shared.OptionInfo(True, "Replace underscores with spaces on insertion"),
"tac_escapeParentheses": shared.OptionInfo(True, "Escape parentheses on insertion"),
@@ -597,7 +599,7 @@ def api_tac(_: gr.Blocks, app: FastAPI):
if get:
ret = func()
if ret is list:
ret = [{"name": t[0], "type": t[1], "count": t[2]} for t in ret]
ret = [{"name": t[0], "type": t[1], "count": t[2], "lastUseDate": t[3]} for t in ret]
return JSONResponse({"result": ret})
else:
func()

View File

@@ -91,7 +91,7 @@ class TagFrequencyDb:
with transaction() as cursor:
cursor.execute(
f"""
SELECT name, type, {count_str}
SELECT name, type, {count_str}, last_used
FROM tag_frequency
ORDER BY {count_str} DESC
"""
@@ -105,15 +105,15 @@ class TagFrequencyDb:
with transaction() as cursor:
cursor.execute(
f"""
SELECT {count_str}
SELECT {count_str}, last_used
FROM tag_frequency
WHERE name = ? AND type = ?
""",
(tag,ttype),
(tag, ttype),
)
tag_count = cursor.fetchone()
return tag_count[0] if tag_count else 0
return tag_count[0], tag_count[1] if tag_count else 0
def get_tag_counts(self, tags: list[str], ttypes: list[str], negative=False):
count_str = "count_neg" if negative else "count_pos"
@@ -121,18 +121,18 @@ class TagFrequencyDb:
for tag, ttype in zip(tags, ttypes):
cursor.execute(
f"""
SELECT {count_str}
SELECT {count_str}, last_used
FROM tag_frequency
WHERE name = ? AND type = ?
""",
(tag,ttype),
(tag, ttype),
)
tag_count = cursor.fetchone()
yield (tag, ttype, tag_count[0]) if tag_count else (tag, ttype, 0)
yield (tag, ttype, tag_count[0], tag_count[1]) if tag_count else (tag, ttype, 0)
def increase_tag_count(self, tag, ttype, negative=False):
pos_count = self.get_tag_count(tag, ttype, False)
neg_count = self.get_tag_count(tag, ttype, True)
pos_count = self.get_tag_count(tag, ttype, False)[0]
neg_count = self.get_tag_count(tag, ttype, True)[0]
if negative:
neg_count += 1
@@ -156,7 +156,7 @@ class TagFrequencyDb:
set_str = "count_pos = 0"
elif negative:
set_str = "count_neg = 0"
with transaction() as cursor:
cursor.execute(
f"""
@@ -164,5 +164,5 @@ class TagFrequencyDb:
SET {set_str}
WHERE name = ? AND type = ?
""",
(tag,ttype),
(tag, ttype),
)