Building a Resume QA Bot with Lyzr Agent
Tags: beginners, tutorial, python, ai
Published: 2024-03-13 08:02:05

In this article, we'll walk through building a Resume QA (Question-Answering) Bot using Streamlit and Lyzr SDK's. This bot allows users to upload a resume in PDF format, ask questions about the resume, and receive answers in real-time.

> In case If you don't want to read it all
{% embed https://youtu.be/WLzH5vnG3kQ?si=5Y1EojvXYwrDrynY %}


## Introduction

With the rise of AI-powered tools, automating tasks like resume analysis has become more accessible. By leveraging Lyzr SDK's which gave us functionality of RAG, and Streamlit for the frontend, we can create an interactive web application for resume analysis in not time.

## Lyzr

Lyzr provides an agentic way to build Gen-AI applications with very little effort and in less time.
For more details: [Lyzr](https://www.lyzr.ai/)

[**Let's Build Together**](https://discord.gg/KnuSUbhbkh)

**Have a look!**
{% embed https://docs.lyzr.ai/lyzr-sdk/opensource/ %}


## Setting Up the Environment and Installing Dependencies 

We'll start by setting up our development environment. Install Streamlit, Lyzr, openai and pdfminer.six library.

```bash
python3 -m venv venv
source venv/bin/activate
pip install streamlit lyzr openai pdfminer.six
```

## Building the Application

Create on script called as _**QAbotapp.py**_

```python
api_key = st.sidebar.text_input("API Key", type="password")
if api_key:
    os.environ["OPENAI_API_KEY"] = api_key
else:
    st.sidebar.warning("Please enter your API key to proceed.")
```

This code creates a text input in the Streamlit sidebar for users to input their OpenAI API key. If provided, it sets the API key in the environment variables.

```python
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}")
```

This function iterates through files in a directory and removes them.

```python
data_directory = "data"
os.makedirs(data_directory, exist_ok=True)
remove_existing_files(data_directory)
```

Sets up a directory named "data" for storing uploaded files. If it doesn't exist, it creates one. Then, it removes any existing files in this directory.

```python
uploaded_file = st.file_uploader("Choose PDF file", type=["pdf"])

if uploaded_file is not None:
    file_path = os.path.join(data_directory, uploaded_file.name)
    with open(file_path, "wb") as file:
        file.write(uploaded_file.getvalue())
    
    
    st.success(f"File successfully saved")
```

This creates a file uploader widget in the Streamlit app, allowing users to upload PDF files. Once the file has been uploaded it will be save inside the **data** directory.

```python
def get_files_in_directory(directory="data"):
    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
```

This function retrieves file paths in a specified directory.

## Implementation of CV QABot along with RAG

```python
def rag_implementation():
    # This function will implement RAG Lyzr QA bot
    path = get_files_in_directory()
    path = path[0]

    rag = QABot.pdf_qa(
        input_files=[str(path)],
        llm_params={"model": "gpt-3.5-turbo"},
  
    )

    return rag
```

The function rag_implementation is designed to set up and return an instance of the Lyzr QA bot using the RAG (Retrieval-Augmented Generation) approach.

It calls the previously defined **_get_files_in_directory _** function to get a list of file paths in the “data” directory. 

```python
if uploaded_file is not None:
    question = st.text_input("Ask a question about the resume:")

    if st.button("Get Answer"):
        rag = rag_implementation()
        response = rag.query(question)
        st.markdown(f"""{response.response}""")

```

Allows users to input questions about the resume and retrieves answers using the Lyzr QABot when the "Get Answer" button is clicked.


## Conclusion

Building a Resume QA Bot with Streamlit and Lyzr SDK's allows for quick and efficient resume analysis. Users can upload their resumes, ask questions, and receive instant responses, for fine tune their resumes to specific job role.

Clone the repository using this app and do some permutation and combination with this resume QABot as well as with the Lyzr SDK.

For more information explore the website: [Lyzr](https://www.lyzr.ai/)

[Resume QA Bot — Github](https://github.com/PrajjwalLyzr/CV-QnA-LyzrBot/tree/main)