-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemotion.js
More file actions
42 lines (36 loc) · 1.19 KB
/
emotion.js
File metadata and controls
42 lines (36 loc) · 1.19 KB
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
const emotion_p = document.getElementById("emotion");
document.addEventListener('DOMContentLoaded', function() {
const inputElement = document.querySelector('.nwp-input');
inputElement.addEventListener('input', async function() {
const currentText = inputElement.value;
let data = await getEmotion(currentText);
console.log("emotion: ", data);
setEmotionLabel(data);
});
});
async function getEmotion(text) {
try {
const response = await fetch('https://actively-feat-makes-tm.trycloudflare.com:443/get_emotion', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
text: text,
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('There was a problem with the fetch operation:', error);
}
}
function setEmotionLabel(emotion){
if (emotion == null) {
emotion = "unknown";
}
emotion_p.textContent = "Emotion: " + emotion
}