File size: 14,058 Bytes
1d46eb9
 
 
 
 
 
 
 
 
a7fd3ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62d99f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dcfc82
 
 
 
 
4056c2c
5dcfc82
 
 
62d99f6
4056c2c
62d99f6
 
 
 
 
 
 
 
5dcfc82
 
 
 
4056c2c
 
5dcfc82
 
 
 
 
 
4056c2c
 
 
 
 
 
 
 
 
 
 
 
fb6b1e8
4056c2c
 
5dcfc82
1d46eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4056c2c
 
 
 
 
 
 
 
 
 
 
62d99f6
 
 
 
 
 
 
 
 
 
e138b0e
4056c2c
 
e2fd976
 
 
 
 
 
4056c2c
c306eb7
4056c2c
e2fd976
f359dc2
 
 
4056c2c
e2fd976
 
 
4056c2c
f359dc2
 
 
4056c2c
 
 
becf438
c306eb7
 
4056c2c
 
 
 
1d46eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7fd3ba
 
1d46eb9
 
f359dc2
 
 
 
 
 
 
 
62d99f6
 
 
 
 
 
 
 
 
 
f359dc2
 
 
 
 
 
 
 
 
90c12a2
f359dc2
 
 
 
90c12a2
f359dc2
 
 
 
 
 
 
 
 
 
 
90c12a2
f359dc2
 
 
1d46eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f359dc2
 
 
 
 
 
