File size: 8,835 Bytes
a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 a503604 a7d24e3 06eadfa 700ea8e 06eadfa 700ea8e 06eadfa 700ea8e 06eadfa 700ea8e 06eadfa a7d24e3 |
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 |
"""
Translation service - handles model loading and translation logic
"""
import os
import time
import warnings
from typing import Tuple, Optional
from huggingface_hub import hf_hub_download
# Handle NumPy compatibility issues
try:
import numpy as np
# Suppress NumPy 2.0 warnings for compatibility
warnings.filterwarnings("ignore", message=".*copy.*", category=np.VisibleDeprecationWarning)
warnings.filterwarnings("ignore", message=".*copy.*", category=UserWarning)
except ImportError:
pass
import ctranslate2
import sentencepiece as spm
import fasttext
from ..core.config import settings
from ..core.logging import get_logger
logger = get_logger()
# Global model instances
lang_model: Optional[fasttext.FastText._FastText] = None
sp_model: Optional[spm.SentencePieceProcessor] = None
translator: Optional[ctranslate2.Translator] = None
def get_model_paths() -> Tuple[str, str, str]:
"""Get model paths from HuggingFace cache (models pre-downloaded in Docker)"""
logger.info("loading_models_from_cache")
try:
# Check if we're in offline mode (Docker environment)
offline_mode = os.environ.get("HF_HUB_OFFLINE", "0") == "1"
if offline_mode:
logger.info("running_in_offline_mode")
# In offline mode, models are already downloaded and cached
spm_path = hf_hub_download(
repo_id=settings.model_repo_id,
filename="spm.model",
local_files_only=True
)
ft_path = hf_hub_download(
repo_id=settings.model_repo_id,
filename="lid218e.bin",
local_files_only=True
)
# Get the translation model path
model_bin_path = hf_hub_download(
repo_id=settings.model_repo_id,
filename=f"translation_models/{settings.translation_model}/model.bin",
local_files_only=True
)
ct_model_full_path = os.path.dirname(model_bin_path)
else:
logger.info("running_in_online_mode")
# Online mode - download models (for local development)
spm_path = hf_hub_download(
repo_id=settings.model_repo_id,
filename="spm.model"
)
ft_path = hf_hub_download(
repo_id=settings.model_repo_id,
filename="lid218e.bin"
)
# Download all necessary CTranslate2 files
model_bin_path = hf_hub_download(
repo_id=settings.model_repo_id,
filename=f"translation_models/{settings.translation_model}/model.bin"
)
hf_hub_download(
repo_id=settings.model_repo_id,
filename=f"translation_models/{settings.translation_model}/config.json"
)
hf_hub_download(
repo_id=settings.model_repo_id,
filename=f"translation_models/{settings.translation_model}/shared_vocabulary.txt"
)
ct_model_full_path = os.path.dirname(model_bin_path)
logger.info(
"model_paths_resolved",
spm_path=spm_path,
ft_path=ft_path,
ct_model_path=ct_model_full_path
)
return spm_path, ft_path, ct_model_full_path
except Exception as e:
logger.error("model_path_resolution_failed", error=str(e))
raise e
def load_models():
"""Load all models into memory"""
global lang_model, sp_model, translator
logger.info("starting_model_loading")
# Get model paths
spm_path, ft_path, ct_model_path = get_model_paths()
# Suppress fasttext warnings
fasttext.FastText.eprint = lambda x: None
try:
# Load language detection model
logger.info("loading_language_detection_model")
lang_model = fasttext.load_model(ft_path)
# Load SentencePiece model
logger.info("loading_sentencepiece_model")
sp_model = spm.SentencePieceProcessor()
sp_model.load(spm_path)
# Load translation model
logger.info("loading_translation_model")
translator = ctranslate2.Translator(ct_model_path, settings.device)
logger.info("all_models_loaded_successfully")
except Exception as e:
logger.error("model_loading_failed", error=str(e))
raise e
def translate_with_detection(text: str, target_lang: str) -> Tuple[str, str, float]:
"""Translate text with automatic source language detection"""
start_time = time.time()
try:
# Prepare input
source_sents = [text.strip()]
target_prefix = [[target_lang]]
# Detect source language
predictions = lang_model.predict(text.replace('\n', ' '), k=1)
source_lang = predictions[0][0].replace('__label__', '')
# Tokenize source text
source_sents_subworded = sp_model.encode(source_sents, out_type=str)
source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded]
# Translate
translations = translator.translate_batch(
source_sents_subworded,
batch_type="tokens",
max_batch_size=2048,
beam_size=settings.beam_size,
target_prefix=target_prefix,
)
# Decode translation
translations = [translation[0]['tokens'] for translation in translations]
translations_desubword = sp_model.decode(translations)
translated_text = translations_desubword[0][len(target_lang):]
inference_time = time.time() - start_time
return source_lang, translated_text, inference_time
except Exception as e:
logger.error("translation_with_detection_failed", error=str(e), error_type=type(e).__name__)
# Re-raise the exception to be handled by the endpoint
raise e
def translate_with_source(text: str, source_lang: str, target_lang: str) -> Tuple[str, float]:
"""Translate text with provided source language"""
start_time = time.time()
try:
# Prepare input
source_sents = [text.strip()]
target_prefix = [[target_lang]]
# Tokenize source text
source_sents_subworded = sp_model.encode(source_sents, out_type=str)
source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded]
# Translate
translations = translator.translate_batch(
source_sents_subworded,
batch_type="tokens",
max_batch_size=2048,
beam_size=settings.beam_size,
target_prefix=target_prefix
)
# Decode translation
translations = [translation[0]['tokens'] for translation in translations]
translations_desubword = sp_model.decode(translations)
translated_text = translations_desubword[0][len(target_lang):]
inference_time = time.time() - start_time
return translated_text, inference_time
except Exception as e:
logger.error("translation_with_source_failed", error=str(e), error_type=type(e).__name__)
# Re-raise the exception to be handled by the endpoint
raise e
def detect_language(text: str) -> Tuple[str, float]:
"""
Detect the language of input text
Returns:
Tuple of (language_code, confidence_score)
"""
try:
# Clean and normalize text for better detection
# FastText models work better with lowercase text
cleaned_text = text.replace('\n', ' ').strip().lower()
# Get predictions with confidence scores
predictions = lang_model.predict(cleaned_text, k=1)
# Extract language code and confidence
language_code = predictions[0][0].replace('__label__', '')
raw_confidence = float(predictions[1][0])
# Normalize confidence to ensure it's within [0.0, 1.0]
# FastText sometimes returns values slightly above 1.0
confidence = min(raw_confidence, 1.0)
logger.info(
"language_detected",
text_length=len(text),
original_text_sample=text[:50] + "..." if len(text) > 50 else text,
cleaned_text_sample=cleaned_text[:50] + "..." if len(cleaned_text) > 50 else cleaned_text,
detected_language=language_code,
raw_confidence=raw_confidence,
normalized_confidence=confidence
)
return language_code, confidence
except Exception as e:
logger.error("language_detection_failed", error=str(e), error_type=type(e).__name__)
# Re-raise the exception to be handled by the endpoint
raise e
def models_loaded() -> bool:
"""Check if all models are loaded"""
return all([lang_model, sp_model, translator])
|