TalentLensAI / utils /spacy_loader.py
Johnny
updated resume_format > template, hide sidebar, download Spacy model with spacy_loader.py
102e49d
"""
SpaCy Model Loader with fallbacks for deployment environments
"""
import spacy
import logging
import subprocess
import sys
logger = logging.getLogger(__name__)
def load_spacy_model(model_name="en_core_web_sm"):
"""
Load spaCy model with fallbacks for deployment environments
Args:
model_name: Name of the spaCy model to load
Returns:
Loaded spaCy model or None if all attempts fail
"""
# Try to load the model directly first
try:
nlp = spacy.load(model_name)
logger.info(f"βœ… Successfully loaded spaCy model: {model_name}")
return nlp
except OSError as e:
logger.warning(f"Failed to load {model_name} directly: {e}")
# Try to download and install the model
try:
logger.info(f"Attempting to download {model_name}...")
subprocess.check_call([
sys.executable, "-m", "spacy", "download", model_name
])
nlp = spacy.load(model_name)
logger.info(f"βœ… Successfully downloaded and loaded spaCy model: {model_name}")
return nlp
except (subprocess.CalledProcessError, OSError) as e:
logger.warning(f"Failed to download {model_name}: {e}")
# Try to load blank English model as fallback
try:
logger.info("Loading blank English model as fallback...")
nlp = spacy.blank("en")
logger.info("βœ… Successfully loaded blank English model")
return nlp
except Exception as e:
logger.error(f"Failed to load blank English model: {e}")
# Final fallback - return None
logger.error("❌ All spaCy model loading attempts failed")
return None
# Global instance
_nlp_instance = None
def get_nlp():
"""Get the global spaCy model instance"""
global _nlp_instance
if _nlp_instance is None:
_nlp_instance = load_spacy_model()
return _nlp_instance
def is_spacy_available():
"""Check if spaCy model is available"""
return get_nlp() is not None