Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
|
3 |
import os
|
4 |
|
5 |
# Initialize Hugging Face model and tokenizer
|
@@ -25,32 +27,41 @@ def analyze_code(code):
|
|
25 |
|
26 |
return results
|
27 |
|
28 |
-
# Function to
|
29 |
-
|
30 |
-
def check_code_quality_and_bugs(code):
|
31 |
suggestions = []
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
suggestions.append("Code seems too short, ensure functionality is implemented correctly.")
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
suggestions.append("`try` block without `except` may lead to unhandled exceptions.")
|
48 |
|
49 |
-
|
|
|
|
|
|
|
50 |
|
51 |
# Streamlit app UI
|
52 |
-
st.title("Code Quality
|
53 |
-
st.markdown("Analyze your code for syntax issues, quality, and
|
54 |
|
55 |
# File uploader
|
56 |
uploaded_file = st.file_uploader("Upload a Python code file", type=["py"])
|
@@ -58,7 +69,7 @@ uploaded_file = st.file_uploader("Upload a Python code file", type=["py"])
|
|
58 |
# Code snippet input
|
59 |
code_snippet = st.text_area("Or paste your code snippet below:")
|
60 |
|
61 |
-
if st.button("Analyze Code"):
|
62 |
if uploaded_file is not None:
|
63 |
code = uploaded_file.read().decode("utf-8")
|
64 |
elif code_snippet.strip():
|
@@ -67,16 +78,20 @@ if st.button("Analyze Code"):
|
|
67 |
st.error("Please upload a file or paste code to analyze.")
|
68 |
st.stop()
|
69 |
|
70 |
-
# Perform code analysis
|
71 |
st.subheader("Analysis Results")
|
72 |
|
73 |
st.write("**Code Quality and Bug Suggestions:**")
|
74 |
-
suggestions =
|
75 |
if suggestions:
|
76 |
for i, suggestion in enumerate(suggestions, 1):
|
77 |
st.write(f"{i}. {suggestion}")
|
78 |
else:
|
79 |
-
st.write("No issues detected. Your code looks good!")
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# Simulated CodeBERT analysis (placeholder)
|
82 |
st.write("**Model Analysis:**")
|
@@ -86,3 +101,4 @@ if st.button("Analyze Code"):
|
|
86 |
|
87 |
st.markdown("---")
|
88 |
st.markdown("*Powered by Hugging Face and Streamlit*")
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import ast
|
4 |
+
import astor
|
5 |
import os
|
6 |
|
7 |
# Initialize Hugging Face model and tokenizer
|
|
|
27 |
|
28 |
return results
|
29 |
|
30 |
+
# Function to detect and fix bugs
|
31 |
+
def detect_and_fix_bugs(code):
|
|
|
32 |
suggestions = []
|
33 |
+
fixed_code = code
|
34 |
|
35 |
+
try:
|
36 |
+
tree = ast.parse(code)
|
|
|
37 |
|
38 |
+
# Example: Check for missing except blocks in try statements
|
39 |
+
for node in ast.walk(tree):
|
40 |
+
if isinstance(node, ast.Try):
|
41 |
+
if not any(isinstance(handler, ast.ExceptHandler) for handler in node.handlers):
|
42 |
+
suggestions.append("Add an 'except' block to handle exceptions in the 'try' statement.")
|
43 |
+
# Add a generic except block as a fix
|
44 |
+
new_handler = ast.ExceptHandler(type=None, name=None, body=[ast.Pass()])
|
45 |
+
node.handlers.append(new_handler)
|
46 |
|
47 |
+
# Example: Check for missing comments
|
48 |
+
if not any(isinstance(node, ast.Expr) and isinstance(node.value, ast.Str) for node in tree.body):
|
49 |
+
suggestions.append("Consider adding a module-level docstring or comments to improve code clarity.")
|
50 |
+
# Add a module-level docstring as a fix
|
51 |
+
docstring = ast.Expr(value=ast.Str(s="""Add a description of the module here."""))
|
52 |
+
tree.body.insert(0, docstring)
|
53 |
|
54 |
+
# Convert the modified AST back to code
|
55 |
+
fixed_code = astor.to_source(tree)
|
|
|
56 |
|
57 |
+
except Exception as e:
|
58 |
+
suggestions.append(f"Error analyzing code: {e}")
|
59 |
+
|
60 |
+
return suggestions, fixed_code
|
61 |
|
62 |
# Streamlit app UI
|
63 |
+
st.title("Code Quality, Bug Detection, and Auto-Correction Tool")
|
64 |
+
st.markdown("Analyze your code for syntax issues, quality, bugs, and get suggested corrections.")
|
65 |
|
66 |
# File uploader
|
67 |
uploaded_file = st.file_uploader("Upload a Python code file", type=["py"])
|
|
|
69 |
# Code snippet input
|
70 |
code_snippet = st.text_area("Or paste your code snippet below:")
|
71 |
|
72 |
+
if st.button("Analyze and Fix Code"):
|
73 |
if uploaded_file is not None:
|
74 |
code = uploaded_file.read().decode("utf-8")
|
75 |
elif code_snippet.strip():
|
|
|
78 |
st.error("Please upload a file or paste code to analyze.")
|
79 |
st.stop()
|
80 |
|
81 |
+
# Perform code analysis and bug fixing
|
82 |
st.subheader("Analysis Results")
|
83 |
|
84 |
st.write("**Code Quality and Bug Suggestions:**")
|
85 |
+
suggestions, fixed_code = detect_and_fix_bugs(code)
|
86 |
if suggestions:
|
87 |
for i, suggestion in enumerate(suggestions, 1):
|
88 |
st.write(f"{i}. {suggestion}")
|
89 |
else:
|
90 |
+
st.write("No major issues detected. Your code looks good!")
|
91 |
+
|
92 |
+
# Display corrected code
|
93 |
+
st.subheader("Corrected Code:")
|
94 |
+
st.code(fixed_code, language="python")
|
95 |
|
96 |
# Simulated CodeBERT analysis (placeholder)
|
97 |
st.write("**Model Analysis:**")
|
|
|
101 |
|
102 |
st.markdown("---")
|
103 |
st.markdown("*Powered by Hugging Face and Streamlit*")
|
104 |
+
|