File size: 2,009 Bytes
102e49d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
"""
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