File size: 8,237 Bytes
045c3ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = []

        # --- LAYOUT CONFIG ---
        self.grid_columnconfigure(1, weight=1)
        self.grid_rowconfigure(0, weight=1)

        # 1. SIDEBAR (Navigation & History)
        self.sidebar = ctk.CTkFrame(self, width=280, corner_radius=0, fg_color="#1a1a1a")
        self.sidebar.grid(row=0, column=0, sticky="nsew")
        
        # Logo Area
        self.logo = ctk.CTkLabel(self.sidebar, text="NEURAL\nAUTOMATA X", font=("Montserrat", 28, "bold"), text_color="#3B8ED0")
        self.logo.pack(pady=40)
        
        # Status Indicators
        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)

        # 2. MAIN CONTENT
        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 Area
        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")
        
        # Results Split (Diagram vs Table)
        split_frame = ctk.CTkFrame(self.tab_gen, fg_color="transparent")
        split_frame.pack(fill="both", expand=True, padx=20, pady=10)
        
        # Left: Diagram
        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)
        
        # Right: Table & Stats
        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)
        
        # Export Buttons
        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")

        # Table
        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)
            
            # 1. Update Image
            with tempfile.TemporaryDirectory() as tmp:
                path = os.path.join(tmp, "machine")
                dot.render(path, cleanup=True)
                img = Image.open(path + ".png")
                
                # Pro scaling
                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="")

            # 2. Update Table
            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()))
            
            # 3. Add to History
            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()