Spaces:
Sleeping
Sleeping
Download static/chatbot.js from Shakeel401/AskRoss-Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 4.03 kB
-
https://huggingface.co/spaces/Shakeel401/AskRoss-Chatbot/resolve/1794deff2b2befe5c98af749b8a1d074f4e5bb45/static/chatbot.js
- Command line
-
hf download hf://spaces/Shakeel401/AskRoss-Chatbot@1794deff2b2befe5c98af749b8a1d074f4e5bb45/static/chatbot.js
-
curl -L -o chatbot.js https://huggingface.co/spaces/Shakeel401/AskRoss-Chatbot/resolve/1794deff2b2befe5c98af749b8a1d074f4e5bb45/static/chatbot.js
4.03 kB
| const launcher = document.getElementById('askross-launcher'); | |
| const openChat = document.getElementById('open-chat'); | |
| const closeChat = document.getElementById('close-chat'); | |
| const chatbotContainer = document.getElementById('chatbot-container'); | |
| const chatForm = document.getElementById('chat-form'); | |
| const userInput = document.getElementById('user-input'); | |
| const chatMessages = document.getElementById('chat-messages'); | |
| const typingIndicator = document.getElementById('typing-indicator'); | |
| let thread_id = localStorage.getItem('askross_thread_id'); | |
| if (!thread_id) { | |
| thread_id = crypto.randomUUID(); | |
| localStorage.setItem('askross_thread_id', thread_id); | |
| } | |
| // Page context | |
| const pageContext = { | |
| page_id: window.location.pathname === '/' ? 'home' : 'page', | |
| page_title: document.title || 'AskRoss', | |
| }; | |
| let isFirstMessage = true; | |
| // Suggestions | |
| const homepageQuestions = [ | |
| "How can I get a mortgage if the bank said no?", | |
| "What mortgage services do you offer?", | |
| "Can I refinance my home?", | |
| "How do I improve my credit score?" | |
| ]; | |
| // Show suggestions | |
| function showSuggestions() { | |
| const wrapper = document.createElement('div'); | |
| wrapper.className = 'suggestions-wrapper'; | |
| homepageQuestions.forEach(q => { | |
| const btn = document.createElement('button'); | |
| btn.className = 'suggestion-btn'; | |
| btn.textContent = q; | |
| btn.onclick = () => sendMessage(q); | |
| wrapper.appendChild(btn); | |
| }); | |
| chatMessages.appendChild(wrapper); | |
| } | |
| // Open chat | |
| openChat.addEventListener('click', () => { | |
| chatbotContainer.classList.add('open'); | |
| launcher.style.display = 'none'; | |
| if (chatMessages.children.length === 0) { | |
| showSuggestions(); | |
| } | |
| }); | |
| // Close chat | |
| closeChat.addEventListener('click', () => { | |
| chatbotContainer.classList.remove('open'); | |
| launcher.style.display = 'flex'; | |
| }); | |
| // Send message | |
| async function sendMessage(message) { | |
| if (!message) return; | |
| // Remove suggestions only once | |
| if (isFirstMessage) { | |
| chatMessages.innerHTML = ''; | |
| isFirstMessage = false; | |
| } | |
| appendMessage('user', message); | |
| typingIndicator.classList.remove('hidden'); | |
| scrollToBottom(); | |
| try { | |
| const response = await fetch('/chat', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/json'}, | |
| body: JSON.stringify({ | |
| message, | |
| thread_id, | |
| page_id: pageContext.page_id, | |
| page_title: pageContext.page_title | |
| }) | |
| }); | |
| const data = await response.json(); | |
| typingIndicator.classList.add('hidden'); | |
| appendMessage('bot', data.message || "Sorry, something went wrong"); | |
| } catch (err) { | |
| typingIndicator.classList.add('hidden'); | |
| appendMessage('bot', "Network error. Please try again."); | |
| } | |
| scrollToBottom(); | |
| } | |
| // Form submit | |
| chatForm.addEventListener('submit', (e) => { | |
| e.preventDefault(); | |
| const message = userInput.value.trim(); | |
| if (!message) return; | |
| userInput.value = ''; | |
| sendMessage(message); | |
| }); | |
| // Markdown support | |
| function markdownToHtml(md) { | |
| return md | |
| .replace(/&/g,'&') | |
| .replace(/</g,'<') | |
| .replace(/>/g,'>') | |
| .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>') | |
| .replace(/\*(.*?)\*/g, '<em>$1</em>') | |
| .replace(/\n/g, '<br>') | |
| .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank">$1</a>'); | |
| } | |
| // Append message | |
| function appendMessage(sender, text) { | |
| const wrapper = document.createElement('div'); | |
| wrapper.className = `message ${sender}`; | |
| const bubble = document.createElement('div'); | |
| bubble.className = 'bubble'; | |
| if (sender === 'bot') { | |
| bubble.innerHTML = markdownToHtml(text); | |
| } else { | |
| bubble.textContent = text; | |
| } | |
| wrapper.appendChild(bubble); | |
| chatMessages.appendChild(wrapper); | |
| } | |
| // Scroll helper | |
| function scrollToBottom() { | |
| chatMessages.scrollTop = chatMessages.scrollHeight; | |
| } |