|
|
import customtkinter as ctk
|
|
|
from tkinter import filedialog, ttk
|
|
|
from PIL import Image
|
|
|
import os
|
|
|
import tempfile
|
|
|
import pandas as pd
|
|
|
from automata_core import AutomataCore
|
|
|
import datetime
|
|
|
|
|
|
ctk.set_appearance_mode("Dark")
|
|
|
ctk.set_default_color_theme("dark-blue")
|
|
|
|
|
|
class ProStudio(ctk.CTk):
|
|
|
def __init__(self):
|
|
|
super().__init__()
|
|
|
self.title("Automata Studio Pro X")
|
|
|
self.geometry("1400x900")
|
|
|
self.core = AutomataCore()
|
|
|
|
|
|
self.history = []
|
|
|
self.current_dot = None
|
|
|
self.current_table = []
|
|
|
|
|
|
|
|
|
self.grid_columnconfigure(1, weight=1)
|
|
|
self.grid_rowconfigure(0, weight=1)
|
|
|
|
|
|
|
|
|
self.sidebar = ctk.CTkFrame(self, width=280, corner_radius=0, fg_color="#1a1a1a")
|
|
|
self.sidebar.grid(row=0, column=0, sticky="nsew")
|
|
|
|
|
|
|
|
|
self.logo = ctk.CTkLabel(self.sidebar, text="NEURAL\nAUTOMATA X", font=("Montserrat", 28, "bold"), text_color="#3B8ED0")
|
|
|
self.logo.pack(pady=40)
|
|
|
|
|
|
|
|
|
self.status_frame = ctk.CTkFrame(self.sidebar, fg_color="transparent")
|
|
|
self.status_frame.pack(pady=10)
|
|
|
self._add_status(self.core.vision_model, "Vision Eye")
|
|
|
self._add_status(self.core.logic_model, "Logic Brain")
|
|
|
|
|
|
ctk.CTkLabel(self.sidebar, text="HISTORY", font=("Arial", 12, "bold"), text_color="gray").pack(pady=(30,10), anchor="w", padx=20)
|
|
|
self.history_scroll = ctk.CTkScrollableFrame(self.sidebar, fg_color="transparent", height=400)
|
|
|
self.history_scroll.pack(fill="x", padx=10)
|
|
|
|
|
|
|
|
|
self.main_view = ctk.CTkTabview(self, fg_color="#2b2b2b", corner_radius=20)
|
|
|
self.main_view.grid(row=0, column=1, padx=20, pady=20, sticky="nsew")
|
|
|
|
|
|
self.tab_gen = self.main_view.add(" Create Machine ")
|
|
|
self.tab_scan = self.main_view.add(" Scan Diagram ")
|
|
|
|
|
|
self._setup_generator()
|
|
|
self._setup_scanner()
|
|
|
|
|
|
def _add_status(self, model, name):
|
|
|
color = "#00ff00" if model else "#ff0000"
|
|
|
status = "ONLINE" if model else "OFFLINE"
|
|
|
lbl = ctk.CTkLabel(self.status_frame, text=f"● {name}: {status}", text_color=color, font=("Arial", 10))
|
|
|
lbl.pack(anchor="w")
|
|
|
|
|
|
def _setup_generator(self):
|
|
|
|
|
|
input_frame = ctk.CTkFrame(self.tab_gen, fg_color="transparent")
|
|
|
input_frame.pack(pady=20, fill="x", padx=40)
|
|
|
|
|
|
self.prompt = ctk.CTkEntry(input_frame, placeholder_text="Describe your machine... (e.g. 'DFA ending with 010')", height=50, font=("Arial", 16))
|
|
|
self.prompt.pack(side="left", fill="x", expand=True, padx=(0, 20))
|
|
|
self.prompt.bind("<Return>", lambda e: self.generate())
|
|
|
|
|
|
btn = ctk.CTkButton(input_frame, text="GENERATE", width=150, height=50, fg_color="#3B8ED0", font=("Arial", 14, "bold"), command=self.generate)
|
|
|
btn.pack(side="right")
|
|
|
|
|
|
|
|
|
split_frame = ctk.CTkFrame(self.tab_gen, fg_color="transparent")
|
|
|
split_frame.pack(fill="both", expand=True, padx=20, pady=10)
|
|
|
|
|
|
|
|
|
self.img_frame = ctk.CTkFrame(split_frame, fg_color="#1e1e1e", corner_radius=15)
|
|
|
self.img_frame.pack(side="left", fill="both", expand=True, padx=(0,10))
|
|
|
self.gen_img_lbl = ctk.CTkLabel(self.img_frame, text="Visualization Area", text_color="gray")
|
|
|
self.gen_img_lbl.pack(expand=True)
|
|
|
|
|
|
|
|
|
self.data_frame = ctk.CTkFrame(split_frame, width=350, fg_color="#1e1e1e", corner_radius=15)
|
|
|
self.data_frame.pack(side="right", fill="y")
|
|
|
|
|
|
ctk.CTkLabel(self.data_frame, text="LOGIC REPORT", font=("Arial", 14, "bold")).pack(pady=15)
|
|
|
self.logic_lbl = ctk.CTkLabel(self.data_frame, text="Waiting...", text_color="#3B8ED0")
|
|
|
self.logic_lbl.pack(pady=5)
|
|
|
|
|
|
|
|
|
ctk.CTkButton(self.data_frame, text="Save Image", command=self.save_img).pack(pady=(20,10), padx=20, fill="x")
|
|
|
ctk.CTkButton(self.data_frame, text="Save CSV", command=self.save_csv).pack(pady=10, padx=20, fill="x")
|
|
|
|
|
|
|
|
|
self.tree_frame = ctk.CTkFrame(self.data_frame, fg_color="transparent")
|
|
|
self.tree_frame.pack(fill="both", expand=True, padx=10, pady=10)
|
|
|
|
|
|
style = ttk.Style()
|
|
|
style.theme_use("clam")
|
|
|
style.configure("Treeview", background="#2b2b2b", foreground="white", fieldbackground="#2b2b2b", rowheight=25)
|
|
|
style.configure("Treeview.Heading", background="#3B8ED0", foreground="white")
|
|
|
|
|
|
self.tree = ttk.Treeview(self.tree_frame, show="headings", height=15)
|
|
|
self.tree.pack(fill="both", expand=True)
|
|
|
|
|
|
def _setup_scanner(self):
|
|
|
btn = ctk.CTkButton(self.tab_scan, text="UPLOAD IMAGE", height=50, command=self.scan)
|
|
|
btn.pack(pady=40)
|
|
|
self.scan_lbl = ctk.CTkLabel(self.tab_scan, text="", font=("Arial", 20))
|
|
|
self.scan_lbl.pack()
|
|
|
self.scan_img_container = ctk.CTkLabel(self.tab_scan, text="")
|
|
|
self.scan_img_container.pack(expand=True, pady=20)
|
|
|
|
|
|
def generate(self):
|
|
|
p = self.prompt.get()
|
|
|
if not p: return
|
|
|
|
|
|
try:
|
|
|
dot, transitions, name = self.core.generate_system(p)
|
|
|
self.current_dot = dot
|
|
|
self.current_table = transitions
|
|
|
self.logic_lbl.configure(text=name)
|
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
|
path = os.path.join(tmp, "machine")
|
|
|
dot.render(path, cleanup=True)
|
|
|
img = Image.open(path + ".png")
|
|
|
|
|
|
|
|
|
w, h = img.size
|
|
|
scale = min(800/w, 500/h, 1)
|
|
|
d_img = ctk.CTkImage(img, img, size=(int(w*scale), int(h*scale)))
|
|
|
self.gen_img_lbl.configure(image=d_img, text="")
|
|
|
|
|
|
|
|
|
for i in self.tree.get_children(): self.tree.delete(i)
|
|
|
if transitions:
|
|
|
cols = list(transitions[0].keys())
|
|
|
self.tree["columns"] = cols
|
|
|
for c in cols:
|
|
|
self.tree.heading(c, text=c)
|
|
|
self.tree.column(c, width=90)
|
|
|
for row in transitions:
|
|
|
self.tree.insert("", "end", values=list(row.values()))
|
|
|
|
|
|
|
|
|
self._add_to_history(p)
|
|
|
|
|
|
except Exception as e:
|
|
|
self.logic_lbl.configure(text=f"Error: {str(e)[:20]}...")
|
|
|
|
|
|
def _add_to_history(self, text):
|
|
|
t = datetime.datetime.now().strftime("%H:%M")
|
|
|
btn = ctk.CTkButton(self.history_scroll, text=f"{t} - {text}", fg_color="transparent", border_width=1, anchor="w", command=lambda: self._recall(text))
|
|
|
btn.pack(fill="x", pady=2)
|
|
|
|
|
|
def _recall(self, text):
|
|
|
self.prompt.delete(0, "end")
|
|
|
self.prompt.insert(0, text)
|
|
|
self.generate()
|
|
|
|
|
|
def scan(self):
|
|
|
path = filedialog.askopenfilename()
|
|
|
if not path: return
|
|
|
img = Image.open(path)
|
|
|
w, h = img.size
|
|
|
scale = min(600/w, 400/h)
|
|
|
d_img = ctk.CTkImage(img, img, size=(int(w*scale), int(h*scale)))
|
|
|
self.scan_img_container.configure(image=d_img)
|
|
|
|
|
|
label, conf = self.core.predict_image(path)
|
|
|
self.scan_lbl.configure(text=f"Detected: {label} ({conf*100:.1f}%)", text_color="#00ff00")
|
|
|
|
|
|
def save_img(self):
|
|
|
if self.current_dot:
|
|
|
f = filedialog.asksaveasfilename(defaultextension=".png")
|
|
|
if f: self.current_dot.render(f.replace(".png",""), format='png', cleanup=True)
|
|
|
|
|
|
def save_csv(self):
|
|
|
if self.current_table:
|
|
|
f = filedialog.asksaveasfilename(defaultextension=".csv")
|
|
|
if f: pd.DataFrame(self.current_table).to_csv(f, index=False)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
app = ProStudio()
|
|
|
app.mainloop() |