Spaces:
Sleeping
Sleeping
File size: 2,945 Bytes
5e0532d |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from pydantic import BaseModel
from typing import Optional, List
from enum import Enum
class UserIntent(str, Enum):
QUESTION = "question"
EMOTIONAL_SUPPORT = "emotional_support"
PRAYER_REQUEST = "prayer_request"
BIBLE_STUDY = "bible_study"
LIFE_DECISION = "life_decision"
CONFESSION = "confession"
UNKNOWN = "unknown"
class IntentAnalysis(BaseModel):
intent: UserIntent
urgency: str = "low" # low, medium, high
requires_scripture: bool
emotional_tone: Optional[str] = None
confidence: float = 1.0
class IntentService:
def __init__(self):
# Keywords for deterministic matching
self.intent_keywords = {
UserIntent.PRAYER_REQUEST: ["pray", "prayer", "intercede", "god help", "need god"],
UserIntent.EMOTIONAL_SUPPORT: ["sad", "depressed", "anxious", "lonely", "hurt", "grief", "pain", "crying", "broken", "suffering"],
UserIntent.BIBLE_STUDY: ["verse", "scripture", "passage", "chapter", "bible", "read", "psalm", "gospel", "book of"],
UserIntent.LIFE_DECISION: ["decide", "choice", "what should i do", "guidance", "direction", "path", "will for me"],
UserIntent.CONFESSION: ["sin", "forgive", "confess", "guilt", "mistake", "wrong", "sorry", "repent"],
UserIntent.QUESTION: ["who", "what", "where", "when", "why", "how", "?"],
}
async def analyze(self, message: str) -> IntentAnalysis:
msg_lower = message.lower()
# Priority 1: High urgency / Specific needs
if any(kw in msg_lower for kw in self.intent_keywords[UserIntent.PRAYER_REQUEST]):
return IntentAnalysis(intent=UserIntent.PRAYER_REQUEST, requires_scripture=True, urgency="medium")
if any(kw in msg_lower for kw in self.intent_keywords[UserIntent.CONFESSION]):
return IntentAnalysis(intent=UserIntent.CONFESSION, requires_scripture=True, urgency="medium")
if any(kw in msg_lower for kw in self.intent_keywords[UserIntent.EMOTIONAL_SUPPORT]):
return IntentAnalysis(intent=UserIntent.EMOTIONAL_SUPPORT, requires_scripture=True, urgency="medium")
# Priority 2: Study and Guidance
if any(kw in msg_lower for kw in self.intent_keywords[UserIntent.BIBLE_STUDY]):
return IntentAnalysis(intent=UserIntent.BIBLE_STUDY, requires_scripture=True)
if any(kw in msg_lower for kw in self.intent_keywords[UserIntent.LIFE_DECISION]):
return IntentAnalysis(intent=UserIntent.LIFE_DECISION, requires_scripture=True, urgency="medium")
# Priority 3: General Questions
if any(kw in msg_lower for kw in self.intent_keywords[UserIntent.QUESTION]):
return IntentAnalysis(intent=UserIntent.QUESTION, requires_scripture=False)
# Fallback
return IntentAnalysis(intent=UserIntent.UNKNOWN, requires_scripture=False)
intent_service = IntentService()
|