Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

PBR VISVODAYA INSTITUTE OF

TECHNOLOGY & SCIENCE

A
MINI PROJECT
ON
RESTAURANT CHATBOT
AIM: To Develop efficiently guide customers through menu choices,
reservations, and inquiries, enhancing their dining experience with seamless
assistance(chatbot).

DESCRIPTION:
This is a basic Flask application for a restaurant chatbot. Let's break down the
components and logic:

1. Flask Setup:
- Import Flask, render_template, request, and jsonify.
- Import random for generating random responses.
- Initialize a Flask application.

2. Data Setup:
- A dictionary named `data` is created to hold different types of responses based
on different user inputs or prompts.
- Each key in the `data` dictionary corresponds to a type of response, such as
greetings, menu display, specialty, etc.
- Each value associated with a key is a list of possible responses for that particular
type of interaction.

3. Flask Routes:
- `@app.route('/')`: This route handles the root URL of the application. It renders
the `index.html` template, which presumably contains the user interface for
interacting with the chatbot.
- `@app.route('/get_response', methods=['post'])`: This route is responsible for
receiving user input from the frontend via a POST request.

4. Main Logic:
- When a POST request is made to the `/get_response` endpoint, the user's input
is retrieved from the form data.
- The input is converted to lowercase for easier comparison.
- A loop iterates over each key-value pair in the `data` dictionary.
- For each key, it checks if any part of the key matches any part of the user input.
If there's a match, it sets `inn` (short for 'in') to True.
- If a match is found, a random response is selected from the corresponding list of
responses.
- If no match is found, it sets `inn` to False.
- If `inn` is True, it returns the selected response as a JSON object.
- If `inn` is False (meaning no matching key was found), it returns a default
response indicating that it couldn't understand the user's input.

5. Running the Application:


- The `if __name__ == '__main__':` block ensures that the Flask app only runs
when the script is executed directly, not when it's imported as a module.
- The `app.run()` function starts the Flask development server on port 5000 with
debug mode enabled.

6.Matching User Input:


The logic for matching user input with the keys in the data dictionary is implemented
using nested loops and conditional checks.
The outer loop iterates over each key-value pair in the data dictionary.
Within the outer loop, there's an inner loop that splits each key into individual words
or phrases (if the key contains multiple words separated by '|').
For each key, it checks if any part of the key matches any part of the user input. If a
match is found, it sets a flag (inn) to True, indicating that a matching response was
found.
If no match is found for a particular key, the flag is set to False, and the inner loop
breaks.
7.Response Selection:
If a match is found (inn is True), a random response is selected from the list of
responses associated with the matched key.
This random selection adds an element of unpredictability to the chatbot's replies,
making interactions more engaging for users.
The selected response is then returned to the frontend as a JSON object.
8.Error Handling:
If no matching response is found for the user's input, the chatbot returns a default
response indicating that it couldn't understand the input.
This ensures that the chatbot provides some form of feedback to the user even when
it encounters input that it doesn't recognize.

SOURCE-CODE:
FRONTED-MODULE:
1.HTML-FILE
2.CSS-FILE
3.JAVASCRIPT-FILE

HTML FILE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="stylesheet" href="style.css">
<title>Chatbot Interface</title>
</head>
<body>
<div class="meta">

<h1>RESTURANT CHATBOT</h1>
</div>
<div class="chat-container">
<div class="chat-box" id="chat-box"></div>
<div class="msg-box">
<input type="text" id="user-input" placeholder="Type your query...">
<button onclick="getUserInput()">Send</button>
</div>
</div>
<script src="{{ url_for('static', filename='app.js') }}"></script>
</body>
</html>

CSS-FILE:
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background:rgb(139, 0, 0);
}

.meta {
display:flex;
max-width: 600px;
margin:auto;
flex-direction: column;
gap:16px;
padding:16px;
border-radius: 10px;
justify-content: center;
align-items: center;
background:#ffffff;
box-shadow: 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22);
}

.meta img {
width:250px;
}

.chat-container {
max-width: 600px;
margin: 32px auto;
padding: 20px;
border-radius: 10px;
background:#ffffff;
box-shadow: 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22);
}

.chat-box {
height: 300px;
overflow-y: scroll;
background:#efefef;
border-radius: 5px;
padding: 10px;
}

.chat-box p {
padding:8px;
border-radius:8px;
}

.msg-box {
display:flex;
flex-direction: row;
flex:1;
width:100%;
gap:8px;
}

input {
width: 100%;
padding: 10px;
margin-top: 10px;
background:#efefef;
border:none;
border-radius: 8px;
}
button {
padding: 10px;
margin-top: 10px;
cursor: pointer;
border:none;
background:darkblue;
color:white;
border-radius: 8px;
}

.user {
background:white;
}

.bot {
background:#555555;
color:#ffffff;
}

::-webkit-scrollbar {
width:0;
height:0;
}
JAVASCRIPT-FILE:
function getUserInput() {
var userInput = document.getElementById("user-input").value;
appendToChat("User: " + userInput,"user");

// Make a POST request to the server to get the bot's response


fetch('/get_response', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'user_input=' + userInput,
})
.then(response => response.json())
.then(data => {
var botResponse = data.bot_response;
appendToChat("Tony: " + botResponse,"bot");
});
}

function appendToChat(message,who) {
var chatBox = document.getElementById("chat-box");
var newMessage = document.createElement("p");
newMessage.setAttribute('class',who)
newMessage.textContent = message;
chatBox.appendChild(newMessage);
document.getElementById("user-input").value = "";
}
BACKEND-MODULE:
PYTHON-FILE:

PYTHON-FILE:
from flask import Flask, render_template, request, jsonify
import random

data = {
"welcome": ["Welcome to our digital restaurant! I'm your waiter. We have a
variety of delicious options for you to choose from. Our menu includes savory
dishes, refreshing drinks, and sweet desserts. Let me know if there's anything in
particular you're in the mood for."],
"menu": ["Here's our menu: \n 1. Savory dishes \n2. Refreshing drinks \n3. Sweet
desserts"],
"speciality":["our restaruant famous for sweet desserts"],
"order": ["What do you want to order?"],
"items": ["We have a long list of items"],
"seats": ["where do you want, Corner/middle?"],
"corner": ["how many seats do you want?"],
"anywhere": ["how many seats do you want?"],
"5": ["booking done..."]
}

app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/get_response', methods=['post'])
def get_response():
user_input = request.form['user_input'].lower()
inn = False
res = ""
for keys, value in data.items():
keys = keys.split()
for key in keys:
key = key.split('|')
if any(k in user_input for k in key):
inn = True
else:
inn = False
break
if inn:
res = random.choice(value)
break
if inn:
return jsonify({'bot_response': res})
else:
return jsonify({'bot_response': "Couldn't understand!"})
return jsonify({"bot_response": "my response"})

if __name__ == '__main__':
app.run(port=5000, debug=True)
OUTPUT:

RESULT:
By implementing this restaurant chatbot, customers can seamlessly explore menu
options, make reservations, and enhance their dining experience with personalized
assistance, ultimately boosting customer satisfaction and operational efficiency.

You might also like