DEV Community

Akshay Keerthi
Akshay Keerthi

Posted on

Building a Story Book Review app using Lyzr SDK

In today’s digital age, the fusion of technology and literature opens up exciting possibilities for readers and enthusiasts alike. With the advent of artificial intelligence (AI), we’re witnessing a revolution in how we consume, analyze, and interact with literary works. In this blog post, we’ll embark on a journey to explore how AI can enhance our experience of literature by building a Story Book Review application using Lyzr’s QABot and Streamlit.

Image description

Introducing Lyzr’s QABot:

Lyzr’s QABot is a powerful tool powered by the Retrieval-Augmented Generation (RAG) model, designed to provide concise summaries and insightful analyses of various texts.With its ability to understand and generate human-like responses, QABot becomes an invaluable companion in exploring and understanding literary works.

Building the Story Book Review App:

Using Streamlit, a popular Python library for building applications, we can create an intuitive and user-friendly interface for our Story Book Review app.The app allows users to upload PDF files containing storybooks, which are then analyzed by Lyzr’s QABot to generate summaries and insights.

Why use Lyzr SDK’s?

With Lyzr SDKs, crafting your own GenAI application is a breeze, requiring only a few lines of code to get up and running swiftly.

Checkout the Lyzr SDK’s

Lets get Started!
Create a new file app.py and use that

import os
from pathlib import Path
from utils import utils
import streamlit as st
from lyzr import QABot
Enter fullscreen mode Exit fullscreen mode

This code imports necessary modules for building a Streamlit application. It includes modules for file system operations (os and pathlib), web interface creation (streamlit), and Lyzr's QABot for text analysis. These imports set the stage for developing an application that allows users to upload storybooks for analysis using Lyzr's QABot within a Streamlit environment.

Next Set Up OpenAI API Key and using Streamlit’s secrets management, set up the OpenAI API key within your Streamlit application. Replace "OPENAI_API_KEY" with the actual key name defined in your Streamlit secrets where your OpenAI API key is stored.

# replace this with your openai api key
os.environ["OPENAI_API_KEY"] = st.secrets["apikey"]
Enter fullscreen mode Exit fullscreen mode
def literature_review():
    # "This function will implement the Lyzr's QA agent to review the story book"
    path = utils.get_files_in_directory(data)
    path = path[0]

    reviewer = QABot.pdf_qa(
        input_files=[Path(path)]
    )

    return reviewer
Enter fullscreen mode Exit fullscreen mode

This function, literature_review(), utilizes Lyzr's QABot to conduct a literature review on a storybook. It retrieves the path of a PDF file from a specified directory using a utility function and then initiates the review process using Lyzr's pdf_qa() method, passing the file path as input. The function returns the result of the review, likely containing insights or analysis generated by Lyzr's QA agent.

def file_checker():
    file = []
    for filename in os.listdir(data):
        file_path = os.path.join(data, filename)
        file.append(file_path)

    return file
Enter fullscreen mode Exit fullscreen mode

The function file_checker() iterates through the files in a specified directory (data) using os.listdir(). It then constructs the full file path for each file using os.path.join() and adds them to a list (file). Finally, it returns the list of file paths. This function essentially provides a way to check and retrieve the paths of files within a given directory.

if __name__ == "__main__":
    research_paper = st.file_uploader("Choose Story Book", type=["pdf"])

    if research_paper is not None:
        utils.save_uploaded_file(directory=data, uploaded_file=research_paper)
        file = file_checker()
        if len(file)>0:
            if st.button("Review"):
                research_review = literature_review()
                responses = utils.reviewer(agent=research_review)
                if responses is not None:
                    utils.get_response(response=responses)

    else:
        st.warning('Please upload a story book in pdf')
Enter fullscreen mode Exit fullscreen mode

This block of code initializes a Streamlit application where users can upload PDF files for review. It prompts the user to upload a PDF file and saves it to a specified directory. Upon uploading, it checks if any files are present in the directory and displays a “Review” button. Clicking this button triggers the execution of a literature review process using Lyzr’s QABot. If no file is uploaded, a warning message is displayed prompting the user to upload a PDF file.

Create a new file utils.py and use that:

This utils.py provides functions for managing files, conducting a literature review using Lyzr’s QABot, and displaying the results within a Streamlit application. It includes functionalities for clearing existing files in a directory, saving uploaded files, querying Lyzr’s QABot for reviews based on predefined prompts, and presenting the responses in an organized manner within the Streamlit interface.

def reviewer(agent)
    results = {}
    prompts = {
        "Summary": "Write the summary of the story in 2 sentences",
        "Analyze": "How the author uses symbolism to enhance the story's themes",
        "Findings": "Identify key turning points or pivotal moments that drive the plot forward",
        "Discussion and Conclusions": "Reflect on the overall message or moral lesson conveyed by the story and its relevance to readers. Make a 4-5 line of response",
    }

    for heading, prompt in prompts.items():
        response = agent.query(prompt)
        results[heading] = response.response

    return results
Enter fullscreen mode Exit fullscreen mode

This function, reviewer(agent), conducts a literature review using Lyzr's QABot by querying it with predefined prompts and collecting the responses. It iterates over a dictionary of prompts, where each prompt corresponds to a specific aspect of the review, such as summarization, analysis of symbolism, identification of key plot points, and reflection on the story's message. For each prompt, it queries the provided agent and stores the responses in a dictionary, which is then returned.

The Story Book Review app represents a marriage of literature and AI, empowering readers to delve into the rich tapestry of stories with newfound clarity and understanding.

As we harness the power of AI to augment our literary experiences, we embark on a journey of discovery, exploration, and appreciation for the timeless art of storytelling. Join us in exploring the endless possibilities at the intersection of literature and AI.

Try out the app : https://storybook-review-lyzr.streamlit.app/

References
Lyzr Website: Lyzr

Book a Demo: Demo

Lyzr Community Channels: Discord

Slack : Slack

Top comments (0)