Model Card for Model ID
Model Details
The model is a fine-tuned NLLB Model, fine-tuned on OPUS paragraph aligned text (25k pairs) with an average of 5.6 sentences/pair. This can be used for document level translation, owing to its large context length as well as the large context fine-tuning that has been done. Future work includes better fine-tuning on more target-based (industry, research, academia, guides) text and document alignment.
How to Get Started with the Model
The model can be loaded directly using the Hugging Face Transformers library. It is based on NLLB-200-Distilled-600M and fine-tuned for paragraph-aligned machine translation.
Fine-tuning data & Training Code
THe exact data, splits, and the code used to fine-tune the model can be found at the GitHub repo: https://github.com/DeerajKarthik/paragraph_aligned_fine-tuning_NLLB You can directly use the fine-tuned model by following the steps below.
Installation
pip install transformers sentencepiece accelerate torch
Inference
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# Model checkpoint
model_name = "drjk16/NLLB-200-Distilled-600M_finetuned_Paragraph-Alligned"
# Set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(
model_name,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
).to(device)
model.eval()
def translate(text, source_lang="eng_Latn", target_lang="hin_Deva",
max_length=512, num_beams=5):
"""
Translate text from the source language to the target language.
"""
tokenizer.src_lang = source_lang
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
max_length=1024
).to(device)
forced_bos_token_id = tokenizer.convert_tokens_to_ids(target_lang)
with torch.inference_mode():
translated_tokens = model.generate(
**inputs,
forced_bos_token_id=forced_bos_token_id,
max_length=max_length,
num_beams=num_beams
)
translation = tokenizer.batch_decode(
translated_tokens,
skip_special_tokens=True
)[0]
return translation
# Example usage
text = """
Artificial intelligence is transforming the way people interact
with technology. Machine translation enables communication
across different languages and cultures.
"""
translated_text = translate(
text,
source_lang="eng_Latn",
target_lang="hin_Deva"
)
print("Original Text:")
print(text)
print("\nTranslated Text:")
print(translated_text)
Batch Inference
For translating multiple paragraphs, the following function supports batch processing while preserving the input paragraph boundaries.
def translate_batch(
paragraphs,
source_lang="eng_Latn",
target_lang="hin_Deva",
batch_size=8,
max_length=512,
num_beams=5
):
tokenizer.src_lang = source_lang
target_lang_id = tokenizer.convert_tokens_to_ids(target_lang)
translations = []
for i in range(0, len(paragraphs), batch_size):
batch = paragraphs[i:i + batch_size]
inputs = tokenizer(
batch,
return_tensors="pt",
padding=True,
truncation=True,
max_length=1024
).to(device)
with torch.inference_mode():
outputs = model.generate(
**inputs,
forced_bos_token_id=target_lang_id,
max_length=max_length,
num_beams=num_beams
)
batch_translations = tokenizer.batch_decode(
outputs,
skip_special_tokens=True
)
translations.extend(batch_translations)
return translations
# Example
paragraphs = [
"Artificial intelligence is transforming healthcare.",
"Machine learning enables computers to learn from data.",
"Natural language processing facilitates multilingual communication."
]
translations = translate_batch(
paragraphs,
source_lang="eng_Latn",
target_lang="hin_Deva"
)
for idx, (source, target) in enumerate(zip(paragraphs, translations)):
print(f"Paragraph {idx + 1}")
print("Source:", source)
print("Translation:", target)
print("-" * 60)
Language Configuration
The model uses the NLLB-200 language code convention. Specify the appropriate source and target language codes during inference.
| Language | Language Code |
|---|---|
| English | eng_Latn |
| Hindi | hin_Deva |
| Gujarati | guj_Gujr |
| Tamil | tam_Taml |
| Bengali | ben_Beng |
| Marathi | mar_Deva |
| Telugu | tel_Telu |
| Punjabi | pan_Guru |
| Kannada | kan_Knda |
| Malayalam | mal_Mlym |
| Urdu | urd_Arab |
| Nepali | npi_Deva |
For the complete list of supported languages, refer to the NLLB-200 documentation.
- Downloads last month
- 41
Model tree for drjk16/NLLB-200-Distilled-600M_finetuned_Paragraph-Alligned
Base model
facebook/nllb-200-distilled-600M