-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.html
298 lines (262 loc) · 13.2 KB
/
chat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mental Health ChatBot</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css')}}"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="container h-100">
<div class="d-flex justify-content-center h-100 align-items-center">
<!-- Sidebar -->
<div class="col-md-3 col-xl-3 sidebar-container">
<div class="sidebar" id="sidebar">
<ul id="chatHistory">
<!-- Chat sessions will be dynamically added here -->
</ul>
</div>
</div>
<div class="col-md-8 col-xl-9 chat">
<div class="card">
<div class="card-header msg_head">
<div class="d-flex bd-highlight">
<div class="img_cont">
<img src="https://i.ibb.co/fSNP7Rz/icons8-chatgpt-512.png" class="rounded-circle user_img">
<span class="online_icon"></span>
</div>
<div class="user_info">
<span>Mental Health ChatBot</span>
<p>Share with me! Whatever you want. I am here for you.</p>
</div>
<!-- Sidebar Toggle Button and New Chat Icon -->
<div class="ml-auto d-flex align-items-center">
<i id="newChatIcon" class="fas fa-comment-dots chat-icon"></i>
<i class="fas fa-bars action_menu_btn"></i>
</div>
</div>
</div>
<div id="messageFormeight" class="card-body msg_card_body">
<!-- Chat messages will go here -->
</div>
<div class="card-footer">
<form id="messageArea" class="input-group" enctype="multipart/form-data">
<input type="text" id="text" name="msg" placeholder="Type your message..." autocomplete="off" class="form-control type_msg" required/>
<div class="input-group-append">
<button type="button" id="uploadButton" class="input-group-text send_btn"><i class="fas fa-paperclip"></i></button>
<input type="file" id="imageUpload" name="image" accept="image/*" style="display:none;"/>
<button type="submit" id="send" class="input-group-text send_btn"><i class="fas fa-location-arrow"></i></button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
let chatSessions = [];
let currentChatIndex = -1;
$(document).ready(function() {
$("#newChatIcon").click(function() {
startNewChat();
});
$(".action_menu_btn").click(function() {
$("#sidebar").toggle(); // Toggle the visibility of the sidebar
});
$("#uploadButton").click(function() {
$("#imageUpload").click(); // Trigger the hidden file input when the upload button is clicked
});
$("#messageArea").on("submit", function(event) {
event.preventDefault();
const date = new Date();
const hour = date.getHours();
const minute = date.getMinutes();
const str_time = hour + ":" + minute;
const rawText = $("#text").val();
const image = $("#imageUpload").prop("files")[0];
if (image) {
// Handle image upload and analysis
const formData = new FormData();
formData.append("image", image);
$.ajax({
url: "/analyze-image",
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function(response) {
const mood = response.mood;
handleBotResponseBasedOnMood(mood, str_time);
},
error: function() {
console.log("Image upload failed.");
}
});
// Clear the file input
$("#imageUpload").val("");
} else if (rawText) {
handleUserMessage(rawText, str_time);
}
});
$(document).on("click", ".chat-session", function() {
const index = $(this).data("index");
loadChatSession(index);
});
$(document).on("click", ".delete-session", function(event) {
event.stopPropagation();
const index = $(this).closest('.chat-session').data('index');
deleteSession(index);
});
});
function handleUserMessage(rawText, str_time) {
if (currentChatIndex >= 0) {
if (chatSessions[currentChatIndex].messages.length === 0) {
chatSessions[currentChatIndex].title = rawText;
updateChatHistory();
}
chatSessions[currentChatIndex].messages.push({
text: rawText,
time: str_time,
sender: 'user',
edited: false
});
var userHtml = `
<div class="message" data-index="${chatSessions[currentChatIndex].messages.length - 1}">
<div class="msg_cotainer_send_container">
<div class="msg_cotainer_send">${rawText}<span class="msg_time_send">${str_time}</span></div>
<div class="edit_btn" onclick="handleEditMessage(${chatSessions[currentChatIndex].messages.length - 1})"><i class="fas fa-edit"></i></div>
</div>
</div>`;
$("#messageFormeight").append(userHtml);
$("#text").val("");
$.ajax({
data: { msg: rawText },
type: "POST",
url: "/get",
}).done(function(data) {
chatSessions[currentChatIndex].messages.push({
text: data,
time: str_time,
sender: 'bot'
});
var botHtml = `
<div class="message">
<div class="msg_cotainer">${data}<span class="msg_time">${str_time}</span></div>
</div>`;
$("#messageFormeight").append($.parseHTML(botHtml));
});
}
}
function handleBotResponseBasedOnMood(mood, time) {
let botMessage = "";
switch(mood) {
case "happy":
botMessage = "You look happy! How can I assist you today?";
break;
case "sad":
botMessage = "I'm sorry you're feeling down. Do you want to talk about it?";
break;
default:
botMessage = "I'm here for you. How can I assist you?";
}
chatSessions[currentChatIndex].messages.push({
text: botMessage,
time: time,
sender: 'bot'
});
var botHtml = `
<div class="message">
<div class="msg_cotainer">${botMessage}<span class="msg_time">${time}</span></div>
</div>`;
$("#messageFormeight").append($.parseHTML(botHtml));
}
function startNewChat() {
const date = new Date();
const hour = date.getHours();
const minute = date.getMinutes();
const str_time = hour + ":" + minute;
currentChatIndex = chatSessions.length;
chatSessions.push({
title: "New Chat",
messages: [],
time: str_time
});
updateChatHistory();
$("#messageFormeight").empty();
}
function updateChatHistory() {
$("#chatHistory").empty();
chatSessions.forEach((session, index) => {
$("#chatHistory").append(`
<li class="chat-session" data-index="${index}">
${session.title} (${session.time})
<div class="delete-session"><i class="fas fa-trash"></i></div>
</li>`);
});
}
function loadChatSession(index) {
currentChatIndex = index;
$("#messageFormeight").empty();
chatSessions[currentChatIndex].messages.forEach((msg, i) => {
let msgHtml;
if (msg.sender === 'user') {
msgHtml = `
<div class="message" data-index="${i}">
<div class="msg_cotainer_send_container">
<div class="msg_cotainer_send">${msg.text} <span class="msg_time_send">${msg.time}${msg.edited ? ' (edited)' : ''}</span></div>
<div class="edit_btn" onclick="handleEditMessage(${i})"><i class="fas fa-edit"></i></div>
</div>
</div>`;
} else {
msgHtml = `
<div class="message">
<div class="msg_cotainer">${msg.text} <span class="msg_time">${msg.time}</span></div>
</div>`;
}
$("#messageFormeight").append(msgHtml);
});
}
function deleteSession(index) {
chatSessions.splice(index, 1);
updateChatHistory();
if (index === currentChatIndex) {
$("#messageFormeight").empty();
currentChatIndex = -1;
}
}
function handleEditMessage(messageIndex) {
const messageContainer = $(`.message[data-index=${messageIndex}]`);
const originalText = messageContainer.find('.msg_cotainer_send').text().trim();
const messageElement = messageContainer.find('.msg_cotainer_send');
messageElement.replaceWith(`
<div class="msg_cotainer_send_container edit_container">
<input type="text" class="edit_input" value="${originalText}" style="width: calc(100% - 100px);"/>
<button class="save_edit">Send</button>
<button class="cancel_edit">Cancel</button>
</div>
`);
$('.edit_input').focus();
$('.save_edit').on('click', function () {
const editedText = $(this).siblings('.edit_input').val();
const timestamp = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
messageContainer.html(`
<div class="msg_cotainer_send">${editedText} <span class="msg_time_send">${timestamp} <em>(edited)</em></span></div>
<div class="edit_btn" onclick="handleEditMessage(${messageIndex})"><i class="fas fa-edit"></i></div>
`);
chatSessions[currentChatIndex].messages[messageIndex].text = editedText;
chatSessions[currentChatIndex].messages[messageIndex].time = timestamp;
chatSessions[currentChatIndex].messages[messageIndex].edited = true;
});
$('.cancel_edit').on('click', function () {
messageContainer.html(`
<div class="msg_cotainer_send">${originalText} <span class="msg_time_send">${messageContainer.find('.msg_time_send').text()}</span></div>
<div class="edit_btn" onclick="handleEditMessage(${messageIndex})"><i class="fas fa-edit"></i></div>
`);
});
}
</script>
</body>
</html>