From 5b231e0170d70fc119758258006f09bb54f8711a Mon Sep 17 00:00:00 2001 From: binary-husky Date: Sat, 23 Sep 2023 22:11:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B4=E4=BD=93=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- request_llm/bridge_all.py | 2 + themes/common.css | 28 ++++++++++++- themes/common.js | 83 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 112 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 70b9219..c848a16 100644 --- a/main.py +++ b/main.py @@ -266,7 +266,7 @@ def main(): cookies.update({'uuid': uuid.uuid4()}) return cookies demo.load(init_cookie, inputs=[cookies, chatbot], outputs=[cookies]) - demo.load(lambda: 0, inputs=None, outputs=None, _js='()=>{ChatBotHeight();}') + demo.load(lambda: 0, inputs=None, outputs=None, _js='()=>{GptAcademicJavaScriptInit();}') # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数 def auto_opentab_delay(): diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index cfe7fac..15ce7b4 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -125,6 +125,7 @@ model_info = { "tokenizer": tokenizer_gpt4, "token_cnt": get_token_num_gpt4, }, + "gpt-4-32k": { "fn_with_ui": chatgpt_ui, "fn_without_ui": chatgpt_noui, @@ -133,6 +134,7 @@ model_info = { "tokenizer": tokenizer_gpt4, "token_cnt": get_token_num_gpt4, }, + # azure openai "azure-gpt-3.5":{ "fn_with_ui": chatgpt_ui, diff --git a/themes/common.css b/themes/common.css index 0f201f0..033bb7a 100644 --- a/themes/common.css +++ b/themes/common.css @@ -23,4 +23,30 @@ /* status bar height */ .min.svelte-1yrv54 { min-height: var(--size-12); -} \ No newline at end of file +} + +/* copy btn */ +.message-btn-row { + width: 19px; + height: 19px; + position: absolute; + left: calc(100% + 3px); + top: 0; + display: flex; + justify-content: space-between; +} +/* .message-btn-row-leading, .message-btn-row-trailing { + display: inline-flex; + gap: 4px; +} */ +.message-btn-row button { + font-size: 18px; + align-self: center; + align-items: center; + flex-wrap: nowrap; + white-space: nowrap; + display: inline-flex; + flex-direction: row; + gap: 4px; + padding-block: 2px !important; +} diff --git a/themes/common.js b/themes/common.js index 7733c7b..150d472 100644 --- a/themes/common.js +++ b/themes/common.js @@ -1,4 +1,85 @@ -function ChatBotHeight() { +function gradioApp() { + // https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript + 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; +} + + +const copiedIcon = ''; +const copyIcon = ''; + + +function addCopyButton(botElement) { + // https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript + // Copy bot button + const messageBtnColumnElement = botElement.querySelector('.message-btn-row'); + if (messageBtnColumnElement) { + // Do something if .message-btn-column exists, for example, remove it + // messageBtnColumnElement.remove(); + 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) { + // https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript + for (var i = 0; i < attempt; i++) { + setTimeout(() => { + gradioApp().querySelectorAll('#gpt-chatbot .message-wrap .message.bot').forEach(addCopyButton); + }, i === 0 ? 0 : 200); + } +} + +function GptAcademicJavaScriptInit() { + chatbotIndicator = gradioApp().querySelector('#gpt-chatbot > div.wrap'); + var chatbotObserver = new MutationObserver(() => { + chatbotContentChanged(1); + }); + chatbotObserver.observe(chatbotIndicator, { attributes: true, childList: true, subtree: true }); + function update_height(){ var { panel_height_target, chatbot_height, chatbot } = get_elements(true); if (panel_height_target!=chatbot_height)