ad
ad

Generative AI project - Poem Generator

Science & Technology


Introduction

In this article, we will explore the process of creating a simple Poem Generator application using Streamlit and the Grok API. This project demonstrates how we can leverage generative AI to create poetry based on user input topics. Follow along as we outline the steps to build your own poem generator.

Getting Started

To begin with, we need to create a new application using Streamlit. First, ensure you have Streamlit installed. If not, you can install it using pip:

pip install streamlit

Next, we will extract our API key from the Grok website. This key is crucial as it allows us to connect to the Grok API and utilize its capabilities to generate poems.

Setting Up the App

With everything ready, let's set up our application. We will start by creating a new file, for example, app.py, and then add the following code:

import streamlit as st
from grok import API  # Ensure you have the right import based on the API you're using

## Introduction
st.[title("Poem Generator")](https://www.topview.ai/blog/detail/poem-generator-video)

## Introduction
topic = st.text_input("Enter the topic:")

## Introduction
if st.button("Generate Poem"):
    with st.[spinner("Generating poem](https://www.topview.ai/blog/detail/poetry-generator)..."):
        # Read API key
        with open("open_api.txt") as f:
            grok_api_key = f.read().strip()
        
        llm = API(grok_api_key)  # Initialize Grok API
       
        # Generate poem using LLM
        response = llm.chat.completions.create(
            messages=[
                ("role": "user", "content": f"Generate a poem on this topic: {topic)"}
            ]
        )
        
        poem = response['choices'][0]['message']['content']
        
        st.write(poem)  # Display the generated poem

Running the Application

To run the application, navigate to the directory where your app.py is located in your terminal and execute the following command:

streamlit run app.py

This command will start the Streamlit server, and you can access your Poem Generator in your web browser. On the page, you'll find an input box to enter a topic and a button labeled "Generate Poem." When you click this button, the app communicates with the Grok API to generate poetry based on the provided topic.

Result

Once you enter a topic and click "Generate Poem," the application will produce a poem that reflects the input you provided. The generated poem might look something like this:

In the realm of code where logic resigns,
A new dawn awaits, and creativity shines...

Feel free to experiment with different topics to see the range of creativity the Grok API can produce.

Conclusion

The Poem Generator project is a simple and effective demonstration of how generative AI can be utilized to create creative content. By integrating Streamlit with the Grok API, we can easily build interactive applications that engage users in a fun and creative way.

Thank you for following this tutorial. I hope you enjoyed building your own poem generator!


Keywords


FAQ

Q1: What is Streamlit?
A1: Streamlit is an open-source app framework designed for machine learning and data science projects, enabling you to create custom web applications easily.

Q2: What is the Grok API used for?
A2: The Grok API allows developers to integrate generative AI capabilities into their applications, enabling features like text generation, including poetry.

Q3: How do I obtain the API key for Grok?
A3: You can obtain your API key by signing up on the Grok website and accessing your account settings.

Q4: Can I customize the poem generator?
A4: Yes, you can customize the poem generator by modifying the input prompts and the API requests to tailor the responses to your specifications.

Q5: Is there a limit to the length of the poem generated?
A5: The length of the generated poem can depend on the settings of the Grok API. You can refer to the API documentation for details on output length control.