from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter.ttk import *
from infer import find_matches
from data import make_train_valid_dfs
from infer import *
from config import CFG

_, valid_df = make_train_valid_dfs()
model = get_image_embeddings(valid_df, CFG.model_path)

plot = None
def get_figure():
    global plot
    print("getting figure for sentence: ", entry1.get())
    fig = find_matches(model, query=entry1.get(), image_filenames=valid_df['image'].values, n=9)
    print("Got figure for sentence: ", entry1.get())
    plot = FigureCanvasTkAgg(fig, root)
    plot.get_tk_widget().grid(row=2, column=0, rowspan=7, columnspan= 7)
    #plot.get_tk_widget().pack(side='top')


root = Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry("%dx%d"%(width,height))

root.title('CLIP Model Demo')

label1 = Label(root, text="Enter Text")
label1.grid(row=0, column=0)
#label1.pack(side='top')
entry1 = Entry(root, width=50)
entry1.grid(row=0, column=1, columnspan=5)
#entry1.pack(side='top')
button1 = Button(root, text="Get Images", command=get_figure)
button1.grid(row=0, column = 6)
#button1.pack(side='top')

root.mainloop()
