Ex 4
Ex 4
Algorithm:
Step 3: Construct the model name using the source and target language codes in the format “Helsinki-
NLP/opus-mt-{src_lang}-{tgt_lang}”.
Step 4: Load the MarianMT model and tokenizer using the constructed model name.
Step 5: Tokenize the input text and convert it into a tensor format with padding and truncation enabled.
Step 9: In the __main__ block, take user input for the text, source language, and target language.
Step 10: Call the translate_text() function with the user-provided inputs and print the translated output.
PROGRAM:
Import torch
Try:
Model_name = f”Helsinki-NLP/opus-mt-{src_lang}-{tgt_lang}”
Tokenizer = MarianTokenizer.from_pretrained(model_name)
Model = MarianMTModel.from_pretrained(model_name)
Model.to(device)
CSE/NM/NN&DL / 513322104003/DHIVYA G
Inputs = tokenizer(text, return_tensors=”pt”, padding=True, truncation=True).to(device)
Translated = model.generate(**inputs)
Return translated_text
Except Exception as e:
If __name__ == “__main__”:
If not text:
Else:
OUTPUT:
RESULT:
Thus, the python code for Language Translation Tool is executed successfully.
CSE/NM/NN&DL / 513322104003/DHIVYA G