Spaces:
Running
Running
Download index.js from Changsensei/Chatbot1: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/spaces/Changsensei/Chatbot1/resolve/main/index.js
- Command line
-
hf download hf://spaces/Changsensei/Chatbot1/index.js
-
curl -L -o index.js https://huggingface.co/spaces/Changsensei/Chatbot1/resolve/main/index.js
1.88 kB
| import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0'; | |
| // Since we will download the model from the Hugging Face Hub, we can skip the local model check | |
| env.allowLocalModels = false; | |
| //selection of the type of LLM and model | |
| 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 to handle sending message | |
| function sendMessage() { | |
| var userInput = document.getElementById("user-input").value; | |
| sendMessageAndUpdateChat(userInput); | |
| } | |
| // Sets context and frames the answer | |
| async function getAnswer(question) { | |
| const context = 'You are an expert web developer. That can answer questions about how to code in HTML, CSS, javascript, and python.'; | |
| const output = await answerer(question, context); | |
| setTimeout(function() { | |
| chatBox.innerHTML += "<p class='bot-message'><strong>Chatbot:</strong> " + output.answer + "</p>"; | |
| // Scroll to bottom of chat box | |
| chatBox.scrollTop = chatBox.scrollHeight; | |
| }, 500); | |
| } | |
| // Function to send message and update chat | |
| function sendMessageAndUpdateChat(message) { | |
| // Display user message | |
| chatBox.innerHTML += "<p class='user-message'><strong>You:</strong> " + message + "</p>"; | |
| getAnswer(message) | |
| } | |
| // Event listener for Enter key press | |
| 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 = ""; // Clear input field after sending message | |
| } | |
| }); | |