AI Content Generator; with Python Flask and OpenAI

AI Content Generator;  with Python Flask and OpenAI

This article is mainly about creating an AI content generator using the OpenAI API established on GPT-3 to come up with content for these use cases:

  • Produce a description of your products
  • Generating tweets from job descriptions
  • Cold emails
  • Social media adverts (eg. Facebook Ads)
  • Business idea pitch
  • Youtube video ideas
  • Youtube video description
  • The process

The GPT-3 algorithm was trained on 45TB of data from the internet and can produce content that is similar to that of a human. In today’s session, we will work with a completion model. It takes our suggestions and uses artificial intelligence to determine what we want. You can think of it as a writing assistant who completes your writing assignments for you.

AI Content Generator

For the first time, developers without the skill set to create and train models will be able to utilise AI functionality in GPT-3. Imagine if you were able to train a model on just 10 TB of data.

Let Us Get Straight To It

AI Content Generator: Python Flask and GPT-3

Due to time constraints, I have made the Python Flask code available for use with our application. You can then use the code as-is or modify it to create your own flask app with all the HTML pages, routes, and files you need. Here is GitHub link:

GitHub – skolo-online/ai-content-starting-template:

You can clone the repo via:

git clone

Then install the required libraries:

pip install flask openai

Then, you need to obtain your API key from OpenAI and add it to the config file.

Video Tutorial: AI Content Generator

You can also watch the full video tutorial on YouTube here:

Let’s begin.

Python Code from Playground

For testing code and functionality, OpenAI has created a playground tool. It’s an excellent tool for putting your instructions to the test and also tweaking model parameters to get the desired outcomes.

Go over to the Playground and generate some email content based on the “cold email” use case.

Go to

Modify the response output to 200, and leave the temperature at 0.7. Also note the model we are using is davinci-instruct-beta-v3 which is much better at this type of use-cases, where you give it an instruction for completion.

Enter the following text into the input:

Generate a cold email about CRM Software.

The output will look like:

AI Content Generator
Cold Email Template with OpenAI

The instruction prompt can be altered until you get the desired results, but in our case it worked pretty well for CRM Software. However, if you’d like to add some description to a brand or keyword that’s not popular, you may want to add a few sentences.

Get the Python API code from Playground

Then now you can click on the “view code” button at the top to get the code for Python.

AI Content Generator
Get the Python API code from Playground
  1. Press this button to view the code.
  2. Press this button to decide on the type of output — you can also select curl and json libraries as alternatives.

Flask Application for the AI Content Generator

In our flask application that we downloaded from GitHub, we then need to export the code we have created above. Create a new file called and paste the following code in this file:

import openai
import config
openai.api_key = config.OPENAI_API_KEY

def openAIQuery(query):
    response = openai.Completion.create(

    if 'choices' in response:
        if len(response['choices']) > 0:
            answer = response['choices'][0]['text']
            answer = 'Opps sorry, you beat the AI this time'
        answer = 'Opps sorry, you beat the AI this time'

    return answer

This code will allow you to call the function:


Provide it with a query word like this statement we tested above:

Generate a cold email about CRM Software.

The API will return a response, which we will parse to get the answer we can display to the user.

Contents of the file.

Inside the Flask Application — rewrite the route for cold email generation to look like so:

from flask import Flask, render_template, request
import config
import aicontent

def page_not_found(e):
  return render_template('404.html'), 404

app = Flask(__name__)
app.register_error_handler(404, page_not_found)
@app.route('/', methods=["GET", "POST"])
def index():
    return render_template('index.html', **locals())

@app.route('/cold-emails', methods=["GET", "POST"])
def coldEmails():

    if request.method == 'POST':
        submission = request.form['coldEmails']
        query = "Write a cold email to potential clients about: {}".format(submission)
        openAIAnswerUnformatted = aicontent.openAIQuery(query)
        openAIAnswer = openAIAnswerUnformatted.replace('\n', '<br>')
        prompt = 'AI Suggestions for {} are:'.format(submission)

    return render_template('cold-emails.html', **locals())

For more information about the HTML template, please refer to the GitHub code and youtube videos.

 Results from the front-end

The following willll happen when you run the application:

  1. From the front-end form, users can enter their search query, which will be captured in the request.form object.
  2. A query for the use case will be added to the hard-coded query already present in the specific route. For instance, “Write a cold email to potential clients about: {}”.format(submission) — is the final query being sent to the API, the submission is what the user entered.
  3. We will call the aicontent function from the Flask View function with: aicontent.openAIQuery(query)
  4. Using the aicontent function, the response is captured and then made available on the front-end for the user to see.

Input Example from the User — AI Cold Email Generation

User input Example — AI Cold Email Generation

User Output Example — AI Cold Email Generation

AI Content Generator

Let me know what you think after you have tried the code. I welcome any feedback. For an article on classifying tweets, check out this blog: Classifying tweets

Leave a Reply

Your email address will not be published.

Subscribe to our mailing list

And get this 💃💃 Free😀 eBook !

Holler Box