Spaces:
Running
Running
File size: 3,786 Bytes
dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b dd4ac10 d9db28b |
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 |
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*")
|