Chatbot that communicates with your textbooks using LYZR SDK

Harshit
3 min readMar 14, 2024

--

Imagine having a personal study buddy that can answer your questions anytime, anywhere. This blog post is your guide to building just that — an educational chatbot! We’ll be using some clever tools like Streamlit, Lyzr, and OpenAI to create a chatbot that can understand your questions about educational PDFs you upload.

Setting Up the Environment

First, let’s ensure we have the necessary libraries installed. We’ll need Streamlit, PIL (Python Imaging Library), Lyzr, OpenAI, and dotenv. If you don’t have them installed already, you can do so using pip:

pip install streamlit lyzr openai-python dotenv
from PIL import Image
import streamlit as st
import tempfile
import os
import time
from lyzr import ChatBot
import openai

Creating the Streamlit App

st.set_page_config(
page_title="Lyzr QA Bot",
layout="centered",
initial_sidebar_state="auto",
page_icon="lyzr-logo-cut.png",
)

# Hide Streamlit's default header
st.markdown(
"""
<style>
.app-header { visibility: hidden; }
.css-18e3th9 { padding-top: 0; padding-bottom: 0; }
.css-1d391kg { padding-top: 1rem; padding-right: 1rem; padding-bottom: 1rem; padding-left: 1rem; }
</style>
""",
unsafe_allow_html=True,
)

# App title and introduction
st.title("Lyzr Educational Bot")
st.markdown("### Welcome to the Lyzr Educational Bot!")
st.markdown("Upload Your Educational PDFs and Ask your queries.")

# Instruction for the users
st.markdown("#### 📄 Upload a Pdf")

Uploading PDF Files

This code which will help us to achieve our goal, means this code is responsible for creating a Chatbot for Educational Purposes for us, and as you can see few lines of code will make our Gen-AI application with less effort.

Here,We use ChatBot.pdf_chat() empowers you to build intelligent chatbots specifically designed to interact with PDF documents.

# File uploader for PDFs
uploaded_files = st.file_uploader(
"Choose PDF files", type=["pdf"], accept_multiple_files=True
)
pdf_file_paths = [] # This will store paths to the saved files

if uploaded_files:
for uploaded_file in uploaded_files:
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmpfile:
tmpfile.write(uploaded_file.getvalue())
pdf_file_paths.append(tmpfile.name)

# Update session state with new PDF file paths
st.session_state.pdf_file_paths = pdf_file_paths

# Generate a unique index name based on the current timestamp
unique_index_name = f"IndexName_{int(time.time())}"
vector_store_params = {"index_name": unique_index_name}
st.session_state["chatbot"] = ChatBot.pdf_chat(
input_files=pdf_file_paths, vector_store_params=vector_store_params
)

# Inform the user that the files have been uploaded and processed
st.success("PDFs uploaded and processed. You can now interact with the chatbot.")

Interacting with the Chatbot

if "messages" not in st.session_state:
st.session_state.messages = []

# Display previous chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])

# Chat interface
if "chatbot" in st.session_state:
if prompt := st.chat_input("What is up?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)

with st.chat_message("assistant"):
response = st.session_state["chatbot"].chat(prompt)
chat_response = response.response
response = st.write(chat_response)
st.session_state.messages.append(
{"role": "assistant", "content": chat_response}
)
else:
st.warning("Please upload PDF files to continue.")

By following these steps, you can create your Chatbot for Educational Purposes using Streamlit, Lyzr Core, and OpenAI. This chatbot can revolutionize the way educational material is accessed and understood, making learning more interactive and engaging for users. Happy coding!

For more information explore the website: Lyzr

Chatbot for Educational Purposes — Github

--

--