Spaces:
Sleeping
Sleeping
Upload classifier.py
Browse files- backend/ml/classifier.py +42 -0
backend/ml/classifier.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from backend.config import settings
|
| 3 |
+
|
| 4 |
+
logger = logging.getLogger(__name__)
|
| 5 |
+
|
| 6 |
+
class RouterClassifier:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.tokenizer = None
|
| 9 |
+
self.model = None
|
| 10 |
+
logger.info("Initializing Classifier (Heuristic Mode)")
|
| 11 |
+
|
| 12 |
+
def is_loaded(self):
|
| 13 |
+
return True
|
| 14 |
+
|
| 15 |
+
async def classify(self, prompt: str):
|
| 16 |
+
# Heuristic Logic
|
| 17 |
+
prompt_lower = prompt.lower()
|
| 18 |
+
|
| 19 |
+
# Complex Keywords (imply Large LLM)
|
| 20 |
+
complex_keywords = [
|
| 21 |
+
"code", "python", "javascript", "react", "sql", "algorithm",
|
| 22 |
+
"explain", "analyze", "compare", "difference", "history",
|
| 23 |
+
"physics", "quantum", "essay", "poem", "generate", "write"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
has_keyword = any(kw in prompt_lower for kw in complex_keywords)
|
| 27 |
+
is_long = len(prompt) > 80
|
| 28 |
+
|
| 29 |
+
if has_keyword or is_long:
|
| 30 |
+
return {
|
| 31 |
+
"selected_tier": settings.LARGE_LLM_TIER,
|
| 32 |
+
"confidence": 0.92 if has_keyword else 0.85,
|
| 33 |
+
"routing_reason": "Complex keywords detected" if has_keyword else "Prompt length > 80"
|
| 34 |
+
}
|
| 35 |
+
else:
|
| 36 |
+
return {
|
| 37 |
+
"selected_tier": settings.SMALL_LLM_TIER,
|
| 38 |
+
"confidence": 0.88,
|
| 39 |
+
"routing_reason": "Simple query detected"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|