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

Select a project
Project Selector Page
Create New Project
Select Project
Google Cloud Console page

Enable necessary APIs

APIs & Services
APIs and Services Page

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

Enable

Obtain project files

gcloud config set project YOUR_PROJECT_ID
git clone https://github.com/NehaKoppikar/ContentGenerationAnalysis.git
cd ContentGeneratorAnalysis
  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

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"]
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/gen-analyzer
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

First Page Screenshot

The second page contains the following

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:

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:

  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.
  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:

<hr><p>How to build an AI-Assisted Content Generation and Analysis Tool using Google Cloud and Gemini was originally published in Google Cloud - Community on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>