ShiroOnigami23 commited on
Commit
045c3ed
·
verified ·
1 Parent(s): 2869e46

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +189 -0
app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import customtkinter as ctk
2
+ from tkinter import filedialog, ttk
3
+ from PIL import Image
4
+ import os
5
+ import tempfile
6
+ import pandas as pd
7
+ from automata_core import AutomataCore
8
+ import datetime
9
+
10
+ ctk.set_appearance_mode("Dark")
11
+ ctk.set_default_color_theme("dark-blue")
12
+
13
+ class ProStudio(ctk.CTk):
14
+ def __init__(self):
15
+ super().__init__()
16
+ self.title("Automata Studio Pro X")
17
+ self.geometry("1400x900")
18
+ self.core = AutomataCore()
19
+
20
+ self.history = []
21
+ self.current_dot = None
22
+ self.current_table = []
23
+
24
+ # --- LAYOUT CONFIG ---
25
+ self.grid_columnconfigure(1, weight=1)
26
+ self.grid_rowconfigure(0, weight=1)
27
+
28
+ # 1. SIDEBAR (Navigation & History)
29
+ self.sidebar = ctk.CTkFrame(self, width=280, corner_radius=0, fg_color="#1a1a1a")
30
+ self.sidebar.grid(row=0, column=0, sticky="nsew")
31
+
32
+ # Logo Area
33
+ self.logo = ctk.CTkLabel(self.sidebar, text="NEURAL\nAUTOMATA X", font=("Montserrat", 28, "bold"), text_color="#3B8ED0")
34
+ self.logo.pack(pady=40)
35
+
36
+ # Status Indicators
37
+ self.status_frame = ctk.CTkFrame(self.sidebar, fg_color="transparent")
38
+ self.status_frame.pack(pady=10)
39
+ self._add_status(self.core.vision_model, "Vision Eye")
40
+ self._add_status(self.core.logic_model, "Logic Brain")
41
+
42
+ ctk.CTkLabel(self.sidebar, text="HISTORY", font=("Arial", 12, "bold"), text_color="gray").pack(pady=(30,10), anchor="w", padx=20)
43
+ self.history_scroll = ctk.CTkScrollableFrame(self.sidebar, fg_color="transparent", height=400)
44
+ self.history_scroll.pack(fill="x", padx=10)
45
+
46
+ # 2. MAIN CONTENT
47
+ self.main_view = ctk.CTkTabview(self, fg_color="#2b2b2b", corner_radius=20)
48
+ self.main_view.grid(row=0, column=1, padx=20, pady=20, sticky="nsew")
49
+
50
+ self.tab_gen = self.main_view.add(" Create Machine ")
51
+ self.tab_scan = self.main_view.add(" Scan Diagram ")
52
+
53
+ self._setup_generator()
54
+ self._setup_scanner()
55
+
56
+ def _add_status(self, model, name):
57
+ color = "#00ff00" if model else "#ff0000"
58
+ status = "ONLINE" if model else "OFFLINE"
59
+ lbl = ctk.CTkLabel(self.status_frame, text=f"● {name}: {status}", text_color=color, font=("Arial", 10))
60
+ lbl.pack(anchor="w")
61
+
62
+ def _setup_generator(self):
63
+ # Input Area
64
+ input_frame = ctk.CTkFrame(self.tab_gen, fg_color="transparent")
65
+ input_frame.pack(pady=20, fill="x", padx=40)
66
+
67
+ self.prompt = ctk.CTkEntry(input_frame, placeholder_text="Describe your machine... (e.g. 'DFA ending with 010')", height=50, font=("Arial", 16))
68
+ self.prompt.pack(side="left", fill="x", expand=True, padx=(0, 20))
69
+ self.prompt.bind("<Return>", lambda e: self.generate())
70
+
71
+ btn = ctk.CTkButton(input_frame, text="GENERATE", width=150, height=50, fg_color="#3B8ED0", font=("Arial", 14, "bold"), command=self.generate)
72
+ btn.pack(side="right")
73
+
74
+ # Results Split (Diagram vs Table)
75
+ split_frame = ctk.CTkFrame(self.tab_gen, fg_color="transparent")
76
+ split_frame.pack(fill="both", expand=True, padx=20, pady=10)
77
+
78
+ # Left: Diagram
79
+ self.img_frame = ctk.CTkFrame(split_frame, fg_color="#1e1e1e", corner_radius=15)
80
+ self.img_frame.pack(side="left", fill="both", expand=True, padx=(0,10))
81
+ self.gen_img_lbl = ctk.CTkLabel(self.img_frame, text="Visualization Area", text_color="gray")
82
+ self.gen_img_lbl.pack(expand=True)
83
+
84
+ # Right: Table & Stats
85
+ self.data_frame = ctk.CTkFrame(split_frame, width=350, fg_color="#1e1e1e", corner_radius=15)
86
+ self.data_frame.pack(side="right", fill="y")
87
+
88
+ ctk.CTkLabel(self.data_frame, text="LOGIC REPORT", font=("Arial", 14, "bold")).pack(pady=15)
89
+ self.logic_lbl = ctk.CTkLabel(self.data_frame, text="Waiting...", text_color="#3B8ED0")
90
+ self.logic_lbl.pack(pady=5)
91
+
92
+ # Export Buttons
93
+ ctk.CTkButton(self.data_frame, text="Save Image", command=self.save_img).pack(pady=(20,10), padx=20, fill="x")
94
+ ctk.CTkButton(self.data_frame, text="Save CSV", command=self.save_csv).pack(pady=10, padx=20, fill="x")
95
+
96
+ # Table
97
+ self.tree_frame = ctk.CTkFrame(self.data_frame, fg_color="transparent")
98
+ self.tree_frame.pack(fill="both", expand=True, padx=10, pady=10)
99
+
100
+ style = ttk.Style()
101
+ style.theme_use("clam")
102
+ style.configure("Treeview", background="#2b2b2b", foreground="white", fieldbackground="#2b2b2b", rowheight=25)
103
+ style.configure("Treeview.Heading", background="#3B8ED0", foreground="white")
104
+
105
+ self.tree = ttk.Treeview(self.tree_frame, show="headings", height=15)
106
+ self.tree.pack(fill="both", expand=True)
107
+
108
+ def _setup_scanner(self):
109
+ btn = ctk.CTkButton(self.tab_scan, text="UPLOAD IMAGE", height=50, command=self.scan)
110
+ btn.pack(pady=40)
111
+ self.scan_lbl = ctk.CTkLabel(self.tab_scan, text="", font=("Arial", 20))
112
+ self.scan_lbl.pack()
113
+ self.scan_img_container = ctk.CTkLabel(self.tab_scan, text="")
114
+ self.scan_img_container.pack(expand=True, pady=20)
115
+
116
+ def generate(self):
117
+ p = self.prompt.get()
118
+ if not p: return
119
+
120
+ try:
121
+ dot, transitions, name = self.core.generate_system(p)
122
+ self.current_dot = dot
123
+ self.current_table = transitions
124
+ self.logic_lbl.configure(text=name)
125
+
126
+ # 1. Update Image
127
+ with tempfile.TemporaryDirectory() as tmp:
128
+ path = os.path.join(tmp, "machine")
129
+ dot.render(path, cleanup=True)
130
+ img = Image.open(path + ".png")
131
+
132
+ # Pro scaling
133
+ w, h = img.size
134
+ scale = min(800/w, 500/h, 1)
135
+ d_img = ctk.CTkImage(img, img, size=(int(w*scale), int(h*scale)))
136
+ self.gen_img_lbl.configure(image=d_img, text="")
137
+
138
+ # 2. Update Table
139
+ for i in self.tree.get_children(): self.tree.delete(i)
140
+ if transitions:
141
+ cols = list(transitions[0].keys())
142
+ self.tree["columns"] = cols
143
+ for c in cols:
144
+ self.tree.heading(c, text=c)
145
+ self.tree.column(c, width=90)
146
+ for row in transitions:
147
+ self.tree.insert("", "end", values=list(row.values()))
148
+
149
+ # 3. Add to History
150
+ self._add_to_history(p)
151
+
152
+ except Exception as e:
153
+ self.logic_lbl.configure(text=f"Error: {str(e)[:20]}...")
154
+
155
+ def _add_to_history(self, text):
156
+ t = datetime.datetime.now().strftime("%H:%M")
157
+ btn = ctk.CTkButton(self.history_scroll, text=f"{t} - {text}", fg_color="transparent", border_width=1, anchor="w", command=lambda: self._recall(text))
158
+ btn.pack(fill="x", pady=2)
159
+
160
+ def _recall(self, text):
161
+ self.prompt.delete(0, "end")
162
+ self.prompt.insert(0, text)
163
+ self.generate()
164
+
165
+ def scan(self):
166
+ path = filedialog.askopenfilename()
167
+ if not path: return
168
+ img = Image.open(path)
169
+ w, h = img.size
170
+ scale = min(600/w, 400/h)
171
+ d_img = ctk.CTkImage(img, img, size=(int(w*scale), int(h*scale)))
172
+ self.scan_img_container.configure(image=d_img)
173
+
174
+ label, conf = self.core.predict_image(path)
175
+ self.scan_lbl.configure(text=f"Detected: {label} ({conf*100:.1f}%)", text_color="#00ff00")
176
+
177
+ def save_img(self):
178
+ if self.current_dot:
179
+ f = filedialog.asksaveasfilename(defaultextension=".png")
180
+ if f: self.current_dot.render(f.replace(".png",""), format='png', cleanup=True)
181
+
182
+ def save_csv(self):
183
+ if self.current_table:
184
+ f = filedialog.asksaveasfilename(defaultextension=".csv")
185
+ if f: pd.DataFrame(self.current_table).to_csv(f, index=False)
186
+
187
+ if __name__ == "__main__":
188
+ app = ProStudio()
189
+ app.mainloop()