-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
382 lines (323 loc) · 16.1 KB
/
index.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Tutor App</title>
<script src="https://cdn.tailwindcss.com"></script>
<!-- <script src="/socket.io/socket.io.js"></script> -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<style>
body {
font-family: 'Inter', sans-serif;
}
.checklist ul {
list-style: none;
padding: 0;
}
.checklist li {
margin-bottom: 10px;
}
.checklist input[type="checkbox"] {
margin-right: 10px;
}
.checklist label {
cursor: pointer;
}
</style>
<link rel="stylesheet" href="styles.css">
</head>
<body class="bg-gray-100">
<div class="max-w-md mx-auto" id="main">
<div id="overlay"></div>
<!-- Header with tabs -->
<div class="flex justify-between bg-white p-4 rounded-t-lg shadow">
<div class="font-semibold text-gray-800">Roleplay</div>
<!-- <div class="font-semibold text-gray-400">Free Talk</div> -->
</div>
<!-- Cards container -->
<div class="grid grid-cols-2 gap-2 bg-white p-4 shadow card-container" id="card-container">
<!-- Over a phone call -->
<div class="flex flex-col items-center p-2 card">
<img src="assets/img/Young adult on a phone call.png" alt="Young adult on a phone call" class="mb-1">
<span class="text-xs text-center">Over a phone call</span>
<!-- <span class="text-xs bg-green-200 text-green-800 px-2 py-1 rounded-full mt-1">FREE</span> -->
</div>
<!-- Restaurant scene -->
<div class="flex flex-col items-center p-2 card">
<img src="assets/img/Young adult conversing with a waiter.png"
alt="Young adult conversing with a waiter.png" class="mb-1">
<span class="text-xs text-center">At a restaurant</span>
</div>
<!-- At a Nike store -->
<div class="flex flex-col items-center p-2 card">
<img src="https://placehold.co/50x50" alt="Image of a sneaker, symbolizing a situation at a Nike store"
class="mb-1">
<span class="text-xs text-center">At a Nike store</span>
</div>
<!-- At a coffee shop -->
<div class="flex flex-col items-center p-2 card">
<img src="https://placehold.co/50x50"
alt="Picture of a coffee cup, indicating a setting at a coffee shop" class="mb-1">
<span class="text-xs text-center">At a coffee shop</span>
</div>
<!-- First day of class -->
<div class="flex flex-col items-center p-2 card">
<img src="https://placehold.co/50x50"
alt="Icon of a male student, denoting the First day of class scenario" class="mb-1">
<span class="text-xs text-center">First day of class</span>
</div>
<!-- Birthday at a Spanish restaurant -->
<div class="flex flex-col items-center p-2 card">
<img src="https://placehold.co/50x50"
alt="Icon of a paella dish, depicting a Birthday at a Spanish restaurant" class="mb-1">
<span class="text-xs text-center">Birthday at a Spanish restaurant</span>
<!-- <span class="text-xs bg-red-200 text-red-800 px-2 py-1 rounded-full mt-1">1</span> -->
</div>
</div>
<!-- Bottom Navigation
<div class="flex justify-between bg-white p-4 rounded-b-lg mt-2 shadow">
<i class="fas fa-home fa-lg text-gray-800"></i>
<i class="fas fa-book-open fa-lg text-gray-400"></i>
<i class="fas fa-save fa-lg text-gray-400"></i>
<i class="fas fa-trophy fa-lg text-gray-400"></i>
<i class="fas fa-user fa-lg text-gray-400"></i>
</div> -->
</div>
<div id="chat-container" class="chat-container">
<div class="chat-header">
<span class="chat-title">Over a phone call</span>
<button id="objectives-btn" class="objectives-btn">☰</button>
</div>
<div class="chat-messages" id="chat-messages">
<canvas id="chatCanvas"></canvas>
<!-- Chat messages go here -->
</div>
<div class="chat-input-container">
<input id="chat-input" type="text" placeholder="Type a message...">
<button id="send-button">Send</button>
</div>
<div class="max-w-md mx-auto mt-8 objectives-panel" id="objectives-panel">
<div class="bg-white p-4 rounded-lg shadow">
<div class="objective-header">
<span class="font-semibold text-lg text-gray-800">Over a phone call</span>
<button id="close-btn" class="close-btn">✕</button>
</div>
<div class="mt-4">
<h3 class="text-sm font-semibold text-gray-800">Scenario</h3>
<p class="text-sm text-gray-600 mt-1">You are calling your friend in Chennai, curious about their
well-being, work life, and upcoming weekend getaway plans.</p>
</div>
<div class="mt-4 checklist">
<h3 class="text-sm font-semibold text-gray-800">Objectives</h3>
<ul class="list-disc list-inside text-sm text-gray-600 mt-1" id="checklistItems">
<li><input type="checkbox" id="item1"><label for="item1">Ask how is your friend doing</label>
</li>
<li><input type="checkbox" id="item2"><label for="item2">Ask how is everyone at his home</label>
</li>
<li><input type="checkbox" id="item3"><label for="item3">Ask how is his work pressure</label>
</li>
<li><input type="checkbox" id="item4"><label for="item4">Ask about his holiday plans for the
weekend</label></li>
</ul>
</div>
</div>
</div>
</div>
<script>
// Chat interface - Simulate opening the chat interface when a card is clicked
document.querySelectorAll('.card').forEach(card => {
card.addEventListener('click', function () {
document.getElementById('chat-container').style.display = 'flex';
document.getElementById('overlay').style.display = 'block';
});
});
// Function to hide the Chat Interface
const mainContainer = document.getElementById('main');
const hideChatInterface = async () => {
document.getElementById('chat-container').style.display = 'none';
document.getElementById('overlay').style.display = 'none';
}
// Toggle Behaviour for the objectives
document.addEventListener('DOMContentLoaded', () => {
const checklist = document.getElementById('checklistItems');
checklist.addEventListener('change', (event) => {
if (event.target.tagName === 'INPUT' && event.target.type === 'checkbox') {
const label = event.target.nextElementSibling;
if (event.target.checked) {
label.style.textDecoration = 'line-through';
label.style.color = '#999';
} else {
label.style.textDecoration = 'none';
label.style.color = '#000';
}
}
});
});
// Objectives panel
document.getElementById('objectives-btn').addEventListener('click', function () {
var objectivesPanel = document.getElementById('objectives-panel');
objectivesPanel.style.display = objectivesPanel.style.display === 'block' ? 'none' : 'block';
});
// Close the objectives panel
document.getElementById('close-btn').addEventListener('click', function () {
var objectivesPanel = document.getElementById('objectives-panel');
objectivesPanel.style.display = 'none';
})
// Chat interface - send message in the chat window
// Get the necessary elements
const chatMessagesDiv = document.querySelector('.chat-messages');
const sendButton = document.querySelector('#send-button');
const chatInput = document.querySelector('#chat-input');
// Function to add the chat message to the chat messages div
function addChatMessage(chatText, sender) {
console.log("Adding message:", chatText, "Sender:", sender); // Check what's being added
// Create a new chat message element
var newChatMessage = document.createElement('div');
newChatMessage.classList.add(sender === 'user' ? 'user-message' : 'ai-message');
if (sender === 'user') {
newChatMessage.textContent = chatText;
} else {
console.log("ChatText:" + chatText);
const regex = /\((.*?)\)/g;
let tamilText = "";
let matches;
while ((matches = regex.exec(chatText)) !== null) {
tamilText += matches[1] + " ";
}
// Removing the substrings from the original string
const englishText = chatText.replace(regex, "").trim();
// Add English message and append to ai-message
var englishMessage = document.createElement('div');
englishMessage.textContent = englishText; // Use '=' instead of '()'
// Style the English message (optional)
englishMessage.style.display = 'flex';
englishMessage.style.justifyContent = 'space-between';
englishMessage.style.alignItems = 'center';
// Create the hint icon button
var hintButton = document.createElement('button');
hintButton.innerHTML = '💡'; // Using a bulb emoji as an example icon
hintButton.style.marginLeft = '10px';
// Append the hint button to the English message div
englishMessage.appendChild(hintButton);
// Append to new chat message
newChatMessage.appendChild(englishMessage);
// Add Tamil message and append to ai-message
var tamilMessage = document.createElement('div');
tamilMessage.classList.add('ai-tamil-message');
tamilMessage.textContent = tamilText;
console.log("Outer:");
console.log(tamilText);
console.log(tamilMessage);
newChatMessage.appendChild(tamilMessage);
hintButton.addEventListener('click', (event) => {
console.log("Inner:");
console.log(tamilMessage);
tamilMessage.style.display = tamilMessage.style.display === 'none' ? 'block' : 'none';
})
}
// Append the new chat message element to the chat-messages div
chatMessagesDiv.appendChild(newChatMessage);
chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight;
}
const sendMessage = async () => {
const message = chatInput.value.trim();
if (message === '') return;
// Display the user's message
addChatMessage(message, 'user');
chatInput.value = '';
try {
// Get and display the AI response
const response = await getAIResponse(message);
addChatMessage(response, 'ai'); // Ensure this function correctly displays the message
} catch (error) {
console.error('Error getting AI response:', error);
}
};
// Function to get a response from OpenAI (placeholder)
const getAIResponse = async (message) => {
// Placeholder: Replace this with a call to OpenAI's API
const response = await askAI(message);
// Mark objective as completed
modifiedResponse = markComplete(response);
// Return the modified response
return modifiedResponse.trim();
};
// Marks the objectives as complete when objective no. appears in AI's message.
// Extracts the objective nos. and return the proper message as `modifiedResponse`.
function markComplete(response) {
// Regular expression for finding [digit] patterns
const regex = /\s?\[\d+\]/g;
// Extracting the substrings
const matches = response.match(regex) || [];
// Removing the substrings from the original string
const modifiedResponse = response.replace(regex, "");
// Get objective
const ul = document.getElementById('checklistItems');
const liArray = Array.from(ul.querySelectorAll('li'));
// Iterate over the matches array
matches.forEach(match => {
// Extract the number from the string, removing spaces and brackets
const matchNumber = match.match(/\d+/)[0];
// Construct the checkbox's id based on the extracted number
const checkboxId = `item${matchNumber}`;
// Get the checkbox element
const checkbox = document.getElementById(checkboxId);
// Check if the checkbox exists
if (checkbox) {
// Mark the checkbox as checked
checkbox.checked = true;
}
})
// Return the proper string
return modifiedResponse;
}
function askAI(message) {
return new Promise((resolve, reject) => {
fetch('http://127.0.0.1:3000/scenario/over-a-phone-call', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: message }),
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
const messages = data.reply;
const foundAssistantMessage = messages.data.find(obj => obj.role === "assistant");
console.log(foundAssistantMessage.content);
// Resolve the promise with the reply
if (foundAssistantMessage.content.length === 1) {
resolve(foundAssistantMessage.content[0].text.value);
} else {
resolve(foundAssistantMessage.content[0].text.value + foundAssistantMessage.content[1].text.value);
}
})
.catch(error => {
console.error('Error:', error);
reject(error); // Reject the promise on error
});
});
}
// Event listeners for sending messages
sendButton.addEventListener('click', sendMessage);
chatInput.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
sendMessage();
}
});
// Add Event Listeners to hide the chat interface
chatInput.addEventListener('keydown', function (event) {
if (event.key === 'Escape') {
hideChatInterface();
}
});
</script>
</body>
</html>