File size: 8,761 Bytes
7600689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcfe051
7600689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6974b12
 
7600689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6974b12
 
 
 
 
 
 
7600689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6974b12
 
 
 
 
 
 
 
 
 
 
7600689
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
import os
import csv
import random

def filter_csv(input_file, output_file, _search_strings):
    output_directory = os.getcwd()
    output_file = os.path.join(output_directory, output_file)
    search_strings = [s.strip() for s in _search_strings.split(',')]
    with open(input_file, 'r', newline='', encoding='utf-8') as f_in, \
         open(output_file, 'w', newline='', encoding='utf-8') as f_out:
        reader = csv.reader(f_in)
        writer = csv.writer(f_out)
        writer_count = 0
        for row in reader:
            if all(search_str in value for search_str in search_strings for value in row):
                writer.writerow(row)
                writer_count += 1
        return writer_count

def open_file():
    initial_dir = os.getcwd()
    filepath = filedialog.askopenfilename(
        initialdir=initial_dir,
        filetypes=[("CSV Files", "*.csv")]
    )
    if filepath:
        entry_file_path.delete(0, tk.END)
        entry_file_path.insert(0, filepath)

def search():
    global total_rows
    input_file = entry_file_path.get()
    keywords = entry_keyword.get()
    keyword_label.config(text="검색할 ν‚€μ›Œλ“œ: ")
    keyword_label_mode.config(text="(ν˜„μž¬ 검색 λͺ¨λ“œλ‘œ λ™μž‘ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.)", fg="blue")
    output_file = 'txt2img_temp_prompt.csv'
    writer_count = filter_csv(input_file, output_file, keywords)
    total_rows = writer_count
    total_rows_count_label.config(text=f"Total Rows: {writer_count}")
    text_output.insert(tk.END, f"총 {writer_count}개의 λ¬Έμžμ—΄μ΄ κ²€μƒ‰λ˜μ–΄ μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.\n")
    cached_rows = None
    
def exclude():
    global total_rows
    input_file = entry_file_path.get()
    keywords = entry_keyword.get()
    output_file = 'txt2img_temp_prompt.csv'
    keyword_label.config(text="검색할 ν‚€μ›Œλ“œ: ")
    keyword_label_mode.config(text="(ν˜„μž¬ μ œμ™Έ λͺ¨λ“œλ‘œ λ™μž‘ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.)", fg="red")
    output_directory = os.getcwd()
    output_file = os.path.join(output_directory, output_file)
    search_strings = [s.strip() for s in keywords.split(',')]
    
    with open(input_file, 'r', newline='', encoding='utf-8') as f_in, \
         open(output_file, 'w', newline='', encoding='utf-8') as f_out:
        reader = csv.reader(f_in)
        writer = csv.writer(f_out)
        writer_count = 0
        for row in reader:
            if not any(search_str in value for search_str in search_strings for value in row):
                writer.writerow(row)
                writer_count += 1
    total_rows = writer_count
    total_rows_count_label.config(text=f"Total Rows: {writer_count}")
    text_output.insert(tk.END, f"총 {writer_count}개의 λ¬Έμžμ—΄μ΄ κ²€μƒ‰λ˜μ–΄ μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.\n")
    cached_rows = None


def reset():
    cached_rows = None
    entry_file_path.delete(0, tk.END)
    entry_keyword.delete(0, tk.END)
    entry_deep_search.delete(0, tk.END)
    text_output.delete('1.0', tk.END)

def random_function():
    global last_deep_search_keywords, cached_rows
    current_deep_search_keywords = entry_deep_search.get().strip()
    if current_deep_search_keywords != last_deep_search_keywords or not cached_rows:
        with open('txt2img_temp_prompt.csv', 'r', newline='', encoding='utf-8') as f:
            reader = csv.reader(f)
            if current_deep_search_keywords:
                keywords = current_deep_search_keywords.split(',')
                exclude_keywords = [s[1:].strip() for s in keywords if s.strip().startswith('~')]
                include_keywords = [s.strip() for s in keywords if not s.strip().startswith('~')]
                rows = [row for row in reader if not any(exclude in cell for exclude in exclude_keywords for cell in row)]
                cached_rows = [row for row in rows if all(include in cell for include in include_keywords for cell in row)]
            else:
                cached_rows = list(reader)
        last_deep_search_keywords = current_deep_search_keywords
    text_output.delete('1.0', tk.END)
    if cached_rows:
        random_index = random.randint(0, len(cached_rows) - 1)
        random_row = cached_rows.pop(random_index)
        text_output.insert(tk.END, f"{', '.join(random_row)}\n")
    else:
        text_output.insert(tk.END, "검색 쑰건에 λ§žλŠ” 데이터가 μ—†κ±°λ‚˜ CSV νŒŒμΌμ— 데이터가 μ—†μŠ΅λ‹ˆλ‹€.\n")

    cached_rows_count_label.config(text=f"Cached Rows: {len(cached_rows)}")




def copy_to_clipboard():
    window.clipboard_clear()
    combined_text = entry_fixed_prompt.get() + "\n" + text_output.get("1.0", tk.END)
    window.clipboard_append(combined_text)

def exit_program():
    window.destroy()


def save_settings():
    with open('app_settings.txt', 'w', encoding='utf-8') as f:
        f.write(entry_file_path.get() + '\n')
        f.write(entry_keyword.get() + '\n')
        f.write(entry_deep_search.get() + '\n')

