0% found this document useful (0 votes)
9 views2 pages

Ex 4

The document outlines the creation of a Language Translation Tool using Python and the transformers library. It provides a step-by-step algorithm for implementing the translation function, including model loading, text tokenization, and output generation. The program successfully translates user-input text from a source language to a target language, demonstrating its functionality with an example.

Uploaded by

amirthamm2083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Ex 4

The document outlines the creation of a Language Translation Tool using Python and the transformers library. It provides a step-by-step algorithm for implementing the translation function, including model loading, text tokenization, and output generation. The program successfully translates user-input text from a source language to a target language, demonstrating its functionality with an example.

Uploaded by

amirthamm2083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EX.

NO: LANGUAGE TRANSLATION TOOL


DATE:
AIM:

To write the python code for Language Translation Tool.

Algorithm:

Step 1: Import MarianMTModel and MarianTokenizer from the transformers library.

Step 2: Define a function translate_text(text, src_lang, tgt_lang) to handle text translation.

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 6: Generate the translated text using the pre-trained model.

Step 7: Decode the generated tokens into human-readable translated text.

Step 8: Return the translated text from the function.

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:

From transformers import MarianMTModel, MarianTokenizer

Import torch

Def translate_text(text, src_lang=”en”, tgt_lang=”fr”):

Try:

Model_name = f”Helsinki-NLP/opus-mt-{src_lang}-{tgt_lang}”

Tokenizer = MarianTokenizer.from_pretrained(model_name)

Model = MarianMTModel.from_pretrained(model_name)

Device = “cuda” if torch.cuda.is_available() else “cpu”

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)

Translated_text = tokenizer.batch_decode(translated, skip_special_tokens=True)[0]

Return translated_text

Except Exception as e:

Return f”Error: {str€}”

If __name__ == “__main__”:

Text = input(“Enter text to translate: “).strip()

Src_lang = input(“Enter source language code (e.g., en for English): “).strip()

Tgt_lang = input(“Enter target language code (e.g., fr for French): “).strip()

If not text:

Print(“Error: Text input cannot be empty.”)

Elif not src_lang or not tgt_lang:

Print(“Error: Language codes cannot be empty.”)

Else:

Translation = translate_text(text, src_lang, tgt_lang)

Print(f”Translated text: {translation}”)

OUTPUT:

Enter text to translate: Hello good morning

Enter source language code (e.g., en for English): en

Enter target language code (e.g., fr for French): fr

Translated text: Bonjour.

RESULT:

Thus, the python code for Language Translation Tool is executed successfully.

CSE/NM/NN&DL / 513322104003/DHIVYA G

You might also like