Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,111 @@
|
|
1 |
import gradio as gr
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
|
3 |
+
def fix_code(code_input, language, fix_mode):
|
4 |
+
"""
|
5 |
+
Placeholder function to 'fix' the code.
|
6 |
+
In a real scenario, you'd integrate your LLM or custom logic here.
|
7 |
+
"""
|
8 |
+
# For demonstration, just returning a mock "fixed" version
|
9 |
+
fixed_code = f"# Fixed [{language} - {fix_mode}]\n" + code_input
|
10 |
+
explanation = (
|
11 |
+
"Here's a mock explanation of what was fixed:\n"
|
12 |
+
"1. Analyzed the code.\n"
|
13 |
+
"2. Applied debugging/optimization.\n"
|
14 |
+
"3. Returned updated code."
|
15 |
+
)
|
16 |
+
return explanation, fixed_code
|
17 |
+
|
18 |
+
def run_code(code_input, language):
|
19 |
+
"""
|
20 |
+
Run the corrected code in a sandbox or a safe environment.
|
21 |
+
Here, we just pretend to run it and return a mock output.
|
22 |
+
|
23 |
+
IMPORTANT: Executing arbitrary code can be dangerous.
|
24 |
+
In real-world apps, you need a secure sandbox environment.
|
25 |
+
"""
|
26 |
+
# Mock execution output
|
27 |
+
return f"Running [{language}] code...\nOutput:\nHello from the mock run!\n\nCode:\n{code_input}"
|
28 |
+
|
29 |
+
def chat_mode_interaction(user_input):
|
30 |
+
"""
|
31 |
+
Example 'Chat Mode' function.
|
32 |
+
"""
|
33 |
+
# Return a mock chat response
|
34 |
+
return f"You said: {user_input}\n\nAI says: This is a placeholder chat response."
|
35 |
+
|
36 |
+
# Build the UI
|
37 |
+
with gr.Blocks(title="AI Code Doctor - Fix, Optimize & Run") as demo:
|
38 |
+
gr.Markdown(
|
39 |
+
"""
|
40 |
+
# AI Code Doctor - Fix, Optimize & Run
|
41 |
+
**The Ultimate Debugging Machine**
|
42 |
+
"""
|
43 |
+
)
|
44 |
+
|
45 |
+
# Tabs for Code Debug vs. Chat Mode
|
46 |
+
with gr.Tab("Code Debug"):
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
language = gr.Dropdown(
|
50 |
+
label="Select Language",
|
51 |
+
choices=["Python", "JavaScript", "C++", "Mistral", "Any"],
|
52 |
+
value="Python"
|
53 |
+
)
|
54 |
+
fix_mode = gr.Dropdown(
|
55 |
+
label="Select Fix Mode",
|
56 |
+
choices=["Fix Errors Only", "Fix + Optimize", "Fix + Explain"],
|
57 |
+
value="Fix + Explain"
|
58 |
+
)
|
59 |
+
code_input = gr.Textbox(
|
60 |
+
label="Paste Your Code",
|
61 |
+
lines=10,
|
62 |
+
placeholder="Enter code here..."
|
63 |
+
)
|
64 |
+
|
65 |
+
# Buttons
|
66 |
+
fix_button = gr.Button("⚙️ Fix Code")
|
67 |
+
|
68 |
+
# AI Output
|
69 |
+
ai_explanation = gr.Textbox(
|
70 |
+
label="AI Explanation",
|
71 |
+
lines=5,
|
72 |
+
interactive=False
|
73 |
+
)
|
74 |
+
corrected_code = gr.Textbox(
|
75 |
+
label="Corrected Code",
|
76 |
+
lines=10,
|
77 |
+
interactive=False
|
78 |
+
)
|
79 |
+
|
80 |
+
with gr.Column():
|
81 |
+
execution_output = gr.Textbox(
|
82 |
+
label="Execution Output",
|
83 |
+
lines=15,
|
84 |
+
interactive=False
|
85 |
+
)
|
86 |
+
run_button = gr.Button("▶️ Run My Code")
|
87 |
+
|
88 |
+
# Define button actions
|
89 |
+
fix_button.click(
|
90 |
+
fix_code,
|
91 |
+
inputs=[code_input, language, fix_mode],
|
92 |
+
outputs=[ai_explanation, corrected_code]
|
93 |
+
)
|
94 |
+
run_button.click(
|
95 |
+
run_code,
|
96 |
+
inputs=[corrected_code, language],
|
97 |
+
outputs=execution_output
|
98 |
+
)
|
99 |
+
|
100 |
+
with gr.Tab("Chat Mode"):
|
101 |
+
chat_input = gr.Textbox(label="Enter your message", lines=2)
|
102 |
+
chat_output = gr.Textbox(label="Chat Output", lines=5, interactive=False)
|
103 |
+
chat_button = gr.Button("Send")
|
104 |
+
|
105 |
+
chat_button.click(
|
106 |
+
chat_mode_interaction,
|
107 |
+
inputs=chat_input,
|
108 |
+
outputs=chat_output
|
109 |
+
)
|
110 |
+
|
111 |
+
demo.launch()
|