baqu2213 commited on
Commit
7600689
β€’
1 Parent(s): 37c5139

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()