def load_settings():
    if os.path.exists('app_settings.txt'):
        with open('app_settings.txt', 'r', encoding='utf-8') as f:
            settings = f.readlines()
            entry_file_path.insert(0, settings[0].strip())
            entry_keyword.insert(0, settings[1].strip())
            entry_deep_search.insert(0, settings[2].strip())

def exit_program():
    save_settings()
    window.destroy()

def on_ctrl_enter(event):
    random_function()

def on_shift_enter(event):
    random_function()
    copy_to_clipboard()

window = tk.Tk()
window.title("Prompt Selector for Danbooru tags")
last_deep_search_keywords = None
cached_rows = []
total_rows = 0


# 파일 경둜 μž…λ ₯μ°½
label_file_path = tk.Label(window, text="CSV 파일 경둜:")
label_file_path.grid(row=0, column=0, columnspan=2, sticky='w')
entry_file_path = tk.Entry(window, width=70)
entry_file_path.grid(row=1, column=0, columnspan=2, padx=5, pady=5)
button_open_file = tk.Button(window, text="파일 μ—΄κΈ°", command=open_file)
button_open_file.grid(row=1, column=2, padx=5, pady=5)

# ν‚€μ›Œλ“œ μž…λ ₯μ°½
keyword_label = tk.Label(window, text="검색할 ν‚€μ›Œλ“œ: ")
keyword_label.grid(row=2, column=0, sticky='w')
entry_keyword = tk.Entry(window, width=70)
entry_keyword.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
keyword_label_mode = tk.Label(window, text="")
keyword_label_mode.grid(row=2, column=1, sticky='w')

# λ²„νŠΌ ν”„λ ˆμž„
frame_buttons = tk.Frame(window)
frame_buttons.grid(row=4, column=0, columnspan=3, pady=10)

# λ²„νŠΌλ“€
button_search = tk.Button(frame_buttons, text="검색", command=search)
button_search.pack(side=tk.LEFT, padx=5)
button_exclude = tk.Button(frame_buttons, text="μ œμ™Έ", command=exclude)
button_exclude.pack(side=tk.LEFT, padx=5)
button_reset = tk.Button(frame_buttons, text="리셋", command=reset)
button_reset.pack(side=tk.LEFT, padx=5)

# 심측검색 ν‚€μ›Œλ“œ μž…λ ₯μ°½
label_deep_search = tk.Label(window, text="심측검색 ν‚€μ›Œλ“œ: ν‚€μ›Œλ“œ μ•žμ— ~λ₯Ό 뢙이면 μ œμ™Έν•©λ‹ˆλ‹€.")
label_deep_search.grid(row=5, column=0, columnspan=2, sticky='w')
entry_deep_search = tk.Entry(window, width=70)
entry_deep_search.grid(row=6, column=0, columnspan=2, padx=5, pady=5)

# Tkinter UI μ„€μ • 뢀뢄에 λ ˆμ΄λΈ” μΆ”κ°€
cached_rows_count_label = tk.Label(window, text="Cached Rows: 0")
cached_rows_count_label.grid(row=8, column=1, padx=5, pady=5)
total_rows_count_label = tk.Label(window, text="Total Rows: 0")
total_rows_count_label.grid(row=8, column=0, padx=5, pady=5)

# μΆ”κ°€ λ²„νŠΌλ“€
button_random = tk.Button(window, text="랜덀", command=random_function)
button_random.grid(row=7, column=0, padx=5, pady=5, sticky='w')
button_copy = tk.Button(window, text="볡사", command=copy_to_clipboard)
button_copy.grid(row=7, column=1, padx=5, pady=5)
button_exit = tk.Button(window, text="μ’…λ£Œ", command=exit_program)
button_exit.grid(row=7, column=2, padx=5, pady=5, sticky='w')

# 좜λ ₯ ν…μŠ€νŠΈ μ°½
text_output = tk.Text(window, height=10, width=70)
text_output.grid(row=9, column=0, columnspan=3, padx=5, pady=5)

fixed_prompt_label = tk.Label(window, text="κ³ μ • ν”„λ‘¬ν”„νŠΈ: 볡사 λ²„νŠΌμ„ λˆŒλ €μ„ λ•Œ λ™μž‘ν•©λ‹ˆλ‹€.")
fixed_prompt_label.grid(row=10, column=0, columnspan=2, sticky='w')
entry_fixed_prompt = tk.Entry(window, width=70)
entry_fixed_prompt.grid(row=11, column=0, columnspan=2, padx=5, pady=5)

shortcut_info_label = tk.Label(window, text="* Ctrl+Enterλ₯Ό λˆ„λ₯΄λ©΄ [랜덀] λ²„νŠΌμ΄ λ™μž‘ν•©λ‹ˆλ‹€.\n* Shift+Enterλ₯Ό λˆ„λ₯΄λ©΄ [랜덀] λ²„νŠΌμ΄ λ™μž‘ν•˜κ³ , κ·Έ κ²°κ³Όκ°€ ν΄λ¦½λ³΄λ“œμ— λ³΅μ‚¬λ©λ‹ˆλ‹€.", justify=tk.LEFT)
shortcut_info_label.grid(row=13, column=0, columnspan=3, padx=5, pady=5, sticky='w')

window.bind('<Control-Return>', on_ctrl_enter)
window.bind('<Shift-Return>', on_shift_enter)

load_settings()

window.mainloop()