Free Automatic Blog Content Writing Tool

Introduction

Free Automatic Blog Content Writing Tool
Free Automatic Blog Content Writing Tool

We will go over a Free Automatic Blog Content Writing Tool.

Did you know that with the use of AI you can write effortlessly good content for your blog?

Content writing today does not need to be a taunting task. Today we will go over on how you can programmatically using Python and OpenAI write content for your blog.

We will break down this in the following sections:

  • Why it is useful to use Python to do automatic content writing
  • How To Get new blog ideas for your blog using AI
  • How To Get an outline draft for a blog post using AI
  • How To Write Content Sections for your blog using AI

I have used this successfully in various projects and it works very well and has saved me a ton of time getting the outline draft for content creation. I also use it a lot to get inspired and find new ideas to write about.

We will go point by point on getting you up and running in less than 5mins, you do not need to have any programming knowledge to use the tool we are going to be developing here.

This is a complete guide and should cover all your questions on using Python to write a complete blog post automatically for you.

All code and examples on how to do this can be found in the Github link here.

Why Is Automatic Content Writing Useful

There’s a lot of advantages of using automatic content writing with the help of an AI library. Below I’m going to list a few important factors:

  • The main reason is that it can come up with ideas you wouldn’t otherwise think about.
  • It can save you tremendous amount of time researching online on specific topics and can actually break down content by pulling this from online sources allowing you to quickly edit and add references to it.
  • It creates an easy outline for you so you can get your headings taken care off fairly quickly.
  • It can help you do keyword research and identify what’s good for ranking which is otherwise a tedious process.
  • It can help you with the SEO content of your blog post along with how this is placed inside your content.
  • It can pull synonyms and related keywords to your target keyword and help you spread it out within your content to make it more attractive to search engines.
  • It can do the work for you simply put if you think a topic is well researched you may not have to write a single word, just clean up the content
  • It can rewrite your content and make it simpler
  • It can correct your grammar
  • It can check your content for plagiarism and do any necessary changes. Sometimes I feel even when I write unique content it gets marked by some places as plagiarism which is very unfair but this is how the world works some people will end up writing similar things to you eventually using an AI can help you fix that issue.

How Does AI Help Automatic Content Writing

Artificial intelligence (AI) can be used to help with a variety of content writing tasks. For example, AI can be used to help generate ideas for new content, to help research and gather information for articles, and to help edit and proofread articles. AI can also be used to help generate targeted content for specific audiences, and to help distribute content across multiple channels.

If you just read the above paragraph and thought it was good then this proves my point because it was written by the code I included in this article. It’s the only piece I generated for this post to demonstrate a point.

How To Setup Python OpenAI

Lets go over quickly how to setup OpenAI in your system. I presume you have Python and PIP installed if you haven’t please check online on how to get these things installed in your system. Furthermore you will be needing virtualenv which is simply a pip package.

We will be installing all the dependencies with the commands shown below.

$ virtualenv venv
created virtual environment CPython3.9.12.final.0-64 in 191ms
  creator CPython3Posix(dest=/Users/alex/code/unbiased/python-ai-content-writing/venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/alex/Library/Application Support/virtualenv)
    added seed packages: pip==22.0.4, setuptools==62.1.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

$ source venv/bin/activate

