Barely four months since OpenAI unleashed ChatGPT, a human-behavior-mimicking chatbot that took the community by storm, they recently announced its successor, GPT-4. This development will continue to disrupt the global market and, unfortunately, take the jobs of millions of people.
While it’s a welcome development for ChatGPT users looking to explore the capabilities of AI in their respective fields of human endeavor, the bad news is that ChatGPT-4 is not for free. However, we are yet to see if it could be freely available following Microsoft’s announcement that its recently introduced Bing AI is operating on GPT-4.
Hence, if you are unwilling to commit to a $20 monthly subscription, or you feel ChatGPT-3 is working flawlessly for you, you may be better off with ChatGPT-3. What is more, ChatGPT-4 is no different than its previous GPT model if it’s about taking information from your question and giving you an answer it deems perfect.
The only difference is that it is more accurate and creative, plus the special graphic features that will turn your text into pictures and videos.
The Purpose of This Tutorial
You will benefit from this tutorial if you have not yet learned how to implement the ChatGPT model using Streamlit.
As a Python developer, you have undoubtedly learned to implement ChatGPT in your Python script and have it running in your terminal. So this tutorial will be nothing new to you except for a few things.
Overall, the purpose of this tutorial is threefold:
- To improve your Python skills.
- To demonstrate how to implement the ChatGPT model using Streamlit.
- To show you how to use the model to write unique blog articles.
🚀 Try this app in live demo here.
Creating a Streamlit Dashboard
Writing a blog article involves a series of steps. First, you have to brainstorm topic ideas based on a selected niche and choose the one you prefer. Then, you outline the sections. In each section, you generate content corresponding to the sections and the topic.
We will try using ChatGPT to automate these tasks. Note that this article is created with ChatGPT-3 in mind. Of course, the principle can be applied to the GPT-4 model.
I usually start with a
main() function that will run as soon as we open the app. But in this tutorial, something came before the function.
import openai import streamlit as st API_KEY = st.sidebar.text_input('Enter your API key') openai.api_key = API_KEY
We made provision for our users to use their API key given that we now have a new model with a paid plan.
Not everyone will let others use their paid plan for free. If you have no problem with that, then you are free to include your key in the script. Now comes the
def main(): st.sidebar.header('AI Blog Writing Tool') st.sidebar.info('An AI tool that can generate blog content') st.sidebar.info('Start with the first option\n before you proceed to the next.') op = st.sidebar.selectbox('Steps', ['topics', 'section', 'content']) if op == 'topics': topics() elif op == 'section': section() else: content()
Everything is self-explanatory. Each step you select will take you to the function that will be executed.
So, let’s imagine we are writing a blog article with Python programming being the selected niche. We narrow down the niche to data science.
Let’s see if the model can generate blog topics for us. To do so we selected the topic option, triggering a callback function.
def topics(): st.header('AI Blog Writing Tool') st.info('To generate blog topic, please follow the pattern given below:') prompt = st.text_area('Write your words', height=50, value='Generate blog topic on data science with Python') if st.button('Send'): st.text(BlogTopics(prompt))
The prompt is the question we will feed to the model. It will be sent to the
BlogTopics() function. What we feed to the model will help it know what to give as an answer. In the
st.text_area() I gave a sample you can use based on your selected niche.
def BlogTopics(prompt): response = openai.Completion.create( engine="davinci-instruct-beta-v3", prompt=prompt, temperature=0.7, max_tokens=100, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices.text
.We have to import the
openai module to enable this function to run.
🧑💻 Recommended: How to Install OpenAI in Python?
Notice the model that was used. In one Django application, I used the
text-davinci-003 model. But in this one, we are using the
davinci-instruct-beta-v3 model. It’s proven to be an ideal one for generating unique blog content.
max_tokens is the number of characters we want the model to generate. Blog topics shouldn’t be more than that. For a detailed explanation of the arguments, check this article.
Let’s now run the app on Streamlit to see the results.
Wow! Can you see 9 blog topic ideas the ChatGPT model has generated for us? That’s interesting. So, let’s select number 2, How to use Pandas for data analysis. This is now our topic.
The next step is sections. When selected, it calls the callback function.
def section(): st.header('AI Blog Writing Tool') st.info('To generate blog section, please follow the pattern given below:') prompt = st.text_area('Write your words', height=50, value='Write blog sections\n\nBlog topic: ') if st.button('Send'): st.text(BlogSections(prompt))
Notice what I suggested in the
st.text_area() function. You can follow the same pattern. As usual, another function gets executed when the button is pressed.
def BlogSections(prompt): response = openai.Completion.create( engine="davinci-instruct-beta-v3", prompt=prompt, temperature=0.6, max_tokens=100, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices.text
This is similar to the
BlogTopics() function. So let’s run it and see the results.
Please note that the results might be different from yours. At times, you may have to run it several times to get what you want. I did that and got ‘Introduction’ as the first section.
Based on the sections, you select one and feed it to the model. Here is the function called when the last step of the
main() function is selected.
def content(): st.header('AI Blog Writing Tool') st.info('To generate blog content, please follow the pattern given below:') prompt = st.text_area('Write your words', height=50, value="Expand the blog section in a professional tone \n\nBlog Topic:\n\nSection:") if st.button('Send'): st.text(BlogContent(prompt))
And here is the
BlogContent() function. The only difference is the
def BlogContent(prompt): response = openai.Completion.create( engine="davinci-instruct-beta-v3", prompt=prompt, temperature=0.7, max_tokens=400, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices.text
Can you see a 400
max_tokens of text have been generated based on the introductory section? The key lies in the prompt you feed to the model. Do the same to all your sections and before long, you will have a unique blog article professionally written by ChatGPT.
Don’t forget to copy each of the text generated.
We have taken advantage of advancements in technology, the latest being the invention of ChatGPT, an AI model that mimics human behavior, to write a unique blog article.
You now have at your disposal an AI writing tool you can use for all your blog articles. Check my GitHub page for the full code. The app is already running on Streamlit Cloud. Make sure you check it out. Enjoy your day.
Jonathan Okah is a professional freelance Python developer whose primary goal is to solve a perceived problem with the use of technology, thus making life easy for you.
Jonathan has a flair for writing and has written several blog articles for clients. He is constantly learning to improve his skills. Currently, he is studying software engineering, hoping to become a full-stack web developer.
In his leisure time, Jonathan enjoys playing games and watching the English Premier League. You can contact him for your freelance projects at Upwork.