min-dalle-test/tkinter_ui.py

148 lines
4.0 KiB
Python
Raw Permalink Normal View History

2022-07-14 13:07:26 +00:00
from min_dalle import MinDalle
import sys
import PIL
import PIL.Image
import PIL.ImageTk
import tkinter
from tkinter import ttk
def regen_root():
global root
global blank_image
global padding_image
root = tkinter.Tk()
root.wm_resizable(False, False)
blank_image = PIL.ImageTk.PhotoImage(PIL.Image.new(size=(256 * 2, 256 * 2), mode="RGB"))
2022-07-14 13:07:26 +00:00
padding_image = PIL.ImageTk.PhotoImage(PIL.Image.new(size=(16, 16), mode="RGBA"))
regen_root()
2022-07-15 13:00:26 +00:00
is_mega = None
2022-07-14 13:07:26 +00:00
def set_mega_true_and_destroy():
2022-07-15 13:00:26 +00:00
global is_mega
is_mega = True
2022-07-14 13:07:26 +00:00
root.destroy()
def set_mega_false_and_destroy():
2022-07-15 13:00:26 +00:00
global is_mega
is_mega = False
2022-07-14 13:07:26 +00:00
root.destroy()
frm = ttk.Frame(root, padding=16)
frm.grid()
ttk.Button(frm, text="Mega", command=set_mega_true_and_destroy).grid(column=0, row=0)
ttk.Label(frm, image=padding_image).grid(column=1, row=0)
ttk.Button(frm, text="Mini", command=set_mega_false_and_destroy).grid(column=2, row=0)
2022-07-14 13:07:26 +00:00
root.mainloop()
2022-07-15 13:00:26 +00:00
if is_mega is None:
print("no option selected")
2022-07-14 13:07:26 +00:00
sys.exit(0)
print("is_mega", is_mega)
2022-07-14 13:07:26 +00:00
model = MinDalle(
models_root="./pretrained",
is_mega=is_mega,
2022-07-14 13:07:26 +00:00
is_reusable=True,
is_verbose=True
)
regen_root()
label_image_content = blank_image
2022-07-15 13:00:26 +00:00
sv_prompt = tkinter.StringVar(value="artificial intelligence")
2022-07-14 13:07:26 +00:00
sv_temperature = tkinter.StringVar(value="1")
2022-07-15 13:00:26 +00:00
sv_topk = tkinter.StringVar(value="128")
2022-07-14 13:07:26 +00:00
sv_supercond = tkinter.StringVar(value="16")
2022-07-17 11:33:31 +00:00
bv_seamless = tkinter.BooleanVar(value=False)
2022-07-14 13:07:26 +00:00
def generate():
# check fields
try:
temperature = float(sv_temperature.get())
except:
sv_temperature.set("ERROR")
return
try:
topk = int(sv_topk.get())
except:
sv_topk.set("ERROR")
return
try:
supercond = int(sv_supercond.get())
except:
sv_supercond.set("ERROR")
return
2022-07-17 11:33:31 +00:00
try:
is_seamless = bool(bv_seamless.get())
except:
return
2022-07-14 13:07:26 +00:00
# and continue
global label_image_content
image_stream = model.generate_image_stream(
2022-07-14 13:07:26 +00:00
sv_prompt.get(),
grid_size=2,
seed=-1,
2022-07-15 21:28:09 +00:00
progressive_outputs=True,
2022-07-17 11:33:31 +00:00
is_seamless=is_seamless,
2022-07-14 13:07:26 +00:00
temperature=temperature,
top_k=topk,
supercondition_factor=supercond,
is_verbose=True
)
for image in image_stream:
2022-07-17 11:33:31 +00:00
global final_image
final_image = image
label_image_content = PIL.ImageTk.PhotoImage(image)
label_image.configure(image=label_image_content)
label_image.update()
2022-07-14 13:07:26 +00:00
2022-07-17 11:33:31 +00:00
def save():
2022-07-17 12:34:32 +00:00
final_image.save('generated/out.png')
2022-07-17 11:33:31 +00:00
2022-07-14 13:07:26 +00:00
frm = ttk.Frame(root, padding=16)
frm.grid()
props = ttk.Frame(frm)
# outer structure (hbox)
label_image = ttk.Label(frm, image=blank_image)
label_image.grid(column=0, row=0)
ttk.Label(frm, image=padding_image).grid(column=1, row=0)
props.grid(column=2, row=0)
# inner structure (properties and shit)
# prompt field
ttk.Label(props, text="Prompt:").grid(column=0, row=0)
ttk.Entry(props, textvariable=sv_prompt).grid(column=1, row=0)
#
ttk.Label(props, image=padding_image).grid(column=0, row=1)
# temperature field
ttk.Label(props, text="Temperature:").grid(column=0, row=2)
ttk.Entry(props, textvariable=sv_temperature).grid(column=1, row=2)
#
ttk.Label(props, image=padding_image).grid(column=0, row=3)
# topk field
ttk.Label(props, text="Top-K:").grid(column=0, row=4)
ttk.Entry(props, textvariable=sv_topk).grid(column=1, row=4)
#
ttk.Label(props, image=padding_image).grid(column=0, row=5)
# superconditioning field
ttk.Label(props, text="Supercondition Factor:").grid(column=0, row=6)
ttk.Entry(props, textvariable=sv_supercond).grid(column=1, row=6)
#
ttk.Label(props, image=padding_image).grid(column=0, row=7)
2022-07-17 11:33:31 +00:00
# seamless
ttk.Label(props, text="Seamless:").grid(column=0, row=8)
ttk.Checkbutton(props, variable=bv_seamless).grid(column=1, row=8)
#
ttk.Label(props, image=padding_image).grid(column=0, row=9)
2022-07-14 13:07:26 +00:00
# buttons
2022-07-17 11:33:31 +00:00
ttk.Button(props, text="Generate", command=generate).grid(column=0, row=10)
ttk.Button(props, text="Quit", command=root.destroy).grid(column=1, row=10)
ttk.Button(props, text="Save", command=save).grid(column=2, row=10)
2022-07-14 13:07:26 +00:00
root.mainloop()