Spaces:
Runtime error
Runtime error
File size: 12,374 Bytes
73a6a7e |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# app/core/model_manager.py
import logging
import os
import asyncio
from pathlib import Path
from typing import Callable, Optional, Dict, List
# Imports for downloading specific model types
import nltk
from huggingface_hub import snapshot_download
import spacy.cli
# Internal application imports
from app.core.config import (
MODELS_DIR,
NLTK_DATA_DIR,
SPACY_MODEL_ID,
SENTENCE_TRANSFORMER_MODEL_ID,
TONE_MODEL_ID,
TRANSLATION_MODEL_ID,
WORDNET_NLTK_ID,
APP_NAME
)
from app.core.exceptions import ModelNotDownloadedError, ModelDownloadFailedError, ServiceError
logger = logging.getLogger(f"{APP_NAME}.core.model_manager")
# Type alias for progress callback
ProgressCallback = Callable[[str, str, float, Optional[str]], None] # (model_id, status, progress, message)
def _get_hf_model_local_path(model_id: str) -> Path:
"""Helper to get the expected local path for a Hugging Face model."""
# snapshot_download creates a specific folder structure inside MODELS_DIR/hf_cache
# For example, for "bert-base-uncased", it might be MODELS_DIR/hf_cache/models--bert-base-uncased
# The actual model files are inside that.
# The `transformers` library usually handles this resolution.
# We just need to check if the directory created by snapshot_download exists.
# A robust check involves looking inside that directory.
return MODELS_DIR / "hf_cache" / model_id.replace("/", "--") # Standard HF cache path logic
def check_model_exists(model_id: str, model_type: str) -> bool:
"""
Checks if a specific model or NLTK data is already downloaded locally.
"""
if model_type == "huggingface":
local_path = _get_hf_model_local_path(model_id)
# Check if the directory exists and contains some files
return local_path.is_dir() and any(local_path.iterdir())
elif model_type == "spacy":
# spaCy models are symlinked or copied into a specific site-packages location
# The easiest check is to try loading it, or check spacy.util.is_package
# For our purposes, we'll check if the directory created by `spacy download` exists
# within our MODELS_DIR, assuming we direct spaCy there.
# However, `spacy.load` is the most reliable. For pre-check, we'll rely on the
# existence check in load_spacy_model. This is a simplified check.
# The actual loading process in app.services.base handles the `is_package` check.
# For `spacy.cli.download` to work with MODELS_DIR, it often requires setting SPACY_DATA.
spacy_target_path = MODELS_DIR / model_id
return spacy_target_path.is_dir() and any(spacy_target_path.iterdir())
elif model_type == "nltk":
# NLTK data check
try:
return nltk.data.find(f"corpora/{model_id}") is not None
except LookupError:
return False
else:
logger.warning(f"Unknown model type for check_model_exists: {model_type}")
return False
# --- Download Functions ---
async def download_hf_model_async(
model_id: str,
feature_name: str,
progress_callback: Optional[ProgressCallback] = None
) -> None:
"""
Asynchronously downloads a Hugging Face model from the Hub.
"""
logger.info(f"Initiating download for Hugging Face model '{model_id}' for '{feature_name}'...")
if check_model_exists(model_id, "huggingface"):
logger.info(f"Hugging Face model '{model_id}' already exists locally. Skipping download.")
if progress_callback:
progress_callback(model_id, "completed", 1.0, "Already downloaded.")
return
# Use a thread pool for blocking download operation
try:
def _blocking_download():
# This downloads to MODELS_DIR/hf_cache by default if HF_HOME is set to MODELS_DIR
# Otherwise, specify cache_dir.
# For simplicity, we rely on `settings.MODELS_DIR` handling HF_HOME in config.py
snapshot_download(
repo_id=model_id,
cache_dir=str(MODELS_DIR / "hf_cache"), # Explicitly set cache directory
local_dir_use_symlinks=False, # Use False for better self-contained app
# The `_` prefix means it's an internal parameter not typically exposed.
# `progress_callback` in `snapshot_download` is not directly exposed for live updates.
# We log at beginning and end.
)
logger.info(f"Hugging Face model '{model_id}' download complete.")
if progress_callback:
progress_callback(model_id, "downloading", 0.05, "Starting download...")
await asyncio.to_thread(_blocking_download) # Run blocking download in a separate thread
if progress_callback:
progress_callback(model_id, "completed", 1.0, "Download successful.")
except Exception as e:
logger.error(f"Failed to download Hugging Face model '{model_id}': {e}", exc_info=True)
if progress_callback:
progress_callback(model_id, "failed", 0.0, f"Error: {e}")
raise ModelDownloadFailedError(model_id, feature_name, original_error=str(e))
async def download_spacy_model_async(
model_id: str,
feature_name: str,
progress_callback: Optional[ProgressCallback] = None
) -> None:
"""
Asynchronously downloads a spaCy model.
"""
logger.info(f"Initiating download for spaCy model '{model_id}' for '{feature_name}'...")
# Check if the model package is already installed/available in the spacy data path
# NOTE: This check might not be sufficient if SPACY_DATA isn't correctly pointing.
# The `spacy.util.is_package` would be more robust but requires `import spacy` first.
# For now, we trust `spacy.cli.download` to handle the check or fail gracefully.
# We must ensure SPACY_DATA environment variable is set to MODELS_DIR
# for spacy.cli.download to put it in our custom path.
original_spacy_data = os.environ.get("SPACY_DATA")
try:
os.environ["SPACY_DATA"] = str(MODELS_DIR)
if check_model_exists(model_id, "spacy"): # Using our own simplified check
logger.info(f"SpaCy model '{model_id}' already exists locally. Skipping download.")
if progress_callback:
progress_callback(model_id, "completed", 1.0, "Already downloaded.")
return
def _blocking_download():
# spacy.cli.download attempts to download and link/copy
# It will raise an error if already downloaded if it can't link, etc.
# We're relying on our check_model_exists before this.
spacy.cli.download(model_id)
logger.info(f"SpaCy model '{model_id}' download complete.")
if progress_callback:
progress_callback(model_id, "downloading", 0.05, "Starting download...")
await asyncio.to_thread(_blocking_download)
if progress_callback:
progress_callback(model_id, "completed", 1.0, "Download successful.")
except Exception as e:
logger.error(f"Failed to download spaCy model '{model_id}': {e}", exc_info=True)
if progress_callback:
progress_callback(model_id, "failed", 0.0, f"Error: {e}")
raise ModelDownloadFailedError(model_id, feature_name, original_error=str(e))
finally:
# Restore original SPACY_DATA if it was set
if original_spacy_data is not None:
os.environ["SPACY_DATA"] = original_spacy_data
else:
if "SPACY_DATA" in os.environ:
del os.environ["SPACY_DATA"]
async def download_nltk_data_async(
data_id: str,
feature_name: str,
progress_callback: Optional[ProgressCallback] = None
) -> None:
"""
Asynchronously downloads NLTK data.
"""
logger.info(f"Initiating download for NLTK data '{data_id}' for '{feature_name}'...")
# NLTK data path should be set by NLTK_DATA environment variable in config.py
# `nltk.download` will use this path.
if check_model_exists(data_id, "nltk"):
logger.info(f"NLTK data '{data_id}' already exists locally. Skipping download.")
if progress_callback:
progress_callback(data_id, "completed", 1.0, "Already downloaded.")
return
def _blocking_download():
# NLTK downloader can show a GUI, so ensure it's not trying to do that
# `download_dir` should be set by NLTK_DATA env variable.
# `quiet=True` is important for programmatic download.
nltk.download(data_id, download_dir=str(NLTK_DATA_DIR), quiet=True)
logger.info(f"NLTK data '{data_id}' download complete.")
try:
if progress_callback:
progress_callback(data_id, "downloading", 0.05, "Starting download...")
await asyncio.to_thread(_blocking_download)
if progress_callback:
progress_callback(data_id, "completed", 1.0, "Download successful.")
except Exception as e:
logger.error(f"Failed to download NLTK data '{data_id}': {e}", exc_info=True)
if progress_callback:
progress_callback(data_id, "failed", 0.0, f"Error: {e}")
raise ModelDownloadFailedError(data_id, feature_name, original_error=str(e))
# --- Comprehensive Model Management ---
def get_all_required_models() -> List[Dict]:
"""
Returns a list of all models required by the application, with their type and feature.
"""
return [
{"id": SPACY_MODEL_ID, "type": "spacy", "feature": "Text Processing (General)"},
{"id": SENTENCE_TRANSFORMER_MODEL_ID, "type": "huggingface", "feature": "Sentence Embeddings"},
{"id": TONE_MODEL_ID, "type": "huggingface", "feature": "Tone Classification"},
{"id": TRANSLATION_MODEL_ID, "type": "huggingface", "feature": "Translation"},
{"id": WORDNET_NLTK_ID, "type": "nltk", "feature": "Synonym Suggestion"},
# Add any other models here as your application grows
]
async def download_all_required_models(progress_callback: Optional[ProgressCallback] = None) -> Dict[str, str]:
"""
Attempts to download all required models.
Returns a dictionary of download statuses.
"""
required_models = get_all_required_models()
download_statuses = {}
for model_info in required_models:
model_id = model_info["id"]
model_type = model_info["type"]
feature_name = model_info["feature"]
if check_model_exists(model_id, model_type):
status_message = f"'{model_id}' ({feature_name}) already downloaded."
logger.info(status_message)
download_statuses[model_id] = "already_downloaded"
if progress_callback:
progress_callback(model_id, "completed", 1.0, status_message)
continue
logger.info(f"Attempting to download '{model_id}' ({feature_name})...")
try:
if model_type == "huggingface":
await download_hf_model_async(model_id, feature_name, progress_callback)
elif model_type == "spacy":
await download_spacy_model_async(model_id, feature_name, progress_callback)
elif model_type == "nltk":
await download_nltk_data_async(model_id, feature_name, progress_callback)
else:
raise ValueError(f"Unsupported model type: {model_type}")
status_message = f"'{model_id}' ({feature_name}) downloaded successfully."
logger.info(status_message)
download_statuses[model_id] = "success"
except ModelDownloadFailedError as e:
status_message = f"Failed to download '{model_id}' ({feature_name}): {e.original_error}"
logger.error(status_message)
download_statuses[model_id] = "failed"
# The progress_callback is already called within the specific download functions on failure
except Exception as e:
status_message = f"An unexpected error occurred while downloading '{model_id}' ({feature_name}): {e}"
logger.error(status_message, exc_info=True)
download_statuses[model_id] = "failed"
if progress_callback:
progress_callback(model_id, "failed", 0.0, status_message)
logger.info("Finished attempting to download all required models.")
return download_statuses |