Download app/modules/analyzer_llm_fallback.py from rk147/secforge: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/analyzer_llm_fallback.py
- Command line
-
hf download hf://spaces/rk147/secforge/app/modules/analyzer_llm_fallback.py
-
curl -L -o analyzer_llm_fallback.py https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/analyzer_llm_fallback.py
2.42 kB
| """LLM-based fallback analyzer for languages without native linters.""" | |
| import json | |
| from groq import Groq | |
| from app.modules.analyzer_base import CodeAnalyzer, AnalysisResult | |
| from app.config import GROQ_API_KEY, MODEL_NAME, MAX_TOKENS, TEMPERATURE | |
| class LLMFallbackAnalyzer(CodeAnalyzer): | |
| def __init__(self, language: str): | |
| super().__init__(language) | |
| self._client = None | |
| def _get_client(self) -> Groq: | |
| if self._client is None: | |
| self._client = Groq(api_key=GROQ_API_KEY) | |
| return self._client | |
| def analyze(self, code: str) -> AnalysisResult: | |
| try: | |
| client = self._get_client() | |
| prompt = f"""Analyze this {self.language} code for issues and return ONLY a JSON array with objects containing: | |
| - line: estimated line number (1-based) | |
| - symbol: issue code/identifier | |
| - message: issue description | |
| - severity: 'high', 'medium', or 'low' | |
| - category: 'correctness', 'performance', 'security', or 'style' | |
| Limit to top 5 issues. Return ONLY valid JSON array, no markdown. | |
| Code: | |
| {code} | |
| JSON array:""" | |
| resp = client.chat.completions.create( | |
| model=MODEL_NAME, | |
| messages=[{"role": "user", "content": prompt}], | |
| max_tokens=MAX_TOKENS, | |
| temperature=TEMPERATURE, | |
| ) | |
| response_text = resp.choices[0].message.content.strip() | |
| try: | |
| start = response_text.find("[") | |
| end = response_text.rfind("]") + 1 | |
| if start >= 0 and end > start: | |
| issues_data = json.loads(response_text[start:end]) | |
| else: | |
| issues_data = [] | |
| except json.JSONDecodeError: | |
| return AnalysisResult(self.language, error="Failed to parse LLM response") | |
| issues = [] | |
| for issue in issues_data[:5]: | |
| formatted = self.format_issue( | |
| line=issue.get("line", 0), | |
| symbol=issue.get("symbol", "llm-issue"), | |
| message=issue.get("message", ""), | |
| category=issue.get("category", "style"), | |
| severity=issue.get("severity", "low"), | |
| ) | |
| issues.append(formatted) | |
| return AnalysisResult(self.language, issues) | |
| except Exception as e: | |
| return AnalysisResult(self.language, error=str(e)) |