Spaces:
Running
Running
File size: 1,692 Bytes
2893fbd 6db3950 b637fc4 6db3950 b637fc4 6db3950 b637fc4 2893fbd 6db3950 2893fbd b637fc4 6db3950 |
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 |
import gradio as gr
import ast
import traceback
def optimize_and_debug(code):
"""Attempts to optimize and debug the given Python code."""
try:
tree = ast.parse(code)
# Rudimentary optimization: This is a placeholder. Real optimization is complex.
# Here we just check for unnecessary nested loops (a very simple example).
nested_loops = False
for node in ast.walk(tree):
if isinstance(node, ast.For) and any(isinstance(subnode, ast.For) for subnode in ast.walk(node)):
nested_loops = True
break
optimization_suggestions = []
if nested_loops:
optimization_suggestions.append("Consider optimizing nested loops. They can significantly impact performance.")
# Execution and error handling
exec(code, {}) # Execute the code (Caution: security implications for untrusted input)
return "Code executed successfully.", optimization_suggestions, ""
except Exception as e:
error_message = traceback.format_exc()
return "Code execution failed.", [], error_message
iface = gr.Interface(
fn=optimize_and_debug,
inputs=gr.Textbox(lines=10, label="Enter your Python code here:"),
outputs=[
gr.Textbox(label="Result"),
gr.Textbox(label="Optimization Suggestions"),
gr.Textbox(label="Error Messages (if any)"),
],
title="Python Code Optimizer & Debugger (Simplified)",
description="Paste your Python code below. This tool provides basic optimization suggestions and error reporting. Note: This is a simplified demonstration and does not replace a full debugger.",
)
iface.launch()
|