ALwrity Backend and Frontend - Stability and Error Handling Improvements
This commit is contained in:
@@ -47,15 +47,26 @@ class AIServiceMetrics:
|
||||
class AIServiceManager:
|
||||
"""Centralized AI service management for content planning system."""
|
||||
|
||||
_instance = None
|
||||
_initialized = False
|
||||
|
||||
def __new__(cls):
|
||||
"""Implement singleton pattern to prevent multiple initializations."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(AIServiceManager, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize AI service manager."""
|
||||
self.logger = logger
|
||||
self.metrics: List[AIServiceMetrics] = []
|
||||
self.prompts = self._load_centralized_prompts()
|
||||
self.schemas = self._load_centralized_schemas()
|
||||
self.config = self._load_ai_configuration()
|
||||
|
||||
logger.info("AIServiceManager initialized")
|
||||
"""Initialize AI service manager (only once)."""
|
||||
if not self._initialized:
|
||||
self.logger = logger
|
||||
self.metrics: List[AIServiceMetrics] = []
|
||||
self.prompts = self._load_centralized_prompts()
|
||||
self.schemas = self._load_centralized_schemas()
|
||||
self.config = self._load_ai_configuration()
|
||||
|
||||
logger.debug("AIServiceManager initialized")
|
||||
self._initialized = True
|
||||
|
||||
def _load_ai_configuration(self) -> Dict[str, Any]:
|
||||
"""Load AI configuration settings."""
|
||||
|
||||
@@ -24,10 +24,21 @@ from services.database import get_db_session
|
||||
class AIEngineService:
|
||||
"""AI engine for content planning insights and analysis."""
|
||||
|
||||
_instance = None
|
||||
_initialized = False
|
||||
|
||||
def __new__(cls):
|
||||
"""Implement singleton pattern to prevent multiple initializations."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(AIEngineService, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the AI engine service."""
|
||||
self.ai_service_manager = AIServiceManager()
|
||||
logger.info("AIEngineService initialized")
|
||||
"""Initialize the AI engine service (only once)."""
|
||||
if not self._initialized:
|
||||
self.ai_service_manager = AIServiceManager()
|
||||
logger.debug("AIEngineService initialized")
|
||||
self._initialized = True
|
||||
|
||||
async def analyze_content_gaps(self, analysis_summary: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
|
||||
@@ -18,13 +18,24 @@ from services.persona.facebook.facebook_persona_service import FacebookPersonaSe
|
||||
class CorePersonaService:
|
||||
"""Core service for generating writing personas using Gemini AI."""
|
||||
|
||||
_instance = None
|
||||
_initialized = False
|
||||
|
||||
def __new__(cls):
|
||||
"""Implement singleton pattern to prevent multiple initializations."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(CorePersonaService, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the core persona service."""
|
||||
self.data_collector = OnboardingDataCollector()
|
||||
self.prompt_builder = PersonaPromptBuilder()
|
||||
self.linkedin_service = LinkedInPersonaService()
|
||||
self.facebook_service = FacebookPersonaService()
|
||||
logger.info("CorePersonaService initialized")
|
||||
"""Initialize the core persona service (only once)."""
|
||||
if not self._initialized:
|
||||
self.data_collector = OnboardingDataCollector()
|
||||
self.prompt_builder = PersonaPromptBuilder()
|
||||
self.linkedin_service = LinkedInPersonaService()
|
||||
self.facebook_service = FacebookPersonaService()
|
||||
logger.debug("CorePersonaService initialized")
|
||||
self._initialized = True
|
||||
|
||||
def generate_core_persona(self, onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Generate core writing persona using Gemini structured response."""
|
||||
|
||||
@@ -26,7 +26,7 @@ class EnhancedLinguisticAnalyzer:
|
||||
import spacy
|
||||
self.nlp = spacy.load("en_core_web_sm")
|
||||
self.spacy_available = True
|
||||
logger.info("SUCCESS: spaCy model loaded successfully - Enhanced linguistic analysis available")
|
||||
logger.debug("SUCCESS: spaCy model loaded successfully - Enhanced linguistic analysis available")
|
||||
except ImportError as e:
|
||||
logger.error(f"ERROR: spaCy is REQUIRED for persona generation. Install with: pip install spacy && python -m spacy download en_core_web_sm")
|
||||
raise ImportError("spaCy is required for enhanced persona generation. Install with: pip install spacy && python -m spacy download en_core_web_sm") from e
|
||||
|
||||
@@ -20,14 +20,25 @@ from services.llm_providers.gemini_provider import gemini_structured_json_respon
|
||||
class FacebookPersonaService:
|
||||
"""Facebook-specific persona generation and optimization service."""
|
||||
|
||||
_instance = None
|
||||
_initialized = False
|
||||
|
||||
def __new__(cls):
|
||||
"""Implement singleton pattern to prevent multiple initializations."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(FacebookPersonaService, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the Facebook persona service."""
|
||||
self.schemas = FacebookPersonaSchema
|
||||
self.constraints = FacebookPersonaConstraints()
|
||||
self.validation = FacebookPersonaValidation()
|
||||
self.optimization = FacebookPersonaOptimization()
|
||||
self.prompts = FacebookPersonaPrompts()
|
||||
logger.info("FacebookPersonaService initialized")
|
||||
"""Initialize the Facebook persona service (only once)."""
|
||||
if not self._initialized:
|
||||
self.schemas = FacebookPersonaSchema
|
||||
self.constraints = FacebookPersonaConstraints()
|
||||
self.validation = FacebookPersonaValidation()
|
||||
self.optimization = FacebookPersonaOptimization()
|
||||
self.prompts = FacebookPersonaPrompts()
|
||||
logger.debug("FacebookPersonaService initialized")
|
||||
self._initialized = True
|
||||
|
||||
def generate_facebook_persona(self, core_persona: Dict[str, Any], onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
|
||||
@@ -14,11 +14,22 @@ from .linkedin_persona_schemas import LinkedInPersonaSchemas
|
||||
class LinkedInPersonaService:
|
||||
"""Service for generating LinkedIn-specific persona adaptations."""
|
||||
|
||||
_instance = None
|
||||
_initialized = False
|
||||
|
||||
def __new__(cls):
|
||||
"""Implement singleton pattern to prevent multiple initializations."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(LinkedInPersonaService, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the LinkedIn persona service."""
|
||||
self.prompts = LinkedInPersonaPrompts()
|
||||
self.schemas = LinkedInPersonaSchemas()
|
||||
logger.info("LinkedInPersonaService initialized")
|
||||
"""Initialize the LinkedIn persona service (only once)."""
|
||||
if not self._initialized:
|
||||
self.prompts = LinkedInPersonaPrompts()
|
||||
self.schemas = LinkedInPersonaSchemas()
|
||||
logger.debug("LinkedInPersonaService initialized")
|
||||
self._initialized = True
|
||||
|
||||
def generate_linkedin_persona(self, core_persona: Dict[str, Any], onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
|
||||
@@ -24,7 +24,7 @@ class PersonaQualityImprover:
|
||||
def __init__(self):
|
||||
"""Initialize the quality improver."""
|
||||
self.linguistic_analyzer = EnhancedLinguisticAnalyzer()
|
||||
logger.info("PersonaQualityImprover initialized")
|
||||
logger.debug("PersonaQualityImprover initialized")
|
||||
|
||||
def assess_persona_quality_comprehensive(
|
||||
self,
|
||||
|
||||
@@ -19,13 +19,24 @@ from services.persona.facebook.facebook_persona_service import FacebookPersonaSe
|
||||
class PersonaAnalysisService:
|
||||
"""Service for analyzing onboarding data and generating writing personas using Gemini AI."""
|
||||
|
||||
_instance = None
|
||||
_initialized = False
|
||||
|
||||
def __new__(cls):
|
||||
"""Implement singleton pattern to prevent multiple initializations."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(PersonaAnalysisService, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the persona analysis service."""
|
||||
self.core_persona_service = CorePersonaService()
|
||||
self.data_collector = OnboardingDataCollector()
|
||||
self.linkedin_service = LinkedInPersonaService()
|
||||
self.facebook_service = FacebookPersonaService()
|
||||
logger.info("PersonaAnalysisService initialized")
|
||||
"""Initialize the persona analysis service (only once)."""
|
||||
if not self._initialized:
|
||||
self.core_persona_service = CorePersonaService()
|
||||
self.data_collector = OnboardingDataCollector()
|
||||
self.linkedin_service = LinkedInPersonaService()
|
||||
self.facebook_service = FacebookPersonaService()
|
||||
logger.debug("PersonaAnalysisService initialized")
|
||||
self._initialized = True
|
||||
|
||||
def generate_persona_from_onboarding(self, user_id: int, onboarding_session_id: int = None) -> Dict[str, Any]:
|
||||
"""
|
||||
|
||||
@@ -215,7 +215,7 @@ class PricingService:
|
||||
self.db.add(pricing)
|
||||
|
||||
self.db.commit()
|
||||
logger.info("Default API pricing initialized")
|
||||
logger.debug("Default API pricing initialized")
|
||||
|
||||
def initialize_default_plans(self):
|
||||
"""Initialize default subscription plans."""
|
||||
@@ -318,7 +318,7 @@ class PricingService:
|
||||
self.db.add(plan)
|
||||
|
||||
self.db.commit()
|
||||
logger.info("Default subscription plans initialized")
|
||||
logger.debug("Default subscription plans initialized")
|
||||
|
||||
def calculate_api_cost(self, provider: APIProvider, model_name: str,
|
||||
tokens_input: int = 0, tokens_output: int = 0,
|
||||
|
||||
Reference in New Issue
Block a user