-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapertus_translator.py
More file actions
321 lines (265 loc) · 11.1 KB
/
Copy pathapertus_translator.py
File metadata and controls
321 lines (265 loc) · 11.1 KB
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
317
318
319
320
321
#!/usr/bin/env python3
"""
Apertus8B Integration for Romansh Translation
Specialized wrapper for Swiss languages including Romansh (Sursilvan)
"""
import os
import sys
import time
import warnings
from pathlib import Path
warnings.filterwarnings("ignore")
try:
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
print("✅ Required packages loaded successfully")
except ImportError as e:
print("❌ Error: Required packages not installed")
print(f"Missing: {e}")
sys.exit(1)
try:
from text_chunker import SmartTextChunker
except ImportError:
# Chunker not available, will process text as single unit
SmartTextChunker = None
class ApertusTranslator:
"""
Apertus8B translator for Swiss languages, optimized for Romansh.
Apertus8B supports 1811 languages including:
- Romansh (Sursilvan, Vallader, Puter, Surmiran, Sutsilvan, Rumantsch Grischun)
- German, French, Italian, English
"""
def __init__(self, model_path=None):
# Try multiple paths in order of preference
if model_path is None:
# 1. Environment variable
# 2. Common install locations
# 3. Relative to project root
possible_paths = [
os.environ.get('APERTUS_PATH'),
os.environ.get('APERTUS8B_PATH'),
'/home/aldn/Apertus8B',
'./models/apertus-8b'
]
# Use first path that exists and has files
for path in possible_paths:
if path and Path(path).exists() and any(Path(path).iterdir()):
model_path = path
break
else:
# Fallback if nothing exists
model_path = './models/apertus-8b'
self.model_path = Path(model_path)
self.model = None
self.tokenizer = None
self.device = "cuda" if torch.cuda.is_available() else "cpu"
# Language mappings for Romansh variants
self.romansh_variants = {
'rm-sursilv': 'Romansh Sursilvan',
'rm-vallader': 'Romansh Vallader',
'rm-puter': 'Romansh Puter',
'rm-surmiran': 'Romansh Surmiran',
'rm-sutsilv': 'Romansh Sutsilvan',
'rm-rumgr': 'Rumantsch Grischun',
'rm': 'Romansh' # Generic
}
self.supported_languages = {
# Core European languages
'de': 'German',
'fr': 'French',
'it': 'Italian',
'en': 'English',
'es': 'Spanish',
'pt': 'Portuguese',
# World languages (added Dec 2025)
'ru': 'Russian',
'zh': 'Chinese',
'hi': 'Hindi',
'ar': 'Arabic',
'ja': 'Japanese',
'ko': 'Korean',
# Romansh variants
**self.romansh_variants
}
print("🇨🇭 Apertus8B Translator for Swiss Languages")
print(f"📁 Model path: {self.model_path}")
print(f"💾 Device: {self.device}")
def load_model(self):
"""Load Apertus8B model and tokenizer."""
if self.model is not None:
print("✅ Model already loaded")
return True
try:
print("⏳ Loading Apertus8B (8B parameters, ~16GB)...")
print(" This may take 30-60 seconds on first load...")
start_time = time.time()
# Load tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(self.model_path)
# Load model with optimizations
self.model = AutoModelForCausalLM.from_pretrained(
self.model_path,
torch_dtype=torch.bfloat16 if self.device == "cuda" else torch.float32,
device_map="auto" if self.device == "cuda" else None,
low_cpu_mem_usage=True
)
if self.device == "cpu":
self.model = self.model.to(self.device)
load_time = time.time() - start_time
print(f"✅ Model loaded in {load_time:.1f}s")
print(f"📊 Parameters: 8B")
print(f"🌍 Languages: 1811 (including all Romansh variants)")
return True
except Exception as e:
print(f"❌ Failed to load model: {str(e)}")
return False
def translate(self, text, src_lang='de', tgt_lang='rm-sursilv', max_tokens=512):
"""
Translate text using Apertus8B with smart chunking for long texts.
Args:
text: Source text to translate
src_lang: Source language code (de, fr, en, it)
tgt_lang: Target language code (rm-sursilv, rm-vallader, etc.)
max_tokens: Maximum output tokens
Returns:
dict with translation results and metadata
"""
if not self.model:
if not self.load_model():
return {"error": "Failed to load model"}
# Validate languages
src_name = self.supported_languages.get(src_lang, src_lang)
tgt_name = self.supported_languages.get(tgt_lang, tgt_lang)
start_time = time.time()
# Use smart chunking if available
if SmartTextChunker:
chunker = SmartTextChunker(max_tokens=400, tokenizer=self.tokenizer)
chunks = chunker.chunk_text(text)
# If text was chunked, show info
if len(chunks) > 1:
print(f"📝 Long text detected: splitting into {len(chunks)} chunks for optimal translation")
else:
# Process as single chunk
chunks = [(text, 'full')]
translations = []
for i, (chunk, chunk_type) in enumerate(chunks, 1):
if not chunk.strip():
continue
# Show progress for long texts
if len(chunks) > 5 and i % 5 == 0:
print(f" Progress: {i}/{len(chunks)} chunks translated...")
# Create translation prompt
prompt = f"""Translate the following text from {src_name} to {tgt_name}.
Provide only the translation, without explanations.
{src_name}: {chunk}
{tgt_name}:"""
try:
# Prepare messages for chat template
messages = [
{"role": "user", "content": prompt}
]
# Apply chat template
text_input = self.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
# Tokenize
model_inputs = self.tokenizer(
[text_input],
return_tensors="pt",
padding=True,
truncation=True
).to(self.model.device)
# Generate translation
with torch.no_grad():
generated_ids = self.model.generate(
**model_inputs,
max_new_tokens=max_tokens,
temperature=0.7,
top_p=0.9,
do_sample=True,
pad_token_id=self.tokenizer.pad_token_id,
eos_token_id=self.tokenizer.eos_token_id
)
# Decode output (skip input prompt)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):]
translation = self.tokenizer.decode(output_ids, skip_special_tokens=True)
# Clean up translation (remove any trailing explanations)
translation = translation.strip()
# If translation contains multiple lines, take first substantial one
lines = [l.strip() for l in translation.split('\n') if l.strip()]
if lines:
translation = lines[0]
translations.append(translation)
except Exception as e:
print(f"⚠️ Error translating chunk {i}: {str(e)}")
translations.append(f"[Translation error: {str(e)}]")
if len(chunks) > 1:
print(f"✅ Translation complete: {len(chunks)} chunks processed")
translation_time = time.time() - start_time
# Join translations, preserving paragraph breaks for paragraph-level chunks
if SmartTextChunker and any(ct == 'paragraph' for _, ct in chunks):
# Preserve paragraph structure
result = []
for translation, (_, chunk_type) in zip(translations, chunks):
result.append(translation)
if chunk_type == 'paragraph':
result.append('\n\n')
final_translation = ''.join(result).strip()
else:
# Standard joining
final_translation = ' '.join(translations)
return {
"translation": final_translation,
"model": "Apertus-8B",
"model_type": "Causal LLM (1811 languages)",
"time": f"{translation_time:.2f}s",
"src_lang": f"{src_lang} ({src_name})",
"tgt_lang": f"{tgt_lang} ({tgt_name})",
"device": self.device
}
def list_languages(self):
"""List supported languages."""
print("\n🌍 Supported Languages (Apertus8B):")
print("=" * 50)
print("\n📌 Common languages:")
for code, name in sorted(self.supported_languages.items()):
if not code.startswith('rm'):
print(f" {code}: {name}")
print("\n🇨🇭 Romansh variants:")
for code, name in sorted(self.romansh_variants.items()):
print(f" {code}: {name}")
print(f"\n💡 Total: 1811 languages supported")
print(f" (Showing most relevant for Swiss context)")
def main():
"""Test the Apertus translator."""
import argparse
parser = argparse.ArgumentParser(description="Apertus8B Translator for Romansh")
parser.add_argument("--src", default="de", help="Source language code")
parser.add_argument("--tgt", default="rm-sursilv", help="Target language (Romansh variant)")
parser.add_argument("--text", help="Text to translate")
parser.add_argument("--list-languages", action="store_true", help="List supported languages")
args = parser.parse_args()
translator = ApertusTranslator()
if args.list_languages:
translator.list_languages()
return
# Test translation
if not args.text:
# Default test
test_text = "Guten Tag! Wie geht es Ihnen?"
print(f"\n🧪 Testing with: '{test_text}'")
result = translator.translate(test_text, args.src, args.tgt)
else:
result = translator.translate(args.text, args.src, args.tgt)
# Display results
if "error" in result:
print(f"❌ {result['error']}")
else:
print(f"\n🔤 Original ({result['src_lang']}): {args.text or 'Guten Tag! Wie geht es Ihnen?'}")
print(f"🌍 Translation ({result['tgt_lang']}): {result['translation']}")
print(f"🤖 Model: {result['model']} ({result['model_type']})")
print(f"⏱️ Time: {result['time']}")
print(f"💾 Device: {result['device']}")
if __name__ == "__main__":
main()