Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Initialize Hugging Face model and tokenizer
|
6 |
+
MODEL_NAME = "microsoft/codebert-base"
|
7 |
+
|
8 |
+
# Load the pre-trained CodeBERT model for understanding code
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
11 |
+
|
12 |
+
# Helper function to analyze code
|
13 |
+
def analyze_code(code):
|
14 |
+
# Split the code into manageable chunks
|
15 |
+
max_length = 512
|
16 |
+
lines = code.split("\n")
|
17 |
+
chunks = ["\n".join(lines[i:i+max_length]) for i in range(0, len(lines), max_length)]
|
18 |
+
|
19 |
+
results = []
|
20 |
+
for chunk in chunks:
|
21 |
+
tokenized_code = tokenizer(chunk, return_tensors="pt", truncation=True, max_length=max_length)
|
22 |
+
outputs = model(**tokenized_code)
|
23 |
+
logits = outputs.logits
|
24 |
+
results.append(logits.argmax(dim=1).item())
|
25 |
+
|
26 |
+
return results
|
27 |
+
|
28 |
+
# Function to simulate quality and bug detection
|
29 |
+
# Note: Replace with a real analysis pipeline.
|
30 |
+
def check_code_quality_and_bugs(code):
|
31 |
+
suggestions = []
|
32 |
+
|
33 |
+
# Example: Check for readability (placeholder logic)
|
34 |
+
if len(code.split("\n")) < 5:
|
35 |
+
suggestions.append("Code seems too short, ensure functionality is implemented correctly.")
|
36 |
+
|
37 |
+
# Example: Check for comments (placeholder logic)
|
38 |
+
if "#" not in code:
|
39 |
+
suggestions.append("Consider adding comments to improve code clarity.")
|
40 |
+
|
41 |
+
# Example: Check for style guide adherence (placeholder logic)
|
42 |
+
if "import" in code and "os" not in code:
|
43 |
+
suggestions.append("Unused imports detected; consider removing them.")
|
44 |
+
|
45 |
+
# Example: Detect code smells (placeholder logic)
|
46 |
+
if "try" in code and "except" not in code:
|
47 |
+
suggestions.append("`try` block without `except` may lead to unhandled exceptions.")
|
48 |
+
|
49 |
+
return suggestions
|
50 |
+
|
51 |
+
# Streamlit app UI
|
52 |
+
st.title("Code Quality and Bug Detection Tool")
|
53 |
+
st.markdown("Analyze your code for syntax issues, quality, and potential bugs.")
|
54 |
+
|
55 |
+
# File uploader
|
56 |
+
uploaded_file = st.file_uploader("Upload a Python code file", type=["py"])
|
57 |
+
|
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():
|
65 |
+
code = code_snippet
|
66 |
+
else:
|
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 = check_code_quality_and_bugs(code)
|
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:**")
|
83 |
+
model_results = analyze_code(code)
|
84 |
+
for idx, result in enumerate(model_results, 1):
|
85 |
+
st.write(f"Chunk {idx} classification result: {result}")
|
86 |
+
|
87 |
+
st.markdown("---")
|
88 |
+
st.markdown("*Powered by Hugging Face and Streamlit*")
|