1d46eb9
 
 
 
 
 
 
 
 
 
 
 
 
f359dc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""
Processing utilities for Vietnamese translation integration
"""

import logging
from typing import Dict, Any, List, Optional, Callable

logger = logging.getLogger(__name__)

def _vi_sanitize_text(s: str) -> str:
    """Light Vietnamese sanitization for finetuning and RAG: strip extra spaces, limit repetition, preserve numbers/units."""
    if not isinstance(s, str):
        return s
    t = s.strip()
    # Collapse repeated punctuation and whitespace
    import re
    t = re.sub(r"\s+", " ", t)
    t = re.sub(r"([.?!]){3,}", r"..", t)
    # Remove obvious repetition chunks (very heuristic)
    parts = t.split()
    if len(parts) > 20:
        window = 6
        seen = set()
        filtered = []
        for i in range(len(parts)):
            ngram = " ".join(parts[max(0, i-window):i+1])
            if ngram in seen:
                continue
            seen.add(ngram)
            filtered.append(parts[i])
        t = " ".join(filtered)
    return t

def _is_vietnamese_text(text: str) -> bool:
    """Check if text is already in Vietnamese"""
    if not text or not isinstance(text, str):
        return False
    
    import re
    # Check for Vietnamese characters
    vietnamese_chars = len(re.findall(r'[àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđ]', text, re.IGNORECASE))
    total_chars = len(re.sub(r'\s', '', text))
    
    # If more than 20% of characters are Vietnamese, consider it Vietnamese text
    if total_chars > 0 and vietnamese_chars / total_chars > 0.2:
        return True
    
    # Check for common Vietnamese words (including single words)
    vietnamese_words = ['chào', 'xin chào', 'cảm ơn', 'tôi', 'bạn', 'là', 'có', 'không', 'và', 'của', 'trong', 'với', 'để', 'cho', 'về', 'từ', 'đến', 'tại', 'này', 'đó', 'đây', 'kia', 'nào', 'sao', 'thế', 'nào', 'gì', 'ai', 'đâu', 'khi', 'nếu', 'mà', 'để', 'cho', 'về', 'từ', 'đến', 'tại', 'triệu', 'chứng', 'bệnh', 'tiểu', 'đường', 'bác', 'sĩ', 'bệnh', 'nhân']
    text_lower = text.lower()
    vietnamese_word_count = sum(1 for word in vietnamese_words if word in text_lower)
    
    # If text contains any Vietnamese words, consider it Vietnamese
    if vietnamese_word_count >= 1:
        return True
    
    return False

def _validate_vi_translation(original: str, translated: str) -> bool:
    """Validate Vietnamese translation quality"""
    if not translated or not isinstance(translated, str):
        return False
    
    # Check if translation is too short
    if len(translated.strip()) < 3:
        return False
    
    # If translation is identical to original, check if original was already Vietnamese
    if translated.strip() == original.strip():
        # If original was already Vietnamese, this is actually a valid case
        if _is_vietnamese_text(original):
            return True
        # Otherwise, it's not a valid translation
        return False
    
    # Check if original was Vietnamese but translated is English (wrong direction)
    if _is_vietnamese_text(original) and not _is_vietnamese_text(translated):
        return False
    
    # Check for common translation failure patterns
    failure_patterns = [
        "translation error", "translation failed", "unable to translate", 
        "cannot translate", "not available", "not found", "invalid translation"
    ]
    translated_lower = translated.lower()
    for pattern in failure_patterns:
        if pattern in translated_lower:
            return False
    
    # Check if translation contains Vietnamese characters (basic check)
    import re
    vietnamese_chars = len(re.findall(r'[àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđ]', translated, re.IGNORECASE))
    total_chars = len(re.sub(r'\s', '', translated))
    
    # If there are Vietnamese characters, it's likely a valid translation
    if vietnamese_chars > 0:
        return True
    
    # If no Vietnamese characters but significantly different from original, accept it
    # (some translations might not have Vietnamese diacritics)
    if len(translated) > len(original) * 0.5 and len(translated) < len(original) * 2.0:
        return True
    
    return False

def translate_sft_row(row: Dict[str, Any], translator, text_fields: List[str] = None) -> Dict[str, Any]:
    """
    Translate specific text fields in an SFT row from English to Vietnamese.
    
    Args:
        row: SFT row dictionary
        translator: VietnameseTranslator instance
        text_fields: List of field names to translate. If None, uses default fields.
        
    Returns:
        Translated SFT row dictionary
    """
    if not translator or not translator.is_loaded():
        logger.warning("Translator not available, skipping translation")
        return row
    
    if text_fields is None:
        # Default fields to translate in SFT format
        text_fields = ["instruction", "input", "output"]
    
    try:
        # Create a copy of the row to avoid modifying the original
        translated_row = row.copy()
        
        # Translate the SFT fields directly
        sft_data = row.get("sft", {})
        translated_sft = {}
        
        for field in text_fields:
            if field in sft_data and isinstance(sft_data[field], str) and sft_data[field].strip():
                try:
                    original = sft_data[field]
                    
                    # Check if text is already in Vietnamese - skip translation if so
                    if _is_vietnamese_text(original):
                        logger.debug(f"Field '{field}' is already in Vietnamese, skipping translation")
                        translated_sft[field] = original
                        # Add success statistics (no translation needed)
                        if hasattr(translator, '_stats'):
                            add_translation_stats(translator._stats, f"sft_{field}", True)
                        continue
                    
                    # Use LLM for Vietnamese translation instead of Opus model
                    translated = translator.translate_text(original)
                    
                    # Debug logging
                    logger.debug(f"Translation attempt for field '{field}':")
                    logger.debug(f"  Original: '{original[:50]}...'")
                    logger.debug(f"  Translated: '{translated[:50]}...'")
                    logger.debug(f"  Are they the same? {original == translated}")
                    
                    # Validate and sanitize translated field
                    if _validate_vi_translation(original, translated):
                        translated_sft[field] = _vi_sanitize_text(translated)
                        logger.debug(f"✅ Successfully translated field '{field}'")
                        # Add success statistics if stats available
                        if hasattr(translator, '_stats'):
                            add_translation_stats(translator._stats, f"sft_{field}", True)
                    else:
                        logger.warning(f"❌ Invalid Vietnamese translation for field {field}, keeping original")
                        logger.warning(f"  Original: '{original[:50]}...'")
                        logger.warning(f"  Translated: '{translated[:50]}...'")
                        translated_sft[field] = original
                        # Add failure statistics if stats available
                        if hasattr(translator, '_stats'):
                            add_translation_stats(translator._stats, f"sft_{field}", False)
                except Exception as e:
                    logger.error(f"Failed to translate field '{field}': {e}")
                    translated_sft[field] = sft_data[field]
                else:
                    # Keep original if field doesn't exist or is empty
                    translated_sft[field] = sft_data.get(field, "")
        
        # Update the translated row
        translated_row["sft"] = translated_sft
        
        logger.debug(f"Translated SFT row with fields: {text_fields}")
        return translated_row
    except Exception as e:
        logger.error(f"Failed to translate SFT row: {e}")
        return row

def translate_rag_row(row: Dict[str, Any], translator, text_fields: List[str] = None) -> Dict[str, Any]:
    """
    Translate specific text fields in a RAG row from English to Vietnamese.
    
    Args:
        row: RAG row dictionary
        translator: VietnameseTranslator instance
        text_fields: List of field names to translate. If None, uses default fields.
        
    Returns:
        Translated RAG row dictionary
    """
    if not translator or not translator.is_loaded():
        logger.warning("Translator not available, skipping translation")
        return row
    
    if text_fields is None:
        # Default fields to translate in RAG format (Q, A, C)
        text_fields = ["question", "answer", "context"]
    
    try:
        # Create a copy of the row to avoid modifying the original
        translated_row = row.copy()
        
        # Translate each field individually with proper validation
        for field in text_fields:
            if field in row and isinstance(row[field], str) and row[field].strip():
                try:
                    original = row[field]
                    
                    # Check if text is already in Vietnamese - skip translation if so
                    if _is_vietnamese_text(original):
                        logger.debug(f"RAG Field '{field}' is already in Vietnamese, skipping translation")
                        translated_row[field] = original
                        # Add success statistics (no translation needed)
                        if hasattr(translator, '_stats'):
                            add_translation_stats(translator._stats, f"rag_{field}", True)
                        continue
                    
                    translated = translator.translate_text(original)
                    
                    # Debug logging
                    logger.debug(f"RAG Translation attempt for field '{field}':")
                    logger.debug(f"  Original: '{original[:50]}...'")
                    logger.debug(f"  Translated: '{translated[:50]}...'")
                    logger.debug(f"  Are they the same? {original == translated}")
                    
                    # Validate and sanitize translated field
                    if _validate_vi_translation(original, translated):
                        translated_row[field] = _vi_sanitize_text(translated)
                        logger.debug(f"✅ Successfully translated RAG field '{field}'")
                        # Add success statistics if stats available
                        if hasattr(translator, '_stats'):
                                add_translation_stats(translator._stats, f"rag_{field}", True)
                    else:
                        logger.warning(f"❌ Invalid Vietnamese translation for RAG field {field}, keeping original")
                        logger.warning(f"  Original: '{original[:50]}...'")
                        logger.warning(f"  Translated: '{translated[:50]}...'")
                        translated_row[field] = original
                        # Add failure statistics if stats available
                        if hasattr(translator, '_stats'):
                            add_translation_stats(translator._stats, f"rag_{field}", False)
                except Exception as e:
                    logger.error(f"Failed to translate RAG field '{field}': {e}")
                    translated_row[field] = row[field]
            else:
                # Keep original if field doesn't exist or is empty
                translated_row[field] = row.get(field, "")
        
        logger.debug(f"Translated RAG row with fields: {text_fields}")
        return translated_row
    except Exception as e:
        logger.error(f"Failed to translate RAG row: {e}")
        return row

def should_translate(vietnamese_translation: bool, translator) -> bool:
    """
    Check if translation should be performed.
    
    Args:
        vietnamese_translation: Flag from user input
        translator: VietnameseTranslator instance
        
    Returns:
        True if translation should be performed
    """
    if not vietnamese_translation:
        return False
    
    if not translator:
        logger.warning("Vietnamese translation requested but translator is None")
        return False
    
    if not hasattr(translator, 'is_loaded') or not translator.is_loaded():
        logger.warning("Vietnamese translation requested but translator not loaded")
        return False
    
    return True

def log_translation_stats(stats: Dict[str, Any], translated_count: int) -> None:
    """
    Log translation statistics.
    
    Args:
        stats: Statistics dictionary to update
        translated_count: Number of items translated
    """
    stats["vietnamese_translated"] = translated_count
    logger.info(f"Vietnamese translation completed: {translated_count} items translated")

def add_translation_stats(stats: Dict[str, Any], field: str, success: bool) -> None:
    """
    Add translation statistics for individual fields.
    
    Args:
        stats: Statistics dictionary to update
        field: Field name that was translated
        success: Whether translation was successful
    """
    if "translation_stats" not in stats:
        stats["translation_stats"] = {}
    
    if field not in stats["translation_stats"]:
        stats["translation_stats"][field] = {"success": 0, "failed": 0}
    
    if success:
        stats["translation_stats"][field]["success"] += 1
    else:
        stats["translation_stats"][field]["failed"] += 1