|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import scrolledtext |
| 3 | +from brion import Brion |
| 4 | + |
| 5 | +class BrionApp: |
| 6 | + def __init__(self, root): |
| 7 | + self.root = root |
| 8 | + self.root.title("Brion Assistant") |
| 9 | + self.root.geometry("600x600") |
| 10 | + |
| 11 | + self.output_box = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=70, height=30) |
| 12 | + self.output_box.pack(pady=10) |
| 13 | + |
| 14 | + self.entry = tk.Entry(root, width=70) |
| 15 | + self.entry.pack(pady=5) |
| 16 | + self.entry.bind('<Return>', lambda event: self.handle_input()) |
| 17 | + |
| 18 | + self.send_button = tk.Button(root, text="Send", command=self.handle_input) |
| 19 | + self.send_button.pack(pady=5) |
| 20 | + |
| 21 | + self.brion = Brion() |
| 22 | + |
| 23 | + def handle_input(self): |
| 24 | + user_input = self.entry.get() |
| 25 | + if not user_input.strip(): |
| 26 | + return |
| 27 | + self.output_box.insert(tk.END, f"You: {user_input}\n") |
| 28 | + self.entry.delete(0, tk.END) |
| 29 | + |
| 30 | + code = self.brion.generate_code(user_input) |
| 31 | + self.output_box.insert(tk.END, f"Assistant:\n{code}\n\n", 'code') |
| 32 | + self.output_box.tag_config('code', font=('Courier', 10), foreground='blue') |
| 33 | + |
| 34 | + # Attempt to execute the code |
| 35 | + self.execute_generated_code(code) |
| 36 | + |
| 37 | + def execute_generated_code(self, code): |
| 38 | + self.output_box.insert(tk.END, "Executing generated code...\n") |
| 39 | + try: |
| 40 | + exec_locals = {} |
| 41 | + exec(code, {}, exec_locals) |
| 42 | + output = exec_locals.get('output', '') |
| 43 | + if output: |
| 44 | + self.output_box.insert(tk.END, f"Execution Output:\n{output}\n\n") |
| 45 | + else: |
| 46 | + self.output_box.insert(tk.END, "Code executed successfully.\n\n") |
| 47 | + except Exception as e: |
| 48 | + self.output_box.insert(tk.END, f"Error during execution: {e}\n\n", 'error') |
| 49 | + self.output_box.tag_config('error', foreground='red') |
| 50 | + |
| 51 | + # Attempt to correct the code |
| 52 | + self.output_box.insert(tk.END, "Attempting to correct the code...\n") |
| 53 | + corrected_code = self.brion.correct_code(code, str(e)) |
| 54 | + self.output_box.insert(tk.END, f"Assistant Corrected Code:\n{corrected_code}\n\n", 'code') |
| 55 | + self.output_box.tag_config('code', font=('Courier', 10), foreground='green') |
| 56 | + |
| 57 | + # Re-attempt execution with corrected code |
| 58 | + self.output_box.insert(tk.END, "Re-executing corrected code...\n") |
| 59 | + try: |
| 60 | + exec_locals = {} |
| 61 | + exec(corrected_code, {}, exec_locals) |
| 62 | + output = exec_locals.get('output', '') |
| 63 | + if output: |
| 64 | + self.output_box.insert(tk.END, f"Execution Output:\n{output}\n\n") |
| 65 | + else: |
| 66 | + self.output_box.insert(tk.END, "Corrected code executed successfully.\n\n") |
| 67 | + except Exception as e: |
| 68 | + self.output_box.insert(tk.END, f"Corrected code failed: {e}\n\n", 'error') |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + root = tk.Tk() |
| 72 | + app = BrionApp(root) |
| 73 | + root.mainloop() |
0 commit comments