Download app/utils/intent_detector.py from rk147/secforge: direct link, hf CLI and curl.
- Browser
- Download file 2.05 kB
-
https://huggingface.co/spaces/rk147/secforge/resolve/main/app/utils/intent_detector.py
- Command line
-
hf download hf://spaces/rk147/secforge/app/utils/intent_detector.py
-
curl -L -o intent_detector.py https://huggingface.co/spaces/rk147/secforge/resolve/main/app/utils/intent_detector.py
2.05 kB
| """Detect user intent from input — code analysis, question, or mixed.""" | |
| import re | |
| def detect_intent(text: str) -> str: | |
| """ | |
| Classify input as: | |
| - 'analyze' : user pasted code to be analyzed | |
| - 'chat' : user asked a question or made a statement | |
| - 'mixed' : question about specific code they also pasted | |
| Returns one of: 'analyze', 'chat', 'mixed' | |
| """ | |
| stripped = text.strip() | |
| # Strong code signals: indented blocks, common keywords at line start | |
| code_patterns = [ | |
| r"^\s*(def |class |import |from |function |const |let |var |public |private |#include)", | |
| r"^\s*(if |for |while |return |async |await |try:|except:|catch\s*\()", | |
| r"[{}\[\]]\s*$", # lines ending with braces | |
| r"^\s+\w+.*[:=]\s*\S", # indented assignments | |
| r"(->|=>|::|===|!==|&&|\|\|)", # operators common in code | |
| ] | |
| question_patterns = [ | |
| r"\?", | |
| r"^(what|why|how|when|where|who|can you|could you|explain|describe|tell me|is there|are there|should i|help me)", | |
| r"^(fix|debug|review|check|analyze|look at|improve)", | |
| ] | |
| lines = stripped.split("\n") | |
| code_line_count = 0 | |
| for line in lines: | |
| for pat in code_patterns: | |
| if re.search(pat, line, re.IGNORECASE): | |
| code_line_count += 1 | |
| break | |
| code_ratio = code_line_count / max(len(lines), 1) | |
| has_question = any( | |
| re.search(pat, stripped, re.IGNORECASE | re.MULTILINE) | |
| for pat in question_patterns | |
| ) | |
| # Multi-line with mostly code lines → analyze | |
| if len(lines) >= 3 and code_ratio >= 0.4: | |
| if has_question: | |
| return "mixed" | |
| return "analyze" | |
| # Single line or low code ratio with question → chat | |
| if has_question: | |
| return "chat" | |
| # Short single line, no question → chat (treat as conversation) | |
| if len(lines) == 1: | |
| return "chat" | |
| # Default: if there's meaningful code present | |
| if code_ratio >= 0.25: | |
| return "analyze" | |
| return "chat" |