Language Translation API using AWS Translate!
This blog explains the step-by-step approach to creating a container image that can be used as an API to translate any language into another language (supported by AWS Translate) using Flask in Python!
At present, the boom of AI is unstoppable, anyone who is looking to grab a decent job, should once consider AI for sure. However, the fact “any single technology won’t help” still holds.
If you want to create something unique, or even a very basic product, the must requirement is “integration of technologies”. Therefore, having the same vision in my mind, I am writing this blog that will help you to do so.
Objective/Goal of the Blog
To create a container image that will act as an API to translate the given text from one language to another using Flask in Python.
To achieve the goal of this blog, there are certain prerequisites that need attention & are listed below.
Pre-requisites
- Experience in any container engine or container management tool like Docker, Kubernetes, Podman, Openshift, etc.
- Experience in Python.
- Experience in Flask.
- Experience in AWS.
Note: Since the goal of this blog is to explain the integration process, that is why it is assumed that you are already having some basic knowledge with the aforementioned tools.
Implementation
The implementation approach is explained below with each step’s code. After the implementation, the complete compiled code for the same is also showcased for a one-stop reference of this interesting use case.
Steps involved in the implementation:
- Importing the required libraries.
# The required libraries are as follows:
1. flask: It is used for the API Development part.
2. boto3: It is used to used the AWS Translate service.
3. os: It is used to fetch the envrionment variables from
the container like access & secret keys.
# Code to import the required libraries
from flask import Flask, request
import boto3
import os
2. Registering the app using flask
# Defining the App with the Name of the file
app = Flask(__name__)
3. Creating a 500 Internal server error handler to handle the 500 internal server errors.
# Creating a error handler for the Internal Server Error
@app.errorhandler(500)
def error_505(error):
return "There is some problem with the application!"
4. Create a route at “/” for the API to perform the translation.
@app.route("/", methods = ["POST"])
def translate():
try:
# Taking the required parameters as input from the API
text = request.args.get("text")
SourceLanguage = request.args.get("source_language")
TargetLanguage = request.args.get("target_language")
# Creating a translate client of AWS to translate the text
language_translator = boto3.client(service_name = "translate", region_name = "ap-south-1",
aws_access_key_id = os.getenv("aws_access_key"),
aws_secret_access_key = os.getenv("aws_secret_key"))
# Translating the text
result = language_translator.translate_text(Text = text,
TargetLanguageCode = TargetLanguage,
SourceLanguageCode = SourceLanguage)
# Returning the output
return result.get('TranslatedText')
except Exception as e:
print(str(e))
In the above code, a client of “AWS Translate” service is been created, & then the client is been used to translate the text from the source language to the target language. Also, the complete code is been written by using exception handling, so that if any exception occurs, then we can handle it.
To see the list of the supported languages and their respective language codes by AWS, please check out the link given below:
Note: To run this API, you need to pass AWS access & secret key as the environment variables to the container.
5. App is set to run on port 80 and on host “0.0.0.0” so that the container can be used to expose the API functionality.
# Running the app on port 80 & on any host to make it accessible through the container
app.run(host = "0.0.0.0", port = 80)
This concludes the step-by-step explanation of the code.
Compiled Code
This concludes the coding part!
Using Docker to run a container of this API directly!
To run this API directly without any code, you can use my image hosted publically on the docker hub. The command to run the container is mentioned below:
docker run -dit -p 80:80 -e aws_access_key=<aws_access_key> -e aws_secret_key=<aws_secret_key> harshitdawar/aws-language-translate-api
If you are running this command on your local computer, then you can access this API on your localhost. If you are running this on some cloud, then please use the public IP of that instance by configuring all the network requirements. A demo call on the localhost using Postman is showcased below.
This concludes this blog. I hope you have enjoyed it a lot. If you are interested in my other docker hub images, then please check the below link.
I hope my article explains each and everything related to the topic with all the detailed concepts and explanations. Thank you so much for investing your time in reading my blog & boosting your knowledge. If you like my work, then I request you to applaud this blog & follow me on Medium, GitHub, & LinkedIn for more amazing content on multiple technologies and their integration!
Also, subscribe to me on Medium to get updates on all my blogs!