Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
|
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
import ast
|
4 |
import astor
|
5 |
-
import
|
6 |
|
7 |
# Initialize Hugging Face model and tokenizer
|
8 |
MODEL_NAME = "microsoft/codebert-base"
|
@@ -27,7 +27,7 @@ def analyze_code(code):
|
|
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
|
@@ -35,33 +35,77 @@ def detect_and_fix_bugs(code):
|
|
35 |
try:
|
36 |
tree = ast.parse(code)
|
37 |
|
38 |
-
#
|
39 |
for node in ast.walk(tree):
|
40 |
-
if isinstance(node, ast.
|
41 |
-
if not
|
42 |
-
suggestions.append("
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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: {
|
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"])
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
import ast
|
4 |
import astor
|
5 |
+
import traceback
|
6 |
|
7 |
# Initialize Hugging Face model and tokenizer
|
8 |
MODEL_NAME = "microsoft/codebert-base"
|
|
|
27 |
|
28 |
return results
|
29 |
|
30 |
+
# Function to detect and fix bugs, including logical errors
|
31 |
def detect_and_fix_bugs(code):
|
32 |
suggestions = []
|
33 |
fixed_code = code
|
|
|
35 |
try:
|
36 |
tree = ast.parse(code)
|
37 |
|
38 |
+
# Detect undefined variable usage
|
39 |
for node in ast.walk(tree):
|
40 |
+
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Load):
|
41 |
+
if node.id not in {n.id for n in ast.walk(tree) if isinstance(n, ast.Name) and isinstance(n.ctx, ast.Store)}:
|
42 |
+
suggestions.append(f"Variable '{node.id}' is used but not defined.")
|
43 |
+
|
44 |
+
undefined_variables = [
|
45 |
+
node.id for node in ast.walk(tree)
|
46 |
+
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Load) and
|
47 |
+
node.id not in {n.id for n in ast.walk(tree) if isinstance(n, ast.Name) and isinstance(n.ctx, ast.Store)}
|
48 |
+
]
|
49 |
+
for var in undefined_variables:
|
50 |
+
fix_statement = ast.Assign(targets=[ast.Name(id=var, ctx=ast.Store())], value=ast.Constant(value=None))
|
51 |
+
tree.body.insert(0, fix_statement)
|
52 |
+
suggestions.append(f"Added a definition for variable '{var}'.")
|
53 |
+
|
54 |
+
# Detect unused variables
|
55 |
+
assigned_vars = {n.id for n in ast.walk(tree) if isinstance(n, ast.Name) and isinstance(n.ctx, ast.Store)}
|
56 |
+
used_vars = {n.id for n in ast.walk(tree) if isinstance(n, ast.Name) and isinstance(n.ctx, ast.Load)}
|
57 |
+
unused_vars = assigned_vars - used_vars
|
58 |
+
for var in unused_vars:
|
59 |
+
suggestions.append(f"Variable '{var}' is defined but never used.")
|
60 |
+
|
61 |
+
# Detect missing imports
|
62 |
+
import_names = {n.name for n in ast.walk(tree) if isinstance(n, ast.Import)}
|
63 |
+
for node in ast.walk(tree):
|
64 |
+
if isinstance(node, ast.Call) and hasattr(node.func, 'id') and node.func.id not in import_names:
|
65 |
+
suggestions.append(f"Missing import for '{node.func.id}'.")
|
66 |
+
|
67 |
+
# Detect invalid function calls
|
68 |
+
for node in ast.walk(tree):
|
69 |
+
if isinstance(node, ast.Call):
|
70 |
+
if isinstance(node.func, ast.Name):
|
71 |
+
if not any(isinstance(n, ast.FunctionDef) and n.name == node.func.id for n in ast.walk(tree)):
|
72 |
+
suggestions.append(f"Function '{node.func.id}' is called but not defined.")
|
73 |
+
|
74 |
+
# Detect type mismatches (example: adding string to integer)
|
75 |
+
for node in ast.walk(tree):
|
76 |
+
if isinstance(node, ast.BinOp):
|
77 |
+
left = node.left
|
78 |
+
right = node.right
|
79 |
+
if isinstance(left, ast.Constant) and isinstance(right, ast.Constant):
|
80 |
+
if type(left.value) != type(right.value):
|
81 |
+
suggestions.append(f"Type mismatch in operation: '{left.value}' ({type(left.value).__name__}) and '{right.value}' ({type(right.value).__name__}).")
|
82 |
+
|
83 |
+
# Detect logical errors (example: unreachable code)
|
84 |
+
for i, node in enumerate(tree.body):
|
85 |
+
if isinstance(node, ast.If):
|
86 |
+
if isinstance(node.test, ast.Constant) and node.test.value is False:
|
87 |
+
suggestions.append(f"Unreachable code detected at line {node.lineno}.")
|
88 |
+
elif isinstance(node.test, ast.Constant) and node.test.value is True:
|
89 |
+
suggestions.append(f"Redundant condition always True at line {node.lineno}.")
|
90 |
+
|
91 |
+
# Detect duplicate keys in dictionaries
|
92 |
+
for node in ast.walk(tree):
|
93 |
+
if isinstance(node, ast.Dict):
|
94 |
+
keys = [k.value for k in node.keys if isinstance(k, ast.Constant)]
|
95 |
+
if len(keys) != len(set(keys)):
|
96 |
+
suggestions.append("Duplicate keys detected in dictionary.")
|
97 |
|
98 |
# Convert the modified AST back to code
|
99 |
fixed_code = astor.to_source(tree)
|
100 |
|
101 |
except Exception as e:
|
102 |
+
suggestions.append(f"Error analyzing code: {traceback.format_exc()}")
|
103 |
|
104 |
return suggestions, fixed_code
|
105 |
|
106 |
# Streamlit app UI
|
107 |
st.title("Code Quality, Bug Detection, and Auto-Correction Tool")
|
108 |
+
st.markdown("Analyze your code for syntax issues, quality, bugs, logical errors, and get suggested corrections.")
|
109 |
|
110 |
# File uploader
|
111 |
uploaded_file = st.file_uploader("Upload a Python code file", type=["py"])
|