-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
351 lines (290 loc) · 12.8 KB
/
Copy pathinference.py
File metadata and controls
351 lines (290 loc) · 12.8 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""
Inference Pipeline for Code-Mixed Machine Translation
Supports: batch translation, ONNX export, and quantization
"""
import os
import time
import logging
from typing import List, Optional, Union, Dict
import torch
import numpy as np
from transformers import (
AutoModelForSeq2SeqLM,
AutoTokenizer,
pipeline,
GenerationConfig,
)
logger = logging.getLogger(__name__)
# ─────────────────────────────────────────────────────────────────────────────
# 1. CORE INFERENCE PIPELINE
# ─────────────────────────────────────────────────────────────────────────────
class CodeMixedTranslator:
"""
Production inference pipeline for code-mixed MT using NLLB-200.
Supports single/batch translation with configurable generation parameters.
"""
def __init__(
self,
model_path: str,
src_lang: str = "hin_Deva",
tgt_lang: str = "eng_Latn",
device: Optional[str] = None,
use_quantization: bool = False,
max_length: int = 256,
):
self.src_lang = src_lang
self.tgt_lang = tgt_lang
self.max_length = max_length
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
logger.info(f"Initializing translator: {src_lang} → {tgt_lang}")
logger.info(f"Device: {self.device}")
# Load tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(
model_path,
src_lang=src_lang,
tgt_lang=tgt_lang,
)
# Load model with optional quantization
if use_quantization and self.device == "cuda":
logger.info("Loading with 8-bit quantization (bitsandbytes)...")
try:
from transformers import BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
self.model = AutoModelForSeq2SeqLM.from_pretrained(
model_path,
quantization_config=quantization_config,
device_map="auto",
)
except ImportError:
logger.warning("bitsandbytes not installed. Loading without quantization.")
self.model = self._load_standard_model(model_path)
else:
self.model = self._load_standard_model(model_path)
self.model.eval()
# Configure generation
self.forced_bos_token_id = self.tokenizer.lang_code_to_id.get(tgt_lang)
if self.forced_bos_token_id is None:
raise ValueError(f"Language code '{tgt_lang}' not found in NLLB tokenizer.")
logger.info(f"Translator ready. Forced BOS token ID: {self.forced_bos_token_id}")
def _load_standard_model(self, model_path: str) -> AutoModelForSeq2SeqLM:
"""Standard model loading without quantization."""
dtype = torch.float16 if self.device == "cuda" else torch.float32
model = AutoModelForSeq2SeqLM.from_pretrained(
model_path,
torch_dtype=dtype,
)
model = model.to(self.device)
return model
def translate(
self,
texts: Union[str, List[str]],
num_beams: int = 4,
length_penalty: float = 1.0,
no_repeat_ngram_size: int = 3,
temperature: float = 1.0,
do_sample: bool = False,
return_scores: bool = False,
) -> Union[str, List[str], Dict]:
"""
Translate code-mixed text(s) to target language.
Args:
texts: Single string or list of strings to translate
num_beams: Beam search width (higher = better quality, slower)
length_penalty: >1 encourages longer, <1 shorter translations
no_repeat_ngram_size: Prevent n-gram repetition
temperature: Sampling temperature (when do_sample=True)
do_sample: Use sampling instead of beam search
return_scores: Return generation scores alongside translations
Returns:
Translated string, list of strings, or dict with scores
"""
single_input = isinstance(texts, str)
if single_input:
texts = [texts]
# Tokenize
self.tokenizer.src_lang = self.src_lang
inputs = self.tokenizer(
texts,
return_tensors="pt",
padding=True,
truncation=True,
max_length=self.max_length,
).to(self.device)
# Generate translations
with torch.no_grad():
outputs = self.model.generate(
**inputs,
forced_bos_token_id=self.forced_bos_token_id,
num_beams=num_beams,
max_length=self.max_length,
length_penalty=length_penalty,
no_repeat_ngram_size=no_repeat_ngram_size,
temperature=temperature,
do_sample=do_sample,
return_dict_in_generate=return_scores,
output_scores=return_scores,
early_stopping=True,
)
if return_scores:
sequences = outputs.sequences
scores = outputs.sequences_scores.cpu().numpy().tolist() if hasattr(outputs, 'sequences_scores') else []
else:
sequences = outputs
# Decode
translations = self.tokenizer.batch_decode(
sequences, skip_special_tokens=True
)
result = translations[0] if single_input else translations
if return_scores:
return {"translations": result, "scores": scores}
return result
def translate_batch_chunked(
self,
texts: List[str],
batch_size: int = 16,
**translation_kwargs,
) -> List[str]:
"""
Translate a large list of texts in batches with progress tracking.
Memory-efficient for large datasets.
"""
all_translations = []
total = len(texts)
for i in range(0, total, batch_size):
batch = texts[i: i + batch_size]
start_time = time.time()
translations = self.translate(batch, **translation_kwargs)
if isinstance(translations, str):
translations = [translations]
all_translations.extend(translations)
elapsed = time.time() - start_time
logger.info(
f"Batch {i // batch_size + 1}/{(total + batch_size - 1) // batch_size} | "
f"{len(batch)} samples | {elapsed:.2f}s | "
f"Speed: {len(batch) / elapsed:.1f} samples/s"
)
return all_translations
def interactive_demo(self):
"""Run an interactive translation session in terminal."""
print("\n" + "=" * 60)
print(f"Code-Mixed MT: {self.src_lang} → {self.tgt_lang}")
print("Type 'quit' to exit | 'lang' to show language codes")
print("=" * 60 + "\n")
while True:
try:
user_input = input("Enter text: ").strip()
if user_input.lower() == 'quit':
print("Goodbye!")
break
elif user_input.lower() == 'lang':
print(f" Source: {self.src_lang}")
print(f" Target: {self.tgt_lang}")
continue
elif not user_input:
continue
start = time.time()
translation = self.translate(user_input)
elapsed = time.time() - start
print(f"Translation: {translation}")
print(f"[{elapsed*1000:.0f}ms]\n")
except KeyboardInterrupt:
print("\nInterrupted. Goodbye!")
break
# ─────────────────────────────────────────────────────────────────────────────
# 2. HUGGINGFACE PIPELINE WRAPPER
# ─────────────────────────────────────────────────────────────────────────────
def build_hf_pipeline(
model_path: str,
src_lang: str = "hin_Deva",
tgt_lang: str = "eng_Latn",
device: int = -1, # -1 = CPU, 0+ = GPU index
max_length: int = 256,
):
"""
Build a standard HuggingFace translation pipeline.
Easier to use for simple translation tasks.
Args:
model_path: Path to fine-tuned model or HuggingFace model name
src_lang: NLLB source language code
tgt_lang: NLLB target language code
device: -1 for CPU, 0 for first GPU, etc.
max_length: Maximum generation length
Returns:
HuggingFace pipeline for translation
"""
tokenizer = AutoTokenizer.from_pretrained(model_path, src_lang=src_lang)
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
forced_bos_token_id = tokenizer.lang_code_to_id[tgt_lang]
translator = pipeline(
task="translation",
model=model,
tokenizer=tokenizer,
src_lang=src_lang,
tgt_lang=tgt_lang,
forced_bos_token_id=forced_bos_token_id,
max_length=max_length,
device=device,
)
return translator
# ─────────────────────────────────────────────────────────────────────────────
# 3. ONNX EXPORT (OPTIONAL - FOR DEPLOYMENT OPTIMIZATION)
# ─────────────────────────────────────────────────────────────────────────────
def export_to_onnx(model_path: str, output_dir: str, opset: int = 13):
"""
Export fine-tuned NLLB model to ONNX format for optimized inference.
Requires: optimum library (pip install optimum[onnxruntime])
Args:
model_path: Path to the fine-tuned HuggingFace model
output_dir: Directory to save ONNX model
opset: ONNX opset version
"""
try:
from optimum.onnxruntime import ORTModelForSeq2SeqLM
from optimum.exporters.onnx import main_export
os.makedirs(output_dir, exist_ok=True)
logger.info(f"Exporting model to ONNX: {output_dir}")
main_export(
model_name_or_path=model_path,
output=output_dir,
task="text2text-generation",
opset=opset,
)
logger.info(f"ONNX export complete. Files saved to: {output_dir}")
return True
except ImportError:
logger.error("ONNX export requires: pip install optimum[onnxruntime]")
return False
# ─────────────────────────────────────────────────────────────────────────────
# 4. TRANSLATION EXAMPLES
# ─────────────────────────────────────────────────────────────────────────────
def run_translation_examples(translator: CodeMixedTranslator, language_pair: str):
"""Run example translations to verify the model."""
examples = {
"hi-en": [
"Mujhe bahut zyada hunger lag raha hai",
"Aaj ka weather bahut acha hai, let's go for a walk",
"Main office se late aaya, traffic bahut zyada tha",
"Yeh movie bahut boring thi, waste of time",
"Kya aap mujhe help kar sakte ho is problem mein",
],
"es-en": [
"Voy a la store para comprar some groceries",
"El weather está muy nice hoy",
"Tengo que hacer el homework antes de salir",
"Mi familia va a tener una barbecue este weekend",
"Necesito upgradar mi phone, el mío está muy old",
],
}
test_examples = examples.get(language_pair, examples["hi-en"])
print("\n" + "=" * 70)
print(f"TRANSLATION EXAMPLES ({language_pair.upper()})")
print("=" * 70)
for i, text in enumerate(test_examples, 1):
start = time.time()
translation = translator.translate(text, num_beams=4)
elapsed = time.time() - start
print(f"\n[Example {i}]")
print(f" Input: {text}")
print(f" Output: {translation}")
print(f" Time: {elapsed*1000:.0f}ms")
print("\n" + "=" * 70)