Spaces:
Sleeping
Sleeping
| 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() | |