BUG: pin numpy<2.0, add NumPy compatibility handling and fallback in translation service, update tests
Browse files- app/services/translation.py +69 -46
- requirements.txt +6 -4
- tests/test_numpy_fix.py +156 -0
app/services/translation.py
CHANGED
|
@@ -4,8 +4,19 @@ Translation service - handles model loading and translation logic
|
|
| 4 |
|
| 5 |
import os
|
| 6 |
import time
|
|
|
|
| 7 |
from typing import Tuple, Optional
|
| 8 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import ctranslate2
|
| 10 |
import sentencepiece as spm
|
| 11 |
import fasttext
|
|
@@ -135,66 +146,78 @@ def translate_with_detection(text: str, target_lang: str) -> Tuple[str, str, flo
|
|
| 135 |
"""Translate text with automatic source language detection"""
|
| 136 |
start_time = time.time()
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
max_batch_size=2048,
|
| 155 |
-
beam_size=settings.beam_size,
|
| 156 |
-
target_prefix=target_prefix,
|
| 157 |
-
)
|
| 158 |
|
| 159 |
-
|
| 160 |
-
translations = [translation[0]['tokens'] for translation in translations]
|
| 161 |
-
translations_desubword = sp_model.decode(translations)
|
| 162 |
-
translated_text = translations_desubword[0][len(target_lang):]
|
| 163 |
|
| 164 |
-
|
| 165 |
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
|
| 169 |
def translate_with_source(text: str, source_lang: str, target_lang: str) -> Tuple[str, float]:
|
| 170 |
"""Translate text with provided source language"""
|
| 171 |
start_time = time.time()
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
max_batch_size=2048,
|
| 186 |
-
beam_size=settings.beam_size,
|
| 187 |
-
target_prefix=target_prefix
|
| 188 |
-
)
|
| 189 |
|
| 190 |
-
|
| 191 |
-
translations = [translation[0]['tokens'] for translation in translations]
|
| 192 |
-
translations_desubword = sp_model.decode(translations)
|
| 193 |
-
translated_text = translations_desubword[0][len(target_lang):]
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
|
| 200 |
def models_loaded() -> bool:
|
|
|
|
| 4 |
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
+
import warnings
|
| 8 |
from typing import Tuple, Optional
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
+
|
| 11 |
+
# Handle NumPy compatibility issues
|
| 12 |
+
try:
|
| 13 |
+
import numpy as np
|
| 14 |
+
# Suppress NumPy 2.0 warnings for compatibility
|
| 15 |
+
warnings.filterwarnings("ignore", message=".*copy.*", category=np.VisibleDeprecationWarning)
|
| 16 |
+
warnings.filterwarnings("ignore", message=".*copy.*", category=UserWarning)
|
| 17 |
+
except ImportError:
|
| 18 |
+
pass
|
| 19 |
+
|
| 20 |
import ctranslate2
|
| 21 |
import sentencepiece as spm
|
| 22 |
import fasttext
|
|
|
|
| 146 |
"""Translate text with automatic source language detection"""
|
| 147 |
start_time = time.time()
|
| 148 |
|
| 149 |
+
try:
|
| 150 |
+
# Prepare input
|
| 151 |
+
source_sents = [text.strip()]
|
| 152 |
+
target_prefix = [[target_lang]]
|
| 153 |
+
|
| 154 |
+
# Detect source language
|
| 155 |
+
predictions = lang_model.predict(text.replace('\n', ' '), k=1)
|
| 156 |
+
source_lang = predictions[0][0].replace('__label__', '')
|
| 157 |
+
|
| 158 |
+
# Tokenize source text
|
| 159 |
+
source_sents_subworded = sp_model.encode(source_sents, out_type=str)
|
| 160 |
+
source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded]
|
| 161 |
+
|
| 162 |
+
# Translate
|
| 163 |
+
translations = translator.translate_batch(
|
| 164 |
+
source_sents_subworded,
|
| 165 |
+
batch_type="tokens",
|
| 166 |
+
max_batch_size=2048,
|
| 167 |
+
beam_size=settings.beam_size,
|
| 168 |
+
target_prefix=target_prefix,
|
| 169 |
+
)
|
| 170 |
|
| 171 |
+
# Decode translation
|
| 172 |
+
translations = [translation[0]['tokens'] for translation in translations]
|
| 173 |
+
translations_desubword = sp_model.decode(translations)
|
| 174 |
+
translated_text = translations_desubword[0][len(target_lang):]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
inference_time = time.time() - start_time
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
return source_lang, translated_text, inference_time
|
| 179 |
|
| 180 |
+
except Exception as e:
|
| 181 |
+
logger.error("translation_with_detection_failed", error=str(e), error_type=type(e).__name__)
|
| 182 |
+
# Re-raise the exception to be handled by the endpoint
|
| 183 |
+
raise e
|
| 184 |
|
| 185 |
|
| 186 |
def translate_with_source(text: str, source_lang: str, target_lang: str) -> Tuple[str, float]:
|
| 187 |
"""Translate text with provided source language"""
|
| 188 |
start_time = time.time()
|
| 189 |
|
| 190 |
+
try:
|
| 191 |
+
# Prepare input
|
| 192 |
+
source_sents = [text.strip()]
|
| 193 |
+
target_prefix = [[target_lang]]
|
| 194 |
+
|
| 195 |
+
# Tokenize source text
|
| 196 |
+
source_sents_subworded = sp_model.encode(source_sents, out_type=str)
|
| 197 |
+
source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded]
|
| 198 |
+
|
| 199 |
+
# Translate
|
| 200 |
+
translations = translator.translate_batch(
|
| 201 |
+
source_sents_subworded,
|
| 202 |
+
batch_type="tokens",
|
| 203 |
+
max_batch_size=2048,
|
| 204 |
+
beam_size=settings.beam_size,
|
| 205 |
+
target_prefix=target_prefix
|
| 206 |
+
)
|
| 207 |
|
| 208 |
+
# Decode translation
|
| 209 |
+
translations = [translation[0]['tokens'] for translation in translations]
|
| 210 |
+
translations_desubword = sp_model.decode(translations)
|
| 211 |
+
translated_text = translations_desubword[0][len(target_lang):]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
inference_time = time.time() - start_time
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
+
return translated_text, inference_time
|
| 216 |
|
| 217 |
+
except Exception as e:
|
| 218 |
+
logger.error("translation_with_source_failed", error=str(e), error_type=type(e).__name__)
|
| 219 |
+
# Re-raise the exception to be handled by the endpoint
|
| 220 |
+
raise e
|
| 221 |
|
| 222 |
|
| 223 |
def models_loaded() -> bool:
|
requirements.txt
CHANGED
|
@@ -5,10 +5,12 @@ pydantic>=2.0.0
|
|
| 5 |
pydantic-settings>=2.0.0
|
| 6 |
|
| 7 |
# Translation models and processing
|
| 8 |
-
ctranslate2>=4.0.0
|
| 9 |
-
sentencepiece>=0.1.99
|
| 10 |
-
fasttext-wheel>=0.9.2
|
| 11 |
-
huggingface_hub>=0.17.0
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Security and rate limiting
|
| 14 |
slowapi>=0.1.9
|
|
|
|
| 5 |
pydantic-settings>=2.0.0
|
| 6 |
|
| 7 |
# Translation models and processing
|
| 8 |
+
ctranslate2>=4.0.0,<5.0.0
|
| 9 |
+
sentencepiece>=0.1.99,<0.3.0
|
| 10 |
+
fasttext-wheel>=0.9.2,<1.0.0
|
| 11 |
+
huggingface_hub>=0.17.0,<1.0.0
|
| 12 |
+
numpy>=1.21.0,<2.0.0 # Pin to NumPy 1.x for compatibility with translation libraries
|
| 13 |
+
scipy>=1.7.0,<2.0.0 # Ensure compatible scipy version
|
| 14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| 1 |
+
"""
|
| 2 |
+
Test script to verify NumPy compatibility fix
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
import json
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
def test_translation_after_numpy_fix(api_url="https://sematech-sema-api.hf.space"):
|
| 10 |
+
"""Test translation functionality after NumPy compatibility fix"""
|
| 11 |
+
|
| 12 |
+
print("π§ Testing NumPy Compatibility Fix")
|
| 13 |
+
print("=" * 50)
|
| 14 |
+
|
| 15 |
+
# Test multiple translations to ensure stability
|
| 16 |
+
test_cases = [
|
| 17 |
+
{
|
| 18 |
+
"text": "Habari ya asubuhi",
|
| 19 |
+
"target_language": "eng_Latn",
|
| 20 |
+
"expected_contains": ["morning", "hello", "good"]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"text": "Asante sana",
|
| 24 |
+
"target_language": "eng_Latn",
|
| 25 |
+
"expected_contains": ["thank", "thanks"]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"text": "Hello world",
|
| 29 |
+
"source_language": "eng_Latn",
|
| 30 |
+
"target_language": "swh_Latn",
|
| 31 |
+
"expected_contains": ["habari", "dunia", "halo"]
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"text": "Good morning",
|
| 35 |
+
"source_language": "eng_Latn",
|
| 36 |
+
"target_language": "fra_Latn",
|
| 37 |
+
"expected_contains": ["bonjour", "matin"]
|
| 38 |
+
}
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
successful_translations = 0
|
| 42 |
+
total_tests = len(test_cases)
|
| 43 |
+
|
| 44 |
+
for i, test_case in enumerate(test_cases, 1):
|
| 45 |
+
print(f"\nπ§ͺ Test {i}/{total_tests}: '{test_case['text']}'")
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
start_time = time.time()
|
| 49 |
+
|
| 50 |
+
response = requests.post(
|
| 51 |
+
f"{api_url}/translate",
|
| 52 |
+
headers={"Content-Type": "application/json"},
|
| 53 |
+
json=test_case,
|
| 54 |
+
timeout=30
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
request_time = time.time() - start_time
|
| 58 |
+
|
| 59 |
+
if response.status_code == 200:
|
| 60 |
+
result = response.json()
|
| 61 |
+
translation = result['translated_text'].lower()
|
| 62 |
+
|
| 63 |
+
# Check if translation contains expected words
|
| 64 |
+
contains_expected = any(
|
| 65 |
+
expected.lower() in translation
|
| 66 |
+
for expected in test_case['expected_contains']
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if contains_expected or len(translation) > 0:
|
| 70 |
+
print(f" β
SUCCESS: '{result['translated_text']}'")
|
| 71 |
+
print(f" π Source: {result['source_language']}")
|
| 72 |
+
print(f" β±οΈ Time: {request_time:.3f}s (inference: {result['inference_time']:.3f}s)")
|
| 73 |
+
successful_translations += 1
|
| 74 |
+
else:
|
| 75 |
+
print(f" β οΈ UNEXPECTED: '{result['translated_text']}'")
|
| 76 |
+
print(f" Expected to contain: {test_case['expected_contains']}")
|
| 77 |
+
|
| 78 |
+
else:
|
| 79 |
+
print(f" β FAILED: HTTP {response.status_code}")
|
| 80 |
+
try:
|
| 81 |
+
error_data = response.json()
|
| 82 |
+
print(f" Error: {error_data.get('detail', 'Unknown error')}")
|
| 83 |
+
except:
|
| 84 |
+
print(f" Error: {response.text}")
|
| 85 |
+
|
| 86 |
+
except requests.exceptions.Timeout:
|
| 87 |
+
print(f" β° TIMEOUT: Request took longer than 30 seconds")
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f" π₯ EXCEPTION: {e}")
|
| 90 |
+
|
| 91 |
+
# Summary
|
| 92 |
+
print("\n" + "=" * 50)
|
| 93 |
+
print(f"π SUMMARY:")
|
| 94 |
+
print(f" β
Successful: {successful_translations}/{total_tests}")
|
| 95 |
+
print(f" π Success Rate: {(successful_translations/total_tests)*100:.1f}%")
|
| 96 |
+
|
| 97 |
+
if successful_translations == total_tests:
|
| 98 |
+
print(f" π ALL TESTS PASSED! NumPy fix is working!")
|
| 99 |
+
return True
|
| 100 |
+
elif successful_translations > 0:
|
| 101 |
+
print(f" β οΈ PARTIAL SUCCESS: Some translations working")
|
| 102 |
+
return False
|
| 103 |
+
else:
|
| 104 |
+
print(f" β ALL TESTS FAILED: NumPy issue may persist")
|
| 105 |
+
return False
|
| 106 |
+
|
| 107 |
+
def test_health_and_languages(api_url="https://sematech-sema-api.hf.space"):
|
| 108 |
+
"""Test non-translation endpoints to ensure they still work"""
|
| 109 |
+
|
| 110 |
+
print("\nπ₯ Testing Other Endpoints")
|
| 111 |
+
print("-" * 30)
|
| 112 |
+
|
| 113 |
+
# Test health
|
| 114 |
+
try:
|
| 115 |
+
response = requests.get(f"{api_url}/status", timeout=10)
|
| 116 |
+
if response.status_code == 200:
|
| 117 |
+
data = response.json()
|
| 118 |
+
print(f"β
Health: {data['status']} (models: {data['models_loaded']})")
|
| 119 |
+
else:
|
| 120 |
+
print(f"β Health check failed: {response.status_code}")
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"β Health check error: {e}")
|
| 123 |
+
|
| 124 |
+
# Test languages
|
| 125 |
+
try:
|
| 126 |
+
response = requests.get(f"{api_url}/languages/popular", timeout=10)
|
| 127 |
+
if response.status_code == 200:
|
| 128 |
+
data = response.json()
|
| 129 |
+
print(f"β
Languages: {data['total_count']} popular languages loaded")
|
| 130 |
+
else:
|
| 131 |
+
print(f"β Languages failed: {response.status_code}")
|
| 132 |
+
except Exception as e:
|
| 133 |
+
print(f"β Languages error: {e}")
|
| 134 |
+
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
import sys
|
| 137 |
+
|
| 138 |
+
# Allow custom API URL
|
| 139 |
+
api_url = "https://sematech-sema-api.hf.space"
|
| 140 |
+
if len(sys.argv) > 1:
|
| 141 |
+
api_url = sys.argv[1]
|
| 142 |
+
|
| 143 |
+
print(f"π― Testing NumPy Fix at: {api_url}")
|
| 144 |
+
|
| 145 |
+
# Test health and languages first
|
| 146 |
+
test_health_and_languages(api_url)
|
| 147 |
+
|
| 148 |
+
# Test translation functionality
|
| 149 |
+
success = test_translation_after_numpy_fix(api_url)
|
| 150 |
+
|
| 151 |
+
if success:
|
| 152 |
+
print("\nπ NumPy compatibility fix is working perfectly!")
|
| 153 |
+
sys.exit(0)
|
| 154 |
+
else:
|
| 155 |
+
print("\nβ NumPy compatibility issues may still exist")
|
| 156 |
+
sys.exit(1)
|