|
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0'; |
|
|
|
|
|
env.allowLocalModels = false; |
|
|
|
const answerer = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad'); |
|
var chatBox = document.getElementById("chat-box"); |
|
const sendMessageButton = document.getElementById('send-btn'); |
|
|
|
sendMessageButton.addEventListener('click', function (e) { |
|
sendMessage() |
|
}); |
|
|
|
|
|
|
|
function sendMessage() { |
|
var userInput = document.getElementById("user-input").value; |
|
sendMessageAndUpdateChat(userInput); |
|
} |
|
|
|
|
|
|
|
async function getAnswer(question) { |
|
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.'; |
|
const output = await answerer(question, context); |
|
setTimeout(function() { |
|
chatBox.innerHTML += "<p class='bot-message'><strong>Chatbot:</strong> " + output.answer + "</p>"; |
|
|
|
chatBox.scrollTop = chatBox.scrollHeight; |
|
}, 500); |
|
} |
|
|
|
|
|
function sendMessageAndUpdateChat(message) { |
|
|
|
|
|
chatBox.innerHTML += "<p class='user-message'><strong>You:</strong> " + message + "</p>"; |
|
getAnswer(message) |
|
|
|
} |
|
|
|
|
|
document.getElementById("user-input").addEventListener("keypress", function(event) { |
|
if (event.key === "Enter") { |
|
var userInput = document.getElementById("user-input").value; |
|
sendMessageAndUpdateChat(userInput); |
|
document.getElementById("user-input").value = ""; |
|
} |
|
}); |
|
|
|
|
|
|
|
|