Unleash Your Creativity with Gemini Pro: Building a Novel Generation Web App with Streamlit

Falah Gatea
4 min readJul 17, 2024

--

Introduction

In the ever-evolving landscape of artificial intelligence, the emergence of powerful language models has opened up new frontiers in creative writing and storytelling. One such model that has captured the attention of writers, developers, and AI enthusiasts alike is Gemini Pro — Google’s latest and most advanced generative AI system.

Gemini Pro, with its unparalleled natural language processing capabilities, offers a unique opportunity to revolutionize the way we approach novel generation. By seamlessly integrating this cutting-edge technology into our creative workflows, we can unlock a world of possibilities, from crafting immersive narratives to exploring uncharted creative territories.

In this post, we will delve into the intricacies of using Gemini Pro for novel generation, showcasing how this remarkable AI model can serve as a powerful tool for writers, authors, and aspiring storytellers. We’ll explore the model’s capabilities, discuss practical implementation strategies, and share insights on how to harness the full potential of Gemini Pro to bring your literary visions to life.

Whether you’re a seasoned novelist seeking to enhance your creative process or an emerging writer eager to explore the frontiers of AI-powered storytelling, this introduction will provide you with a solid foundation to embark on your Gemini Pro-powered novel generation journey. Get ready to witness the fusion of human creativity and artificial intelligence, as we uncover the transformative potential of Gemini Pro in the realm of captivating novel generation.

The Code

import os
import google.generativeai as genai
import streamlit as st
from dotenv import load_dotenv

# Load environment variables
load_dotenv()
GOOGLE_API_KEY = os.getenv(key="GEMINI_API_KEY")
# Configure the Gemini Pro model
genai.configure(api_key=GOOGLE_API_KEY)
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
},
]

model = genai.GenerativeModel('gemini-pro',safety_settings=safety_settings)

# Streamlit app
st.title("Novel Generation with Gemini Pro")

# User input
prompt = st.text_area("Enter a prompt to generate a novel:", height=200)

# Generate novel
if st.button("Generate Novel"):
response = model.generate_content(prompt)
st.markdown(response.text)

# Instructions
st.write("## How to Use")
st.write("1. Enter a prompt to generate a novel.")
st.write("2. Click the 'Generate Novel' button to see the generated text.")
st.write("3. The generated novel will be displayed below.")

Here’s how the code works:

  1. Configure the Gemini Pro model: We first configure the Gemini Pro model by setting the API key. You’ll need to replace os.environ[‘GOOGLE_API_KEY’] with your actual API key.
  2. Create the Streamlit app: We use the Streamlit library to create a web-based user interface. The app has a title, a text area for the user to enter a prompt, and a button to generate the novel.
  3. Generate the novel: When the user clicks the “Generate Novel” button, the code calls the generate_content() method of the Gemini Pro model, passing the user’s prompt as an argument. The generated text is then displayed on the web page using Streamlit’s Markdown formatting.
  4. Provide instructions: Finally, we add some instructions to guide the user on how to use the app.

To run this app, you’ll need to have the following dependencies installed:

  • google-generativeai: Install this package to use the Gemini Pro model.
  • streamlit: Install this package to create the web-based user interface.

You can install these dependencies using pip:

pip install google-generativeai streamlit

Once you have the dependencies installed, you can save the code above to a file (e.g., novel_generator.py) and run the Streamlit app using the following command:

streamlit run novel_generator.py

This will start the web server and open the app in your default web browser. Users can then enter a prompt and click the “Generate Novel” button to see the generated text.

for a test with this example:

I need a novel about a beautiful female who helps her blind mother in a poor village

the final result:

Title: A Guiding Light

Synopsis:

In the impoverished village of Willow Creek, nestled amidst rugged mountains, lived a young woman named Anya. Possessing exquisite beauty that turned heads wherever she went, Anya had sacrificed her own aspirations to become the sole caretaker of her blind mother, Lira.

Lira, once a renowned weaver, had lost her sight in an accident. Now, she depended entirely on Anya’s unwavering support. Unable to leave their modest cottage, Lira yearned for the world she could no longer see.

As the villagers struggled with poverty and isolation, Anya found solace in her bond with her mother. She would read to her, describe the vibrant colors of nature, and guide her through their daily routine with a gentle hand.

One fateful day, a traveling artist named Elias arrives in Willow Creek. Intrigued by Anya’s kindness and the village’s unique atmosphere, he offers to paint a portrait of Lira. As he works, Elias witnesses the extraordinary connection between the women.

Inspired by their story, Elias uses his artistic talents to bring a sense of beauty and hope to the village. He captures the delicate curves of Lira’s face and the unwavering gaze of Anya, illuminating the hidden depths of their spirits.

As the portrait becomes a symbol of the bonds that sustain them, Anya and Lira find an unexpected source of strength in the unexpected kindness of a stranger. Together, they navigate the challenges of their isolated existence, proving that even in adversity, beauty and love can shine as a guiding light.

Thanks for reading If you love this tutorial, give some claps.

Connect with me on FB, Github,linkedin,my blog, PyPi, my YouTube channel,Email:falahgs07@gmail.com

Citations:
[1] https://pyimagesearch.com/2024/01/01/introduction-to-gemini-pro-vision/
[2] https://www.techtarget.com/whatis/feature/Gemini-15-Pro-explained-Everything-you-need-to-know
[3] https://pyimagesearch.com/2024/04/22/integrating-document-embedding-in-gemini-pro-an-approach-to-retrieval-augmented-generation/
[4] https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_pro_python.ipynb
[5] https://github.com/KalyanM45/Getting-Started-with-Gemini

--

--

Falah Gatea
Falah Gatea

Written by Falah Gatea

Developer Programmer, in Python and deep learning. IOT Microcontroller Developer iraqprogrammer.wordpress.com

No responses yet