Spaces:
Sleeping
Sleeping
File size: 1,359 Bytes
2f235a0 20a1017 2f235a0 ddc5c21 2f235a0 20a1017 2f235a0 20a1017 2f235a0 |
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 |
from dataclasses import dataclass, field
from typing import Dict, List
@dataclass
class IntentClassifier:
intent_keywords: Dict[str, List[str]] = field(default_factory=lambda:{
"rag":["document","policy","manual","procedure","hr"],
"web":["latest","today","news","current","price","stock","breaking","update","recent","now","trending","happening","what's new","what is new"],
"admin":["delete","remove","export","salary","confidential"],
"general":["explain","summary","help"]
})
llm_client: any = None
async def classify(self, text: str) -> str:
t = text.lower()
scores={k:0 for k in self.intent_keywords}
for k, words in self.intent_keywords.items():
for w in words:
if w in t: scores[k]+=1
best = max(scores, key=scores.get)
if scores[best] > 0: return best
# LLM fallback with error handling
if self.llm_client:
try:
prompt=f"Classify into rag/web/admin/general. User: '{text}'"
out = (await self.llm_client.simple_call(prompt)).strip().lower()
return out if out in scores else "general"
except Exception:
# LLM failed (not configured or unavailable), default to general
return "general"
return "general"
|