Simplify Restaurant Reservations with Lyzr.ai's Chatbot-Powered App
Tags: ai, lyzr, beginners, python
Published: 2024-05-01 17:29:29

In the bustling world of restaurants, managing reservations efficiently is crucial for providing excellent customer service. To simplify this process, the Restaurant-Reservations app by [Lyzr.ai](https://www.lyzr.ai/) offers an innovative solution powered by chatbot technology. This Streamlit-based application allows users to interact with a chatbot to handle restaurant reservations seamlessly. Let's explore how the app works and how it can revolutionize the way restaurants manage their reservations.

## Introducing the Restaurant-Reservations App

The Restaurant-Reservations app leverages Lyzr's agent-centric approach to rapidly develop Large Language Model (LLM) applications with minimal code and time investment. Whether you're familiar with the GenAI stack or not, Lyzr empowers you to build AI applications effortlessly, making it the go-to solution for constructing GenAI apps without requiring an in-depth understanding of Generative AI.

## Setting up the Project

Getting started with the Restaurant-Reservations app is easy. Follow these steps to set up the project:

**Clone the App**: Clone the Restaurant-Reservation app repository from GitHub.

   ```bash
   git clone https://github.com/PrajjwalLyzr/Restaurant-Reservation
   ```

**Create and Activate Virtual Environment**: Set up a virtual environment and activate it.

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

**Create an Environment Variable**: Create a `.env` file and add your OpenAI API key.

   ```python
   OPENAI_API_KEY = "Paste your openai api key here"
   ```

   You can obtain an OpenAI API key by visiting the [OpenAI website](https://openai.com).

**Install Dependencies**: Install the required dependencies.

   ```bash
   pip install lyzr streamlit docx2txt audio-recorder-streamlit
   ```


## Utils Module for Common Functions

The `utils.py` file in the project serves as a utility module containing common functions utilized throughout the application. It includes functions for writing to and reading from a file to store chat history, as well as providing a system prompt for the chatbot.

```python
import streamlit as st


def write_to_file(user_input, chatbot_response, file_path):
    with open(file_path, 'a') as file:
        file.write(f"User: {user_input}\n")
        file.write(f"Chatbot: {chatbot_response}\n")
        file.write("\n")


def read_chat_history(file_path):
    with open(file_path, 'r') as file:
        return file.readlines()


def system_prompt():
    sys_prompt = """
        Your name is Eva.
        You are the Manager at Gourmet Delight restaurant.
        You represent the Gourmet Delight restaurant.
        Output Length: Keep your answers less than 30 words so that it sounds more natural chat.
        Your Persona: Eva is a middle-aged woman born in the US. She is friendly, with a great sense of humor that is understated and professional, but she understands that making people comfortable includes using humor.
    """


    return str(sys_prompt)
```

## Entry Point for the Application (`app.py`)

The `app.py` file defines the main functionality of the Restaurant-Reservations app using Streamlit. It sets up a temporary directory for storing chat history, configures the OpenAI API key, and defines functions for handling chatbot and voice interactions.

```python
from utils import utils
import streamlit as st
from lyzr import ChatBot, VoiceBot
import os
from PIL import Image
from pathlib import Path
from dotenv import load_dotenv; load_dotenv()
from audio_recorder_streamlit import audio_recorder


tempDir = 'tempDir'
if not os.path.exists(tempDir):
    os.makedirs(tempDir)


file = 'responses.txt'


# setting up the OpenAI API Key
os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')


#  ChatBot
def chatagent():
    chatbot = ChatBot.pdf_chat(
        input_files=[Path('./data/restaurant data.pdf')], system_prompt=utils.system_prompt(),
    )
    return chatbot


# Voice bot
def voiceagent(audio_bytes):
    voice = VoiceBot()
    if audio_bytes:
        st.audio(audio_bytes, format="audio/wav")
        # Save the recorded audio for transcription
        with open('tempDir/output.wav', 'wb') as f:
            f.write(audio_bytes)
        audiopath = 'tempDir/output.wav'
        if audiopath:
            user_input = voice.transcribe(audiofilepath='tempDir/output.wav')
            # st.write(user_input)
               
        chatbot = chatagent()
        response = chatbot.chat(user_input)
        voice.text_to_speech(response.response)
        utils.write_to_file(user_input=user_input, chatbot_response=response.response, file_path=file)
        # st.text(response.response)
        tts_audio_file = 'tts_output.mp3'
        if os.path.isfile(tts_audio_file):
            st.audio(tts_audio_file, format='audio/mp3', start_time=0)
   
    else:
        st.warning('Record your audio to start the conversation')


def delete_conversation():
        os.remove('tts_output.mp3')
        os.remove(file)
        st.warning('Record your audio to start the conversation')
```

## Executing the Restaurant-Reservations Application

The Restaurant-Reservations app simplifies the process of handling restaurant reservations

- **Chatbot Interaction**: Users can interact with the chatbot to make reservations by speaking.
  
- **Voice Interaction**: The app supports voice input for a more convenient user experience.
  
- **Conversation History**: Users can view and delete conversation history.


```python
if __name__ == "__main__":
    audio_bytes = audio_recorder()
    voiceagent(audio_bytes)

    if os.path.exists(file):
        if st.button('Delete Conversation'):
            delete_conversation()

    if os.path.exists(file):
        chat_history = utils.read_chat_history(file_path=file)
        st.sidebar.subheader("Conversation History")
        for line in chat_history:
            st.sidebar.write(line.strip())
```


The Restaurant-Reservations app by Lyzr.ai offers a streamlined solution for managing restaurant reservations through chatbot technology. With its user-friendly interface and powerful features, it revolutionizes the way restaurants handle reservations. Try it out today and elevate your restaurant's reservation management!

## References

- [Lyzr Website](https://www.lyzr.ai/)
- [Book a 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)
- [Restaurant-Reservation GitHub Repository](https://github.com/PrajjwalLyzr/Restaurant-Reservation)