Streamline Your Literature Review Process with Lyzr's AI-Powered Application
Tags: ai, tutorial, python, beginners
Published: 2024-03-25 12:18:29

In the fast-paced world of academia and research, staying updated with the latest findings and insights is crucial. However, the process of reviewing numerous research papers to extract relevant information can be time-consuming and laborious.

Literature Review by [Lyzr ](lyzr.ai) is a user-friendly web application aimed at researchers, students, and anyone seeking to efficiently review academic literature. Leveraging advanced AI capabilities, Lyzr offers a seamless experience for extracting key insights from research papers with ease and precision.


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

---


In this article, we'll walk you through the process of building this GenAI application with Lyzr's QABot agent. We'll cover everything from setting up the project to implementing key functionalities using low code and minimal effort.

## Setting up the Project
Clone the "**[Literature Review Assistant](https://github.com/PrajjwalLyzr/Literature-Review-Assistant/tree/main)**" app repository.

**Set up a virtual environment**

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

```
**Create an environment file named .env and add your OpenAI API key**

```python
OPENAI_API_KEY = "Your_OpenAI_API_Key"

```
**Install the required dependencies**

```bash
pip install lyzr streamlit pdfminer.six

```

## Project Structure

```scss
Literature-Review
│
│
├── utils/
│ ├── __init__.py
│ └── utils.py
│
├── app.py
│
├── README.md
│
├── .env
│
├── .gitignore
│
└── requirements.txt
```

## Setup the Utils file for common functions

utils.py file in the project serves as a utility module containing common functions that are utilized throughout the application.

```python
import streamlit as st
import os
import shutil



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 help 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(directory, uploaded_file):
    remove_existing_files(directory=directory)
    file_path = os.path.join(directory, uploaded_file.name)
    with open(file_path, "wb") as file:
        file.write(uploaded_file.read())
    st.success("File uploaded successfully")
```
**remove_existing_files**: Removes existing files and directories within a specified directory.
**get_files_in_directory**: Retrieves a list of file paths within a specified directory.
**save_uploaded_file**: Saves an uploaded file to a specified directory.

## Guidance for QABot Agent
The QABot agent facilitates the review process by generating prompts for summarizing research papers. Functions like reviewer and get_response handle interactions with the QABot agent and present the results to the user.

```python
def reviewer(agent):
    results = {}
    prompts = {
        "Summary" : "Write a 2 lines of brief summary about this research paper in simplest manner",
        "Research Objectives":"What is the main research question the paper investigates? What are the specific objectives or hypotheses outlined in the paper? Make a 4-5 line of response",
        "Methodology":"What research methodology was used in the study (e.g., survey, experiment, case study)? What is the population or sample size used in the research? How was the data collected and analyzed? Use 3-5 bullet points to show the response",
        "Findings and Results":"What are the key findings or results presented in the paper? Are there any specific statistics, figures, or tables that highlight the results? How do the findings relate to the research question and objectives? Use 3-5 bullet points to show the response",
        "Discussion and Conclusions":"What are the main conclusions drawn by the authors based on the findings? What are the limitations of the study or areas for future research? How do the paper's conclusions contribute to the existing body of knowledge in the field? Make a 4-5 line of response",
        "Related Resarch":"Write down 5 research topic along with their titles based on in this research paper",
        "Prototype":"User wants to write an extended research paper on the provided research paper, so what are the key points I should take care of and how can I proceed with this?"
    }

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

    return results


def get_response(response:dict):
    for heading, response in response.items():
        st.subheader(heading)
        st.write(response)
        st.markdown("---")  
```

## Initiating the Application
The app.py file serves as the entry point for the application. It prompts users to upload PDF files for review and initiates the review process upon user interaction

```python

import os
from PIL import Image
from pathlib import Path
from utils import utils
import streamlit as st
from urllib.parse import urlparse, parse_qs
from dotenv import load_dotenv; load_dotenv()
from lyzr import QABot


# Literature Review Application
   
# replace this with your openai api key or create an environment variable for storing the key.
os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY')


data = "data"
os.makedirs(data, exist_ok=True)


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


    return file

```
A directory named “data” is created using os.makedirs() to store the uploaded research papers. If the directory already exists, it will not be recreated due to the exist_ok=True parameter.

file_checker() is designed to check for the presence of files in the specified directory. This function is useful for verifying if there are any files present in the directory, which is helpful for subsequent operations or checks within the application.

## Literature Review by QABot Agent

```python
def literature_review():
    # "This function will implement the Lyzr's QA agent to review the Research Paper"
    path = utils.get_files_in_directory(data)
    path = path[0]
   
    reviewer = QABot.pdf_qa(
        input_files=[Path(path)]
    )
   
    return reviewer
```
Lyzr’s QA agent using the pdf_qa() method from the QABot class, passing the path of the research paper as an input file and it returns the initialized QA agent, which is ready to review the research paper.

## Initiate the Application
This script serves as the main entry point for a Streamlit application designed to facilitate the review of research papers. It prompts users to upload PDF files, which are then processed by Lyzr’s QA agent for analysis. If a file is uploaded, the application displays a “Review” button, triggering the QA analysis upon clicking.

```python
if __name__ == "__main__":
    research_paper = st.file_uploader("Choose Research paper", 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 research paper in pdf')
```

## Conclusion

Literature Review by Lyzr revolutionizes the literature review process, empowering researchers to extract key insights from research papers efficiently. By combining AI capabilities with a user-friendly interface, Lyzr simplifies the task of summarizing and analyzing academic literature.

## References
For further exploration and engagement, refer to Lyzr’s website, book a demo, or join the community channels on Discord and Slack.

**Literature Review** : [GitHub](https://github.com/PrajjwalLyzr/Literature-Review-Assistant/tree/main)

**Lyzr Website**: [https://www.lyzr.ai/](https://www.lyzr.ai/)

**Book a Demo**: [https://www.lyzr.ai/book-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)