Spaces:
Sleeping
Sleeping
Download static/js/script.js from abdullah9873/e-comm-flask: direct link, hf CLI and curl.
- Browser
- Download file 22.8 kB
-
https://huggingface.co/spaces/abdullah9873/e-comm-flask/resolve/main/static/js/script.js
- Command line
-
hf download hf://spaces/abdullah9873/e-comm-flask/static/js/script.js
-
curl -L -o script.js https://huggingface.co/spaces/abdullah9873/e-comm-flask/resolve/main/static/js/script.js
22.8 kB
| function initChatInterface() { | |
| // Create chat container | |
| const chatContainer = document.createElement("div"); | |
| chatContainer.id = "chat-container"; | |
| chatContainer.style.cssText = ` | |
| position: fixed; | |
| bottom: 24px; | |
| right: 24px; | |
| width: 380px; | |
| max-width: 90vw; | |
| height: 600px; | |
| max-height: 80vh; | |
| background-color: #ffffff; | |
| border-radius: 20px; | |
| box-shadow: 0 15px 40px rgba(0,0,0,0.12); | |
| border: 1px solid rgba(0,0,0,0.05); | |
| z-index: 10000; | |
| display: flex; | |
| flex-direction: column; | |
| font-family: 'Segoe UI', Roboto, sans-serif; | |
| overflow: hidden; | |
| transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1); | |
| transform: translateY(20px); | |
| opacity: 0; | |
| `; | |
| // Chat header | |
| chatContainer.innerHTML = ` | |
| <div id="chat-header" style=" | |
| padding: 16px 20px; | |
| background: #1d355d; | |
| cursor: pointer; | |
| display: flex; | |
| justify-content: space-between; | |
| align-items: center; | |
| color: white; | |
| "> | |
| <div style="display: flex; align-items: center; gap: 12px;"> | |
| <div style=" | |
| width: 40px; | |
| height: 40px; | |
| background-color: rgba(255,255,255,0.1); | |
| border-radius: 50%; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| font-size: 20px; | |
| "><i class="ri-customer-service-2-line"></i></div> | |
| <div> | |
| <h3 style="margin: 0; font-size: 16px; font-weight: 600; letter-spacing: 0.5px;">Furniture Expert</h3> | |
| <p style="margin: 0; font-size: 12px; color: #D4A017;">Online & ready to help</p> | |
| </div> | |
| </div> | |
| <div style="display: flex; gap: 8px;"> | |
| <button id="minimize-chat" style=" | |
| background: transparent; | |
| border: none; | |
| width: 30px; | |
| height: 30px; | |
| border-radius: 50%; | |
| color: white; | |
| font-size: 18px; | |
| cursor: pointer; | |
| transition: background 0.2s; | |
| "><i class="ri-subtract-line"></i></button> | |
| <button id="close-chat" style=" | |
| background: transparent; | |
| border: none; | |
| width: 30px; | |
| height: 30px; | |
| border-radius: 50%; | |
| color: white; | |
| font-size: 18px; | |
| cursor: pointer; | |
| transition: background 0.2s; | |
| "><i class="ri-close-line"></i></button> | |
| </div> | |
| </div> | |
| <div id="chat-box" style=" | |
| flex-grow: 1; | |
| overflow-y: auto; | |
| padding: 20px; | |
| background: #fdfdfd; | |
| display: flex; | |
| flex-direction: column; | |
| gap: 16px; | |
| "></div> | |
| <div id="suggestion-box" style=" | |
| padding: 12px 16px; | |
| background: #ffffff; | |
| border-top: 1px solid #f0f0f0; | |
| display: flex; | |
| gap: 8px; | |
| overflow-x: auto; | |
| "></div> | |
| <div style=" | |
| padding: 16px; | |
| background: #ffffff; | |
| border-top: 1px solid #f0f0f0; | |
| display: flex; | |
| gap: 12px; | |
| align-items: center; | |
| "> | |
| <div style="position: relative; flex-grow: 1;"> | |
| <input type="text" id="user-input" placeholder="Type your message..." style=" | |
| width: 100%; | |
| padding: 14px 20px; | |
| padding-right: 45px; | |
| border: 1px solid #e2e8f0; | |
| background: #f8fafc; | |
| border-radius: 30px; | |
| outline: none; | |
| font-size: 14px; | |
| transition: border-color 0.3s; | |
| "> | |
| <label for="image-upload" style=" | |
| position: absolute; | |
| right: 15px; | |
| top: 50%; | |
| transform: translateY(-50%); | |
| cursor: pointer; | |
| color: #94a3b8; | |
| font-size: 18px; | |
| transition: color 0.3s; | |
| "> | |
| <i class="ri-camera-line"></i> | |
| <input type="file" id="image-upload" accept="image/*" style="display: none"> | |
| </label> | |
| </div> | |
| <button id="send-button" style=" | |
| padding: 0; | |
| width: 48px; | |
| height: 48px; | |
| background: #1d355d; | |
| color: white; | |
| border: none; | |
| border-radius: 50%; | |
| cursor: pointer; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| font-size: 20px; | |
| transition: transform 0.2s, background 0.2s; | |
| box-shadow: 0 4px 10px rgba(29, 53, 93, 0.2); | |
| "><i class="ri-send-plane-2-fill"></i></button> | |
| </div> | |
| `; | |
| document.body.appendChild(chatContainer); | |
| // Floating chat button | |
| const chatButton = document.createElement("button"); | |
| chatButton.id = "chat-button"; | |
| chatButton.innerHTML = ` | |
| <i class="ri-customer-service-2-fill"></i> | |
| `; | |
| chatButton.style.cssText = ` | |
| position: fixed; | |
| bottom: 24px; | |
| right: 24px; | |
| width: 65px; | |
| height: 65px; | |
| background: #1d355d; | |
| color: white; | |
| border: none; | |
| border-radius: 50%; | |
| cursor: pointer; | |
| box-shadow: 0 8px 25px rgba(29, 53, 93, 0.3); | |
| z-index: 10000; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| font-size: 28px; | |
| transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1); | |
| animation: float 4s ease-in-out infinite; | |
| display: none; | |
| `; | |
| const style = document.createElement("style"); | |
| style.textContent = ` | |
| @keyframes float { | |
| 0% { transform: translateY(0px); } | |
| 50% { transform: translateY(-8px); } | |
| 100% { transform: translateY(0px); } | |
| } | |
| @keyframes popIn { | |
| 0% { transform: translateY(30px); opacity: 0; } | |
| 100% { transform: translateY(0); opacity: 1; } | |
| } | |
| #minimize-chat:hover, #close-chat:hover { | |
| background: rgba(255,255,255,0.1) !important; | |
| } | |
| #send-button:hover { | |
| background: #D4A017 !important; | |
| transform: scale(1.05); | |
| } | |
| #chat-button:hover { | |
| transform: scale(1.1); | |
| background: #D4A017 !important; | |
| } | |
| #user-input:focus { | |
| border-color: #1d355d !important; | |
| background: #ffffff !important; | |
| } | |
| label[for="image-upload"]:hover { | |
| color: #1d355d !important; | |
| } | |
| `; | |
| document.head.appendChild(style); | |
| document.body.appendChild(chatButton); | |
| // Show pop-up animation when page loads | |
| setTimeout(() => { | |
| chatContainer.style.animation = "popIn 0.4s ease-out forwards"; | |
| // Add welcome message | |
| displayAssistantResponse(` | |
| <div style="margin-bottom: 8px;"> | |
| Assalam-o-Alaikum! 👋 I'm your personal furniture assistant. | |
| </div> | |
| <div style="margin-bottom: 8px;"> | |
| I can help you: | |
| <ul style="margin: 8px 0; padding-left: 20px;"> | |
| <li>Find products</li> | |
| <li>Compare prices</li> | |
| <li>Suggest alternatives</li> | |
| <li>Place orders</li> | |
| </ul> | |
| </div> | |
| <div> | |
| How can I help you today? | |
| </div> | |
| `); | |
| showSuggestions([ | |
| "Show sofas under 50,000", | |
| "Best plants for home", | |
| "New arrivals", | |
| "Help me order", | |
| ]); | |
| }, 1000); | |
| // Toggle chat visibility | |
| const chatHeader = document.getElementById("chat-header"); | |
| const minimizeBtn = document.getElementById("minimize-chat"); | |
| const closeBtn = document.getElementById("close-chat"); | |
| chatHeader.addEventListener("click", () => { | |
| if (chatContainer.style.display === "none") { | |
| chatContainer.style.display = "flex"; | |
| minimizeBtn.textContent = "−"; | |
| } | |
| }); | |
| minimizeBtn.addEventListener("click", (e) => { | |
| e.stopPropagation(); | |
| chatContainer.style.height = | |
| chatContainer.style.height === "60px" ? "500px" : "60px"; | |
| minimizeBtn.textContent = chatContainer.style.height === "60px" ? "+" : "−"; | |
| }); | |
| closeBtn.addEventListener("click", (e) => { | |
| e.stopPropagation(); | |
| chatContainer.style.display = "none"; | |
| chatButton.style.display = "flex"; | |
| }); | |
| chatButton.addEventListener("click", () => { | |
| chatContainer.style.display = "flex"; | |
| chatButton.style.display = "none"; | |
| minimizeBtn.textContent = "−"; | |
| chatContainer.style.height = "500px"; | |
| }); | |
| // Add typing indicator function | |
| window.showTypingIndicator = function () { | |
| const chatBox = document.getElementById("chat-box"); | |
| const typingDiv = document.createElement("div"); | |
| typingDiv.id = "typing-indicator"; | |
| typingDiv.style.cssText = ` | |
| text-align: left; | |
| margin: 10px 0; | |
| display: flex; | |
| align-items: center; | |
| gap: 8px; | |
| `; | |
| typingDiv.innerHTML = ` | |
| <div style=" | |
| display: inline-block; | |
| padding: 8px 12px; | |
| background: #f5f5f5; | |
| border-radius: 18px; | |
| "> | |
| <div class="typing-dots"> | |
| <div></div> | |
| <div></div> | |
| <div></div> | |
| </div> | |
| </div> | |
| `; | |
| chatBox.appendChild(typingDiv); | |
| chatBox.scrollTop = chatBox.scrollHeight; | |
| // Add typing dots animation | |
| const typingStyle = document.createElement("style"); | |
| typingStyle.textContent = ` | |
| .typing-dots { | |
| display: flex; | |
| gap: 4px; | |
| } | |
| .typing-dots div { | |
| width: 8px; | |
| height: 8px; | |
| background: #666; | |
| border-radius: 50%; | |
| animation: typingAnimation 1.4s infinite ease-in-out; | |
| } | |
| .typing-dots div:nth-child(1) { animation-delay: 0s; } | |
| .typing-dots div:nth-child(2) { animation-delay: 0.2s; } | |
| .typing-dots div:nth-child(3) { animation-delay: 0.4s; } | |
| @keyframes typingAnimation { | |
| 0%, 60%, 100% { transform: translateY(0); } | |
| 30% { transform: translateY(-5px); } | |
| } | |
| `; | |
| document.head.appendChild(typingStyle); | |
| }; | |
| // Remove typing indicator function | |
| window.hideTypingIndicator = function () { | |
| const typingIndicator = document.getElementById("typing-indicator"); | |
| if (typingIndicator) { | |
| typingIndicator.remove(); | |
| } | |
| }; | |
| } | |
| // ======= Enhanced Chat Message Handling ======= | |
| async function sendChatMessage() { | |
| try { | |
| const response = await fetch("/chat", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ message: message }), | |
| }); | |
| const data = await response.json(); | |
| hideTypingIndicator(); | |
| // Handle WhatsApp-specific responses | |
| if (data.reply && data.reply.includes("whatsapp")) { | |
| displayAssistantResponse( | |
| "I've sent the order details to your WhatsApp. Please check your messages!" | |
| ); | |
| } else { | |
| displayAssistantResponse(data.reply); | |
| } | |
| // Show suggestions if available | |
| if (data.suggestions) { | |
| showSuggestions(data.suggestions); | |
| } | |
| } catch (error) { | |
| // ... error handling ... | |
| } | |
| const input = document.getElementById("user-input"); | |
| const message = input.value.trim(); | |
| const imageUpload = document.getElementById("image-upload"); | |
| const chatBox = document.getElementById("chat-box"); | |
| if (imageUpload.files[0]) { | |
| // Handle image upload | |
| const file = imageUpload.files[0]; | |
| const reader = new FileReader(); | |
| reader.onload = async function (e) { | |
| // Display user's image in chat | |
| chatBox.innerHTML += ` | |
| <div style=" | |
| margin: 8px 0; | |
| align-self: flex-end; | |
| max-width: 85%; | |
| "> | |
| <div style=" | |
| padding: 8px; | |
| background: #1d355d; | |
| border-radius: 18px 18px 4px 18px; | |
| box-shadow: 0 4px 10px rgba(29, 53, 93, 0.1); | |
| text-align: left; | |
| "> | |
| <img src="${e.target.result}" style=" | |
| max-width: 250px; | |
| max-height: 250px; | |
| border-radius: 10px; | |
| display: block; | |
| "> | |
| <div style=" | |
| font-size: 11px; | |
| color: rgba(255,255,255,0.7); | |
| text-align: right; | |
| padding-top: 6px; | |
| ">You</div> | |
| </div> | |
| </div> | |
| `; | |
| showTypingIndicator(); | |
| try { | |
| const response = await fetch("/chat", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ image: e.target.result }), | |
| }); | |
| const data = await response.json(); | |
| hideTypingIndicator(); | |
| displayAssistantResponse(data.reply); | |
| // Show suggestions if available | |
| if (data.suggestions) { | |
| showSuggestions(data.suggestions); | |
| } | |
| } catch (error) { | |
| hideTypingIndicator(); | |
| displayAssistantResponse( | |
| "❌ Couldn't process the image. Please try again or describe what you're looking for." | |
| ); | |
| } | |
| imageUpload.value = ""; | |
| chatBox.scrollTop = chatBox.scrollHeight; | |
| }; | |
| reader.readAsDataURL(file); | |
| } else if (message) { | |
| // Handle text message | |
| chatBox.innerHTML += ` | |
| <div style=" | |
| margin: 8px 0; | |
| align-self: flex-end; | |
| max-width: 85%; | |
| "> | |
| <div style=" | |
| padding: 12px 16px; | |
| background: #1d355d; | |
| color: white; | |
| border-radius: 18px 18px 4px 18px; | |
| word-wrap: break-word; | |
| box-shadow: 0 4px 10px rgba(29, 53, 93, 0.1); | |
| font-size: 14px; | |
| text-align: left; | |
| "> | |
| ${message} | |
| <div style=" | |
| font-size: 11px; | |
| color: rgba(255,255,255,0.7); | |
| text-align: right; | |
| padding-top: 6px; | |
| ">You</div> | |
| </div> | |
| </div> | |
| `; | |
| input.value = ""; | |
| showTypingIndicator(); | |
| try { | |
| const response = await fetch("/chat", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ message: message }), | |
| }); | |
| const data = await response.json(); | |
| hideTypingIndicator(); | |
| displayAssistantResponse(data.reply); | |
| // Show suggestions if available | |
| if (data.suggestions) { | |
| showSuggestions(data.suggestions); | |
| } | |
| } catch (error) { | |
| hideTypingIndicator(); | |
| displayAssistantResponse( | |
| "❌ Sorry, I'm having trouble responding. Please try again in a moment." | |
| ); | |
| } | |
| } | |
| } | |
| // Enhanced assistant response display with proper link handling | |
| function displayAssistantResponse(response) { | |
| const chatBox = document.getElementById("chat-box"); | |
| let formattedResponse = ""; | |
| // Handle object responses (from tools) | |
| if (typeof response === "object" && response !== null) { | |
| // Add the main message text | |
| if (response.message) { | |
| formattedResponse += `<div>${response.message}</div>`; | |
| } | |
| // Handle link objects | |
| if (response.link && typeof response.link === "object") { | |
| const linkText = response.link.text || "View Details"; | |
| const linkUrl = response.link.url || "#"; | |
| formattedResponse += ` | |
| <div style="margin-top: 8px;"> | |
| <a href="${linkUrl}" target="_blank" | |
| style="display: inline-block; | |
| padding: 8px 16px; | |
| background-color: #f4a742; | |
| color: white; | |
| text-decoration: none; | |
| border-radius: 18px; | |
| font-weight: 500; | |
| transition: background-color 0.2s;"> | |
| ${linkText} | |
| </a> | |
| </div> | |
| `; | |
| } | |
| // Handle direct string links in response | |
| if (response.url) { | |
| formattedResponse += ` | |
| <div style="margin-top: 8px;"> | |
| <a href="${response.url}" target="_blank" | |
| style="display: inline-block; | |
| padding: 8px 16px; | |
| background-color: #f4a742; | |
| color: white; | |
| text-decoration: none; | |
| border-radius: 18px; | |
| font-weight: 500;"> | |
| ${response.text || "Click Here"} | |
| </a> | |
| </div> | |
| `; | |
| } | |
| } | |
| // Handle string responses | |
| else if (typeof response === "string") { | |
| // Convert URLs to clickable links | |
| formattedResponse = response.replace( | |
| /(https?:\/\/[^\s]+)/g, | |
| '<a href="$1" target="_blank" style="color:#2196F3;text-decoration:underline">$1</a>' | |
| ); | |
| } | |
| // Handle other response types | |
| else { | |
| formattedResponse = String(response); | |
| } | |
| // Add timestamp | |
| const now = new Date(); | |
| const timeString = now.toLocaleTimeString([], { | |
| hour: "2-digit", | |
| minute: "2-digit", | |
| }); | |
| // Append the final formatted message to the chat box | |
| chatBox.innerHTML += ` | |
| <div style=" | |
| text-align: left; | |
| margin: 8px 0; | |
| align-self: flex-start; | |
| "> | |
| <div style=" | |
| display: inline-block; | |
| padding: 14px 18px; | |
| background: white; | |
| color: #334155; | |
| border-radius: 18px 18px 18px 4px; | |
| max-width: 85%; | |
| word-wrap: break-word; | |
| box-shadow: 0 4px 12px rgba(0,0,0,0.06); | |
| border: 1px solid rgba(0,0,0,0.02); | |
| font-size: 14px; | |
| line-height: 1.5; | |
| "> | |
| ${formattedResponse} | |
| <div style=" | |
| font-size: 11px; | |
| color: #94a3b8; | |
| text-align: left; | |
| padding-top: 8px; | |
| display: flex; | |
| align-items: center; | |
| gap: 6px; | |
| "> | |
| <i class="ri-customer-service-2-fill"></i> | |
| <span>Assistant</span> | |
| <span>•</span> | |
| <span>${timeString}</span> | |
| </div> | |
| </div> | |
| </div> | |
| `; | |
| chatBox.scrollTop = chatBox.scrollHeight; | |
| } | |
| // Show quick reply suggestions | |
| function showSuggestions(suggestions) { | |
| const suggestionBox = document.getElementById("suggestion-box"); | |
| suggestionBox.innerHTML = ""; | |
| suggestions.forEach((suggestion) => { | |
| const button = document.createElement("button"); | |
| button.textContent = suggestion; | |
| button.style.cssText = ` | |
| padding: 8px 14px; | |
| background: white; | |
| color: #1d355d; | |
| border: 1px solid #e2e8f0; | |
| border-radius: 20px; | |
| font-size: 13px; | |
| font-weight: 500; | |
| cursor: pointer; | |
| white-space: nowrap; | |
| transition: all 0.2s; | |
| box-shadow: 0 2px 4px rgba(0,0,0,0.02); | |
| flex-shrink: 0; | |
| `; | |
| button.addEventListener("mouseenter", () => { | |
| button.style.background = "#1d355d"; | |
| button.style.color = "white"; | |
| button.style.borderColor = "#1d355d"; | |
| }); | |
| button.addEventListener("mouseleave", () => { | |
| button.style.background = "white"; | |
| button.style.color = "#1d355d"; | |
| button.style.borderColor = "#e2e8f0"; | |
| }); | |
| button.addEventListener("click", () => { | |
| document.getElementById("user-input").value = suggestion; | |
| sendChatMessage(); | |
| }); | |
| suggestionBox.appendChild(button); | |
| }); | |
| } | |
| // ======= Event Listeners ======= | |
| document.addEventListener("DOMContentLoaded", () => { | |
| initChatInterface(); | |
| // Handle Enter key in input | |
| const input = document.getElementById("user-input"); | |
| input?.addEventListener("keypress", (e) => { | |
| if (e.key === "Enter") { | |
| sendChatMessage(); | |
| } | |
| }); | |
| // Handle send button click | |
| const sendButton = document.getElementById("send-button"); | |
| sendButton?.addEventListener("click", sendChatMessage); | |
| // Handle image upload | |
| const imageUpload = document.getElementById("image-upload"); | |
| imageUpload?.addEventListener("change", () => { | |
| if (imageUpload.files[0]) { | |
| sendChatMessage(); | |
| } | |
| }); | |
| }); | |
| // ======= Navbar Section ======= | |
| const menuIcon = document.getElementById("menuIcon"); | |
| const navLinks = document.querySelector(".nav-links"); | |
| menuIcon.addEventListener("click", () => { | |
| navLinks.classList.toggle("active"); | |
| }); | |
| // ======= Product Cards Section ======= | |
| document.addEventListener("DOMContentLoaded", () => { | |
| const items = { | |
| plant: [ | |
| "/static/images/plant3.png", | |
| "/static/images/plant4.webp", | |
| "/static/images/plant5.png", | |
| "/static/images/plant6.png", | |
| ], | |
| table: [ | |
| "/static/images/table2.png", | |
| "/static/images/table3.webp", | |
| "/static/images/table4.webp", | |
| "/static/images/table5.png", | |
| ], | |
| lamp: [ | |
| "/static/images/lamp1.webp", | |
| "/static/images/lamp2.webp", | |
| "/static/images/lamp3.webp", | |
| "/static/images/lamp4.png", | |
| "/static/images/lamp5.webp", | |
| "/static/images/lamp6.webp", | |
| ], | |
| sofa: [ | |
| "/static/images/sofa3.webp", | |
| "/static/images/sofa4.webp", | |
| "/static/images/sofa5.png", | |
| "/static/images/sofa6.png", | |
| ], | |
| }; | |
| document.querySelectorAll(".plus-btn").forEach((button) => { | |
| button.addEventListener("click", () => { | |
| const item = button.dataset.item; | |
| const card = document.getElementById(`${item}-card`); | |
| const mainImage = button.previousElementSibling; | |
| // Hide if already active | |
| if (card.classList.contains("active")) { | |
| card.innerHTML = ""; | |
| card.classList.remove("active"); | |
| return; | |
| } | |
| // Apply card styling | |
| Object.assign(card.style, { | |
| display: "grid", | |
| gridTemplateColumns: "repeat(auto-fit, minmax(80px, 1fr))", | |
| gap: "10px", | |
| padding: "10px", | |
| border: "2px solid #ccc", | |
| borderRadius: "10px", | |
| marginTop: "12px", | |
| maxWidth: "100%", | |
| backgroundColor: "#f5f5f5", | |
| }); | |
| card.innerHTML = ""; | |
| items[item].forEach((imgSrc) => { | |
| const img = document.createElement("img"); | |
| img.src = imgSrc; | |
| img.alt = `${item} option`; | |
| // Apply image styles | |
| Object.assign(img.style, { | |
| width: "100%", | |
| maxWidth: "80px", | |
| height: "80px", | |
| objectFit: "cover", | |
| borderRadius: "8px", | |
| cursor: "pointer", | |
| transition: "transform 0.2s, border 0.2s", | |
| border: "2px solid transparent", | |
| }); | |
| img.addEventListener("mouseenter", () => { | |
| img.style.transform = "scale(1.1)"; | |
| img.style.border = "2px solid #48c6ef"; | |
| }); | |
| img.addEventListener("mouseleave", () => { | |
| img.style.transform = "scale(1)"; | |
| img.style.border = "2px solid transparent"; | |
| }); | |
| img.addEventListener("click", () => { | |
| mainImage.src = imgSrc; | |
| }); | |
| card.appendChild(img); | |
| }); | |
| card.classList.add("active"); | |
| }); | |
| }); | |
| }); | |