Spaces:
Runtime error
Runtime error
Download src/recommendation_engine/command_handler.py from InnoTrack/Graduation_Project-v1.2: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/spaces/InnoTrack/Graduation_Project-v1.2/resolve/3d4e2d349a7de45d07dccbd2b4d6caa3768f3082/src/recommendation_engine/command_handler.py
- Command line
-
hf download hf://spaces/InnoTrack/Graduation_Project-v1.2@3d4e2d349a7de45d07dccbd2b4d6caa3768f3082/src/recommendation_engine/command_handler.py
-
curl -L -o command_handler.py https://huggingface.co/spaces/InnoTrack/Graduation_Project-v1.2/resolve/3d4e2d349a7de45d07dccbd2b4d6caa3768f3082/src/recommendation_engine/command_handler.py
2.62 kB
| # src/recommendation_engine/command_handler.py | |
| import re | |
| # ===================================================== | |
| # Command Detection (Improved) | |
| # ===================================================== | |
| def is_command(text: str) -> bool: | |
| """ | |
| Detect if input is a system/terminal command | |
| """ | |
| if not text: | |
| return False | |
| text = text.strip().lower() | |
| # ========================================= | |
| # Exact commands | |
| # ========================================= | |
| if text in {"exit", "quit", "clear"}: | |
| return True | |
| # ========================================= | |
| # Command-like patterns (regex) | |
| # ========================================= | |
| command_patterns = [ | |
| r"^python\s+\S+\.py", # python file.py | |
| r"^pip\s+install", # pip install | |
| r"^npm\s+install", # npm install | |
| r"^node\s+\S+", # node file.js | |
| r"^cd\s+.+", # cd folder | |
| r"^ls\b", # ls | |
| r"^dir\b", # dir | |
| r"^git\s+.+", # git command | |
| r"^sudo\s+.+", # sudo command | |
| r".+\.py\s*$", # ends with file.py (ONLY if standalone) | |
| ] | |
| for pattern in command_patterns: | |
| if re.match(pattern, text): | |
| return True | |
| # ========================================= | |
| # Inline execution attempts | |
| # ========================================= | |
| if "run" in text and re.search(r"\.py\b", text): | |
| return True | |
| return False | |
| # ===================================================== | |
| # Command Handling | |
| # ===================================================== | |
| def handle_command(text: str) -> str: | |
| """ | |
| Return safe response for command-like inputs | |
| """ | |
| text = text.lower().strip() | |
| # exit commands | |
| if text in {"exit", "quit"}: | |
| return "👋 Session ended. Start a new chat anytime." | |
| # python execution | |
| if re.search(r"python\s+\S+\.py", text): | |
| return ( | |
| "⚠️ This looks like a code execution command.\n" | |
| "I only help with graduation project ideas and development." | |
| ) | |
| # package installation | |
| if "pip install" in text or "npm install" in text: | |
| return ( | |
| "⚠️ Installation commands are outside my scope.\n" | |
| "I can help you design your graduation project instead." | |
| ) | |
| # generic fallback | |
| return ( | |
| "⚠️ This looks like a system command.\n" | |
| "Please ask about graduation projects (ideas, features, or system design)." | |
| ) |