Creating a Language Translator Application Using Gradio

All species have their way of communicating. Speaking is an essential skill for human communication. Speech and language are two of the most important aspects we pay attention to since childhood. Communicating using language allows us to share our ideas, thoughts and feelings with others. It has the power to build communities, to get the message out to others, and to motivate them or make them cry. People communicate effectively by learning a language, which tells us how they have mastered a complex system of words, structure, and grammar to convey the message to others. It is unique to our species as it offers us the passage to express and exchange unique ideas and customs within different cultures and societies. By learning a foreign language, one can understand ideas and thoughts distinct from one’s own culture. By learning the language, it is easy to learn the customs of interacting with different societies. Whether talking to your friends, partner, or family, having a common language to communicate with is necessary for all types of interactions.
Language translation is another important aspect when it comes to effective communication. However, the importance of translation in our daily life is more multidimensional than we realize. As the world advances in travel, distance and geography are becoming less of a barrier to doing business. As a result, companies started to link up with other companies overseas and saw the benefits of working abroad. They take advantage of the lower cost of products and services in some countries, cheap labor and professional industry expertise in others, and additional markets to market and expand their business.
Although English is a widely spoken language, today’s world is simply not forbidden to speak English.
Different countries have started taking pride in speaking their language and promoting their culture all over the world through such gestures. Therefore, in order to communicate and do business with such cultures and get the business done in these countries, one needs to know exactly what they are trying to convey to facilitate communication and avoid any confusion that prevails. Language translators can help break down these barriers without even having to learn the language completely. A translator app can help translate dozens of languages. Some apps offer features such as allowing translation either by text or by voice; just type, write or speak in the app. Some even allow you to point your smartphone or device at a sign or menu written in a foreign language to view a live translation. These apps allow you to have a two-way conversation with someone, even if you don’t speak the same language.
What is Gradio?
Gradio is an open-source library for the Python language that lets you quickly create customizable, easy-to-use UI components for any ML model, API, or arbitrary function with just a few lines of code. The Gradio GUI can be embedded directly into your Python notebook, and the link for the created GUI can be shared with anyone. Components can easily be wrapped around TensorFlow or PyTorch models, creating a good mix and match of components to support a wide range of input and output combinations. Jupyter, PyTorch, scikit-learn, and Matplotlib are some of the most popular tools built into Gradio. Gradio can be installed directly via pip. Creating a Gradio interface only requires adding a few lines of code to your project. You can choose from a variety of interface types to interface your function. Gradio works well and can handle a wide range of media, text, images, videos and sounds. Apart from being used in ML models, it can be used very well as an embedding of python code. During application development, you can interactively debug your models and get feedback on the performance of users’ models. As a result, you can improve your model faster.
Get started with the language translation app
In this article, we will try to implement a language translation application using Gradio and its transformer model, translate text from a different language using a translation pipeline, and create a lightweight application that translates text into a specified language in real time. The following implementation is partly inspired by a video tutorial on Gradio whose link is here.
Installing dependencies
The first step in building our sample language translation application will be to install all the necessary dependencies. Here we are installing the long-term stable version of pytorch for our model, which will help us make processing easier. To try to find the combination that best suits your system, you can follow the link here to get the appropriate command.
#installing the dependencies
!pip install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio===0.8.1 -f https://download.pytorch.org/whl/lts/1.8/torch_lts.html
Installation of the Gradio transformer
Next, we will install the Gradio transformer and further configure our model. This will provide us with a user interface to interact with our model. To do this, you can run the following command,
#installing the gradio transformer
!pip install transformers ipywidgets gradio --upgrade
Importing Libraries
Importing our required library and transformer model for easy processing,
#importing the libraries
import gradio as gr # UI library
from transformers import pipeline # Transformers pipeline
Creation of the model
Now that everything is ready, we will start implementing the model. For this, we will first import a pre-trained translation pipeline model from HuggingFace, which we will use. To use it, we first need to create a pipeline class and then pass it along with the functionality we want to use. We will use the translation function and define our initial language and what it will be translated into. Here I will translate from English to French.
#Installing the Pre Trained HuggingFace Pipeline and setting up for En To Fr
translation_pipeline = pipeline('translation_en_to_fr')
To go out :
Pass a line to be translated and test if it works correctly,
#testing the first result
results = translation_pipeline('I love to travel the world')
Display of translated text result
#displaying the result
results[0]['translation_text']
To go out :
J'aime la crème glacée
Creating a Gradio function and interface
We will then create a user interface using Gradio; for this we will now configure our translation transformer in Gradio.
#creating a fuction called translate
def translate_transformers(from_text):
results = translation_pipeline(from_text)
return results[0]['translation_text']
Checking results by passing a single line of text to see if it works in Gradio,
#translation through Gradio
translate_transformers('My name is Victor')
To go out :
Mon nom est Victor
We can see that the model successfully translates our text during initial testing.
We will now configure the UI space; for this we will create two text boxes to get our input and our output, specifically using the Gradio Interface class. We have also placed texts that tell us which text box does what. Gradio makes it very easy to set up such an interface and to add components to it in just a few lines of code!
#Creating the User Interface Space
interface = gr.Interface(fn=translate_transformers,
inputs=gr.inputs.Textbox(lines=2, placeholder="Text to translate"),
outputs="text")
With the interface created and configured, let’s run it,
#launching the interface
interface.launch()
When launched through jupyter notebook, it can take you to a separate browser window where it will run the created UI. However, for colab notebook users, it will automatically detect and run the interface in the notebook itself.
To go out :

As we can see, our interactive interface application has now been created using Gradio components. The results show us that we successfully converted an English-language text into French using the translation pipeline.
Endnotes
This article has attempted to understand what it takes to build a lightweight language translation application using Gradio and has attempted to explore the Gradio components. I would recommend using Gradio and creating highly user-friendly and interactive application interfaces with just a few lines of code. The following implementation can be found as a Colab notebook using the link here.
Good learning!