File size: 9,130 Bytes
d01de5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130ce6d
 
d01de5d
 
 
 
 
 
 
 
 
 
 
 
 
130ce6d
d01de5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Translation Engine for Carsa AI

A comprehensive translation engine that supports translation from English
to multiple African languages using Helsinki-NLP models.

Supported Languages:
- Twi (Akan) - 'twi'
- Ga - 'ga' 
- Ewe - 'ewe'
- Igbo - 'igbo'
- Swahili - 'swahili'
- Amharic - 'amharic'
- Zulu - 'zulu'
- Xhosa - 'xhosa'

Author: Carsa AI Team
Version: 1.0.0
"""

import torch
from transformers import pipeline
import logging
import time

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class TranslationEngine:
    """
    A production-ready translation engine for African languages.
    
    This class provides translation capabilities from English to various
    African languages using pre-trained Helsinki-NLP models.
    """
    
    def __init__(self):
        """
        Initialize the Translation Engine with support for multiple African languages.
        
        Raises:
            Exception: If critical models fail to load
        """
        self.device = 0 if torch.cuda.is_available() else -1
        device_name = "GPU" if torch.cuda.is_available() else "CPU"
        logger.info(f"Translation Engine using device: {device_name}")
        
        # Define supported language models
        self.language_models = {
            "twi": "Helsinki-NLP/opus-mt-en-tw",
            "ga": "Helsinki-NLP/opus-mt-en-gaa", 
            "ewe": "Helsinki-NLP/opus-mt-en-ee",
            # Hausa removed - model discontinued
            # "hausa": "Helsinki-NLP/opus-mt-en-ha",
            # Note: Yoruba model temporarily disabled - no valid model found
            # "yoruba": "Helsinki-NLP/opus-mt-en-yo",  # This model doesn't exist
            "igbo": "Helsinki-NLP/opus-mt-en-ig", 
            "swahili": "Helsinki-NLP/opus-mt-en-sw",
            "amharic": "Helsinki-NLP/opus-mt-en-am",
            "zulu": "Helsinki-NLP/opus-mt-en-zu",
            "xhosa": "Helsinki-NLP/opus-mt-en-xh"
        }
        
        # Store loaded translators
        self.translators = {}
        
        # Load critical models (the ones your Flutter app primarily uses)
        self.critical_languages = ["twi", "ga", "ewe"]
        self._load_critical_models()
        
        logger.info("Translation Engine initialized successfully!")
    
    def _load_critical_models(self):
        """Load the most important models that your Flutter app uses."""
        for lang in self.critical_languages:
            try:
                self._load_single_model(lang)
            except Exception as e:
                logger.error(f"Failed to load critical model for '{lang}': {e}")
                # Don't raise exception - continue with other models
                continue
    
    def _load_single_model(self, language_code):
        """
        Load a single translation model.
        
        Args:
            language_code (str): The language code to load
            
        Returns:
            bool: True if successful, False otherwise
        """
        if language_code in self.translators:
            return True
            
        if language_code not in self.language_models:
            logger.warning(f"Language '{language_code}' not supported")
            return False
        
        try:
            model_name = self.language_models[language_code]
            logger.info(f"Loading model for '{language_code}': {model_name}")
            
            # Create the appropriate task name
            if language_code == "twi":
                task = "translation_en_to_tw"
            else:
                task = f"translation_en_to_{language_code}"
            
            # Load the model
            translator = pipeline(
                task,
                model=model_name,
                device=self.device,
                torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
            )
            
            self.translators[language_code] = translator
            logger.info(f"βœ… Successfully loaded model for '{language_code}'")
            return True
            
        except Exception as e:
            logger.error(f"❌ Failed to load model for '{language_code}': {e}")
            return False
    
    def translate(self, text, target_language):
        """
        Translate text from English to the specified target language.
        
        Args:
            text (str): The English text to translate
            target_language (str): Target language code
            
        Returns:
            str: The translated text
            
        Raises:
            ValueError: If input parameters are invalid
            RuntimeError: If translation fails
        """
        # Input validation
        if not text or not text.strip():
            raise ValueError("Text cannot be empty")
        
        if not target_language:
            raise ValueError("Target language cannot be empty")
        
        target_language = target_language.lower().strip()
        
        # Check if language is supported
        if target_language not in self.language_models:
            supported = ", ".join(self.language_models.keys())
            raise ValueError(f"Language '{target_language}' not supported. Supported languages: {supported}")
        
        # Load model if not already loaded
        if target_language not in self.translators:
            logger.info(f"Loading model for '{target_language}' on demand...")
            if not self._load_single_model(target_language):
                raise RuntimeError(f"Failed to load translation model for '{target_language}'")
        
        try:
            # Perform translation
            translator = self.translators[target_language]
            
            # Log translation request
            logger.info(f"Translating to {target_language}: '{text[:50]}{'...' if len(text) > 50 else ''}'")
            
            start_time = time.time()
            result = translator(text)
            translation_time = time.time() - start_time
            
            # Extract translated text
            if isinstance(result, list) and len(result) > 0:
                translated_text = result[0].get('translation_text', '')
            else:
                translated_text = str(result)
            
            logger.info(f"Translation completed in {translation_time:.2f}s: '{translated_text[:50]}{'...' if len(translated_text) > 50 else ''}'")
            
            return translated_text
            
        except Exception as e:
            logger.error(f"Translation failed for '{target_language}': {e}")
            raise RuntimeError(f"Translation failed: {str(e)}")
    
    def get_supported_languages(self):
        """
        Get list of supported languages.
        
        Returns:
            list: List of supported language codes
        """
        return list(self.language_models.keys())
    
    def get_loaded_languages(self):
        """
        Get list of currently loaded languages.
        
        Returns:
            list: List of loaded language codes
        """
        return list(self.translators.keys())
    
    def is_language_supported(self, language_code):
        """
        Check if a language is supported.
        
        Args:
            language_code (str): Language code to check
            
        Returns:
            bool: True if supported, False otherwise
        """
        return language_code.lower() in self.language_models
    
    def get_engine_info(self):
        """
        Get information about the translation engine.
        
        Returns:
            dict: Engine information including supported and loaded languages
        """
        return {
            "engine": "Translation Engine",
            "version": "1.0.0",
            "device": "GPU" if torch.cuda.is_available() else "CPU",
            "supported_languages": self.get_supported_languages(),
            "loaded_languages": self.get_loaded_languages(),
            "total_models": len(self.language_models),
            "loaded_models": len(self.translators)
        }


def main():
    """Example usage and testing of the Translation Engine."""
    try:
        # Initialize the engine
        logger.info("Testing Translation Engine...")
        engine = TranslationEngine()
        
        # Print engine info
        info = engine.get_engine_info()
        logger.info(f"Engine Info: {info}")
        
        # Test translations for critical languages
        test_text = "Hello, how are you today? This is a test of the translation system."
        
        for lang in ["twi", "ga", "ewe", "hausa"]:
            try:
                translated = engine.translate(test_text, lang)
                logger.info(f"🎯 {lang.upper()}: {translated}")
            except Exception as e:
                logger.error(f"❌ Failed to translate to {lang}: {e}")
        
        logger.info("πŸŽ‰ Translation Engine testing completed!")
        
    except Exception as e:
        logger.error(f"❌ Translation Engine test failed: {e}")


if __name__ == "__main__":
    main()