function isImgUrl(url) { const imageExtensions = /\.(jpg|jpeg|png|gif|bmp|webp)$/i; if (url.startsWith('data:image/')) { return true; } if (url.match(imageExtensions)) { return true; } if (url.startsWith('http://') || url.startsWith('https://')) { return true; } return false; } function escapeMarkdown(text) { /* Escape Markdown special characters to HTML-safe equivalents. */ const escapeChars = { // ' ': ' ', "_": "_", "*": "*", "[": "[", "]": "]", "(": "(", ")": ")", "{": "{", "}": "}", "#": "#", "+": "+", "-": "-", ".": ".", "!": "!", "`": "`", ">": ">", "<": "<", "|": "|", "$": "$", ":": ":", }; text = text.replace(/ {4}/g, " "); // Replace 4 spaces with non-breaking spaces let escapedText = ""; for (let i = 0; i < text.length; i++) { const currentChar = text.charAt(i); escapedText += escapeChars[currentChar] || currentChar; } return escapedText; } function downloadHistory(gradioUsername, historyname, format=".json") { let fileUrl; if (gradioUsername === null || gradioUsername.trim() === "") { fileUrl = `/file=./history/${historyname}`; } else { fileUrl = `/file=./history/${gradioUsername}/${historyname}`; } downloadFile(fileUrl, historyname, format); } function downloadFile(fileUrl, filename = "", format = "", retryTimeout = 200, maxAttempts = 10) { fileUrl = fileUrl + format; filename = filename + format; let attempts = 0; async function tryDownload() { if (attempts >= maxAttempts) { console.error('Max attempts reached, download failed.'); alert('Download failed:' + filename); return; } try { const response = await fetch(fileUrl); if (!response.ok) { attempts++; console.error("Error fetching file, retrying..."); setTimeout(tryDownload, retryTimeout); } else { response.blob() .then(blob => { const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.style.display = 'none'; a.href = url; a.download = filename; document.body.appendChild(a); a.click(); URL.revokeObjectURL(url); document.body.removeChild(a); }) .catch(error => { console.error('Error downloading file:', error); }); } } catch (error) { attempts++; setTimeout(tryDownload, retryTimeout); } } tryDownload(); } function statusDisplayMessage(message) { statusDisplayBlock = statusDisplay.querySelector("#status-display .md p"); statusDisplayBlock.innerText = message; } function bindFancyBox() { Fancybox.bind('[data-fancybox]', { Carousel: { Panzoom: { decelFriction: 0.5 } } }); } function rebootingChuanhu() { reloadSpinner = new Spin.Spinner({color:'#06AE56',lines:9}).spin(); pageInfo = document.createElement('div'); pageInfo.appendChild(reloadSpinner.el); pageInfo.innerHTML += '