How to use Gen AI for Social Media Marketing with PaLM API and Maker Suite

Nathaly Alarcón
Google Cloud - Community
7 min readSep 6, 2023

Generative Artificial Intelligence allows us to generate new creative content in text, code, images, etc. This time, we are going to create a web application that allows us to create marketing content.

To do this, we will use the following tools:

  • PaLM API: It is the interface that Google provides us to access the most advanced Large Language Models (LLMs) in the PaLM 2 family.
  • Maker Suite: It is the web interface that allows us to interact with Generative Language Models to create prototypes more easily.
  • Streamlit: It is a framework that allows us to create websites using Python.

Web App Overview

We are going to create an application to help promote Online Courses. The application has been designed under the following scheme:

Web App design

The idea is that the user inserts the name of the course they want to promote, and the PaLM API automatically delivers the creative marketing content to promote the result on Twitter, LinkedIn, and gives us the design guidelines to be able to create a poster or banner for the course.
Ok, let’s get to work. Let’s proceed to create the application.

Step 1. Create your PaLM API key.

In Maker Suite, go to the sidebar menu, select the “Get API Key” option, and then click on the “Create API key in new project” button.

Generate your API Key and save it in a safe place. We’ll need it in the next steps.

Step 2. Let’s generate some sample content for promoting a course.

From the sidebar, select the option “Create New” and then select “Text prompt”.

Now, let’s create the sample content we need.

For Twitter, we’ll use the following prompt: “Write a tweet to promote a Python Course for Teenagers”, then click Run.

Prompt to get social media content

You can repeat the process until the model returns the content you like the most.

Copy and save the result in a text editor.

Follow the same procedure to get examples of course promotion on LinkedIn and for banner guidelines. You can use the following prompts.

For LinkedIn: “Write a short LinkedIn post to promote a Python Course for Teenagers

For the banner: “Generate guidelines to create a banner for a Python Course for Teenagers

Step 3. Let’s build the Data Prompt

We will structure the output format we need for the web application. From the sidebar, select “Create New” and then “Data Prompt”

Data Prompt Format on Maker Suite

First, we will add the context to improve the results.

Context: “We are an online academy that generates online courses, create formal marketing content

To achieve the format of the target website, we will add 3 output columns: O_twitter, O_linkedin, O_banner. Note that we are using the prefix “O_” in the output names. This will serve as an indicator to separate the results returned by the API later.

Data Prompt Structure for the Web Application

Copy the content you generated in the previous step into the Outputs columns. The data prompt in Maker Suite will look as follows:

Data Prompt for the Web Application

Now let’s test the result of the Prompt we just configured in Maker Suite. Let’s generate content for a new course, for example: “Python for Finance”

Prompt testing

Let’s see the content generated for the new course:

Great! We already have the Prompt configured. Now let’s export the generated code in Python. To do this, click on “Get code” and copy the generated code into a text editor.

Step 4. Let’s analyze the generated code in a Jupyter Notebook

First, it is necessary to install and import the google.generativeai library, then we proceed to configure the API Key that we generated in step 1.

PaLM Api Configuration in Python
  • The prompt variable: contains the format of the prompt generated by Maker Suite. As you can see, the entire configured prompt table has been translated into a single string, keeping the output names, in our case O_twitter, O_linkedin and O_banner.
Prompt generated in Python

To be able to use the generated result, we will only take the resulting string and split it by the prefix we have configured (“O_”) (Step 6 in the notebook) and that’s it, we already have the three configured outputs in separate variables.

You can find the notebook code in the following repository:

https://github.com/nathalyAlarconT/PalmAPI_Sprint/blob/main/PalmAPI_MakerSuite_PromptAnalysis.ipynb

Step 5. Create the Web App with Streamlit

Install the required libraries

pip install streamlit 
pip install google.generativeai

Create a file named app.py. Import streamlit and google.generativeai libraries in to the scripts, and add your PaLM API Key.

import streamlit as st
import google.generativeai as palm

# Set your own Palm API Key
palm.configure(api_key="ADD YOUR API KEY HERE")

Next, let’s define the sidebar of the application. Note that we are only adding the input text to enter the course name and a button.

st.set_page_config(layout="wide")

