File size: 7,232 Bytes
e2dd3a8 | 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 | """
Simple inference examples for English to Kannada translation
Quick start guide for using the model
"""
import torch
from tokenizers import Tokenizer
from main import Transformer, greedy_decode
def simple_translate(english_sentence):
"""
Translate a single English sentence to Kannada
Args:
english_sentence: English text to translate
Returns:
Kannada translation
"""
# Setup device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Load model
checkpoint = torch.load('best_model.pt', map_location=device)
vocab_info = checkpoint['vocab_info']
# Initialize model
model = Transformer(
d_model=384,
ffn_hidden=1536,
num_heads=6,
drop_prob=0.1,
num_layers=4,
max_sequence_length=75,
src_vocab_size=vocab_info['source_vocab_size'],
tgt_vocab_size=vocab_info['target_vocab_size']
)
# Load weights
model.load_state_dict(checkpoint['model_state_dict'])
model.to(device)
model.eval()
# Load tokenizers
src_tokenizer = Tokenizer.from_file('source_tokenizer.json')
tgt_tokenizer = Tokenizer.from_file('target_tokenizer.json')
# Translate
translation = greedy_decode(
model=model,
src_sentence=english_sentence,
source_tokenizer=src_tokenizer,
target_tokenizer=tgt_tokenizer,
vocab_info=vocab_info,
device=device,
max_length=75
)
return translation
class TranslationPipeline:
"""
Reusable translation pipeline for multiple translations
Loads model once and reuses it
"""
def __init__(self, model_path='best_model.pt',
src_tokenizer_path='source_tokenizer.json',
tgt_tokenizer_path='target_tokenizer.json'):
"""
Initialize the translation pipeline
Args:
model_path: Path to model checkpoint
src_tokenizer_path: Path to source tokenizer
tgt_tokenizer_path: Path to target tokenizer
"""
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Initializing translation pipeline on {self.device}...")
# Load model
checkpoint = torch.load(model_path, map_location=self.device)
self.vocab_info = checkpoint['vocab_info']
self.model = Transformer(
d_model=384,
ffn_hidden=1536,
num_heads=6,
drop_prob=0.1,
num_layers=4,
max_sequence_length=75,
src_vocab_size=self.vocab_info['source_vocab_size'],
tgt_vocab_size=self.vocab_info['target_vocab_size']
)
self.model.load_state_dict(checkpoint['model_state_dict'])
self.model.to(self.device)
self.model.eval()
# Load tokenizers
self.src_tokenizer = Tokenizer.from_file(src_tokenizer_path)
self.tgt_tokenizer = Tokenizer.from_file(tgt_tokenizer_path)
print("Pipeline ready!")
def translate(self, english_text):
"""
Translate English text to Kannada
Args:
english_text: English sentence or text
Returns:
Kannada translation
"""
return greedy_decode(
model=self.model,
src_sentence=english_text,
source_tokenizer=self.src_tokenizer,
target_tokenizer=self.tgt_tokenizer,
vocab_info=self.vocab_info,
device=self.device,
max_length=75
)
def translate_batch(self, english_texts):
"""
Translate multiple English texts
Args:
english_texts: List of English sentences
Returns:
List of Kannada translations
"""
return [self.translate(text) for text in english_texts]
# Example 1: Simple one-time translation
def example_1_simple():
"""Example: Translate a single sentence"""
print("Example 1: Simple Translation")
print("-" * 50)
sentence = "Good morning, have a nice day!"
translation = simple_translate(sentence)
print(f"English: {sentence}")
print(f"Kannada: {translation}")
print()
# Example 2: Using the pipeline for multiple translations
def example_2_pipeline():
"""Example: Translate multiple sentences efficiently"""
print("Example 2: Pipeline Translation")
print("-" * 50)
# Initialize pipeline once
pipeline = TranslationPipeline()
# Translate multiple sentences
sentences = [
"Hello world!",
"How are you today?",
"What is your name?",
"I am learning Kannada.",
"Thank you for your help."
]
print("Translating sentences:\n")
for eng in sentences:
kan = pipeline.translate(eng)
print(f" EN: {eng}")
print(f" KN: {kan}")
print()
# Example 3: Batch translation
def example_3_batch():
"""Example: Batch translation"""
print("Example 3: Batch Translation")
print("-" * 50)
pipeline = TranslationPipeline()
sentences = [
"The weather is beautiful.",
"Where is the nearest hospital?",
"Can you help me please?",
"I love this city.",
"See you tomorrow!"
]
translations = pipeline.translate_batch(sentences)
print("Batch translation results:\n")
for eng, kan in zip(sentences, translations):
print(f"EN: {eng}")
print(f"KN: {kan}")
print()
# Example 4: Custom usage with error handling
def example_4_with_error_handling():
"""Example: Translation with error handling"""
print("Example 4: Translation with Error Handling")
print("-" * 50)
try:
pipeline = TranslationPipeline()
test_cases = [
"Hello!",
"", # Empty string
"This is a very long sentence that might exceed the maximum token length and we need to see how the model handles it when processing.",
"Short.",
]
for text in test_cases:
try:
if not text.strip():
print("Skipping empty input")
continue
translation = pipeline.translate(text)
print(f"✓ EN: {text}")
print(f" KN: {translation}")
print()
except Exception as e:
print(f"✗ Error translating '{text}': {e}")
print()
except Exception as e:
print(f"Failed to initialize pipeline: {e}")
if __name__ == "__main__":
print("="*70)
print("English to Kannada Translation - Inference Examples")
print("="*70)
print()
# Run examples
example_1_simple()
print("\n" + "="*70 + "\n")
example_2_pipeline()
print("\n" + "="*70 + "\n")
example_3_batch()
print("\n" + "="*70 + "\n")
example_4_with_error_handling()
print("\n" + "="*70)
print("All examples completed!")
|