Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
update chat.html
Browse files- app.py +7 -1
- templates/chat.html +3 -135
app.py
CHANGED
@@ -707,7 +707,7 @@ def respond_to_user(image, message):
|
|
707 |
from PIL import Image
|
708 |
import numpy as np
|
709 |
|
710 |
-
@app.route("/", methods=["POST"])
|
711 |
def get_answer():
|
712 |
global CURR_CONTEXT
|
713 |
global CURR_SESSION_KEY
|
@@ -760,6 +760,12 @@ def get_answer():
|
|
760 |
return jsonify(response)
|
761 |
|
762 |
|
|
|
|
|
|
|
|
|
|
|
|
|
763 |
|
764 |
# Main function to run the app
|
765 |
if __name__ == '__main__':
|
|
|
707 |
from PIL import Image
|
708 |
import numpy as np
|
709 |
|
710 |
+
@app.route("/get-answer", methods=["POST"])
|
711 |
def get_answer():
|
712 |
global CURR_CONTEXT
|
713 |
global CURR_SESSION_KEY
|
|
|
760 |
return jsonify(response)
|
761 |
|
762 |
|
763 |
+
# Home route
|
764 |
+
@app.route("/")
|
765 |
+
def index_view():
|
766 |
+
return render_template('chat.html')
|
767 |
+
|
768 |
+
|
769 |
|
770 |
# Main function to run the app
|
771 |
if __name__ == '__main__':
|
templates/chat.html
CHANGED
@@ -10,146 +10,14 @@
|
|
10 |
</head>
|
11 |
|
12 |
<body class="bg-gray-100 p-5 pb-50 h-screen flex flex-col">
|
13 |
-
<div class="chat-container max-w-5xl w-full h-full
|
14 |
-
<
|
15 |
-
<
|
16 |
-
<input type="file" id="image-input"
|
17 |
-
class="block w-96 px-4 py-2 text-sm text-gray-500 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 focus:outline-none mb-2"
|
18 |
-
accept="image/*">
|
19 |
-
<div class="input-container flex space-x-2">
|
20 |
-
<input type="text" id="chat-input"
|
21 |
-
class="block w-full px-4 py-4 text-sm text-gray-700 bg-gray-50 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500"
|
22 |
-
placeholder="Type your message here...">
|
23 |
-
|
24 |
-
<button id="send-button"
|
25 |
-
class="bg-blue-600 text-white px-10 py-2 rounded-lg hover:bg-blue-700 focus:ring-4 focus:ring-blue-300">
|
26 |
-
Send
|
27 |
-
</button>
|
28 |
-
</div>
|
29 |
-
</div>
|
30 |
</div>
|
31 |
-
|
32 |
-
<script src="https://cdn.socket.io/4.5.0/socket.io.min.js"></script>
|
33 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js"
|
34 |
integrity="sha512-LhccdVNGe2QMEfI3x4DVV3ckMRe36TfydKss6mJpdHjNFiV07dFpS2xzeZedptKZrwxfICJpez09iNioiSZ3hA=="
|
35 |
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
36 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.5/flowbite.min.js"></script>
|
37 |
-
|
38 |
-
<script>
|
39 |
-
const socket = io.connect(document.baseURI);
|
40 |
-
const chatBox = document.getElementById('chat-box');
|
41 |
-
const chatInput = document.getElementById('chat-input');
|
42 |
-
const imageInput = document.getElementById('image-input');
|
43 |
-
const sendButton = document.getElementById('send-button');
|
44 |
-
const converter = new showdown.Converter();
|
45 |
-
let response = "";
|
46 |
-
// Function to add a loader element
|
47 |
-
function addLoader() {
|
48 |
-
const loaderEle = document.createElement('div');
|
49 |
-
loaderEle.classList.add('dot-loader');
|
50 |
-
loaderEle.innerHTML = `
|
51 |
-
<div></div>
|
52 |
-
<div></div>
|
53 |
-
<div></div>
|
54 |
-
`;
|
55 |
-
chatBox.appendChild(loaderEle);
|
56 |
-
}
|
57 |
-
// Function to append a message to the chat box
|
58 |
-
function appendMessage(message, sender) {
|
59 |
-
if (sender === 'bot') {
|
60 |
-
response += message;
|
61 |
-
const loaderEle = chatBox.lastElementChild;
|
62 |
-
if (loaderEle && loaderEle.classList.contains('dot-loader')) {
|
63 |
-
chatBox.removeChild(loaderEle);
|
64 |
-
const messageElement = document.createElement('div');
|
65 |
-
messageElement.classList.add('chat-message', sender);
|
66 |
-
messageElement.innerHTML = `<span>${response}</span>`;
|
67 |
-
chatBox.append(messageElement);
|
68 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
69 |
-
} else {
|
70 |
-
const lastMessageEle = chatBox.lastElementChild;
|
71 |
-
if (lastMessageEle) {
|
72 |
-
lastMessageEle.innerHTML = response;
|
73 |
-
}
|
74 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
75 |
-
}
|
76 |
-
} else {
|
77 |
-
const messageElement = document.createElement('div');
|
78 |
-
messageElement.classList.add('chat-message', sender);
|
79 |
-
messageElement.innerHTML = `<span>${message}</span>`;
|
80 |
-
chatBox.append(messageElement);
|
81 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
82 |
-
setTimeout(addLoader, 500);
|
83 |
-
}
|
84 |
-
}
|
85 |
-
// Function to handle image as Base64 and send with message
|
86 |
-
function readImageAsBase64(imageFile) {
|
87 |
-
return new Promise((resolve, reject) => {
|
88 |
-
const reader = new FileReader();
|
89 |
-
reader.onload = () => resolve(reader.result);
|
90 |
-
reader.onerror = reject;
|
91 |
-
reader.readAsDataURL(imageFile);
|
92 |
-
});
|
93 |
-
}
|
94 |
-
// Function to handle sending the message and image
|
95 |
-
sendButton.addEventListener('click', async () => {
|
96 |
-
const message = chatInput.value.trim();
|
97 |
-
const imageFile = imageInput.files[0];
|
98 |
-
// Prepare the message object
|
99 |
-
let messageData = {
|
100 |
-
message: message,
|
101 |
-
session_id: 'abc123'
|
102 |
-
};
|
103 |
-
// If there is an image, read it as base64 and add to messageData
|
104 |
-
if (imageFile) {
|
105 |
-
try {
|
106 |
-
const imageBase64 = await readImageAsBase64(imageFile);
|
107 |
-
messageData.image = imageBase64;
|
108 |
-
} catch (error) {
|
109 |
-
console.error('Error reading image file:', error);
|
110 |
-
}
|
111 |
-
}
|
112 |
-
if (message || imageFile) {
|
113 |
-
// appendMessage(imageFile || 'Image sent', 'user');
|
114 |
-
appendMessage(message || 'Image sent', 'user');
|
115 |
-
|
116 |
-
// Send message and image (if available) to the server via socket
|
117 |
-
socket.emit('message', messageData);
|
118 |
-
// Clear inputs
|
119 |
-
chatInput.value = '';
|
120 |
-
imageInput.value = ''; // Clear the image input
|
121 |
-
response = "";
|
122 |
-
} else {
|
123 |
-
console.error("Message or image cannot be empty.");
|
124 |
-
}
|
125 |
-
});
|
126 |
-
// Event listener for 'Enter' key press in the chat input
|
127 |
-
chatInput.addEventListener('keypress', (e) => {
|
128 |
-
if (e.key === 'Enter') {
|
129 |
-
sendButton.click();
|
130 |
-
}
|
131 |
-
});
|
132 |
-
// Handle incoming responses from the server
|
133 |
-
socket.on('response', (data) => {
|
134 |
-
if (data && typeof data === 'string') {
|
135 |
-
appendMessage(data, 'bot');
|
136 |
-
} else {
|
137 |
-
console.error("Invalid response format received from the server.");
|
138 |
-
}
|
139 |
-
});
|
140 |
-
// Handle connection errors
|
141 |
-
socket.on('connect_error', (error) => {
|
142 |
-
appendMessage("Sorry, there was a problem connecting to the server. Please try again later.", 'bot');
|
143 |
-
});
|
144 |
-
// Handle connection
|
145 |
-
socket.on('connect', (reason) => {
|
146 |
-
console.log("Connected to server.");
|
147 |
-
});
|
148 |
-
// Handle disconnection
|
149 |
-
socket.on('disconnect', (reason) => {
|
150 |
-
appendMessage("You have been disconnected from the server. Please refresh the page to reconnect.", 'bot');
|
151 |
-
});
|
152 |
-
</script>
|
153 |
</body>
|
154 |
|
155 |
</html>
|
|
|
10 |
</head>
|
11 |
|
12 |
<body class="bg-gray-100 p-5 pb-50 h-screen flex flex-col">
|
13 |
+
<div class="chat-container max-w-5xl w-full h-full flex items-center justify-center mx-auto bg-white shadow-lg rounded-lg p-4">
|
14 |
+
<p>Nutrigenics chatbot</p>
|
15 |
+
<a class="btn py-3 px-6" href="https://www.nutrigenics.care">Visit Nutrigenics</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
</div>
|
|
|
|
|
17 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js"
|
18 |
integrity="sha512-LhccdVNGe2QMEfI3x4DVV3ckMRe36TfydKss6mJpdHjNFiV07dFpS2xzeZedptKZrwxfICJpez09iNioiSZ3hA=="
|
19 |
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
20 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.5/flowbite.min.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
</body>
|
22 |
|
23 |
</html>
|