def inputs():
# Here we are defining the sidebar with :
# 1) a basic input text and 2) a button
# We will receive the Course Name as Prompt and we are going to call Palm API to generate the new content

st.sidebar.header("Marketing Content Generator")

prompt_input = st.sidebar.text_input("Prompt", placeholder = "Write the name of the course you want to promote", )
button = st.sidebar.button("Get Palm API response")

return prompt_input, button

Now let’s define a function to interact with the PaLM API and get the 3 desired outputs (twitter, LinkedIn, and banner content).

def get_generative_data(prompt_input):
# Code from Maker Suite
# We are receiving the Prompt from the input configured on the sidebar.

defaults = {
'model': 'models/text-bison-001',
'temperature': 0.7,
'candidate_count': 1,
'top_k': 40,
'top_p': 0.95,
'max_output_tokens': 1024,
'stop_sequences': [],
'safety_settings': [{"category":"HARM_CATEGORY_DEROGATORY","threshold":1},{"category":"HARM_CATEGORY_TOXICITY","threshold":1},{"category":"HARM_CATEGORY_VIOLENCE","threshold":2},{"category":"HARM_CATEGORY_SEXUAL","threshold":2},{"category":"HARM_CATEGORY_MEDICAL","threshold":2},{"category":"HARM_CATEGORY_DANGEROUS","threshold":2}],
}

prompt = f"""We are an online academy that generates online courses, create formal marketing content
input: Basic Python for Teenagers.
O_twitter: Want to learn a new skill that will help you in your future career? Learn Python! Our Python for Teenagers course will teach you the basics of Python, so you can start coding today.
[Course link]
O_linkedin: Are you a teenager who wants to learn how to code? Python is a powerful programming language that can be used for a variety of tasks. Our Python for Teenagers course will teach you the basics of Python, so you can start coding today.

Enroll today and start learning Python!
O_banner: A relevant image or graphic, such as a computer screen with code being typed on it, or a group of teenagers working on a coding project.
A short, catchy phrase that summarizes the course, such as "Learn Python for Teenagers" or "Coding for the Future."
A call to action, such as "Enroll now" or "Learn more."
The course name, instructor, and start date.
The course logo or branding.
input: {prompt_input}
O_twitter:"""


# Let's call Palm API
response = palm.generate_text(
**defaults,
prompt=prompt
)
new_gen_content = response.result

# Let's split the result
splitted_res = new_gen_content.split("O_")
twitter_content = splitted_res[0]
linkedin_content = splitted_res[1]
banner_guidelines = splitted_res[2]

# We will return the 3 outputs (twitter, linkedIn, and the content for the banner)
return twitter_content, linkedin_content, banner_guidelines

Now we are going to connect the input and the button with get_generative_data function.

def main():
# We call the sidebar
prompt_input, button = inputs()

if button:
# When the button is clicked, then we are going to show the prompt
st.header('Content Generated for Course: "'+prompt_input+'"', divider='rainbow')

# We call the function to get the content from Palm API and Maker Suite
# We will receive the 3 outputs :twitter, linkedIn, and the content for the banner
palm_res1, palm_res2, palm_res3 = get_generative_data(prompt_input)


# Now we just need to display the results :)
# Show the Tweet
st.header('Twitter', divider='rainbow')
st.text(palm_res1)

# Show the LinkedIn Post content
st.header('LinkedIn Content', divider='rainbow')
st.text(palm_res2)

# Show the banner instruction
st.header('Banner Guides', divider='rainbow')
st.text(palm_res3)

if __name__ == "__main__":
main()

Run the application from your terminal with the following command:

streamlit run app.py

That’s it! You have created your interactive web app and you can try it with any prompts you want.

Web App created with streamlit

If you want, you can improve the application by adding more styles, features, and outputs.

Web App with extra marketing content.

We used Streamlit version 1.26.0. The code for the app can be found here: https://github.com/nathalyAlarconT/PalmAPI_Sprint/blob/main/Marketing_Web_App/app.py

Tutorial on Youtube

That’s all. Thank you for reaching the end of this guide.

If you liked it, share this guide so that more people can learn about and interact with these simple tools.

--

--

Nathaly Alarcón
Google Cloud - Community

I code in my sleep - ♡ I love Coffee ♡ - Data Scientist — Google Developer Expert in Machine Learning - Google Cloud Champion Innovator