File size: 777 Bytes
f2a7654
faa1118
f2a7654
 
 
 
faa1118
f2a7654
faa1118
f2a7654
 
 
 
faa1118
 
f2a7654
 
 
 
d7c2055
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from transformers import MarianMTModel, MarianTokenizer

# Step 1: Load the model and tokenizer
model_name = "Helsinki-NLP/opus-mt-en-ml"
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)

# Step 2: Define a translation function
def translate(text):
    inputs = tokenizer(text, return_tensors="pt", padding=True)
    # Perform translation
    outputs = model.generate(**inputs, max_length=128, num_beams=4, early_stopping=True)
    translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return translated_text

# Step 3: Example usage
english_text = "Hello, how are you?"
malayalam_translation = translate(english_text)
print(f"English: {english_text}")
print(f"Malayalam: {malayalam_translation}")