baqu2213's picture
Rename Danbooru Prompt Selector/prompt selector.py to Danbooru Prompt Selector/old/prompt selector.py
f0bcabb
raw
history blame
8.76 kB
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()