In this practical hands-on, you will learn to harness the power of Artificial Intelligence to automate the creation of high-quality and engaging blog posts. We will guide you step by step in building an interactive application that transforms user-provided topics into complete, optimized content ready for publication. Using modern technologies such as Streamlit for the interface and advanced AI models, you will experience how automation can streamline the writing process and bring more efficiency to content creation.

In addition to learning how to set up and use the blog post generator, you will have the opportunity to customize the style and tone of the generated texts, view the result in Markdown format, and even copy or download the posts to use on your blog, website, or social media.

This hands-on is ideal for developers, marketing professionals, bloggers, or anyone interested in exploring AI capabilities for content automation. Get ready to transform the way you create texts and learn how to apply AI in your day-to-day content strategy!

Step 1: Creating a Groq API Key

Groq

Step 2: Preparing the Environment

python -m venv tcb-venv
source tcb-venv/bin/activate

touch backend.py frontend.py

deactivate # Use it to deactivate virtual environment

Step 3: Backend Python Code

from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq

# Configuring the chat model
chat = ChatGroq(    
    model="llama3-70b-8192"  # mixtral-8x7b-32768   gemma-7b-it   llama3-70b-8192
)

# Defining the prompt template
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a writer specialized in creating informative and engaging blog posts."),
    ("human", "I need a blog post about {input} with tone {tone} and style {style}."),
])

chain = prompt | chat

# Function to generate the blog post with customized tone and style
def get_blog_post(input_text, tone, style):
    response = chain.invoke({"input": input_text, "tone": tone, "style": style})
    return response.content

Step 4: Frontend Python Code:

import streamlit as st
from backend import get_blog_post

# Function to generate the blog post
def generate_blog_post():
    if input_text:
        # Generate the blog post with tone and style parameters
        blog_post = get_blog_post(input_text, tone, style)

        st.subheader("Generated Blog Post")

        # Display the blog post content in markdown format
        st.markdown(blog_post)

        # Display the blog post content for copying
        st.text_area("Copy the Blog Post:", blog_post, height=300)

        # Button for downloading/copying the content
        st.download_button(
            label="Download Blog Post",
            data=blog_post,
            file_name='blog_post.md',
            mime='text/markdown'
        )

        st.success("Blog post generated successfully. You can copy or download the content using the button above.")
    else:
        st.sidebar.warning("Please enter a topic for the blog post.")

# Page Title
st.set_page_config(page_title="TCB Streamlit App")

# Application Title
st.title("TCB - Challenge Day")
st.title("Blog Post Generator")

# Using the sidebar for user input
st.sidebar.header("Your Prompt is My Command...")

# Input for the blog post topic
input_text = st.sidebar.text_input("Enter the blog post topic:")

# Selectors to choose tone and style
tone = st.sidebar.selectbox(
    "Choose the tone:",
    options=["Formal", "Casual", "Persuasive", "Neutral", "Humorous"]
)

style = st.sidebar.selectbox(
    "Choose the style:",
    options=["Informative", "Opinion-based", "Technical", "Narrative", "Didactic"]
)

# Generate the blog post when pressing Enter or clicking the button
if st.sidebar.button("Generate Blog Post"):
    generate_blog_post()

Step 5: Running the Application

# Running Python Streamlit application:
streamlit run frontend.py --server.port 8080

If you face any issue using the command above, due to security stuff in Google Cloud Shell, try this one below:
**streamlit run frontend.py --server.port 8080 --server.enableCORS false --server.enableXsrfProtection false** 

# Installing streamlit
pip install streamlit

# Running Python Streamlit application:
streamlit run frontend.py --server.port 8080
# Error:
ModuleNotFoundError: No module named 'langchain_core'

# Installing LangChain Groq Library
pip install langchain-groq
pip show langchain-groq

# Running Python Streamlit application:
streamlit run frontend.py --server.port 8080
# Error:
AuthenticationError: Error code: 401 - {'error': {'message': 'Invalid API Key', 'type': 'invalid_request_error', 'code': 'invalid_api_key'}}

export GROQ_API_KEY="YOUR-KEY"
env | grep GROQ_API_KEY

export GROQ_API_KEY="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"

# Running Python Streamlit application:
streamlit run frontend.py --server.port 8080