Spaces:
Running
Running
import streamlit as st | |
from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
import ast | |
import astor | |
import os | |
# Initialize Hugging Face model and tokenizer | |
MODEL_NAME = "microsoft/codebert-base" | |
# Load the pre-trained CodeBERT model for understanding code | |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) | |
# Helper function to analyze code | |
def analyze_code(code): | |
# Split the code into manageable chunks | |
max_length = 512 | |
lines = code.split("\n") | |
chunks = ["\n".join(lines[i:i+max_length]) for i in range(0, len(lines), max_length)] | |
results = [] | |
for chunk in chunks: | |
tokenized_code = tokenizer(chunk, return_tensors="pt", truncation=True, max_length=max_length) | |
outputs = model(**tokenized_code) | |
logits = outputs.logits | |
results.append(logits.argmax(dim=1).item()) | |
return results | |
# Function to detect and fix bugs | |
def detect_and_fix_bugs(code): | |
suggestions = [] | |
fixed_code = code | |
try: | |
tree = ast.parse(code) | |
# Example: Check for missing except blocks in try statements | |
for node in ast.walk(tree): | |
if isinstance(node, ast.Try): | |
if not any(isinstance(handler, ast.ExceptHandler) for handler in node.handlers): | |
suggestions.append("Add an 'except' block to handle exceptions in the 'try' statement.") | |
# Add a generic except block as a fix | |
new_handler = ast.ExceptHandler(type=None, name=None, body=[ast.Pass()]) | |
node.handlers.append(new_handler) | |
# Example: Check for missing comments | |
if not any(isinstance(node, ast.Expr) and isinstance(node.value, ast.Str) for node in tree.body): | |
suggestions.append("Consider adding a module-level docstring or comments to improve code clarity.") | |
# Add a module-level docstring as a fix | |
docstring = ast.Expr(value=ast.Str(s="""Add a description of the module here.""")) | |
tree.body.insert(0, docstring) | |
# Convert the modified AST back to code | |
fixed_code = astor.to_source(tree) | |
except Exception as e: | |
suggestions.append(f"Error analyzing code: {e}") | |
return suggestions, fixed_code | |
# Streamlit app UI | |
st.title("Code Quality, Bug Detection, and Auto-Correction Tool") | |
st.markdown("Analyze your code for syntax issues, quality, bugs, and get suggested corrections.") | |
# File uploader | |
uploaded_file = st.file_uploader("Upload a Python code file", type=["py"]) | |
# Code snippet input | |
code_snippet = st.text_area("Or paste your code snippet below:") | |
if st.button("Analyze and Fix Code"): | |
if uploaded_file is not None: | |
code = uploaded_file.read().decode("utf-8") | |
elif code_snippet.strip(): | |
code = code_snippet | |
else: | |
st.error("Please upload a file or paste code to analyze.") | |
st.stop() | |
# Perform code analysis and bug fixing | |
st.subheader("Analysis Results") | |
st.write("**Code Quality and Bug Suggestions:**") | |
suggestions, fixed_code = detect_and_fix_bugs(code) | |
if suggestions: | |
for i, suggestion in enumerate(suggestions, 1): | |
st.write(f"{i}. {suggestion}") | |
else: | |
st.write("No major issues detected. Your code looks good!") | |
# Display corrected code | |
st.subheader("Corrected Code:") | |
st.code(fixed_code, language="python") | |
# Simulated CodeBERT analysis (placeholder) | |
st.write("**Model Analysis:**") | |
model_results = analyze_code(code) | |
for idx, result in enumerate(model_results, 1): | |
st.write(f"Chunk {idx} classification result: {result}") | |
st.markdown("---") | |
st.markdown("*Powered by Hugging Face and Streamlit*") | |