How to build an AI-Assisted Content Generation and Analysis Tool using Google Cloud and Gemini

Neha Koppikar
Google Cloud - Community
6 min readAug 4, 2024

Introduction

The digital age has drastically transformed how we create, interpret, and use text. Crafting compelling narratives and extracting valuable insights from large amounts of textual data, text generation, and analysis are revolutionizing industries worldwide.

For instance, Google has been at the forefront of AI advancements with its robust suite of tools and models. One notable model is Google’s Gemini, which offers advanced text generation and analysis capabilities. The AI boom, initially sparked by various AI models, has led to a surge in innovation and the development of competitive products, driving public interest and investment in artificial intelligence. Generative AI has assisted us in various ways, including code debugging, email composing, and explaining complex concepts.

In this blog, we will build a Python-based web application using Flask to generate text based on Google’s Gemini 1.5 Pro Model. We will also analyze the generated text for sentiment, key topics, and readability. Finally, we will deploy the Flask app using Cloud Run. This tutorial is designed for beginners with basic Python, Flask, and Google Cloud knowledge.

Personal Experience

As someone passionate about leveraging AI to solve real-world problems, I was inspired to create this tool to simplify content creation and analysis. The ease of use and power of Google’s AI models motivated me to share this process with fellow developers.

Audience

This blog is aimed at beginner developers who have a basic understanding of Python programming and are interested in exploring AI-powered applications using Google Cloud.

Outcome

By the end of this tutorial, you will have a fully functional AI-powered content generation and analysis tool deployed on Google Cloud.

Design

For this project, simple architecture is used: The user inputs their prompt or incomplete text on the first page of a Flask application. The Gemini 1.5 Pro Model processes this input to generate content. The generated content is then analyzed for sentiment, key topics, and readability and displayed on the second page of the application.

Rationale

This design ensures usability and functionality while keeping the implementation straightforward. The separation of content generation and analysis on different pages enhances user experience by logically organizing tasks.

Below is a simple diagram illustrating the high-level architecture:

Architecture

Frontend: A simple HTML form for input and display generated content and analysis.

Backend: Flask server handling requests, interacting with the Gemini model, and processing text.

Docker: For containerizing the Flask App

Cloud Deployment: Google Cloud Run for serverless deployment, ensuring scalability and ease of management.

Step-by-step instructions

Create a new Google Cloud project

  • Go to https://console.cloud.google.com/
  • At the top left corner, beside the Google Cloud logo, click on the existing project name and the “New Project” button on the Project Selector Page.
Select a project
Project Selector Page
  • Write the project name and click on “Create”.
Create New Project
  • Select the newly created project once redirected to the Google Cloud console page.
Select Project
  • After selecting the project, your console page should look something like this:
Google Cloud Console page

Enable necessary APIs

  • On the Google Cloud Console page, click on “APIs & Services” (Right below “Quick Access”.) Alternatively, you can search for “APIs & Services” in the search bar at the top.
APIs & Services
  • Once redirected to the APIs and Services page, click on “+Enable APIs and Services”
APIs and Services Page

Search for the following APIs and click on “Enable.”

  • Cloud Run Admin API
  • Cloud Build API
  • Vertex AI API
Enable

Obtain project files

  • Click on “Activate Cloud Shell” at the top right corner
  • Run the following command to set the default project ID of the cloud CLI:
gcloud config set project YOUR_PROJECT_ID
  • Clone the repository using the following command:
git clone https://github.com/NehaKoppikar/ContentGenerationAnalysis.git
  • Change the working directory using the following command:
cd ContentGeneratorAnalysis
  • Check if the app works as expected by running the following command on the cloud shell by using the following steps:
  1. Create virtual environment
python -m venv env

2. Activate virtual environment

source env/bin/activate

3. Install Dependencies

pip install -r requirements.txt

4. Test Flask App from Cloud Shell

python app.py

Once the app works as expected, deploy it on a serverless computing service.

Deploy Flask App on Cloud Run

  • Create DockerFile to containerize the application
FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

ENV PORT=8080

CMD ["sh", "-c", "gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 app:app"]
  • Build Docker image
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/gen-analyzer
  • Deploy the image to Cloud Run
gcloud run deploy --image gcr.io/YOUR_PROJECT_ID/gen-analyzer --platform managed

Result / Demo

Once the application is deployed on Cloud Run, a publicly shareable link will be available on the cloud shell (Service URL: https://ai-analyzer-s3yjgpacla-em.a.run.app). The application will look something like the following video demo:

The first page contains the following

  • The header of the page (Text Generator)
  • A multi-line text input control where you can type a sentence or a phrase
  • Generate button that redirects to the next page
First Page Screenshot

The second page contains the following

  • Main header of the Page (Generated Content)
  • A sentence or phrased type by you and generated content
  • Second header (Analysis)
  • Sentiment Scores (Positive, Negative, Neutral)
  • Top 5 topics
  • Readability Score
  • Button to go back to the first page (Go Back)
Second Page Screenshot

This application can be used to draft emails, write essays, or even help us write social media posts.

Troubleshooting Tips/Common Errors:

  • Ensure all required APIs are enabled.
  • Verify the project ID is correctly set in Cloud Shell.
  • Check for any syntax errors in the Dockerfile or Flask app.

What’s next?

Congratulations on building your Flask app. Now that you have a solid foundation, here are some ideas to take your project to the next level:

  • Improve the frontend with a more sophisticated UI
  • Explore alternative generative AI models
  1. Other Gemini models can also be tried by referring to this GitHub repo.
  2. The Gemini API key can be used for this, as obtained here.
  • Add analysis types to the generated content. Some analyses to consider:
  1. Named Entity Recognition
  2. Text Summarization
  3. Emotion Classification (joy, fear, anger, etc)

Call to action

To learn more about Google Cloud services and to create an impact for the work you do, get around to these steps right away:

--

--