English to Hindi translation made simple with Transformers library

Prateek Joshi
2 min readApr 30, 2022

--

source: https://www.indialegallive.com/viewpoint/hindi-as-national-language-caught-in-time-warp-india/

In this tutorial, we will learn how to use the Transformers library and a pretrained deep learning model to automatically translate English sentences into Hindi. You can use Google Colab or any other Python IDE of your choice to execute the following code.

1. Install the Transformers library and Sentencepiece tokenizer

2. Import model and tokenizer

Here we will use the Facebook’s mBART model to perform machine translation. This model can translate not only English to Hindi but also over 50 other languages.

3. Convert English sentences to tensors

Now we will convert our input sentences into PyTorch tensors. As you can see below we have a list of three English sentences that we wish to translate into Hindi.

It is necessary to convert these sentences into tensors before passing them to the model.

4. Translate input sentences to Hindi

Output:

[‘एलॉन मस्क ने टेस्ला स्टॉक में 8.5 बिलियन डालर की बिक्री की’,

‘मैं एक पेशेवर अकादमिक और अनुसंधान लेखक हूं।’,

‘अमेरिका में नौकरी और जर्मनी में काम’]

End Notes

So, this is how you can translate multiple sentences from English to any language of your choice. As mentioned earlier that mBART model supports multiple target languages. Other than that you also make use of GPU to speed up the inference.

Stay tuned for more such content. Thanks for reading!

--

--