forked from WWFYOcelot/CSGames2024-pathogens-Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (60 loc) · 1.68 KB
/
index.js
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
const chatForm = get('form');
const chatInput = get('input');
const chatBox = get('main');
// Store user prompt history
let responses = [];
appendMessage('bot', 'Hi, I am a chatbot here to help you with your medical questions. What seems to be the problem?');
chatForm.addEventListener('submit', event => {
event.preventDefault();
var text = chatInput.value;
if (!text) return;
appendMessage('user', text);
chatInput.value = '';
if(responses.length == 0){
appendMessage('bot', 'What are your symptoms?');
}
if(responses.length < 1){
responses.push(text);
} else{
response1 = responses[0];
data = {
"inputs": `${response1 + " Here are my symptoms: " + text + " What do these symptoms indicate? Should I see a doctor?"}`,
"parameters": {}
}
// Get bot response
response = query(data).then((response) => {
console.log(response);
// Add response to chat
appendMessage('bot', response[0].generated_text);
responses = [];
});
}
});
function appendMessage(side, text) {
const bubble = `
<div class="msg -${side}">
<div class="bubble">${text}</div>
</div>`;
chatBox.insertAdjacentHTML('beforeend', bubble);
chatBox.scrollTop += 500;
}
// Utils
function get(selector, root = document) {
return root.querySelector(selector);
}
// Bot Section
async function query(data) {
const response = await fetch(
"https://xevhza5rhd1jhkq8.us-east-1.aws.endpoints.huggingface.cloud",
{
headers: {
"Accept" : "application/json",
"Content-Type": "application/json"
},
method: "POST",
body: JSON.stringify(data),
}
);
const result = await response.json();
return result;
}