Language Translator Using OpenAI

Juveriya Mahreen 13 Jun, 2023 • 7 min read


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.

Getting Started on Language Translator

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.

Create an OpenAI API Key

Go to the link 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:

OpenAI API Key | Language Translator | OpenAI

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.

Installing the Necessary Libraries

Install Django

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


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

Install OpenAI Library

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

Install Dotenv

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

Create a New Django Project

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 runserver

You will be directed to a web page on the localhost and the following page will be displayed

Language Translator | OpenAI

Create a New App

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 file by adding the app to the INSTALLED_APPS.










Add URLs

Open the file 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 = [

The include function will include all the URL patterns of ‘core\’ to the project. But since we have no file named ‘’ in our app folder, we must go ahead and
create an empty file named ‘’ in the application directory. We will be adding urls to our views later in this article.

Creating Templates

Create a new folder named templates in the application directory and create two template files as shown in the screenshot below:

Creating templates | Language Translator | OpenAI

Add the following code in the “base.html” file

<html lang="en">

    <title>{% block title %}{% endblock %}</title>

    <!-- CSS only -->
    <link rel="stylesheet" href=""
        integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <!-- JS, Popper.js, and jQuery -->
    <script src=""
    <script src="[email protected]/dist/umd/popper.min.js"
    <script src=""

    {% block content %}
    {% endblock %}


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'

Create Views

Open your “” 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

Next, we get our API key in a variable called api_key using the getenv() function as follows:


Let us now create a function-based view as follows:

def chatbot(request):
    if api_key is not None and request.method=='POST':
        user_input = request.POST.get('user_input')
        prompt=f"translate this text to hindi: {user_input}"


    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.

Update Template

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>
{% if response %}
    {{ response|safe }}
{% endif %}
{% endblock %}

Now, It’s time to run the server and check our application. Run the application using the command

python runserver

You will get the page as shown below

Language Translator | OpenAI

Let us give any input and check

Language Translator | OpenAI

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

Language Translator | OpenAI

If we give an input message of more than 350 chars, we will get a response of only up to 350 chars.


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.

Language Translator

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 OpenAI API can be used to perform any task that involves natural language processing.
  • You can either generate context (text, or images) and even translate text from one language to another as we did in this article
  • Designing your prompt is an essential step as you program the model by giving a set of instructions.
  • Finally, you also learned how to use the OpenAI API and access it through the Django web app.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Frequently Asked Questions

Q1. How to use OpenAI for translation?

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.

Q2. What languages are supported by OpenAI translator?

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.

Q3. How good is OpenAI translation?

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.

Q4. Can OpenAI write in other languages?

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.

Juveriya Mahreen 13 Jun 2023

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Related Courses