File size: 3,688 Bytes
b81292f
8a7793a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

def fix_code(code_input, language, fix_mode):
    """
    Placeholder function to 'fix' the code.
    In a real scenario, you'd integrate your LLM or custom logic here.
    """
    # For demonstration, just returning a mock "fixed" version
    fixed_code = f"# Fixed [{language} - {fix_mode}]\n" + code_input
    explanation = (
        "Here's a mock explanation of what was fixed:\n"
        "1. Analyzed the code.\n"
        "2. Applied debugging/optimization.\n"
        "3. Returned updated code."
    )
    return explanation, fixed_code

def run_code(code_input, language):
    """
    Run the corrected code in a sandbox or a safe environment.
    Here, we just pretend to run it and return a mock output.
    
    IMPORTANT: Executing arbitrary code can be dangerous. 
    In real-world apps, you need a secure sandbox environment.
    """
    # Mock execution output
    return f"Running [{language}] code...\nOutput:\nHello from the mock run!\n\nCode:\n{code_input}"

def chat_mode_interaction(user_input):
    """
    Example 'Chat Mode' function.
    """
    # Return a mock chat response
    return f"You said: {user_input}\n\nAI says: This is a placeholder chat response."

# Build the UI
with gr.Blocks(title="AI Code Doctor - Fix, Optimize & Run") as demo:
    gr.Markdown(
        """
        # AI Code Doctor - Fix, Optimize & Run
        **The Ultimate Debugging Machine**  
        """
    )

    # Tabs for Code Debug vs. Chat Mode
    with gr.Tab("Code Debug"):
        with gr.Row():
            with gr.Column():
                language = gr.Dropdown(
                    label="Select Language",
                    choices=["Python", "JavaScript", "C++", "Mistral", "Any"],
                    value="Python"
                )
                fix_mode = gr.Dropdown(
                    label="Select Fix Mode",
                    choices=["Fix Errors Only", "Fix + Optimize", "Fix + Explain"],
                    value="Fix + Explain"
                )
                code_input = gr.Textbox(
                    label="Paste Your Code",
                    lines=10,
                    placeholder="Enter code here..."
                )
                
                # Buttons
                fix_button = gr.Button("⚙️ Fix Code")
                
                # AI Output
                ai_explanation = gr.Textbox(
                    label="AI Explanation",
                    lines=5,
                    interactive=False
                )
                corrected_code = gr.Textbox(
                    label="Corrected Code",
                    lines=10,
                    interactive=False
                )

            with gr.Column():
                execution_output = gr.Textbox(
                    label="Execution Output",
                    lines=15,
                    interactive=False
                )
                run_button = gr.Button("▶️ Run My Code")
        
        # Define button actions
        fix_button.click(
            fix_code, 
            inputs=[code_input, language, fix_mode], 
            outputs=[ai_explanation, corrected_code]
        )
        run_button.click(
            run_code,
            inputs=[corrected_code, language],
            outputs=execution_output
        )

    with gr.Tab("Chat Mode"):
        chat_input = gr.Textbox(label="Enter your message", lines=2)
        chat_output = gr.Textbox(label="Chat Output", lines=5, interactive=False)
        chat_button = gr.Button("Send")
        
        chat_button.click(
            chat_mode_interaction,
            inputs=chat_input,
            outputs=chat_output
        )

demo.launch()