|
|
|
|
|
|
|
|
|
function gradioApp() { |
|
|
|
const elems = document.getElementsByTagName('gradio-app'); |
|
const elem = elems.length == 0 ? document : elems[0]; |
|
if (elem !== document) { |
|
elem.getElementById = function (id) { |
|
return document.getElementById(id); |
|
}; |
|
} |
|
return elem.shadowRoot ? elem.shadowRoot : elem; |
|
} |
|
|
|
function setCookie(name, value, days) { |
|
var expires = ""; |
|
|
|
if (days) { |
|
var date = new Date(); |
|
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); |
|
expires = "; expires=" + date.toUTCString(); |
|
} |
|
|
|
document.cookie = name + "=" + value + expires + "; path=/"; |
|
} |
|
|
|
function getCookie(name) { |
|
var decodedCookie = decodeURIComponent(document.cookie); |
|
var cookies = decodedCookie.split(';'); |
|
|
|
for (var i = 0; i < cookies.length; i++) { |
|
var cookie = cookies[i].trim(); |
|
|
|
if (cookie.indexOf(name + "=") === 0) { |
|
return cookie.substring(name.length + 1, cookie.length); |
|
} |
|
} |
|
|
|
return null; |
|
} |
|
|
|
let toastCount = 0; |
|
function toast_push(msg, duration) { |
|
duration = isNaN(duration) ? 3000 : duration; |
|
const existingToasts = document.querySelectorAll('.toast'); |
|
existingToasts.forEach(toast => { |
|
toast.style.top = `${parseInt(toast.style.top, 10) - 70}px`; |
|
}); |
|
const m = document.createElement('div'); |
|
m.innerHTML = msg; |
|
m.classList.add('toast'); |
|
m.style.cssText = `font-size: var(--text-md) !important; color: rgb(255, 255, 255); background-color: rgba(0, 0, 0, 0.6); padding: 10px 15px; border-radius: 4px; position: fixed; top: ${50 + toastCount * 70}%; left: 50%; transform: translateX(-50%); width: auto; text-align: center; transition: top 0.3s;`; |
|
document.body.appendChild(m); |
|
setTimeout(function () { |
|
m.style.opacity = '0'; |
|
setTimeout(function () { |
|
document.body.removeChild(m); |
|
toastCount--; |
|
}, 500); |
|
}, duration); |
|
toastCount++; |
|
} |
|
|
|
function toast_up(msg) { |
|
var m = document.getElementById('toast_up'); |
|
if (m) { |
|
document.body.removeChild(m); |
|
} |
|
m = document.createElement('div'); |
|
m.id = 'toast_up'; |
|
m.innerHTML = msg; |
|
m.style.cssText = "font-size: var(--text-md) !important; color: rgb(255, 255, 255); background-color: rgba(0, 0, 100, 0.6); padding: 10px 15px; margin: 0 0 0 -60px; border-radius: 4px; position: fixed; top: 50%; left: 50%; width: auto; text-align: center;"; |
|
document.body.appendChild(m); |
|
} |
|
function toast_down() { |
|
var m = document.getElementById('toast_up'); |
|
if (m) { |
|
document.body.removeChild(m); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function addCopyButton(botElement) { |
|
|
|
|
|
const copiedIcon = '<span><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height=".8em" width=".8em" xmlns="http://www.w3.org/2000/svg"><polyline points="20 6 9 17 4 12"></polyline></svg></span>'; |
|
const copyIcon = '<span><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height=".8em" width=".8em" xmlns="http://www.w3.org/2000/svg"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg></span>'; |
|
|
|
const messageBtnColumnElement = botElement.querySelector('.message-btn-row'); |
|
if (messageBtnColumnElement) { |
|
|
|
|
|
return; |
|
} |
|
|
|
var copyButton = document.createElement('button'); |
|
copyButton.classList.add('copy-bot-btn'); |
|
copyButton.setAttribute('aria-label', 'Copy'); |
|
copyButton.innerHTML = copyIcon; |
|
copyButton.addEventListener('click', async () => { |
|
const textToCopy = botElement.innerText; |
|
try { |
|
if ("clipboard" in navigator) { |
|
await navigator.clipboard.writeText(textToCopy); |
|
copyButton.innerHTML = copiedIcon; |
|
setTimeout(() => { |
|
copyButton.innerHTML = copyIcon; |
|
}, 1500); |
|
} else { |
|
const textArea = document.createElement("textarea"); |
|
textArea.value = textToCopy; |
|
document.body.appendChild(textArea); |
|
textArea.select(); |
|
try { |
|
document.execCommand('copy'); |
|
copyButton.innerHTML = copiedIcon; |
|
setTimeout(() => { |
|
copyButton.innerHTML = copyIcon; |
|
}, 1500); |
|
} catch (error) { |
|
console.error("Copy failed: ", error); |
|
} |
|
document.body.removeChild(textArea); |
|
} |
|
} catch (error) { |
|
console.error("Copy failed: ", error); |
|
} |
|
}); |
|
var messageBtnColumn = document.createElement('div'); |
|
messageBtnColumn.classList.add('message-btn-row'); |
|
messageBtnColumn.appendChild(copyButton); |
|
botElement.appendChild(messageBtnColumn); |
|
} |
|
|
|
function chatbotContentChanged(attempt = 1, force = false) { |
|
|
|
for (var i = 0; i < attempt; i++) { |
|
setTimeout(() => { |
|
gradioApp().querySelectorAll('#gpt-chatbot .message-wrap .message.bot').forEach(addCopyButton); |
|
}, i === 0 ? 0 : 200); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function chatbotAutoHeight() { |
|
|
|
function update_height() { |
|
var { height_target, chatbot_height, chatbot } = get_elements(true); |
|
if (height_target != chatbot_height) { |
|
var pixelString = height_target.toString() + 'px'; |
|
chatbot.style.maxHeight = pixelString; chatbot.style.height = pixelString; |
|
} |
|
} |
|
|
|
function update_height_slow() { |
|
var { height_target, chatbot_height, chatbot } = get_elements(); |
|
if (height_target != chatbot_height) { |
|
new_panel_height = (height_target - chatbot_height) * 0.5 + chatbot_height; |
|
if (Math.abs(new_panel_height - height_target) < 10) { |
|
new_panel_height = height_target; |
|
} |
|
|
|
var pixelString = new_panel_height.toString() + 'px'; |
|
chatbot.style.maxHeight = pixelString; chatbot.style.height = pixelString; |
|
} |
|
} |
|
monitoring_input_box() |
|
update_height(); |
|
setInterval(function () { |
|
update_height_slow() |
|
}, 50); |
|
} |
|
|
|
swapped = false; |
|
function swap_input_area() { |
|
|
|
var element1 = document.querySelector("#input-panel"); |
|
var element2 = document.querySelector("#basic-panel"); |
|
|
|
|
|
var parent = element1.parentNode; |
|
|
|
|
|
var nextSibling = element2.nextSibling; |
|
|
|
|
|
parent.insertBefore(element2, element1); |
|
parent.insertBefore(element1, nextSibling); |
|
if (swapped) {swapped = false;} |
|
else {swapped = true;} |
|
} |
|
|
|
function get_elements(consider_state_panel = false) { |
|
var chatbot = document.querySelector('#gpt-chatbot > div.wrap.svelte-18telvq'); |
|
if (!chatbot) { |
|
chatbot = document.querySelector('#gpt-chatbot'); |
|
} |
|
const panel1 = document.querySelector('#input-panel').getBoundingClientRect(); |
|
const panel2 = document.querySelector('#basic-panel').getBoundingClientRect() |
|
const panel3 = document.querySelector('#plugin-panel').getBoundingClientRect(); |
|
|
|
const panel_active = document.querySelector('#state-panel').getBoundingClientRect(); |
|
if (consider_state_panel || panel_active.height < 25) { |
|
document.state_panel_height = panel_active.height; |
|
} |
|
|
|
var height_target = panel1.height + panel2.height + panel3.height + 0 + 0 - 25 + 16 * 2; |
|
|
|
height_target = height_target + (document.state_panel_height - panel_active.height) |
|
var height_target = parseInt(height_target); |
|
var chatbot_height = chatbot.style.height; |
|
|
|
if (!swapped){ |
|
if (panel1.top!=0 && panel1.top < 0){ swap_input_area(); } |
|
} |
|
else if (swapped){ |
|
if (panel2.top!=0 && panel2.top > 0){ swap_input_area(); } |
|
} |
|
|
|
const err_tor = 5; |
|
if (Math.abs(panel1.left - chatbot.getBoundingClientRect().left) < err_tor){ |
|
|
|
height_target = window.innerHeight * 0.6; |
|
}else{ |
|
|
|
const chatbot_height_exceed = 15; |
|
const chatbot_height_exceed_m = 10; |
|
b_panel = Math.max(panel1.bottom, panel2.bottom, panel3.bottom) |
|
if (b_panel >= window.innerHeight - chatbot_height_exceed) { |
|
height_target = window.innerHeight - chatbot.getBoundingClientRect().top - chatbot_height_exceed_m; |
|
} |
|
else if (b_panel < window.innerHeight * 0.75) { |
|
height_target = window.innerHeight * 0.8; |
|
} |
|
} |
|
var chatbot_height = parseInt(chatbot_height); |
|
return { height_target, chatbot_height, chatbot }; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var elem_upload = null; |
|
var elem_upload_float = null; |
|
var elem_input_main = null; |
|
var elem_input_float = null; |
|
var elem_chatbot = null; |
|
var exist_file_msg = '⚠️请先删除上传区(左上方)中的历史文件,再尝试上传。' |
|
|
|
function add_func_paste(input) { |
|
let paste_files = []; |
|
if (input) { |
|
input.addEventListener("paste", async function (e) { |
|
const clipboardData = e.clipboardData || window.clipboardData; |
|
const items = clipboardData.items; |
|
if (items) { |
|
for (i = 0; i < items.length; i++) { |
|
if (items[i].kind === "file") { |
|
const file = items[i].getAsFile(); |
|
|
|
paste_files.push(file); |
|
e.preventDefault(); |
|
} |
|
} |
|
if (paste_files.length > 0) { |
|
|
|
await upload_files(paste_files); |
|
paste_files = [] |
|
|
|
} |
|
} |
|
}); |
|
} |
|
} |
|
|
|
function add_func_drag(elem) { |
|
if (elem) { |
|
const dragEvents = ["dragover"]; |
|
const leaveEvents = ["dragleave", "dragend", "drop"]; |
|
|
|
const onDrag = function (e) { |
|
e.preventDefault(); |
|
e.stopPropagation(); |
|
if (elem_upload_float.querySelector("input[type=file]")) { |
|
toast_up('⚠️释放以上传文件') |
|
} else { |
|
toast_up(exist_file_msg) |
|
} |
|
}; |
|
|
|
const onLeave = function (e) { |
|
toast_down(); |
|
e.preventDefault(); |
|
e.stopPropagation(); |
|
}; |
|
|
|
dragEvents.forEach(event => { |
|
elem.addEventListener(event, onDrag); |
|
}); |
|
|
|
leaveEvents.forEach(event => { |
|
elem.addEventListener(event, onLeave); |
|
}); |
|
|
|
elem.addEventListener("drop", async function (e) { |
|
const files = e.dataTransfer.files; |
|
await upload_files(files); |
|
}); |
|
} |
|
} |
|
|
|
async function upload_files(files) { |
|
const uploadInputElement = elem_upload_float.querySelector("input[type=file]"); |
|
let totalSizeMb = 0 |
|
if (files && files.length > 0) { |
|
|
|
if (uploadInputElement) { |
|
for (let i = 0; i < files.length; i++) { |
|
|
|
totalSizeMb += files[i].size / 1024 / 1024; |
|
} |
|
|
|
if (totalSizeMb > 20) { |
|
toast_push('⚠️文件夹大于 20MB 🚀上传文件中', 3000) |
|
|
|
} |
|
|
|
|
|
let event = new Event("change"); |
|
Object.defineProperty(event, "target", { value: uploadInputElement, enumerable: true }); |
|
Object.defineProperty(event, "currentTarget", { value: uploadInputElement, enumerable: true }); |
|
Object.defineProperty(uploadInputElement, "files", { value: files, enumerable: true }); |
|
uploadInputElement.dispatchEvent(event); |
|
} else { |
|
toast_push(exist_file_msg, 3000) |
|
} |
|
} |
|
} |
|
|
|
function begin_loading_status() { |
|
|
|
var loader = document.createElement('div'); |
|
loader.id = 'Js_File_Loading'; |
|
loader.style.position = "absolute"; |
|
loader.style.top = "50%"; |
|
loader.style.left = "50%"; |
|
loader.style.width = "60px"; |
|
loader.style.height = "60px"; |
|
loader.style.border = "16px solid #f3f3f3"; |
|
loader.style.borderTop = "16px solid #3498db"; |
|
loader.style.borderRadius = "50%"; |
|
loader.style.animation = "spin 2s linear infinite"; |
|
loader.style.transform = "translate(-50%, -50%)"; |
|
document.body.appendChild(loader); |
|
|
|
var styleSheet = document.createElement('style'); |
|
|
|
styleSheet.id = 'Js_File_Loading_Style' |
|
styleSheet.innerText = ` |
|
@keyframes spin { |
|
0% { transform: rotate(0deg); } |
|
100% { transform: rotate(360deg); } |
|
}`; |
|
document.head.appendChild(styleSheet); |
|
} |
|
|
|
function cancel_loading_status() { |
|
var loadingElement = document.getElementById('Js_File_Loading'); |
|
if (loadingElement) { |
|
document.body.removeChild(loadingElement); |
|
} |
|
var loadingStyle = document.getElementById('Js_File_Loading_Style'); |
|
if (loadingStyle) { |
|
document.head.removeChild(loadingStyle); |
|
} |
|
let clearButton = document.querySelectorAll('div[id*="elem_upload"] button[aria-label="Clear"]'); |
|
for (let button of clearButton) { |
|
button.addEventListener('click', function () { |
|
setTimeout(function () { |
|
register_upload_event(); |
|
}, 50); |
|
}); |
|
} |
|
} |
|
|
|
function register_upload_event() { |
|
elem_upload_float = document.getElementById('elem_upload_float') |
|
const upload_component = elem_upload_float.querySelector("input[type=file]"); |
|
if (upload_component) { |
|
upload_component.addEventListener('change', function (event) { |
|
toast_push('正在上传中,请稍等。', 2000); |
|
begin_loading_status(); |
|
}); |
|
} |
|
} |
|
|
|
function monitoring_input_box() { |
|
register_upload_event(); |
|
|
|
elem_upload = document.getElementById('elem_upload') |
|
elem_upload_float = document.getElementById('elem_upload_float') |
|
elem_input_main = document.getElementById('user_input_main') |
|
elem_input_float = document.getElementById('user_input_float') |
|
elem_chatbot = document.getElementById('gpt-chatbot') |
|
|
|
if (elem_input_main) { |
|
if (elem_input_main.querySelector("textarea")) { |
|
add_func_paste(elem_input_main.querySelector("textarea")) |
|
} |
|
} |
|
if (elem_input_float) { |
|
if (elem_input_float.querySelector("textarea")) { |
|
add_func_paste(elem_input_float.querySelector("textarea")) |
|
} |
|
} |
|
if (elem_chatbot) { |
|
add_func_drag(elem_chatbot) |
|
} |
|
} |
|
|
|
|
|
|
|
window.addEventListener("DOMContentLoaded", function () { |
|
|
|
gradioApp().addEventListener("render", monitoring_input_box); |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function audio_fn_init() { |
|
let audio_component = document.getElementById('elem_audio'); |
|
if (audio_component) { |
|
let buttonElement = audio_component.querySelector('button'); |
|
let specificElement = audio_component.querySelector('.hide.sr-only'); |
|
specificElement.remove(); |
|
|
|
buttonElement.childNodes[1].nodeValue = '启动麦克风'; |
|
buttonElement.addEventListener('click', function (event) { |
|
event.stopPropagation(); |
|
toast_push('您启动了麦克风!下一步请点击“实时语音对话”启动语音对话。'); |
|
}); |
|
|
|
|
|
let buttons = document.querySelectorAll('button'); |
|
let audio_button = null; |
|
for (let button of buttons) { |
|
if (button.textContent.includes('语音')) { |
|
audio_button = button; |
|
break; |
|
} |
|
} |
|
if (audio_button) { |
|
audio_button.addEventListener('click', function () { |
|
toast_push('您点击了“实时语音对话”启动语音对话。'); |
|
}); |
|
let parent_element = audio_component.parentElement; |
|
audio_button.appendChild(audio_component); |
|
buttonElement.style.cssText = 'border-color: #00ffe0;border-width: 2px; height: 25px;' |
|
parent_element.remove(); |
|
audio_component.style.cssText = 'width: 250px;right: 0px;display: inline-flex;flex-flow: row-reverse wrap;place-content: stretch space-between;align-items: center;background-color: #ffffff00;'; |
|
} |
|
|
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function GptAcademicJavaScriptInit(LAYOUT = "LEFT-RIGHT") { |
|
audio_fn_init(); |
|
chatbotIndicator = gradioApp().querySelector('#gpt-chatbot > div.wrap'); |
|
var chatbotObserver = new MutationObserver(() => { |
|
chatbotContentChanged(1); |
|
}); |
|
chatbotObserver.observe(chatbotIndicator, { attributes: true, childList: true, subtree: true }); |
|
if (LAYOUT === "LEFT-RIGHT") { chatbotAutoHeight(); } |
|
} |