DEV Community

harshit-lyzr
harshit-lyzr

Posted on

Elevating Dining Experiences with Lyzr's SDK and Streamlit Voice Menu Chatbot

Image descriptionVoice-enabled chatbots are revolutionizing the way users interact with applications. By leveraging the power of natural language processing (NLP) and Lyzr SDK’s speech synthesis, we can create conversational interfaces that are intuitive and engaging. In this tutorial, we’ll explore how to integrate Lyzr’s SDK with Streamlit to build a voice menu chatbot tailored for educational purposes.

Prerequisites
Before we begin, ensure you have the following installed:

  • Python (preferably the latest version)
  • Streamlit
  • Lyzr SDK
  • OpenAI API key
  • Any necessary dependencies mentioned in the code

Setting Up the Environment

First, let’s set up our development environment by importing the required libraries and configuring Streamlit settings. Importing Lyzr SDK’s Chatbot and VoiceBot.We’ll also load our OpenAI API key using dotenv for secure access to natural language processing capabilities.

import streamlit as st
import tempfile
import os
import time
from lyzr import ChatBot,VoiceBot
import openai
from dotenv import load_dotenv

load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
Enter fullscreen mode Exit fullscreen mode

Creating the Streamlit App

st.title("Lyzr Voice Menu Chatbot")
st.markdown("### Welcome to the Lyzr Voice Menu Chatbot!")
st.markdown("Upload Your Educational PDFs and Ask your queries.")
st.markdown("#### 📄 Upload a Pdf")
Enter fullscreen mode Exit fullscreen mode

Uploading PDF Files and Use Lyzr SDK for pdf_chat

uploaded_files = st.file_uploader(
    "Choose PDF files", type=["pdf"], accept_multiple_files=True
)
pdf_file_paths = []

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.")
Enter fullscreen mode Exit fullscreen mode

This code snippet uploads user-selected PDF files, saves them temporarily, updates session state with file paths, generates a unique index name, and initializes a chatbot using Lyzr’s SDK, enabling interaction with the uploaded educational content.

Using Lyzr SDK’s VoiceBot

vb = VoiceBot()
Enter fullscreen mode Exit fullscreen mode
if "messages" not in st.session_state:
    st.session_state.messages = []


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

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"):
            if os.path.exists('tts_output.mp3'):
                os.remove('tts_output.mp3')
            response = st.session_state["chatbot"].chat(prompt)
            chat_response = response.response
            audio = vb.text_to_speech(chat_response)
            # response = st.write(chat_response)
            st.audio('tts_output.mp3', format='audio/mp3')
        st.session_state.messages.append(
            {"role": "assistant", "content": chat_response}
        )
else:
    st.warning("Please upload PDF files to continue.")
Enter fullscreen mode Exit fullscreen mode

This code checks if there are existing chat messages in the session state and displays them. If a chatbot exists in the session state, it prompts the user for input, sends the input to the chatbot, retrieves the response, converts it to speech with Lyzr SDK’s Voicebot, and displays it as an audio message. If no chatbot exists, it displays a warning message prompting the user to upload PDF files.

we’ve demonstrated how to build a voice menu chatbot using Lyzr’s SDK and Streamlit. By combining the power of NLP, educational content processing, and speech synthesis, we’ve created a versatile tool for assisting students with their educational queries.

Ready to build your own voice-enabled chatbot? Dive into the code and start innovating!

For more information explore the website: Lyzr

Voice Chatbot for Restaurant Menu — Github

Top comments (0)