Spaces:
Running
Running
File size: 5,786 Bytes
87ce049 65f9a95 87ce049 65f9a95 87ce049 65f9a95 87ce049 65f9a95 87ce049 65f9a95 87ce049 65f9a95 835c4ff 65f9a95 87ce049 65f9a95 87ce049 65f9a95 87ce049 65f9a95 835c4ff |
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
import gradio as gr
from model_wrapper import CodeDebuggerWrapper
import logging
# Set up logging for the main app
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Global variable to store the debugger instance
debugger = None
def initialize_debugger():
"""Initialize the debugger with proper error handling."""
global debugger
try:
logger.info("π Initializing Code Debugger...")
debugger = CodeDebuggerWrapper()
logger.info("β
Debugger initialized successfully!")
return "β
Model loaded successfully!"
except Exception as e:
error_msg = f"β Failed to initialize debugger: {str(e)}"
logger.error(error_msg)
return error_msg
def debug_code(code: str):
"""Debug code with proper error handling."""
global debugger
if not code or not code.strip():
return "β Please paste some code to debug."
# Initialize debugger if not already done
if debugger is None:
init_result = initialize_debugger()
if "β" in init_result:
return init_result
try:
logger.info("π Processing code debug request...")
result = debugger.debug(code)
logger.info("β
Debug request completed")
return result
except Exception as e:
error_msg = f"β Error during model inference: {str(e)}"
logger.error(error_msg)
return error_msg
# Create the Gradio interface
def create_interface():
"""Create the Gradio interface."""
# Custom CSS for better styling
css = """
.gradio-container {
font-family: 'Arial', sans-serif;
}
.gr-button {
background: linear-gradient(45deg, #FF6B6B, #4ECDC4);
border: none;
color: white;
font-weight: bold;
}
.gr-button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
"""
with gr.Blocks(css=css, title="π AI Code Debugger", theme=gr.themes.Soft()) as demo:
# Header
gr.Markdown(
"""
# π AI Code Debugger
### Fine-tuned AI model for debugging Python code
**Instructions:**
1. Paste your Python code in the input box below
2. Click "π§ Debug Code" to get suggestions and fixes
3. The AI will analyze your code and provide improvements
---
"""
)
# Status indicator
status_box = gr.Textbox(
value="π Initializing model...",
label="π System Status",
interactive=False,
max_lines=2
)
# Main interface
with gr.Row():
with gr.Column(scale=1):
code_input = gr.Code(
language="python",
lines=15,
value="""# Paste your Python code here
# Example:
def fibonacci(n):
if n <= 1
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(10))""",
label="π Input Code",
elem_id="code_input"
)
debug_btn = gr.Button(
"π§ Debug Code",
variant="primary",
size="lg",
elem_id="debug_button"
)
with gr.Column(scale=1):
output = gr.Code(
language="python",
lines=15,
label="β¨ Debugged Code / Suggestions",
elem_id="code_output"
)
# Examples section
gr.Markdown("### π Example Issues to Try:")
examples = [
["""def calculate_average(numbers):
total = 0
for num in numbers:
total += num
return total / len(numbers)
# This will cause division by zero error
result = calculate_average([])
print(result)"""],
["""import math
def find_roots(a, b, c):
discriminant = b**2 - 4*a*c
root1 = (-b + math.sqrt(discriminant)) / (2*a)
root2 = (-b - math.sqrt(discriminant)) / (2*a)
return root1, root2
# This might have issues with negative discriminant
print(find_roots(1, 2, 5))"""],
["""def process_list(items):
result = []
for i in range(len(items)):
if items[i] > 0:
result.append(items[i] * 2)
return result
# Test the function
data = [1, -2, 3, -4, 5]
print(process_list(data))"""]
]
gr.Examples(
examples=examples,
inputs=code_input,
label="Click any example to load it:"
)
# Event handlers
debug_btn.click(
fn=debug_code,
inputs=code_input,
outputs=output
)
# Initialize on startup
demo.load(
fn=initialize_debugger,
outputs=status_box
)
# Footer
gr.Markdown(
"""
---
**Tips for better results:**
- Include complete, runnable code snippets
- Add comments explaining what you expect the code to do
- Include any error messages you're seeing
**Model:** `Girinath11/aiml_code_debug_model` | **Framework:** Transformers + Gradio
"""
)
return demo
# Main execution
if __name__ == "__main__":
# Create and launch the interface
demo = create_interface()
# Launch with appropriate settings for Hugging Face Spaces
demo.launch(
share=True) |