Download content/flask/static/chatbot.js from ColaCoca/Jeju-ChatBot: direct link, hf CLI and curl.
- Browser
- Download file 7.74 kB
-
https://huggingface.co/ColaCoca/Jeju-ChatBot/resolve/main/content/flask/static/chatbot.js
- Command line
-
hf download hf://ColaCoca/Jeju-ChatBot/content/flask/static/chatbot.js
-
curl -L -o chatbot.js https://huggingface.co/ColaCoca/Jeju-ChatBot/resolve/main/content/flask/static/chatbot.js
7.74 kB
| var sendForm = document.querySelector('#chatform'), | |
| textInput = document.querySelector('.chatbox'), | |
| chatList = document.querySelector('.chatlist'), | |
| userBubble = document.querySelectorAll('.userInput'), | |
| botBubble = document.querySelectorAll('.bot__output'), | |
| animateBotBubble = document.querySelectorAll('.bot__input--animation'), | |
| overview = document.querySelector('.chatbot__overview'), | |
| hasCorrectInput, | |
| imgLoader = false, | |
| animationCounter = 1, | |
| animationBubbleDelay = 600, | |
| input, | |
| previousInput, | |
| isReaction = false, | |
| unkwnCommReaction = "I didn't quite get that.", | |
| chatbotButton = document.querySelector(".submit-button") | |
| let printVoice = false; | |
| let bar = document.getElementById("loadding_bar") | |
| sendForm.onkeydown = function(e){ | |
| if(e.keyCode == 13){ | |
| e.preventDefault(); | |
| //No mix ups with upper and lowercases | |
| var input = textInput.value.toLowerCase(); | |
| //Empty textarea fix | |
| if(input.length > 0) { | |
| createBubble(input) | |
| } | |
| } | |
| }; | |
| sendForm.addEventListener('submit', function(e) { | |
| //so form doesnt submit page (no page refresh) | |
| e.preventDefault(); | |
| //No mix ups with upper and lowercases | |
| var input = textInput.value.toLowerCase(); | |
| //Empty textarea fix | |
| if(input.length > 0) { | |
| createBubble(input) | |
| } | |
| }) //end of eventlistener | |
| var createBubble = function(input) { | |
| //create input bubble | |
| var chatBubble = document.createElement('li'); | |
| chatBubble.classList.add('userInput'); | |
| //adds input of textarea to chatbubble list item | |
| chatBubble.innerHTML = input; | |
| //adds chatBubble to chatlist | |
| chatList.appendChild(chatBubble) | |
| // checkInput(input); | |
| checkInput(input) | |
| } | |
| // ํ๋ก๋ฏธ์ค๋ฅผ ์ฌ์ฉํ ๋น๋๊ธฐ ํจ์ | |
| function asyncGetAnswer(input) { | |
| return new Promise((resolve, reject) => { | |
| let state = getAnswer(input) | |
| resolve(state) | |
| }); | |
| } | |
| // ํ๋ก๋ฏธ์ค๋ฅผ ์ฌ์ฉํ ๋น๋๊ธฐ ํจ์ | |
| function asyncGetVoice(input) { | |
| return new Promise((resolve, reject) => { | |
| let audio_src = getVoice(input) | |
| resolve(audio_src) | |
| }); | |
| } | |
| // ์คํ ํจ์ ์ ์ | |
| async function executeGetAnswer(input) { | |
| if (!input){ | |
| return false | |
| } | |
| console.log("asyncGetAnswer ๋๊ธฐ ํจ์ ์คํ ์์"); | |
| // ๋๊ธฐ ํจ์ ์คํ | |
| await asyncGetAnswer(input); | |
| console.log("asyncGetAnswer ๋๊ธฐ ํจ์ ์คํ ์ข ๋ฃ"); | |
| } | |
| // ์คํ ํจ์ ์ ์ | |
| async function executeGetVoice(input) { | |
| if (!input){ | |
| return false | |
| } | |
| console.log("asyncGetVoice ๋๊ธฐ ํจ์ ์คํ ์์"); | |
| // ๋๊ธฐ ํจ์ ์คํ | |
| await asyncGetVoice(input); | |
| console.log("asyncGetVoice ๋๊ธฐ ํจ์ ์คํ ์ข ๋ฃ"); | |
| } | |
| // ํ ์คํธ ์ ๋ ฅ ํ ์ ์ก ์ ํธ์ถ๋จ | |
| async function checkInput(input) { | |
| isReaction = true; | |
| await executeGetAnswer(input) | |
| console.log("answer: ", answer); | |
| console.log("jeju_answer: ", jeju_answer); | |
| // ์์ฑ ์ถ๋ ฅ ์ฌ๋ถ ํ์ธ | |
| // voice toggle button | |
| let voice_check = document.getElementById("voice_yn"); | |
| let is_checked = voice_check.checked | |
| console.log("is_checked:", is_checked) | |
| if (is_checked) { | |
| console.log("์๋ฆฌO") | |
| await executeGetVoice(jeju_answer) | |
| }else{ | |
| console.log("์๋ฆฌX") | |
| } | |
| if (answer == ""){ | |
| responseCommand("๋ค์ ์ ๋ ฅํด์ฃผ์ธ์.", "๋ค์ ์ ๋ ฅํด์ฃผ์ธ์."); | |
| }else{ | |
| // ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ | |
| responseCommand(answer, jeju_answer); | |
| } | |
| } | |
| // ๋ต๋ณ ๋ฐ์์ค๊ธฐ | |
| function getAnswer(input){ | |
| $.ajax({ | |
| type:"get", // fetch์ method ๊ธฐ๋ฅ | |
| url: "/process_input/"+input, | |
| timeout:100000, | |
| async:false, | |
| // ์ฑ๊ณต | |
| success:function(result){ | |
| console.log("success getAnswer() ํจ์ ์ฑ๊ณต" + input); | |
| answer = result.answer | |
| jeju_answer = result.jeju_answer | |
| console.log("answer " + answer); | |
| console.log("jeju_answer " + jeju_answer); | |
| }, | |
| error:function(request,error){ | |
| alert("fail getAnswer() ํจ์ ์คํจ " + input); | |
| } | |
| }) | |
| } | |
| // ์์ฑ ๋ง๋ค๊ธฐ | |
| function getVoice(sentence){ | |
| $.ajax({ | |
| type:"get", // fetch์ method ๊ธฐ๋ฅ | |
| url: "/voice/"+sentence, | |
| timeout:100000, | |
| async:false, | |
| // ์ฑ๊ณต | |
| success:function(audio_src){ | |
| console.log("success " + audio_src); | |
| print_voice(audio_src) | |
| return audio_src | |
| }, | |
| error:function(request,error){ | |
| alert("fail " + sentence); | |
| return "error" | |
| } | |
| }) | |
| } | |
| // ์์ฑ ์๋ฆฌ ์ถ๋ ฅํ๊ธฐ | |
| function print_voice(audio_src){ | |
| let hidden_area = document.querySelector("#hidden_area"); | |
| voice_tag_html = ` | |
| <audio src="../${audio_src}" autoplay></audio>`; | |
| hidden_area.insertAdjacentHTML("beforeend", voice_tag_html); | |
| printVoice = true; | |
| } | |
| function responseCommand(comm, jeju_comm) { | |
| // animationCounter = 1; | |
| //create response bubble | |
| var successResponse = document.createElement('li'); | |
| successResponse.classList.add('bot__output'); | |
| successResponse.classList.add('bot__output--failed'); | |
| response_html = `<p>${comm}</p> | |
| <p style="font-size:11px;color:lightslategray">${jeju_comm}</p>` | |
| //Add text to successResponse | |
| successResponse.innerHTML = response_html; //adds input of textarea to chatbubble list item | |
| //add list item to chatlist | |
| chatList.appendChild(successResponse) //adds chatBubble to chatlist | |
| animateBotOutput(); | |
| // reset text area input | |
| textInput.value = ""; | |
| //Sets chatlist scroll to bottom | |
| chatList.scrollTop = chatList.scrollHeight; | |
| animationCounter = 1; | |
| } | |
| function responseText(e) { | |
| var response = document.createElement('li'); | |
| response.classList.add('bot__output'); | |
| //Adds whatever is given to responseText() to response bubble | |
| response.innerHTML = e; | |
| chatList.appendChild(response); | |
| animateBotOutput(); | |
| console.log(response.clientHeight); | |
| //Sets chatlist scroll to bottom | |
| setTimeout(function(){ | |
| chatList.scrollTop = chatList.scrollHeight; | |
| console.log(response.clientHeight); | |
| }, 0) | |
| } | |
| function responseImg(e) { | |
| var image = new Image(); | |
| image.classList.add('bot__output'); | |
| //Custom class for styling | |
| image.classList.add('bot__outputImage'); | |
| //Gets the image | |
| image.src = "/images/"+e; | |
| chatList.appendChild(image); | |
| animateBotOutput() | |
| if(image.completed) { | |
| chatList.scrollTop = chatList.scrollTop + image.scrollHeight; | |
| } | |
| else { | |
| image.addEventListener('load', function(){ | |
| chatList.scrollTop = chatList.scrollTop + image.scrollHeight; | |
| }) | |
| } | |
| } | |
| //change to SCSS loop | |
| function animateBotOutput() { | |
| chatList.lastElementChild.style.animationDelay= (animationCounter * animationBubbleDelay)+"ms"; | |
| animationCounter++; | |
| chatList.lastElementChild.style.animationPlayState = "running"; | |
| } | |
| function commandReset(e){ | |
| animationCounter = 1; | |
| previousInput = Object.keys(possibleInput)[e]; | |
| } | |
| var reactionInput = { | |
| "best work" : function(){ | |
| //Redirects you to a different page after 3 secs | |
| responseText("On this GitHub page you'll find everything about Navvy"); | |
| responseText("<a href='https://github.com/meesrutten/chatbot'>Navvy on GitHub</a>") | |
| animationCounter = 1; | |
| return | |
| }, | |
| "about" : function(){ | |
| responseText("Things I want to learn or do:"); | |
| responseText("Get great at CSS & JS animation"); | |
| responseText("Create 3D browser experiences"); | |
| responseText("Learn Three.js and WebGL"); | |
| responseText("Combine Motion Design with Front-End"); | |
| animationCounter = 1; | |
| return | |
| } | |
| } | |