Building a Travel Advisor App with Lyzr and Streamlit
In today’s fast-paced world, planning a vacation can be a daunting task. From choosing the right destination to arranging accommodations and activities, the process can often feel overwhelming. However, with advancements in technology, planning your dream vacation has never been easier. In this blog post, we’ll explore how to create a Travel Advisor app using Lyzr and Streamlit, two powerful tools that can streamline the vacation planning process.
Why LYZR?
Lyzr stands out as the most user-friendly framework for swiftly constructing and deploying Generative AI applications. Embracing an ‘agentic’ approach, Lyzr simplifies the process compared to Langchain’s function and chain methodology and DSPy’s more programmatic approach. Unlike its counterparts, Lyzr’s SDKs don’t demand a profound understanding of underlying complexities. With just a few lines of code, users can swiftly craft their GenAI applications, streamlining the development process significantly.
Setting Up the Environment
Imports:
pip install streamlit lyzr openai-python
import os
import streamlit as st
from lyzr import QABot
import shutil
from PIL import Image
os
: This module provides functions for interacting with the operating system, like creating directories and deleting files.streamlit as st
: This imports the Streamlit library and assigns it the aliasst
. Streamlit is a framework for creating web apps in Python.from lyzr import QABot
: This imports theQABot
class from thelyzr
library. This class is likely used for processing PDFs and answering questions about their content.shutil
: This module provides functions for manipulating file systems, like deleting directories and their contents.from PIL import Image
: This imports theImage
class from the Pillow library (also known as PIL). This class is used for working with images.
File Processing:
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:
print(e)
data_directory = "data"
os.makedirs(data_directory, exist_ok=True)
remove_existing_files(data_directory)
uploaded_file = st.sidebar.file_uploader("Choose PDF file", type=["pdf"])
if uploaded_file is not None:
# Save the uploaded PDF file to the data directory
file_path = os.path.join(data_directory, uploaded_file.name)
with open(file_path, "wb") as file:
file.write(uploaded_file.getvalue())
# Display the path of the stored file
st.success(f"File successfully saved")
def get_all_files(directory):
# List to store all file paths
file_paths = []
# Walk through the directory tree
for root, dirs, files in os.walk(data_directory):
for file in files:
# Join the root path with the file name to get the absolute path
file_path = os.path.join(root, file)
# Append the file path to the list
file_paths.append(file_path)
return file_paths
def remove_existing_files(directory)
: This defines a function that takes a directory path as input.- The function iterates through all files and subdirectories within the provided directory.
- It attempts to delete each file or directory using
os.unlink
orshutil.rmtree
depending on its type. data_directory = "data"
: This variable assigns the string "data" to the variabledata_directory
. This directory will likely store uploaded PDFs.def get_all_files(directory)
: This defines a function that takes a directory path as input.- It initializes an empty list
file_paths
to store file paths. - The function uses
os.walk
to iterate through all directories and files within the specified directory. - For each file encountered, its absolute path is constructed and appended to the
file_paths
list. - Finally, the function returns the list containing all file paths within the directory.
Creating QABot Function:
def book_qabot(filepath):
with st.spinner("Generating Embeddings...."):
qa_bot = QABot.pdf_qa(
input_files=[file_path],
)
return qa_bot
ef book_qabot(filepath)
: This defines a function that takes a file path as input.- It uses
st.spinner()
to display a loading indicator with the message "Generating Embeddings...". - It creates a
QABot
instance using thepdf_qa
method, likely initializing it for processing the PDF at the provided filepath. - The function returns the created
QABot
instance.
Processing User Question:
paths = get_all_files(data_directory)
question = st.text_input("What are you planning?")
if st.button("Plan"):
for path in paths:
qa = book_qabot(path)
with st.spinner("Retrieving Answer...."):
response = qa.query(question)
st.markdown(response.response)
for path in paths
: This iterates through the list of file paths (paths
). In theory, this should only have one path, the uploaded PDF.qa = book_qabot(path)
: This calls thebook_qabot
function for each file path, creating aQABot
instance for processing the PDF.with st.spinner("Retrieving Answer...."):
: This displays a loading indicator with the message "Retrieving Answer..." within the indented block.response = qa.query(question)
: This calls thequery
method on theqa
(QABot) instance, passing the user's question (question
) as input. This likely retrieves the answer to the question based on the processed PDF content.st.markdown(response.response)
: This displays the retrieved answer (response.response
) as formatted markdown text within the app.
In this blog post, we’ve demonstrated how to build a Travel Advisor app using Lyzr and Streamlit. By harnessing the power of AI and interactive web development tools, we can simplify the vacation planning process and provide users with personalized recommendations tailored to their preferences. Whether you’re a seasoned traveler or a first-time vacationer, the Travel Advisor app offers a convenient and efficient way to plan your next adventure.
try it now: https://lyzr-planning-genius.streamlit.app/
For more information explore the website: Lyzr