Build a Pro-Level LinkedIn Post Generator in Minutes with GPT-3 and Streamlit

Alexandre t'Kint
6 min readJan 19, 2023

--

Welcome to this tutorial on how to use Streamlit to create an easy-to-use and efficient web application that utilizes the power of GPT-3 to generate LinkedIn posts. With just a few lines of code, you can create an application that can help you save time and effort in crafting the perfect post for your LinkedIn profile. In this tutorial, we will be walking you through the process of creating the application, step by step, and by the end of this tutorial, you will have a fully functional web application that you can use to generate unique and professional LinkedIn posts. Let’s get started!

Table of contents:

Feel free to check out the YouTube tutorial

1. Let’s start with the necessary python code for using GPT-3.

Prerequisites:

  • An OpenAI API key: You will need an OpenAI key to use the API. You can sign up for a free trial on the OpenAI website. Link to API keys here!
  • Python 3: This tutorial is written in Python 3, so you will need to have Python 3 installed on your machine.
  • The OpenAI library. You can install it using pip: pip install openai

First, let’s import the necessary libraries and set up the API key.

import openai

# Replace YOUR_API_KEY with your OpenAI API key
openai.api_key = "YOUR_API_KEY"

Sending a request to the API:

Now that we have our API key, we can start making requests to the API. We will use the openai.Completion.create() function with the following parameters:

  • model_engine: The name of the model you want to use. We will be using the text-davinci-003 model for this tutorial. This is the most capable GPT-3 model. More info here. (Note that the latest Chat GPT model isn’t available yet via API)
  • prompt: The prompt or context for the conversation. This can be a single line of text or a multi-line prompt separated by newlines.
  • max_tokens: The maximum number of tokens (words and punctuation) to generate in the response. The minimum is 1 and the maximum is 2048.
  • And a few more parameters that you can use to tweak the answer.

Here’s an example of how to ask a question to the API:

# Set the model and compose the prompt
model_engine = "text-davinci-003"
post_content = "a new blog on using chatgpt in a streamlit app"
prompt = f"Write a catchy linkedin post about {post_content}"
# Set the maximum number of tokens to generate in the response
max_tokens = 1024
# Generate a response
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=max_tokens,
temperature=0.5,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)

2. Integrate within Streamlit

Open your favorite code editor (e.g. VScode or Pycharm). Make a new python file called app.py. Open your code terminal and install Streamlit with the use of pip install streamlit.

pip install streamlit

After installing, you import the Streamlit library by ‘import streamlit as st’ in the app.py file and you can start coding.

import streamlit as st

Use st.write(“Welcome to the LinkedIn post generator!”) to write your first code into the app.

st.write("Welcome to the LinkedIn post generator!")

After coding your application, it can be launched into your browser. Run the following code and see Streamlit in action.

streamlit run app.py

Once this works, we’ll add the code that we created in the first section. Of course, we can’t add passwords to our code. That is why we have to create .streamlit file in our repository, which contains a secrets.toml document that contains:

[api-keys]
open_ai = "YOUR_API_KEY"

The app.py final code looks like:

import streamlit as st
import openai

def get_openai_answer(post_content):
# Get API key
openai.api_key = st.secrets["api-keys"]["open_ai"]

# Set the model and compose the prompt
model_engine = "text-davinci-003"
prompt = f"Write a catchy linkedin post about {post_content}"

# Set the maximum number of tokens to generate in the response
max_tokens = 1024

# Generate a response
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=max_tokens,
temperature=0.5,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)

# Get the response
answer = completion.choices[0].text

return answer

# Write intro text
st.write("Welcome to the LinkedIn post generator!")

# Get input
user_input = st.text_input("Describe what you would like to have a LinkedIn post about: ")

# Wait until click
if(st.button("Generate LinkedIn post")):
answer = get_openai_answer(user_input)
# Write answer
st.write(answer)

Finally, let's add a requirements.txt file to our app which fixes some of the package versions that we would use in our app.

# pip install -r requirements.txt
streamlit==1.17.0
charset-normalizer==2.0.9
openai

Finally, run the below code in your terminal to test your project. Once this works well locally, we can start getting it online in the next steps.

streamlit run app.py

3. Push to GitHub

Next, push your code to a GitHub repository. There are multiple ways to do so, this can be done via the command line interface or via the integrated tools in your editing tool.

Of course, don’t push the secrets.toml file to Github. You can add this to your .gitignore file so that your API keys are not exposed to the public.

4. Publish on Streamlit cloud

Alright, our code is working locally. Let’s get this project available to the crowd! Once you pushed your GitHub code to your repository, create a new application. Click on “New app” on your Streamlit homepage.

In the settings:

  • Link this to your GitHub repository. If your Streamlit account is linked to GitHub, it will automatically suggest paths to your repo’s.
  • Link to your branch. If you didn’t create one at the push, keep this on master.
  • Main path file name: app.py (as defined above)
  • In the Sharing section, put your application as: “This app is public and searchable”
  • Finally, as our secrets.toml file has not been pushed to GitHub in step 3. We’ll give the API key as an environment variable in Streamlit.

5. Share and promote

And that’s it! With just a few lines of code, we were able to create a simple web application that utilizes the power of GPT-3 to generate LinkedIn posts. By using this, users don't have to think anymore about framing their message.

Of course, we just did the bare minimum to have everything working. Streamlit provides you with multiple ways to enhance the look and feel of your application. Have a look here.

Thanks for reading, the GitHub repo, and YouTube tutorial are linked here! In the meantime, this is what my final version of the LinkedIn Post Generator looks like here.

❤ ️If you found this article helpful, I’d be grateful if you could follow me on Medium and give it a clap or two. Your support means a lot to me. Thank you!

Enjoy!
Alexandre t’Kint

--

--