555e282cc4
ci / changelog_check (push) Waiting to run
ci / check_changes (push) Waiting to run
ci / build_mem0 (3.10) (push) Blocked by required conditions
ci / build_mem0 (3.11) (push) Blocked by required conditions
ci / build_mem0 (3.12) (push) Blocked by required conditions
CLI Node CI / lint (push) Waiting to run
CLI Node CI / test (20) (push) Waiting to run
CLI Node CI / test (22) (push) Waiting to run
CLI Node CI / build (push) Waiting to run
CLI Python CI / lint (push) Waiting to run
CLI Python CI / test (3.10) (push) Waiting to run
CLI Python CI / test (3.11) (push) Waiting to run
CLI Python CI / test (3.12) (push) Waiting to run
CLI Python CI / build (push) Waiting to run
openclaw checks / lint (push) Waiting to run
openclaw checks / test (20) (push) Waiting to run
openclaw checks / test (22) (push) Waiting to run
openclaw checks / build (push) Waiting to run
opencode-plugin checks / build (push) Waiting to run
pi-agent-plugin checks / lint (push) Waiting to run
pi-agent-plugin checks / test (20) (push) Waiting to run
pi-agent-plugin checks / test (22) (push) Waiting to run
pi-agent-plugin checks / build (push) Waiting to run
TypeScript SDK CI / check_changes (push) Waiting to run
TypeScript SDK CI / changelog_check (push) Blocked by required conditions
TypeScript SDK CI / build_ts_sdk (20) (push) Blocked by required conditions
TypeScript SDK CI / build_ts_sdk (22) (push) Blocked by required conditions
TypeScript SDK CI / integration_ts_sdk (20) (push) Blocked by required conditions
TypeScript SDK CI / integration_ts_sdk (22) (push) Blocked by required conditions
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
"""
|
|
Shared spaCy model loader.
|
|
|
|
Consolidates spaCy model loading into a single module so that
|
|
entity_extraction and lemmatization share one instance instead of
|
|
each loading their own copy from disk.
|
|
"""
|
|
|
|
import logging
|
|
import threading
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_nlp_full = None
|
|
_nlp_lemma = None
|
|
_load_failed_full = False
|
|
_load_failed_lemma = False
|
|
_lock = threading.Lock()
|
|
|
|
|
|
def _ensure_model_available():
|
|
"""Download en_core_web_sm if spaCy is installed but model is missing."""
|
|
try:
|
|
import spacy
|
|
except ImportError:
|
|
raise ImportError(
|
|
"spaCy is not installed. Install it with: pip install mem0ai[nlp]"
|
|
)
|
|
|
|
if not spacy.util.is_package("en_core_web_sm"):
|
|
logger.info("Downloading spaCy model en_core_web_sm...")
|
|
try:
|
|
from spacy.cli import download
|
|
|
|
download("en_core_web_sm")
|
|
logger.info("spaCy model en_core_web_sm downloaded successfully")
|
|
except Exception as e:
|
|
raise RuntimeError(
|
|
f"Failed to download spaCy model en_core_web_sm: {e}. "
|
|
"Please install manually: python -m spacy download en_core_web_sm"
|
|
) from e
|
|
|
|
|
|
def get_nlp_full():
|
|
"""Return spaCy model with all pipelines (NER, tagger, etc.) for entity extraction."""
|
|
global _nlp_full, _load_failed_full
|
|
if _load_failed_full:
|
|
return None
|
|
if _nlp_full is not None:
|
|
return _nlp_full
|
|
with _lock:
|
|
if _nlp_full is not None:
|
|
return _nlp_full
|
|
if _load_failed_full:
|
|
return None
|
|
try:
|
|
_ensure_model_available()
|
|
import spacy
|
|
|
|
_nlp_full = spacy.load("en_core_web_sm")
|
|
logger.info("spaCy full model loaded")
|
|
except Exception as e:
|
|
logger.warning(f"Failed to load spaCy full model: {e}")
|
|
_load_failed_full = True
|
|
return None
|
|
return _nlp_full
|
|
|
|
|
|
def get_nlp_lemma():
|
|
"""Return spaCy model with only lemmatizer for BM25 text processing."""
|
|
global _nlp_lemma, _load_failed_lemma
|
|
if _load_failed_lemma:
|
|
return None
|
|
if _nlp_lemma is not None:
|
|
return _nlp_lemma
|
|
with _lock:
|
|
if _nlp_lemma is not None:
|
|
return _nlp_lemma
|
|
if _load_failed_lemma:
|
|
return None
|
|
try:
|
|
_ensure_model_available()
|
|
import spacy
|
|
|
|
_nlp_lemma = spacy.load("en_core_web_sm", disable=["ner", "parser"])
|
|
logger.info("spaCy lemma model loaded")
|
|
except Exception as e:
|
|
logger.warning(f"Failed to load spaCy lemma model: {e}")
|
|
_load_failed_lemma = True
|
|
return None
|
|
return _nlp_lemma
|