DEV Community

Akshay Keerthi
Akshay Keerthi

Posted on

Building Research Paper Assistant with Lyzr SDK

In today’s fast-paced world of research and academia, staying updated with the latest findings and insights is crucial. However, the sheer volume of research papers can be overwhelming, making it challenging to extract relevant information efficiently. To address this challenge, we introduce a Streamlit application powered by Lyzr Chatbot .

Image description

The Need for Streamlined Research Paper Analysis : With the exponential growth of research literature across various domains, researchers, students, and professionals often struggle to navigate through vast amounts of information. Traditional methods of literature review and analysis can be time-consuming and labor-intensive, leading to inefficiencies in knowledge extraction and utilization.

Introducing Lyzr Chatbot : Lyzr Chatbot is an advanced natural language processing (NLP) tool designed to streamline the process of analyzing research papers. Leveraging cutting-edge AI technologies, Lyzr Chatbot can comprehend complex queries and extract key insights from scholarly documents swiftly and accurately.

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

Setting Up Dependencies:
Before delving into the intricacies of the app’s functionality, it’s essential to ensure that all necessary dependencies are in place.


import os
import streamlit as st
from lyzr import ChatBot
from utils import utils 
Enter fullscreen mode Exit fullscreen mode

This code sets up a Streamlit application featuring a custom ChatBot module called “lyzr”, alongside utility functions from a module named “utils”. This enables the creation of an interactive chatbot interface within a Streamlit web application, enhancing user engagement and functionality.

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.

os.environ["OPENAI_API_KEY"] = st.secrets["apikey"]
Enter fullscreen mode Exit fullscreen mode
# Function to initialize ChatBot for PDF files
def initialize_chatbot(file_path):
    vector_store_params = {
        "vector_store_type": "WeaviateVectorStore",
        "url": "https:sample.network",
        "api_key": "your-api-key",
        "index_name": "Ronaldo"
    }
    chatbot = ChatBot.pdf_chat(input_files=[file_path], vector_store_params=vector_store_params)
    return chatbot
Enter fullscreen mode Exit fullscreen mode

The function *initialize_chatbot * is designed to initialize a ChatBot for processing PDF files. It takes a file_path parameter indicating the path to the PDF file. Within the function, it specifies parameters for the vector store, including the type of vector store ("WeaviateVectorStore"), the URL of the vector store service , an API key for authentication, and an index name . Then, it initializes the ChatBot for PDF processing using the specified input file and vector store parameters.

# Function to integrate ChatBot for PDF files
def pdf_chat_integration():

    # Upload PDF file
    uploaded_file = st.file_uploader("Upload PDF file", type=['pdf'])
    if uploaded_file is not None:  # Check if file is uploaded
        st.success(f"File uploaded: {uploaded_file.name} (PDF)")
        question = st.text_input("Ask a question about the document:")
        if st.button("Get Answer"):
            st.text("Processing...")
            file_path = utils.save_uploaded_file(uploaded_file)  # Save uploaded file
            if file_path is not None:  # Check if file is saved successfully
                print("File saved at:", file_path)  # Print file path for debugging
                chatbot = initialize_chatbot(file_path)
                if chatbot:
                    response = chatbot.chat(question)  # Corrected method call
                    st.text("Answer:")
                    st.write(response.response)
                else:
                    st.error("Failed to initialize chatbot. Please try again.")
    else:
        st.warning("Please upload a PDF file.")
Enter fullscreen mode Exit fullscreen mode

The pdf_chat_integration function allows users to upload a PDF file via Streamlit, input a question about the document, and request an answer from the integrated chatbot. Upon uploading a PDF file, users can enter a question and click the "Get Answer" button. The system then processes the question using the initialized chatbot and displays the response. If the chatbot initialization fails, an error message is displayed.

Create a new file utils.py :

import os
import shutil
import streamlit as st

def remove_existing_files(directory):
    for filename in os.listdir(directory):
        file_path = os.path.join(directory, filename)
        try:
            if os.path.isfile(file_path) or os.path.islink(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        except Exception as e:
            st.error(f"Error while removing existing files: {e}")

def get_files_in_directory(directory):
    # This function helps us to get the file path along with filename.
    files_list = []

    if os.path.exists(directory) and os.path.isdir(directory):
        for filename in os.listdir(directory):
            file_path = os.path.join(directory, filename)

            if os.path.isfile(file_path):
                files_list.append(file_path)

    return files_list

def save_uploaded_file(uploaded_file):
    # Function to save uploaded file
    remove_existing_files('data')

    file_path = os.path.join('data', uploaded_file.name)
    with open(file_path, "wb") as file:
        file.write(uploaded_file.read())
    return file_path
Enter fullscreen mode Exit fullscreen mode

The provided code encompasses utility functions tailored for file management within a Streamlit application.

Firstly, remove_existing_files(directory) facilitates the deletion of all files and directories within a specified directory. It traverses the directory, removing each file and recursively deleting directories.

Secondly, get_files_in_directory(directory) retrieves a list of file paths from a specified directory, verifying its existence and iterating through its contents to collect file paths.

Lastly, save_uploaded_file(uploaded_file) serves to store an uploaded file within a designated directory named "data".

It leverages remove_existing_files to clear any existing files in "data", then constructs the file path using the uploaded file's name, writes its contents to that path, and returns the resulting file path. Together, these functions streamline file handling operations within a Streamlit application,ensuring efficient management of uploaded files and directory contents.

In the age of information abundance, the ability to navigate and derive meaning from vast repositories of knowledge is paramount. With Lyzr ChatBot , the landscape of research paper analysis has been reimagined, ushering in a new era of accessibility, efficiency, and engagement. By fusing cutting-edge technology with user-centric design, Lyzr ChatBot paves the way for a future where insights are not just gleaned but experienced.

With Lyzr SDK’s , the journey from concept to creation is streamlined, empowering developers to craft captivating applications with ease. Imagine the possibilities — whether it’s the creation of a Chatbot for Research Paper or the exploration of other innovative projects. The possibilities are endless, and with Lyzr SDK’s, the future of Gen-AI applications is limited only by imagination.

Try the app out : https://research-paper-chatbot-lyzr.streamlit.app/

Source Code: https://github.com/isakshay007/Research-Paper-Chatbot

Book your demo: https://www.lyzr.ai/book-demo/

Top comments (0)