S-Dreamer's picture
Update app.py
6db3950 verified
raw
history blame
1.69 kB
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()