$ pip install -r requirements.txt
Collecting openai
  Downloading openai-0.18.1.tar.gz (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.4/42.4 KB 815.0 kB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting requests>=2.20
  Using cached requests-2.27.1-py2.py3-none-any.whl (63 kB)
Collecting pandas>=1.2.3
  Downloading pandas-1.4.2-cp39-cp39-macosx_11_0_arm64.whl (10.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.1/10.1 MB 10.8 MB/s eta 0:00:00
Collecting pandas-stubs>=1.1.0.11
  Downloading pandas_stubs-1.2.0.58-py3-none-any.whl (162 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 162.9/162.9 KB 4.6 MB/s eta 0:00:00
Collecting openpyxl>=3.0.7
  Downloading openpyxl-3.0.9-py2.py3-none-any.whl (242 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 242.2/242.2 KB 5.4 MB/s eta 0:00:00
Collecting tqdm
  Using cached tqdm-4.64.0-py2.py3-none-any.whl (78 kB)
Collecting et-xmlfile
  Downloading et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)
Collecting numpy>=1.20.0
  Using cached numpy-1.22.3-cp39-cp39-macosx_11_0_arm64.whl (12.8 MB)
Collecting python-dateutil>=2.8.1
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting pytz>=2020.1
  Downloading pytz-2022.1-py2.py3-none-any.whl (503 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 503.5/503.5 KB 7.9 MB/s eta 0:00:00
Collecting urllib3<1.27,>=1.21.1
  Using cached urllib3-1.26.9-py2.py3-none-any.whl (138 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
Collecting idna<4,>=2.5
  Using cached idna-3.3-py3-none-any.whl (61 kB)
Collecting charset-normalizer~=2.0.0
  Using cached charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
Collecting six>=1.5
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Building wheels for collected packages: openai
  Building wheel for openai (pyproject.toml) ... done
  Created wheel for openai: filename=openai-0.18.1-py3-none-any.whl size=53146 sha256=7e9c8bbee07bdfe70abdd20197603392b463c6cb5d36fbd02462383157762439
  Stored in directory: /Users/alex/Library/Caches/pip/wheels/85/29/67/87d3709a07eda551bf9157849407d259869f7d8da694f0356d
Successfully built openai
Installing collected packages: pytz, pandas-stubs, certifi, urllib3, tqdm, six, numpy, idna, et-xmlfile, charset-normalizer, requests, python-dateutil, openpyxl, pandas, openai
Successfully installed certifi-2021.10.8 charset-normalizer-2.0.12 et-xmlfile-1.1.0 idna-3.3 numpy-1.22.3 openai-0.18.1 openpyxl-3.0.9 pandas-1.4.2 pandas-stubs-1.2.0.58 python-dateutil-2.8.2 pytz-2022.1 requests-2.27.1 six-1.16.0 tqdm-4.64.0 urllib3-1.26.9

$ ipython
In [1]: import openai

We are doing three things above:

  • First we are creating a virtual environment to store our data and then activate it
  • We install all the Python packages using the requirements.txt file that I provided in the Github repo
  • Test and see if the OpenAI library is installed correctly

How To Get New Blog Ideas Using Python

To simplify the code and make it a bit more re-useable I implemented a shared library that’s used across all modules. The code as shown below contains most of the completions.

OpenAI Abstraction Library Layer

import os
import openai
from dotenv import load_dotenv

OPEN_AI_CMDS = {
    'create_outline' : 'Create an outline for an essay about',
    'create_section' : 'Expand the blog section in to a detailed',
    'blog_ideas'     : 'Give me blog topic ideas on'
}

# davinci is the most accurate engine so default to this for now
OPEN_AI_ENGINE = 'text-davinci-002'

load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')

def openai_blog_ideas(keyword, max_count=256):
    full_cmd = '%s %s:'%(OPEN_AI_CMDS['blog_ideas'], keyword)

    res = openai.Completion.create(
        engine=OPEN_AI_ENGINE,
        prompt=full_cmd,
        temperature=0.7,
        max_tokens=max_count,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )

    return openai_get_completion_result(res)


def openai_create_outline(topic, max_count=150):
    full_cmd = '%s %s:'%(OPEN_AI_CMDS['create_outline'], topic)

    res = openai.Completion.create(
        engine=OPEN_AI_ENGINE,
        prompt=full_cmd,
        temperature=0,
        max_tokens=max_count,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )

    return openai_get_completion_result(res)

def openai_create_section(section, max_count=256):
    full_cmd = '%s %s:'%(OPEN_AI_CMDS['create_section'], section)

    res = openai.Completion.create(
        engine=OPEN_AI_ENGINE,
        prompt=full_cmd,
        temperature=0.7,
        max_tokens=max_count,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )

    return openai_get_completion_result(res)

def openai_get_completion_result(openai_res):
    return openai_res['choices'][0]['text']

Lets go ahead and explain the functions and what each of them does.

  • OPEN_AI_CMDS: This is a list of commands that we have defined on how to instruct what the OpenAI API should do. We will be sending these later with helper functions.
  • OPEN_AI_ENGINE: This is the engine we will be using for the AI code. Right now we will be using a GPT-3 engine which is called DaVinci. This is the most accurate from the ones I tried on the OpenAI but feel free to adjust and test with others ones too.
  • Next we simply load the environment variables into Python in a secure manner using the Python dotenv module. One thing that we load is basically the OpenAI API key that was given to us (make sure you replace that with yours).
  • openai_blog_ideas: This function simply takes as input a maximum number of characters to generate and a sample keyword. So in the example below we will be giving it with some sample keywords. It gives back to us a blob of text with sample keyword ideas to write about.
  • openai_create_outline: This function lets you create heading titles for your content. This is going to be returned as a numbered list text buffer
  • openai_create_section: This generates the content for each section we will be writing in our headings. It also takes an optional parameter which limits the amount of characters we will be generating in our text.
  • openai_get_completion_result: This is a general abstraction for parsing the result of a completion. Currently it’s assumed there’s only one choice sent in the request. If you were to send more than one then you will need to slightly adjust the code to return all of them rather than the first one only.
  • temprature: This adjusts how strict the algorithm will be on returning results. The closer you take it to 0 the more strict it would be and accurate however when I lowered the value too much I noticed a lot of repetition which wasn’t very productive. Feel free to adjust the level according to your needs.
  • penalties: The penalties is basically to survive a bit on the word picking and your token utilization. If you have no problem with the billing initially I recommend you do not penalize the Davinci Engine and let it do it’s thing. So a value of 0 is good here.
  • max_tokens: This is basically how many words you allow the engine to generate. Typically for most cases you can keep this close to 256 characters but if you feel some sections may be longer go for it.

Get Blog Content Ideas

Now that we have the basic concepts down of our library we can proceed more into the fun stuff and this is to start getting some ideas for our blog. The first code we will be exploring is a simple logic function that basically receives the following:

  • Input: Takes as input a keyword you want to get blog post ideas for you
  • Output: Generates a list of blog ideas you can write content for

In the example below we will supply it with some given keyword and get the result for it.

from openai_helper import openai_blog_ideas

keywords = 'Nichola Teslas Inventions'
print ('Generating ideas for: ', keywords)
print(openai_blog_ideas(keywords))

The code above leverages the library we have previously written as a helper to the OpenAI framework. It basically abstracts the logic of requesting the Davinci code to give us some blog content ideas. The function we will be using here is openai_blog_ideas that we referenced and implemented earlier.

The code above takes as input the following keyword

Nichola Teslas Inventions

If we were to run it in our code we will get the following output:

$ python ./python-ai-generate-blog-topics.py

Generating ideas for:  Nichola Teslas Inventions

1. How Tesla's inventions changed the world
2. The impact of Tesla's inventions on society
3. The legacy of Tesla's inventions
4. The science behind Tesla's inventions
5. The engineering of Tesla's inventions
6. The history of Tesla's inventions

The DaVinci engine successfully pulled 6 topics we can write about. You can select any or all of them and have it start writing and drafting the outlines for them. As you can see the output is very accurate and relevant to what we were looking for, it appears as if a human being did the research and found them.

Now that we have some blog content creation ideas generated by our AI let’s proceed into getting some blog headings and titles for the outline of our blog post.

How To Get Automatically Create A Blog Headings/Outline Using Python

The headings and outline of our blog post is the next critical step in the process. The code we will be implementing below basically makes use of the openai_create_outline function. Again that function was implemented earlier in our helper code. It works as follows:

  • Input: Takes as input a blog topic you want to write about
  • Output: Generates a list of headings and subheadings for your blog outline
from openai_helper import openai_create_outline

headings = 'The history of Tesla\'s inventions'
print ('Generating headings for: ', headings)
print(openai_create_outline(headings))

Lets go ahead and put this function to a test. In the example below which we will be writing we will be looking for the following blog post idea:

The history of Tesla’s inventions

This may be an easy topic to write about but if you are not a scholar that knows Tesla’s history very well you would surely have to do a lot of googling before you start writing about it. With the help of AI we will see below how easy that makes the whole process and how much of a time saver it is.

$ python ./python-ai-generate-blog-headings.py
Generating headings for:  The history of Tesla's inventions

1. Nikola Tesla was born in 1856 in what is now Croatia.
2. He studied engineering and physics, and later worked for Thomas Edison.
3. Tesla invented the AC induction motor and the Tesla coil.
4. He also developed the concept of wireless power transmission.
5. Tesla's inventions were ahead of their time and were not fully appreciated during his lifetime.
6. However, his work laid the foundation for many modern technologies.

Executing the code above we can see surprisingly again that the DaVinci engine produced very accurate results that we can use as title heads and talk about. Based on those you can quickly have a baseline of where your blogpost will be based on and have to do less research and corrections using Google.

Now that we have the headings and the outline of our blog post lets move on to talking about writing the actual content paragraphs for each of those sections.

How To Write Content Automatically For Your Blog Using Python

In this last section we will be filling in the most important bit of our content creation process. Using AI like we did earlier in the sections we will basically implement the code that generates a blob of text in the form of one or more paragraphs using the headers.

  • Input: Heading or title that you want to generate content for
  • Output: A paragraph or more with the max count limit as defined on the second parameter in the openai_create_section function below. It must be noted here that there’s no guarantee that the OpenAI DaVinci code will generate as long text as you want you may have to drill down by giving it repeat options of different variations of your titles or things within the title itself.

Lets dive into the code and see how it works. Similar to the previous sections we will be making use of the OpenAI helper library we wrote at the beginning of this article. Basically this entails using completions which work in a similar manner as everything else taking into account the temperature and the penalties.

The code that implements this is the following.

from openai_helper import openai_create_section

section='Describe nuclear fusion reaction'
print('Generating content for: ', section)
print(openai_create_section(section))

In the code above we are basically asking the DaVinci to write us something about nuclear fusion reactions and then we simply output what it came up with back to the screen.

$ python ./python-ai-generate-blog-heading-content.py

Generating content for:  Describe nuclear fusion reaction

Nuclear fusion is a reaction in which two atoms join together to form a single, heavier atom. This process releases energy, which can be used to generate electricity.
Nuclear fusion reactions take place under extremely high temperatures and pressures. The sun is powered by nuclear fusion, and scientists are working on ways to replicate this process here on Earth.
There are many challenges to overcome in nuclear fusion research, but the potential rewards are great. If we can harness the power of nuclear fusion, it could provide a virtually limitless source of clean energy.

When executed above as you can see it has successfully generated for us some text on nuclear reaction and how it works. It does so in a good amount of detail and the English it has produced is perfectly useable. At this stage you have the option to use it as is in your blog content or you can take it and do your own modifications or changes.

I believe the above text is generated find and it doesn’t really need a lot of additions or changes.

Combining Everything

One bonus step that I want to talk about is that now we have all the ingredients to put a whole content writing together. To briefly summarize what we have demonstrated and explained above are:

  • We presented a way for the AI to find us blog topic ideas, one of which we selected to write about
  • We showed a way to create headings and titles to one of the topic ideas we found
  • We implemented code to write the content for these headings and ideas it had previously generated.

As you can see you have all the ingredients necessary to combine the three pieces of code above in one big application and have it write the full article for you. For the sake of time I haven’t put it all together for you but it should be straight forward to copy paste them all in one Python file and do so yourself. If you have any trouble doing this please do not hesitate to drop me a line below and I’ll help you out.

Conclusion

We were able to successfully go over a Free Automatic Blog Content Writing Tool, hopefully I answered any questions you may have had and helped you get started on your quest of automatic your blog content creation using AI.

If you found this useful and you think it may have helped you please drop me a cheer below I would appreciate it.

If you have any questions, comments please post them below or send me a note on my twitter. I check periodically and try to answer them in the priority they come in. Also if you have any corrections please do let me know and I’ll update the article with new updates or mistakes I did.

Would you consider using AI to automate parts of your content writing?

I personally still do a lot of the work manually as I do not think we are fully there yet but I do use it for ideas and making an outline of my headings. Every now and then I try to get blog content paragraphs too on topics that others have already researched a lot before me.

If you would like to visit the official OpenAI documentation here.

If you would like to find more articles related to automating computer tasks with Python you can check the list below:

Leave a Comment

Your email address will not be published. Required fields are marked *