Spaces:
Sleeping
Sleeping
File size: 7,593 Bytes
fc2d789 | 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | """
Code Analyzer using CodeBERT and CodeT5.
- CodeBERT : embeddings + code quality classification
- CodeT5 : docstring / comment generation
"""
from __future__ import annotations
import re
from dataclasses import dataclass, field
from typing import Optional
import torch
from transformers import (
AutoTokenizer,
AutoModel,
RobertaTokenizer,
T5ForConditionalGeneration,
)
# ββ Data classes βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@dataclass
class CodeQualityResult:
overall_score: float
complexity_score: float
documentation_score: float
naming_score: float
issues: list[str] = field(default_factory=list)
suggestions: list[str] = field(default_factory=list)
generated_docstring: str = ""
embedding: Optional[list] = None
# ββ Heuristic helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _has_docstrings(code: str) -> bool:
return '"""' in code or "'''" in code
def _count_comments(code: str) -> int:
return len([l for l in code.splitlines() if l.strip().startswith("#")])
def _avg_name_length(code: str) -> float:
names = re.findall(r'\b([a-zA-Z_]\w*)\s*(?:\(|=)', code)
meaningful = [n for n in names if n not in {
"if", "else", "for", "while", "def", "class",
"return", "import", "from", "True", "False", "None",
}]
if not meaningful:
return 5.0
return sum(len(n) for n in meaningful) / len(meaningful)
def _detect_issues(code: str) -> list[str]:
issues = []
lines = code.splitlines()
long = [i + 1 for i, l in enumerate(lines) if len(l) > 79]
if long:
issues.append(f"Lines exceeding PEP-8 limit (79 chars): {long[:5]}")
if re.search(r'(?<![.\w])\d{2,}(?![\w.])', code):
issues.append("Magic numbers detected β use named constants")
if re.search(r'except\s*:', code):
issues.append("Bare `except:` clause β catch specific exceptions")
if re.search(r'^global\s+\w+', code, re.MULTILINE):
issues.append("Use of `global` β consider refactoring")
defs = re.findall(r'def\s+\w+\(([^)]*)\)', code)
if [d for d in defs if d and ':' not in d]:
issues.append("Function parameters missing type hints")
if re.search(r'#\s*(TODO|FIXME|HACK)', code, re.IGNORECASE):
issues.append("TODO/FIXME comments found β resolve before production")
return issues
def _score_documentation(code: str) -> float:
score = 40.0
if _has_docstrings(code):
score += 40.0
comment_density = _count_comments(code) / max(len(code.splitlines()), 1)
score += min(comment_density * 200, 20.0)
return min(score, 100.0)
def _score_naming(code: str) -> float:
avg = _avg_name_length(code)
if avg < 2: return 30.0
if avg < 4: return 55.0
if avg <= 20: return 85.0 + min((avg - 4) * 1.5, 15.0)
return 60.0
def _score_complexity(code: str) -> float:
lines = code.splitlines()
branches = sum(
1 for l in lines
if re.search(r'\b(if|elif|for|while|try|except|with)\b', l)
)
nesting = max(
(len(l) - len(l.lstrip())) // 4 for l in lines if l.strip()
) if lines else 0
penalty = branches * 3 + nesting * 5
return max(100 - penalty, 10.0)
# ββ Main analyzer class βββββββββββββββββββββββββββββββββββββββββββββββββββ
class CodeReviewAnalyzer:
CODEBERT_MODEL = "microsoft/codebert-base"
CODET5_MODEL = "Salesforce/codet5-base-codexglue-sum-python"
def __init__(self, use_gpu: bool = False):
self.device = torch.device(
"cuda" if use_gpu and torch.cuda.is_available() else "cpu"
)
self._bert_tokenizer = None
self._bert_model = None
self._t5_tokenizer = None
self._t5_model = None
def _load_codebert(self):
if self._bert_model is None:
print("Loading CodeBERT ...")
self._bert_tokenizer = AutoTokenizer.from_pretrained(self.CODEBERT_MODEL)
self._bert_model = AutoModel.from_pretrained(self.CODEBERT_MODEL)
self._bert_model.to(self.device).eval()
def _load_codet5(self):
if self._t5_model is None:
print("Loading CodeT5 ...")
self._t5_tokenizer = RobertaTokenizer.from_pretrained(self.CODET5_MODEL)
self._t5_model = T5ForConditionalGeneration.from_pretrained(
self.CODET5_MODEL
)
self._t5_model.to(self.device).eval()
def get_embedding(self, code: str) -> list[float]:
self._load_codebert()
tokens = self._bert_tokenizer(
code,
return_tensors="pt",
max_length=512,
truncation=True,
padding=True,
)
tokens = {k: v.to(self.device) for k, v in tokens.items()}
with torch.no_grad():
out = self._bert_model(**tokens)
return out.last_hidden_state.mean(dim=1).squeeze().tolist()
def generate_docstring(self, code: str) -> str:
self._load_codet5()
inputs = self._t5_tokenizer(
code,
return_tensors="pt",
max_length=512,
truncation=True,
).to(self.device)
with torch.no_grad():
outputs = self._t5_model.generate(
**inputs,
max_new_tokens=128,
num_beams=4,
early_stopping=True,
)
raw = self._t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
return f'"""\n{raw.strip()}\n"""'
def analyze(
self,
code: str,
language: str = "python",
generate_doc: bool = True,
get_embedding: bool = False,
) -> CodeQualityResult:
issues = _detect_issues(code)
doc_score = _score_documentation(code)
name_score = _score_naming(code)
comp_score = _score_complexity(code)
overall = (doc_score * 0.35 + name_score * 0.30 + comp_score * 0.35)
suggestions = []
if doc_score < 60: suggestions.append("Add docstrings to all public functions")
if name_score < 60: suggestions.append("Use descriptive variable names (4+ chars)")
if comp_score < 50: suggestions.append("Reduce nesting β aim for complexity <= 10")
suggestions.append("Run `black` for formatting and `flake8` for linting")
docstring = ""
if generate_doc:
try:
docstring = self.generate_docstring(code)
except Exception as exc:
docstring = f"# Could not generate: {exc}"
embedding = None
if get_embedding:
try:
embedding = self.get_embedding(code)
except Exception:
pass
return CodeQualityResult(
overall_score = round(overall, 1),
complexity_score = round(comp_score, 1),
documentation_score = round(doc_score, 1),
naming_score = round(name_score, 1),
issues = issues,
suggestions = suggestions,
generated_docstring = docstring,
embedding = embedding,
) |