Spaces:
Running
Running
Download static/script.js from TANMIHA/document-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 6.26 kB
-
https://huggingface.co/spaces/TANMIHA/document-chatbot/resolve/main/static/script.js
- Command line
-
hf download hf://spaces/TANMIHA/document-chatbot/static/script.js
-
curl -L -o script.js https://huggingface.co/spaces/TANMIHA/document-chatbot/resolve/main/static/script.js
6.26 kB
| let lightMode = true; | |
| const responses = []; | |
| const baseUrl = window.location.origin; | |
| async function showBotLoadingAnimation() { | |
| await sleep(200); | |
| $(".loading-animation")[1].style.display = "inline-block"; | |
| document.getElementById('send-button').disabled = true; | |
| } | |
| function hideBotLoadingAnimation() { | |
| $(".loading-animation")[1].style.display = "none"; | |
| if (!isFirstMessage) { | |
| document.getElementById('send-button').disabled = false; | |
| } | |
| } | |
| const processUserMessage = async (userMessage) => { | |
| let response = await fetch(baseUrl + "/process-message", { | |
| method: "POST", | |
| headers: { Accept: "application/json", "Content-Type": "application/json" }, | |
| body: JSON.stringify({ userMessage: userMessage }), | |
| }); | |
| response = await response.json(); | |
| return response; | |
| }; | |
| const cleanTextInput = (value) => { | |
| return value | |
| .trim() | |
| .replace(/[\n\t]/g, "") | |
| .replace(/<[^>]*>/g, "") | |
| .replace(/[<>&;]/g, ""); | |
| }; | |
| const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time)); | |
| const scrollToBottom = () => { | |
| $("#chat-window").animate({ | |
| scrollTop: $("#chat-window")[0].scrollHeight, | |
| }); | |
| }; | |
| const populateUserMessage = (userMessage) => { | |
| $("#message-input").val(""); | |
| $("#message-list").append( | |
| `<div class='message-line my-text'><div class='message-box my-text${ | |
| !lightMode ? " dark" : "" | |
| }'><div class='me'>${userMessage}</div></div></div>` | |
| ); | |
| scrollToBottom(); | |
| }; | |
| let isFirstMessage = true; | |
| const populateBotResponse = async (userMessage) => { | |
| await showBotLoadingAnimation(); | |
| let response; | |
| let uploadButtonHtml = ''; | |
| if (isFirstMessage) { | |
| response = { botResponse: "Hello there! I'm your friendly data assistant, ready to answer any questions regarding your data. Could you please upload a PDF file for me to analyze?"}; | |
| uploadButtonHtml = ` | |
| <input type="file" id="file-upload" accept=".pdf" hidden> | |
| <button id="upload-button" class="btn btn-primary btn-sm">Upload File</button> | |
| `; | |
| } else { | |
| response = await processUserMessage(userMessage); | |
| } | |
| renderBotResponse(response, uploadButtonHtml); | |
| if (isFirstMessage) { | |
| $("#upload-button").on("click", function () { | |
| $("#file-upload").click(); | |
| }); | |
| $("#file-upload").on("change", async function () { | |
| const file = this.files[0]; | |
| await showBotLoadingAnimation(); | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| let response = await fetch(baseUrl + "/process-document", { | |
| method: "POST", | |
| headers: { Accept: "application/json" }, | |
| body: formData, | |
| }); | |
| if (response.status !== 400) { | |
| document.querySelector('#upload-button').disabled = true; | |
| } | |
| response = await response.json(); | |
| renderBotResponse(response, ''); | |
| }); | |
| isFirstMessage = false; | |
| } | |
| }; | |
| // Text-to-Speech (TTS) | |
| const speakText = (text) => { | |
| if ('speechSynthesis' in window) { | |
| window.speechSynthesis.cancel(); | |
| const utterance = new SpeechSynthesisUtterance(text); | |
| utterance.lang = 'en-IN'; | |
| window.speechSynthesis.speak(utterance); | |
| } | |
| }; | |
| const renderBotResponse = (response, uploadButtonHtml) => { | |
| responses.push(response); | |
| hideBotLoadingAnimation(); | |
| const botText = response.botResponse.trim(); | |
| $("#message-list").append( | |
| `<div class='message-line'> | |
| <div class='message-box${!lightMode ? " dark" : ""}'> | |
| ${botText}<br>${uploadButtonHtml} | |
| <button class="btn btn-sm btn-link speak-btn p-0 mt-2" onclick="speakText('${botText.replace(/'/g, "\\'")}')"> | |
| <i class="fa fa-volume-up"></i> Listen | |
| </button> | |
| </div> | |
| </div>` | |
| ); | |
| scrollToBottom(); | |
| speakText(botText); | |
| }; | |
| populateBotResponse(); | |
| $(document).ready(function () { | |
| document.getElementById('send-button').disabled = true; | |
| // Speech-to-Text (STT) Integration | |
| const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; | |
| if (SpeechRecognition) { | |
| const recognition = new SpeechRecognition(); | |
| recognition.continuous = false; | |
| recognition.interimResults = false; | |
| recognition.lang = 'en-IN'; | |
| $("#mic-button").click(function () { | |
| try { | |
| recognition.start(); | |
| } catch (e) { | |
| console.log("Recognition already started"); | |
| } | |
| }); | |
| recognition.onstart = function () { | |
| $("#mic-button").addClass("listening"); | |
| $("#message-input").attr("placeholder", "Listening..."); | |
| }; | |
| recognition.onresult = function (event) { | |
| const transcript = event.results[0][0].transcript; | |
| $("#message-input").val(transcript); | |
| }; | |
| recognition.onerror = function (event) { | |
| console.error("Speech recognition error", event.error); | |
| $("#mic-button").removeClass("listening"); | |
| $("#message-input").attr("placeholder", "Type or speak your message..."); | |
| }; | |
| recognition.onend = function () { | |
| $("#mic-button").removeClass("listening"); | |
| $("#message-input").attr("placeholder", "Type or speak your message..."); | |
| const inputVal = cleanTextInput($("#message-input").val()); | |
| if (inputVal !== "") { | |
| populateUserMessage(inputVal); | |
| populateBotResponse(inputVal); | |
| } | |
| }; | |
| } else { | |
| $("#mic-button").hide(); | |
| } | |
| $("#message-input").keyup(function (event) { | |
| let inputVal = cleanTextInput($("#message-input").val()); | |
| if (event.keyCode === 13 && inputVal != "") { | |
| const message = inputVal; | |
| populateUserMessage(message); | |
| populateBotResponse(message); | |
| } | |
| }); | |
| $("#send-button").click(async function () { | |
| const message = cleanTextInput($("#message-input").val()); | |
| if (message !== "") { | |
| populateUserMessage(message); | |
| populateBotResponse(message); | |
| } | |
| }); | |
| $("#reset-button").click(async function () { | |
| $("#message-list").empty(); | |
| responses.length = 0; | |
| isFirstMessage = true; | |
| document.querySelector('#upload-button').disabled = false; | |
| populateBotResponse(); | |
| }); | |
| $("#light-dark-mode-switch").change(function () { | |
| $("body").toggleClass("dark-mode"); | |
| $(".message-box").toggleClass("dark"); | |
| $(".loading-dots").toggleClass("dark"); | |
| $(".dot").toggleClass("dark-dot"); | |
| lightMode = !lightMode; | |
| }); | |
| }); |