Spaces:
Runtime error
Runtime error
File size: 5,794 Bytes
7ffcd85 |
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
let lightMode = true;
let recorder = null;
let recording = false;
const responses = [];
const botRepeatButtonIDToIndexMap = {};
const userRepeatButtonIDToRecordingMap = {};
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;
}
}
async function showUserLoadingAnimation() {
await sleep(100);
$(".loading-animation")[0].style.display = "flex";
}
function hideUserLoadingAnimation() {
$(".loading-animation")[0].style.display = "none";
}
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();
console.log(response);
return response;
};
const cleanTextInput = (value) => {
return value
.trim() // remove starting and ending spaces
.replace(/[\n\t]/g, "") // remove newlines and tabs
.replace(/<[^>]*>/g, "") // remove HTML tags
.replace(/[<>&;]/g, ""); // sanitize inputs
};
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
const scrollToBottom = () => {
// Scroll the chat window to the bottom
$("#chat-window").animate({
scrollTop: $("#chat-window")[0].scrollHeight,
});
};
const populateUserMessage = (userMessage, userRecording) => {
// Clear the input field
$("#message-input").val("");
// Append the user's message to the message list
$("#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)
// Event listener for file upload
if (isFirstMessage) {
$("#upload-button").on("click", function () {
$("#file-upload").click();
});
$("#file-upload").on("change", async function () {
const file = this.files[0];
await showBotLoadingAnimation();
// Create a new FormData instance
const formData = new FormData();
// Append the file to the FormData instance
formData.append('file', file);
// Now send this data to /process-document endpoint
let response = await fetch(baseUrl + "/process-document", {
method: "POST",
headers: { Accept: "application/json" }, // "Content-Type" should not be explicitly set here, the browser will automatically set it to "multipart/form-data"
body: formData, // send the FormData instance as the body
});
if (response.status !== 400) {
document.querySelector('#upload-button').disabled = true;
}
response = await response.json();
console.log('/process-document', response)
renderBotResponse(response, '')
});
isFirstMessage = false; // after the first message, set this to false
}
};
const renderBotResponse = (response, uploadButtonHtml) => {
responses.push(response);
hideBotLoadingAnimation();
$("#message-list").append(
`<div class='message-line'><div class='message-box${!lightMode ? " dark" : ""}'>${response.botResponse.trim()}<br>${uploadButtonHtml}</div></div>`
);
scrollToBottom();
}
populateBotResponse()
$(document).ready(function () {
//start the chat with send button disabled
document.getElementById('send-button').disabled = true;
// Listen for the "Enter" key being pressed in the input field
$("#message-input").keyup(function (event) {
let inputVal = cleanTextInput($("#message-input").val());
if (event.keyCode === 13 && inputVal != "") {
const message = inputVal;
populateUserMessage(message, null);
populateBotResponse(message);
}
inputVal = $("#message-input").val();
});
// When the user clicks the "Send" button
$("#send-button").click(async function () {
// Get the message the user typed in
const message = cleanTextInput($("#message-input").val());
populateUserMessage(message, null);
populateBotResponse(message);
});
//reset chat
// When the user clicks the "Reset" button
$("#reset-button").click(async function () {
// Clear the message list
$("#message-list").empty();
// Reset the responses array
responses.length = 0;
// Reset isFirstMessage flag
isFirstMessage = true;
document.querySelector('#upload-button').disabled = false;
// Start over
populateBotResponse();
});
// handle the event of switching light-dark mode
$("#light-dark-mode-switch").change(function () {
$("body").toggleClass("dark-mode");
$(".message-box").toggleClass("dark");
$(".loading-dots").toggleClass("dark");
$(".dot").toggleClass("dark-dot");
lightMode = !lightMode;
});
});
|