Changsensei commited on
Commit
ba40790
·
verified ·
1 Parent(s): 159c47f

Create index.js

Browse files
Files changed (1) hide show
  1. index.js +50 -0
index.js ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0';
2
+
3
+ // Since we will download the model from the Hugging Face Hub, we can skip the local model check
4
+ env.allowLocalModels = false;
5
+
6
+ //selection of the type of LLM and model
7
+ const answerer = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad');
8
+ var chatBox = document.getElementById("chat-box");
9
+ const sendMessageButton = document.getElementById('send-btn');
10
+
11
+ sendMessageButton.addEventListener('click', function (e) {
12
+ sendMessage()
13
+ });
14
+
15
+
16
+ // Function to handle sending message
17
+ function sendMessage() {
18
+ var userInput = document.getElementById("user-input").value;
19
+ sendMessageAndUpdateChat(userInput);
20
+ }
21
+
22
+
23
+ // Sets context and frames the answer
24
+ async function getAnswer(question) {
25
+ const context = 'As of December 2022, MrBeast is the most-subscribed YouTuber, with 116 million subscribers. PewDiePie is the second most popular with 111 million subscribers. To celebrate reaching 100 million subscribers, MrBeast gave away a private island which is probably a part of the reason he took the top position from PewDiePie.';
26
+ const output = await answerer(question, context);
27
+ setTimeout(function() {
28
+ chatBox.innerHTML += "<p class='bot-message'><strong>Chatbot:</strong> " + output.answer + "</p>";
29
+ // Scroll to bottom of chat box
30
+ chatBox.scrollTop = chatBox.scrollHeight;
31
+ }, 500);
32
+ }
33
+
34
+ // Function to send message and update chat
35
+ function sendMessageAndUpdateChat(message) {
36
+
37
+ // Display user message
38
+ chatBox.innerHTML += "<p class='user-message'><strong>You:</strong> " + message + "</p>";
39
+ getAnswer(message)
40
+
41
+ }
42
+
43
+ // Event listener for Enter key press
44
+ document.getElementById("user-input").addEventListener("keypress", function(event) {
45
+ if (event.key === "Enter") {
46
+ var userInput = document.getElementById("user-input").value;
47
+ sendMessageAndUpdateChat(userInput);
48
+ document.getElementById("user-input").value = ""; // Clear input field after sending message
49
+ }
50
+ });