Spaces:
Sleeping
Sleeping
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}") |