In this article, you will learn how to build a simple language translator app using OpenAI API and Django Web Application Framework. Django is one of the popular open-source web application frameworks of Python. Along the way, you will learn the key concepts and techniques to use the OpenAI API for any task. OpenAI provides us with various models that can be used to solve any task that involves language processing such as analyzing and understanding the language as well as generating content(such as code, images, etc).
As you all know, in today’s world language translation plays a crucial role in bridging communication gaps between people who speak different languages. Moreover, there is a high demand for language translation apps and services on the internet today. To get along with this tutorial, you must be familiar with Python language and a little bit of Django web application framework.
This article was published as a part of the Data Science Blogathon.
Before getting into the development process, initially, we need to set up the development environment along with the necessary packages. To use the OpenAI API, you need to register an account and generate an API key. Let us start by generating an API Key.
Go to the link https://platform.openai.com/ and create a new account. Login to your account and click on your account in the top right corner and then select “View API Keys” from the drop-down menu as shown below:
And you can now create a new secret key by clicking on the button as shown in the below screenshot.
After clicking on the button, you will get another window as shown below:
Give it a name and click on Create a secret key. Save this as you will be using it in the future.
Let us start by creating a virtual environment as it is the best practice to always build your projects in a virtual environment. Create a virtual environment using the following command for Windows users
virtualenv env1
Activate the virtual environment using the following command
env1\Scripts\activate.bat
Now that we are in our environment, we need to install Django as it is an external package. Install Django using the pip command as follows:
pip install django
The Openai Python library provides access to the OpenAI API for all the applications written in Python language. Use the following command to install the library
pip install --upgrade openai
We are installing dotenv to read an API key from the .env file. Python dotenv reads key-value pairs from the .env file and set them to environment variables. Install it using the pip command as follows:
pip install python-dotenv
Let us start creating a new Django project and give it a name. I am naming it as “mysite”. Use the following command to create a new project
django-admin startproject mysite
At this point, you can crosscheck if everything is working or not. To do so, navigate to the project directory:
cd mysite
And run the server using the following command:
python manage.py runserver
You will be directed to a web page on the localhost http://127.0.0.1:8000/ and the following page will be displayed
Create a new Django app in the project folder using the below command
django-admin startapp core
You can give any name to your app and I am naming it “core”. Every time, you create a new app you must register it in the settings.py file by adding the app to the INSTALLED_APPS.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
]
Open the file urls.py that is in the projects directory and add the URL path to the application URLs. Add the below code:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('core.urls')),
]
The include function will include all the URL patterns of ‘core\urls.py’ to the project. But since we have no file named ‘urls.py’ in our app folder, we must go ahead and
create an empty file named ‘urls.py’ in the application directory. We will be adding urls to our views later in this article.
Create a new folder named templates in the application directory and create two template files as shown in the screenshot below:
Add the following code in the “base.html” file
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
crossorigin="anonymous"></script>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
Next, open the file “main.html” and add the below code as of now.
{% extends 'core/base.html' %}
{% block content %}
{% endblock %}
We will be adding a form after creating a view.
Create .env File and Set Your API Key
create a .env file in the project directory and add the following lines :
OPENAI_KEY='insert your key here'
Open your “views.py” file. Here, I am using simple function-based views. So, let us add all the imports initially
from django.shortcuts import render
import openai,os
from dotenv import load_dotenv
load_dotenv()
Next, we get our API key in a variable called api_key using the getenv() function as follows:
api_key=os.getenv("OPENAI_KEY",None)
Let us now create a function-based view as follows:
def chatbot(request):
chatbot_response=None
if api_key is not None and request.method=='POST':
openai.api_key=api_key
user_input = request.POST.get('user_input')
prompt=f"translate this text to hindi: {user_input}"
response=openai.Completion.create(
engine='text-davinci-003',
prompt=prompt,
max_tokens=350,
temperature=0.5
)
print(response)
chatbot_response=response["choices"][0]["text"]
return render(request,'core/main.html',{"response":chatbot_response})
In the beginning, we are setting the variable chatbot_response to none. Next, we are checking if the request method is POST and also if the API key is not None, then we try to generate a response from Openai. We gather the input of the user in a variable named “user_input” by using the get method and passing the name of the input field which we will be adding in the main.html file. We are setting the prompt to translate the input message to Hindi. You can specify any language over here.
We then create a response object and declare the engine as “text-DaVinci-003”. You can go through the documentation to know more about this. Setting the prompt to prompt and max tokens is given as 350 which indicates that the response will be of max 350 characters.
Now that we have done with our view, let us create a form in the “main.html” file to take the input message from the user. We then generate the response in the view by passing the input message and finally print the response in our template. Add the below code to the “main.html” file:
{% extends 'core/base.html' %}
{% block content %}
<h3> Write an instruction </h3>
<form method="post">
{% csrf_token %}
<input type="text" class="form-control" name="user_input">
<button type="submit" class="btn btn-primary mt-3">Submit</button>
</form>
<br/>
{% if response %}
<div>
{{ response|safe }}
</div>
{% endif %}
{% endblock %}
Now, It’s time to run the server and check our application. Run the application using the command
python manage.py runserver
You will get the page as shown below
Let us give any input and check
Let us try to change the language in the prompt message
prompt=f"translate this text to telugu: {user_input}"
Save and reload the page
If we give an input message of more than 350 chars, we will get a response of only up to 350 chars.
Example:
Input: Data science is the study of data to extract meaningful insights for business. It is a multidisciplinary approach that combines principles and practices from the fields of mathematics, statistics, artificial intelligence, and computer engineering to analyze large amounts of data.
As you can see, the response will be less than 350 tokens.
This article demonstrates on using the OpenAI API with Python for language translator and a basic guide to creating interesting web apps using AI technology. We are designing a simple web app using the Django web application framework that takes input in the form of a sentence and translates it into a different language.
Key takeaways:
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
A. To use OpenAI for translation, you can utilize the OpenAI API by sending text in one language as input and specifying the target language for translation. The API will return the translated text, allowing you to integrate it into your application or workflow.
A. OpenAI Translator currently supports a wide range of languages including but not limited to English, Spanish, French, German, Italian, Portuguese, Dutch, Russian, Chinese, Japanese, Korean, and many more.
A. OpenAI translation has shown impressive results, delivering accurate translations in various language pairs. However, the quality can vary depending on factors like the complexity of the text and the availability of training data. OpenAI continues to refine and improve its translation models.
A. Yes, OpenAI can generate text in multiple languages. It has models trained in various languages, allowing it to produce coherent and contextually relevant text in languages it supports. The ability to generate text in different languages can be beneficial for multilingual applications and content creation.