Create Danbooru Prompt Selector/prompt selector.py
Browse files
Danbooru Prompt Selector/prompt selector.py
ADDED
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tkinter as tk
|
2 |
+
import tkinter.ttk as ttk
|
3 |
+
from tkinter import filedialog
|
4 |
+
import os
|
5 |
+
import csv
|
6 |
+
import random
|
7 |
+
|
8 |
+
def filter_csv(input_file, output_file, _search_strings):
|
9 |
+
output_directory = os.getcwd()
|
10 |
+
output_file = os.path.join(output_directory, output_file)
|
11 |
+
search_strings = [s.strip() for s in _search_strings.split(',')]
|
12 |
+
with open(input_file, 'r', newline='', encoding='utf-8') as f_in, \
|
13 |
+
open(output_file, 'w', newline='', encoding='utf-8') as f_out:
|
14 |
+
reader = csv.reader(f_in)
|
15 |
+
writer = csv.writer(f_out)
|
16 |
+
writer_count = 0
|
17 |
+
for row in reader:
|
18 |
+
if all(search_str in value for search_str in search_strings for value in row):
|
19 |
+
writer.writerow(row)
|
20 |
+
writer_count += 1
|
21 |
+
return writer_count
|
22 |
+
|
23 |
+
def open_file():
|
24 |
+
initial_dir = os.getcwd()
|
25 |
+
filepath = filedialog.askopenfilename(
|
26 |
+
initialdir=initial_dir,
|
27 |
+
filetypes=[("CSV Files", "*.csv")]
|
28 |
+
)
|
29 |
+
if filepath:
|
30 |
+
entry_file_path.delete(0, tk.END)
|
31 |
+
entry_file_path.insert(0, filepath)
|
32 |
+
|
33 |
+
def search():
|
34 |
+
global total_rows
|
35 |
+
input_file = entry_file_path.get()
|
36 |
+
keywords = entry_keyword.get()
|
37 |
+
keyword_label.config(text="κ²μν ν€μλ: ")
|
38 |
+
keyword_label_mode.config(text="(νμ¬ κ²μ λͺ¨λλ‘ λμνκ³ μμ΅λλ€.)", fg="blue")
|
39 |
+
output_file = 'txt2img_temp_prompt.csv'
|
40 |
+
writer_count = filter_csv(input_file, output_file, keywords)
|
41 |
+
total_rows = writer_count
|
42 |
+
total_rows_count_label.config(text=f"Total Rows: {writer_count}")
|
43 |
+
text_output.insert(tk.END, f"μ΄ {writer_count}κ°μ λ¬Έμμ΄μ΄ κ²μλμ΄ μ μ₯λμμ΅λλ€.\n")
|
44 |
+
cached_rows = None
|
45 |
+
|
46 |
+
def exclude():
|
47 |
+
global total_rows
|
48 |
+
input_file = entry_file_path.get()
|
49 |
+
keywords = entry_keyword.get()
|
50 |
+
output_file = 'txt2img_temp_prompt.csv'
|
51 |
+
keyword_label.config(text="κ²μν ν€μλ: ")
|
52 |
+
keyword_label_mode.config(text="(νμ¬ μ μΈ λͺ¨λλ‘ λμνκ³ μμ΅λλ€.)", fg="red")
|
53 |
+
output_directory = os.getcwd()
|
54 |
+
output_file = os.path.join(output_directory, output_file)
|
55 |
+
search_strings = [s.strip() for s in keywords.split(',')]
|
56 |
+
|
57 |
+
with open(input_file, 'r', newline='', encoding='utf-8') as f_in, \
|
58 |
+
open(output_file, 'w', newline='', encoding='utf-8') as f_out:
|
59 |
+
reader = csv.reader(f_in)
|
60 |
+
writer = csv.writer(f_out)
|
61 |
+
writer_count = 0
|
62 |
+
for row in reader:
|
63 |
+
if not all(search_str in value for search_str in search_strings for value in row):
|
64 |
+
writer.writerow(row)
|
65 |
+
writer_count += 1
|
66 |
+
total_rows = writer_count
|
67 |
+
total_rows_count_label.config(text=f"Total Rows: {writer_count}")
|
68 |
+
text_output.insert(tk.END, f"μ΄ {writer_count}κ°μ λ¬Έμμ΄μ΄ κ²μλμ΄ μ μ₯λμμ΅λλ€.\n")
|
69 |
+
cached_rows = None
|
70 |
+
|
71 |
+
|
72 |
+
def reset():
|
73 |
+
cached_rows = None
|
74 |
+
entry_file_path.delete(0, tk.END)
|
75 |
+
entry_keyword.delete(0, tk.END)
|
76 |
+
entry_deep_search.delete(0, tk.END)
|
77 |
+
text_output.delete('1.0', tk.END)
|
78 |
+
|
79 |
+
def random_function():
|
80 |
+
global last_deep_search_keywords, cached_rows
|
81 |
+
current_deep_search_keywords = entry_deep_search.get().strip()
|
82 |
+
if current_deep_search_keywords != last_deep_search_keywords or not cached_rows:
|
83 |
+
with open('txt2img_temp_prompt.csv', 'r', newline='', encoding='utf-8') as f:
|
84 |
+
reader = csv.reader(f)
|
85 |
+
if current_deep_search_keywords:
|
86 |
+
keywords = current_deep_search_keywords.split(',')
|
87 |
+
exclude_keywords = [s[1:].strip() for s in keywords if s.strip().startswith('~')]
|
88 |
+
include_keywords = [s.strip() for s in keywords if not s.strip().startswith('~')]
|
89 |
+
rows = [row for row in reader if not any(exclude in cell for exclude in exclude_keywords for cell in row)]
|
90 |
+
cached_rows = [row for row in rows if all(include in cell for include in include_keywords for cell in row)]
|
91 |
+
else:
|
92 |
+
cached_rows = list(reader)
|
93 |
+
last_deep_search_keywords = current_deep_search_keywords
|
94 |
+
text_output.delete('1.0', tk.END)
|
95 |
+
if cached_rows:
|
96 |
+
random_index = random.randint(0, len(cached_rows) - 1)
|
97 |
+
random_row = cached_rows.pop(random_index)
|
98 |
+
text_output.insert(tk.END, f"{', '.join(random_row)}\n")
|
99 |
+
else:
|
100 |
+
text_output.insert(tk.END, "κ²μ 쑰건μ λ§λ λ°μ΄ν°κ° μκ±°λ CSV νμΌμ λ°μ΄ν°κ° μμ΅λλ€.\n")
|
101 |
+
|
102 |
+
cached_rows_count_label.config(text=f"Cached Rows: {len(cached_rows)}")
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
def copy_to_clipboard():
|
108 |
+
window.clipboard_clear()
|
109 |
+
window.clipboard_append(text_output.get("1.0", tk.END))
|
110 |
+
|
111 |
+
def exit_program():
|
112 |
+
window.destroy()
|
113 |
+
|
114 |
+
|
115 |
+
def save_settings():
|
116 |
+
with open('app_settings.txt', 'w', encoding='utf-8') as f:
|
117 |
+
f.write(entry_file_path.get() + '\n')
|
118 |
+
f.write(entry_keyword.get() + '\n')
|
119 |
+
f.write(entry_deep_search.get() + '\n')
|
120 |
+
|
121 |
+
def load_settings():
|
122 |
+
if os.path.exists('app_settings.txt'):
|
123 |
+
with open('app_settings.txt', 'r', encoding='utf-8') as f:
|
124 |
+
settings = f.readlines()
|
125 |
+
entry_file_path.insert(0, settings[0].strip())
|
126 |
+
entry_keyword.insert(0, settings[1].strip())
|
127 |
+
entry_deep_search.insert(0, settings[2].strip())
|
128 |
+
|
129 |
+
def exit_program():
|
130 |
+
save_settings()
|
131 |
+
window.destroy()
|
132 |
+
|
133 |
+
window = tk.Tk()
|
134 |
+
window.title("Prompt Selector for Danbooru tags")
|
135 |
+
last_deep_search_keywords = None
|
136 |
+
cached_rows = []
|
137 |
+
total_rows = 0
|
138 |
+
|
139 |
+
|
140 |
+
# νμΌ κ²½λ‘ μ
λ ₯μ°½
|
141 |
+
label_file_path = tk.Label(window, text="CSV νμΌ κ²½λ‘:")
|
142 |
+
label_file_path.grid(row=0, column=0, columnspan=2, sticky='w')
|
143 |
+
entry_file_path = tk.Entry(window, width=70)
|
144 |
+
entry_file_path.grid(row=1, column=0, columnspan=2, padx=5, pady=5)
|
145 |
+
button_open_file = tk.Button(window, text="νμΌ μ΄κΈ°", command=open_file)
|
146 |
+
button_open_file.grid(row=1, column=2, padx=5, pady=5)
|
147 |
+
|
148 |
+
# ν€μλ μ
λ ₯μ°½
|
149 |
+
keyword_label = tk.Label(window, text="κ²μν ν€μλ: ")
|
150 |
+
keyword_label.grid(row=2, column=0, sticky='w')
|
151 |
+
entry_keyword = tk.Entry(window, width=70)
|
152 |
+
entry_keyword.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
|
153 |
+
keyword_label_mode = tk.Label(window, text="")
|
154 |
+
keyword_label_mode.grid(row=2, column=1, sticky='w')
|
155 |
+
|
156 |
+
# λ²νΌ νλ μ
|
157 |
+
frame_buttons = tk.Frame(window)
|
158 |
+
frame_buttons.grid(row=4, column=0, columnspan=3, pady=10)
|
159 |
+
|
160 |
+
# λ²νΌλ€
|
161 |
+
button_search = tk.Button(frame_buttons, text="κ²μ", command=search)
|
162 |
+
button_search.pack(side=tk.LEFT, padx=5)
|
163 |
+
button_exclude = tk.Button(frame_buttons, text="μ μΈ", command=exclude)
|
164 |
+
button_exclude.pack(side=tk.LEFT, padx=5)
|
165 |
+
button_reset = tk.Button(frame_buttons, text="리μ
", command=reset)
|
166 |
+
button_reset.pack(side=tk.LEFT, padx=5)
|
167 |
+
|
168 |
+
# μ¬μΈ΅κ²μ ν€μλ μ
λ ₯μ°½
|
169 |
+
label_deep_search = tk.Label(window, text="μ¬μΈ΅κ²μ ν€μλ: ν€μλ μμ ~λ₯Ό λΆμ΄λ©΄ μ μΈν©λλ€.")
|
170 |
+
label_deep_search.grid(row=5, column=0, columnspan=2, sticky='w')
|
171 |
+
entry_deep_search = tk.Entry(window, width=70)
|
172 |
+
entry_deep_search.grid(row=6, column=0, columnspan=2, padx=5, pady=5)
|
173 |
+
|
174 |
+
# Tkinter UI μ€μ λΆλΆμ λ μ΄λΈ μΆκ°
|
175 |
+
cached_rows_count_label = tk.Label(window, text="Cached Rows: 0")
|
176 |
+
cached_rows_count_label.grid(row=8, column=1, padx=5, pady=5)
|
177 |
+
total_rows_count_label = tk.Label(window, text="Total Rows: 0")
|
178 |
+
total_rows_count_label.grid(row=8, column=0, padx=5, pady=5)
|
179 |
+
|
180 |
+
# μΆκ° λ²νΌλ€
|
181 |
+
button_random = tk.Button(window, text="λλ€", command=random_function)
|
182 |
+
button_random.grid(row=7, column=0, padx=5, pady=5, sticky='w')
|
183 |
+
button_copy = tk.Button(window, text="볡μ¬", command=copy_to_clipboard)
|
184 |
+
button_copy.grid(row=7, column=1, padx=5, pady=5)
|
185 |
+
button_exit = tk.Button(window, text="μ’
λ£", command=exit_program)
|
186 |
+
button_exit.grid(row=7, column=2, padx=5, pady=5, sticky='w')
|
187 |
+
|
188 |
+
# μΆλ ₯ ν
μ€νΈ μ°½
|
189 |
+
text_output = tk.Text(window, height=10, width=70)
|
190 |
+
text_output.grid(row=9, column=0, columnspan=3, padx=5, pady=5)
|
191 |
+
|
192 |
+
load_settings()
|
193 |
+
|
194 |
+
window.mainloop()
|