Upgrade and revise this script ensuring the logic flows as intended for an autonomous code debugging system. import tkinter as tk 
from tkinter import ttk, scrolledtext
from gradio_client import Client
import re
import uuid
import subprocess
import os

# Initialize Gradio Client
client = Client("yuntian-deng/o1mini")
MAX_RETRIES = 5

# Function to execute a script and capture output
def execute_code(filename):
    try:
        result = subprocess.run(["python", filename], capture_output=True, text=True)
        return result.returncode == 0, result.stdout if result.returncode == 0 else result.stderr
    except Exception as e:
        return False, str(e)

# Function to save code to a temporary file
def save_code(code):
    filename = f"temp_{uuid.uuid4().hex}.py"
    with open(filename, "w") as file:
        file.write(code)
    return filename

# Function to fix code using the chatbot
def fix_code(original_code, error_message):
    prompt = (
        f"An error occurred:\n{error_message}\n\n"
        f"Please provide a corrected script."
    )
    fixed_code = client.predict(
        inputs=prompt,
        top_p=1.0,
        temperature=1.0,
        chat_counter=0.0,
        chatbot=[],
        api_name="/predict"
    )
    return fixed_code[0]  # Assuming the response is the corrected code

# Modified process_code function with Parser_Code and Debugger calls
def process_code(initial_code):
    current_code = initial_code
    for attempt in range(1, MAX_RETRIES + 1):
        filename = save_code(current_code)
        
        # Run the main code first
        success, output = execute_code(filename)
        os.remove(filename)
        
        if success:
            chatbox.configure(state='normal')
            chatbox.insert(tk.END, f"[Execution Success]:\n{output}\n", "success")
            chatbox.configure(state='disabled')
            status_var.set("Script executed successfully.")
            return
        else:
            chatbox.configure(state='normal')
            chatbox.insert(tk.END, f"[Error Attempt {attempt}]:\n{output}\n", "error")
            chatbox.configure(state='disabled')
            status_var.set(f"Error on attempt {attempt}.")
            
            # Attempt to fix with Parser_Code.py
            parser_output = subprocess.run(["python", "Parser_Code.py"], capture_output=True, text=True)
            if parser_output.returncode == 0:
                chatbox.insert(tk.END, "[Parser_Code Success]:\n" + parser_output.stdout + "\n", "info")
                chatbox.configure(state='disabled')
                continue  # Proceed with the parsed code
            
            # If Parser_Code fails, attempt Debugger.py
            debugger_output = subprocess.run(["python", "Debugger.py"], capture_output=True, text=True)
            if debugger_output.returncode == 0:
                chatbox.insert(tk.END, "[Debugger Success]:\n" + debugger_output.stdout + "\n", "info")
                chatbox.configure(state='disabled')
                continue  # Proceed with the debugged code
            
            # If both Parser_Code and Debugger fail, use the chatbot to fix the code
            if attempt < MAX_RETRIES:
                chatbox.configure(state='normal')
                chatbox.insert(tk.END, "Attempting to fix the error using the chatbot...\n", "info")
                chatbox.configure(state='disabled')
                current_code = fix_code(current_code, output)
            else:
                chatbox.configure(state='normal')
                chatbox.insert(tk.END, "Max retries reached. Manual check required.\n", "error")
                chatbox.configure(state='disabled')
                status_var.set("Max retries reached. Manual intervention needed.")
                return

# Function to handle sending messages
def send_message():
    user_input = input_text.get()
    if not user_input.strip():
        status_var.set("Please enter a message to send.")
        return
    chatbox.configure(state='normal')
    chatbox.insert(tk.END, f"User: {user_input}\n", "user")
    chatbox.configure(state='disabled')
    status_var.set("Sending message...")
    root.update_idletasks()
    result = client.predict(
        inputs=user_input,
        top_p=1.0,
        temperature=1.0,
        chat_counter=0.0,
        chatbot=[],
        api_name="/predict"
    )
    display_result(result)
    bot_response = result[0]
    codes = extract_code(bot_response)
    if codes:
        for code in codes:
            process_code(code)
    else:
        chatbox.configure(state='normal')
        chatbox.insert(tk.END, "Bot: No Python code detected in the response.\n", "info")
        chatbox.configure(state='disabled')
    status_var.set("Message sent.")

# Rest of the code for GUI setup remains unchanged
# Initialize Tkinter root window
root = tk.Tk()
root.title("Chatbot Interface with Python Script Executor")
root.geometry("900x600")
root.minsize(700, 500)

# Apply a modern ttk theme
style = ttk.Style(root)
style.theme_use('clam')  # You can choose other themes like 'alt', 'default', 'classic'

# Configure grid layout
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

# Main frame
main_frame = ttk.Frame(root, padding="10 10 10 10")
main_frame.grid(row=0, column=0, sticky='NSEW')

main_frame.columnconfigure(0, weight=1)
main_frame.rowconfigure(1, weight=1)

# Chatbox Label
chat_label = ttk.Label(main_frame, text="Chat History", font=("Helvetica", 12, "bold"))
chat_label.grid(row=0, column=0, sticky='W')

# Chatbox ScrolledText
chatbox = scrolledtext.ScrolledText(main_frame, wrap=tk.WORD, height=20, width=80, state='disabled', font=("Helvetica", 10))
chatbox.grid(row=1, column=0, columnspan=3, sticky='NSEW', pady=(5, 10))
chatbox.tag_configure("user", foreground="blue")
chatbox.tag_configure("bot", foreground="green")
chatbox.tag_configure("system", foreground="purple")
chatbox.tag_configure("error", foreground="red")
chatbox.tag_configure("success", foreground="dark green")
chatbox.tag_configure("info", foreground="orange")
chatbox.tag_configure("status", foreground="grey")

# Continue setting up the Tkinter GUI...

root.mainloop()