Spaces:
Sleeping
Sleeping
| from pydantic import BaseModel, Field | |
| from typing import List, Optional | |
| from enum import Enum | |
| import sqlite3 | |
| import json | |
| class TonePreference(str, Enum): | |
| PASTORAL = "pastoral" | |
| SCHOLARLY = "scholarly" | |
| GENTLE = "gentle" | |
| DIRECT = "direct" | |
| class SpiritualGoal(str, Enum): | |
| PRAYER = "prayer" | |
| STUDY = "study" | |
| HEALING = "healing" | |
| LEADERSHIP = "leadership" | |
| DISCERNMENT = "discernment" | |
| class UserProfile(BaseModel): | |
| user_id: str | |
| denomination: Optional[str] = None | |
| bible_translations: List[str] = Field(default=["NIV", "ESV"]) | |
| tone_preference: TonePreference = TonePreference.PASTORAL | |
| spiritual_goals: List[SpiritualGoal] = [] | |
| class Config: | |
| json_schema_extra = { | |
| "example": { | |
| "user_id": "usr_123", | |
| "denomination": "non-denominational", | |
| "bible_translations": ["NIV"], | |
| "tone_preference": "gentle", | |
| "spiritual_goals": ["prayer", "healing"] | |
| } | |
| } | |
| class ProfileService: | |
| def __init__(self, db_path: str = "memory.db"): | |
| self.db_path = db_path | |
| self._init_db() | |
| def _init_db(self): | |
| conn = sqlite3.connect(self.db_path) | |
| cursor = conn.cursor() | |
| cursor.execute(''' | |
| CREATE TABLE IF NOT EXISTS user_profiles ( | |
| user_id TEXT PRIMARY KEY, | |
| denomination TEXT, | |
| bible_translations TEXT, | |
| tone_preference TEXT, | |
| spiritual_goals TEXT | |
| ) | |
| ''') | |
| conn.commit() | |
| conn.close() | |
| async def get_profile(self, user_id: str) -> UserProfile: | |
| conn = sqlite3.connect(self.db_path) | |
| conn.row_factory = sqlite3.Row | |
| cursor = conn.cursor() | |
| cursor.execute('SELECT * FROM user_profiles WHERE user_id = ?', (user_id,)) | |
| row = cursor.fetchone() | |
| conn.close() | |
| if row: | |
| return UserProfile( | |
| user_id=row['user_id'], | |
| denomination=row['denomination'], | |
| bible_translations=json.loads(row['bible_translations']) if row['bible_translations'] else ["NIV"], | |
| tone_preference=row['tone_preference'] or TonePreference.PASTORAL, | |
| spiritual_goals=json.loads(row['spiritual_goals']) if row['spiritual_goals'] else [] | |
| ) | |
| # Return default profile if none exists | |
| return UserProfile(user_id=user_id) | |
| async def update_profile(self, user_id: str, data: dict) -> UserProfile: | |
| # Get current profile to merge (or default) | |
| current = await self.get_profile(user_id) | |
| # Merge data and validate to ensure Enums are correctly parsed | |
| merged_data = current.model_dump() | |
| merged_data.update(data) | |
| updated_model = UserProfile(**merged_data) | |
| conn = sqlite3.connect(self.db_path) | |
| cursor = conn.cursor() | |
| cursor.execute(''' | |
| INSERT OR REPLACE INTO user_profiles (user_id, denomination, bible_translations, tone_preference, spiritual_goals) | |
| VALUES (?, ?, ?, ?, ?) | |
| ''', ( | |
| updated_model.user_id, | |
| updated_model.denomination, | |
| json.dumps(updated_model.bible_translations), | |
| updated_model.tone_preference.value, | |
| json.dumps([g.value for g in updated_model.spiritual_goals]) | |
| )) | |
| conn.commit() | |
| conn.close() | |
| return updated_model | |
| profile_service = ProfileService() | |