Revolutionize Your Writing Process with Lyzr's Book Writing Assistant
Tags: tutorial, ai, python, lyzrai
Published: 2024-04-02 05:02:53

In the realm of literature, the journey from idea to publication can be arduous. [Lyzr](lyzr.ai), a frontrunner in AI-powered solutions, unveils its latest breakthrough: the Book Writing Assistant. This innovative tool is poised to revolutionize the way authors craft their masterpieces, providing indispensable support and inspiration at every step of the writing process.

The Book Writing Assistant by Lyzr is your ultimate AI muse, offering unparalleled assistance in various aspects of book creation, from generating captivating titles to designing stunning book covers. With its intuitive interface and cutting-edge AI models, this tool empowers authors and aspiring writers to unleash their creativity and bring their literary visions to life.

---

{% embed https://docs.lyzr.ai/homepage?source=post_page-----5a9e6e9f3e01-------------------------------- %}

---


## Setting up the Project

Let's dive into setting up the project for Lyzr's Book Writing Assistant

**Clone the App**: Start by cloning the Book Writing Assistant app repository.

   ```bash
   git clone https://github.com/PrajjwalLyzr/Book-Writing-Assistant
   ```

**Create a Virtual Environment**: Set up a virtual environment and activate it.

   ```bash
   python3 -m venv venv
   source venv/bin/activate
   ```

**Set Environment Variables**: Create a `.env` file and add your OpenAI API key.

   ```plaintext
   OPENAI_API_KEY = "Paste your openai api key here"
   ```

**Install Dependencies**: Install the required dependencies.

   ```bash
   pip install lyzr-automata streamlit python-dotenv
   ```

## Project Structure

```scss
Book-Writing-Assistant
│
│
├── app.py
│
├── README.md
│
├── .gitignore
│
└── requirements.txt
```

### Core Components of the Book Writing Assistant

Imports necessary libraries like os, PIL, streamlit, and modules from Lyzr’s automata. It also loads the OpenAI API key from environment variables. The API_KEY is retrieved using os.getenv. This key is crucial for accessing OpenAI’s services.

```python
import os
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent, Task, Logger
from lyzr_automata.tasks.task_literals import InputType, OutputType
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from dotenv import load_dotenv

load_dotenv()

API_KEY = os.getenv('OPENAI_API_KEY')
```

## Setup the OpenAI models for Book Writing Assistant

Initializes two instances of the OpenAIModel class, which facilitate interactions with OpenAI’s models. The first instance, open_ai_model_image, is configured to work with the DALL-E model, designed for image generation tasks. It specifies parameters such as the API key, the number of samples to generate (n), and the model name.

```python
open_ai_model_image = OpenAIModel(
    api_key=API_KEY,
    parameters={
        "n": 1,
        "model": "dall-e-3",
    },
)


open_ai_model_text = OpenAIModel(
    api_key= API_KEY,
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.5,
        "max_tokens": 1500,
    },
)
```

## Kernel of Book Writing Assistant Application

[Lyzr Automata](https://github.com/LyzrCore/lyzr-automata) is a sophisticated multi-agent automation framework designed to keep things simple, with a focus on workflow efficiency and effectiveness. It enables the creation of multiple agents that are coupled with specific tasks.

```python
def book_writing_assistant(genre):
   
    writer_agent = Agent(
        prompt_persona="You are an expert book writer and a graphic designer, good at writing books on a single keyword as well as making attractive cover pages for your books, you can also provide the chapters regarding the any topic",
        role=" book writer",
    )


    title_generation = Task(
        name="Book Title Creator",
        agent=writer_agent,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
        model=open_ai_model_text,
        instructions="Use the description provided and write some set of titles for the book. Use your creativity. [IMPORTANT!] Setup the events in a detailed manner",
        log_output=True,
        enhance_prompt=False,
        default_input=genre
    )


    chapter_generation = Task(
        name="Book Chapters Creator",
        agent=writer_agent,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
        model=open_ai_model_text,
        instructions="Use the description provided and write some set book chapters and well give the blueprint of how to write the book on the provided description. Use your creativity. [IMPORTANT!] Setup the events in a detailed manner",
        log_output=True,
        enhance_prompt=False,
        default_input=genre
    )


    book_writing_tips =  Task(
        name="Book Writing Tips",
        agent=writer_agent,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
        model=open_ai_model_text,
        instructions="Use the description provided and give the 4-5 bullet points in 20-30 words of book writing tips on the provided description. Use your creativity. [IMPORTANT!] Setup the events in a detailed manner",
        log_output=True,
        enhance_prompt=False,
        default_input=genre
    )


   
    book_cover_image = Task(
        name="Book Cover Image Creation",
        output_type=OutputType.IMAGE,
        input_type=InputType.TEXT,
        model=open_ai_model_image,
        log_output=True,
        instructions="Generate an Image which is suitable to the given description. Capture every detail. Minimalistic style. [IMPORTANT!] Avoid any text or numbers in the image.",
    )


    logger = Logger()


    main_output = LinearSyncPipeline(
        logger=logger,
        name="Book Writing Assistant",
        completion_message="Book Writing Assistant Generated all things!",
        tasks=[
            title_generation,
            chapter_generation,
            book_writing_tips,
            book_cover_image,
        ],
    ).run()


    return main_output
```
It comes up with potential titles for the book based on what the writer wants to write about. Makes a plan for what each chapter of the book could be about, helping the writer organize their ideas. 

## Executing the Application

To run Lyzr's Book Writing Assistant:

```python
if __name__ == "__main__":
    style_app() 
    book_brief = st.text_input("Enter about the book, a title or a genre")
    button=st.button('Submit')
    if (button==True):
        
        generated_output = book_writing_assistant(book_brief)

        # DISPLAY OUTPUT
        title_output = generated_output[0]['task_output']
        st.header('Suggested Titles')
        st.write(title_output)
        st.markdown('---')

        chapter_output = generated_output[1]['task_output']
        st.header("Suggested Chapter's")
        st.write(chapter_output)
        st.markdown('---')

        writing_tips = generated_output[2]['task_output']
        st.header('Writing Tips')
        st.write(writing_tips)
        st.markdown('---')

        image_file_name = generated_output[3]['task_output'].local_file_path
        st.header('Book Cover Page')
        st.image(image_file_name, caption='Book Writing Assistant - Lyzr') 
```

With Lyzr's Book Writing Assistant, the daunting task of writing a book is transformed into an inspiring and fulfilling journey. Embrace the power of AI to unleash your creativity and bring your literary dreams to fruition.

## References

- Book Writing Assistant - [GitHub Repository](https://github.com/lyzrai/book-writing-assistant)
- [Lyzr Website](https://www.lyzr.ai/)
- [Book a Demo](https://www.lyzr.ai/book-demo/)
- Lyzr Community Channels: [Discord](https://discord.gg/nm7zSyEFA2), [Slack](https://join.slack.com/t/genaiforenterprise/shared_invite/zt-2a7fr38f7-_QDOY1W1WSlSiYNAEncLGw)