diff --git a/.gitattributes b/.gitattributes index a6344aac8c09253b3b630fb776ae94478aa0275b..ded73ad40ad74c3541493ec5f59d4d06bd2e4668 100644 --- a/.gitattributes +++ b/.gitattributes @@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text *.zip filter=lfs diff=lfs merge=lfs -text *.zst filter=lfs diff=lfs merge=lfs -text *tfevents* filter=lfs diff=lfs merge=lfs -text +assets/generate_image.png filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..1e5b720f4b1166347c871c420cf6d1f2ee255b8d --- /dev/null +++ b/.gitignore @@ -0,0 +1,163 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea/ + +# VSCode +.vscode/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..ffc5f1c50c5d6174a4b305002c348a3b980d3e31 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.9 as builder +RUN apt-get update && apt-get install -y build-essential +COPY requirements.txt . +COPY requirements_advanced.txt . +RUN pip install --user -r requirements.txt +# RUN pip install --user -r requirements_advanced.txt + +FROM python:3.9 +MAINTAINER iskoldt +COPY --from=builder /root/.local /root/.local +ENV PATH=/root/.local/bin:$PATH +COPY . /app +WORKDIR /app +ENV dockerrun yes +CMD ["python3", "-u", "main.py", "2>&1", "|", "tee", "/var/log/application.log"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..98985cd96015d4975ee14584b210369c73582e2d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Tsumugii + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 26049a999ed200514cde73463615a986ecc9e3a2..44a067ddcf4e87566111704e4188fb208710c102 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,130 @@ --- title: PoetryChat -emoji: 👁 -colorFrom: yellow -colorTo: red +emoji: 🤗 +colorFrom: indigo +colorTo: indigo sdk: gradio -sdk_version: 4.36.1 +sdk_version: 3.43.2 app_file: app.py -pinned: false +pinned: true license: mit + --- -Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference + + +
+ Latest Version: getting latest version... +
++ Getting update... +
+ + 元素,则不添加按钮
+ }
+ var firstChild = code.firstChild;
+ if (!firstChild) {
+ return; // 如果 元素没有子节点,则不添加按钮
+ }
+ var button = document.createElement('button');
+ button.textContent = '\uD83D\uDCCE'; // 使用 📎 符号作为“复制”按钮的文本
+ button.style.position = 'relative';
+ button.style.float = 'right';
+ button.style.fontSize = '1em'; // 可选:调整按钮大小
+ button.style.background = 'none'; // 可选:去掉背景颜色
+ button.style.border = 'none'; // 可选:去掉边框
+ button.style.cursor = 'pointer'; // 可选:显示指针样式
+ button.addEventListener('click', function () {
+ var range = document.createRange();
+ range.selectNodeContents(code);
+ range.setStartBefore(firstChild); // 将范围设置为第一个子节点之前
+ var selection = window.getSelection();
+ selection.removeAllRanges();
+ selection.addRange(range);
+
+ try {
+ var success = document.execCommand('copy');
+ if (success) {
+ button.textContent = '\u2714';
+ setTimeout(function () {
+ button.textContent = '\uD83D\uDCCE'; // 恢复按钮为“复制”
+ }, 2000);
+ } else {
+ button.textContent = '\u2716';
+ }
+ } catch (e) {
+ console.error(e);
+ button.textContent = '\u2716';
+ }
+
+ selection.removeAllRanges();
+ });
+ code.insertBefore(button, firstChild); // 将按钮插入到第一个子元素之前
+ }
+
+ function handleNewElements(mutationsList, observer) {
+ for (var mutation of mutationsList) {
+ if (mutation.type === 'childList') {
+ for (var node of mutation.addedNodes) {
+ if (node.nodeName === 'PRE') {
+ addCopyButton(node);
+ }
+ }
+ }
+ }
+ }
+
+ var observer = new MutationObserver(handleNewElements);
+ observer.observe(document.documentElement, { childList: true, subtree: true });
+
+ document.querySelectorAll('pre').forEach(addCopyButton);
+})();
diff --git a/assets/javascript/chat-history.js b/assets/javascript/chat-history.js
new file mode 100644
index 0000000000000000000000000000000000000000..349cb49b9a6e1421b65d744bfb19370f58a2eecf
--- /dev/null
+++ b/assets/javascript/chat-history.js
@@ -0,0 +1,71 @@
+
+var historyLoaded = false;
+var loadhistorytime = 0; // for debugging
+
+
+function saveHistoryHtml() {
+ var historyHtml = document.querySelector('#chuanhu-chatbot>.wrapper>.wrap');
+ if (!historyHtml) return; // no history, do nothing
+ localStorage.setItem('chatHistory', historyHtml.innerHTML);
+ // console.log("History Saved")
+ historyLoaded = false;
+}
+
+function loadHistoryHtml() {
+ var historyHtml = localStorage.getItem('chatHistory');
+ const tempDiv = document.createElement('div');
+ tempDiv.innerHTML = historyHtml;
+ if (!historyHtml || tempDiv.innerText.trim() === "") {
+ historyLoaded = true;
+ return; // no history, do nothing
+ }
+ userLogged = localStorage.getItem('userLogged');
+ hideHistoryWhenNotLoggedIn = gradioApp().querySelector('#hideHistoryWhenNotLoggedIn_config').innerText === "True";
+ if (userLogged || (!userLogged && !hideHistoryWhenNotLoggedIn)){
+ historyLoaded = true;
+ return; // logged in, do nothing. OR, not logged in but not hide history list, do nothing.
+ }
+
+ // 只有用户未登录,还隐藏历史记录列表时,才选用只读历史记录
+ if (!historyLoaded) {
+ // preprocess, gradio buttons in history lost their event listeners
+ var gradioCopyButtons = tempDiv.querySelectorAll('button.copy_code_button');
+ for (var i = 0; i < gradioCopyButtons.length; i++) {
+ gradioCopyButtons[i].parentNode.removeChild(gradioCopyButtons[i]);
+ }
+ var messageBtnRows = tempDiv.querySelectorAll('.message-btn-row');
+ for (var i = 0; i < messageBtnRows.length; i++) {
+ messageBtnRows[i].parentNode.removeChild(messageBtnRows[i]);
+ }
+ var latestMessages = tempDiv.querySelectorAll('.message.latest');
+ for (var i = 0; i < latestMessages.length; i++) {
+ latestMessages[i].classList.remove('latest');
+ }
+
+ var fakeHistory = document.createElement('div');
+ fakeHistory.classList.add('history-message');
+ fakeHistory.innerHTML = tempDiv.innerHTML;
+ const forViewStyle = document.createElement('style');
+ forViewStyle.innerHTML = '.wrapper>.wrap>.history-message>:last-child::after { content: "' + i18n(forView_i18n) + '"!important; }';
+ document.head.appendChild(forViewStyle);
+ chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
+ // var fakeHistory = document.createElement('div');
+ // fakeHistory.classList.add('history-message');
+ // fakeHistory.innerHTML = historyHtml;
+ // chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
+ historyLoaded = true;
+ // console.log("History Loaded");
+ loadhistorytime += 1; // for debugging
+ } else {
+ historyLoaded = false;
+ }
+}
+
+function clearHistoryHtml() {
+ localStorage.removeItem("chatHistory");
+ historyMessages = chatbotWrap.querySelector('.history-message');
+ if (historyMessages) {
+ chatbotWrap.removeChild(historyMessages);
+ console.log("History Cleared");
+ }
+}
diff --git a/assets/javascript/chat-list.js b/assets/javascript/chat-list.js
new file mode 100644
index 0000000000000000000000000000000000000000..52ecdcfe818fa5b775d6af62edd7ebd3069a82ff
--- /dev/null
+++ b/assets/javascript/chat-list.js
@@ -0,0 +1,88 @@
+
+var currentChatName = null;
+
+function setChatListHeader() {
+ var grHistoryRefreshBtn = gradioApp().querySelector('button#gr-history-refresh-btn');
+ var grHistoryUploadBtn = gradioApp().querySelector('button#gr-history-upload-btn');
+
+ grHistoryRefreshBtn.className = "";
+ grHistoryUploadBtn.className = "";
+
+
+ grHistoryRefreshBtn.innerHTML = HistoryRefreshIcon;
+ grHistoryUploadBtn.innerHTML = HistoryUploadIcon;
+}
+
+function setChatList() {
+ var selectedChat = null;
+ var chatList = gradioApp().querySelector('fieldset#history-select-dropdown');
+ selectedChat = chatList.querySelector(".wrap label.selected")
+ if (!selectedChat) {
+ currentChatName = null;
+ return;
+ }
+
+ // if (userLogged) {
+ // currentChatName = username + "/" + selectedChat.querySelector('span').innerText;
+ // } else {
+ currentChatName = selectedChat.querySelector('span').innerText;
+ // }
+
+ if (selectedChat.classList.contains('added-chat-btns')) {
+ return;
+ }
+
+ chatList.querySelector('.chat-selected-btns')?.remove(); // remove old buttons
+ chatList.querySelectorAll('.added-chat-btns').forEach(chat => chat.classList.remove('added-chat-btns'));
+
+ var ChatSelectedBtns = document.createElement('div');
+ ChatSelectedBtns.classList.add('chat-selected-btns');
+ selectedChat.classList.add('added-chat-btns');
+ ChatSelectedBtns.innerHTML = selectedChatBtns;
+
+ var renameBtn = ChatSelectedBtns.querySelector('#history-rename-btn');
+ renameBtn.addEventListener('click', function () {
+ gradioApp().querySelector('#gr-history-save-btn').click();
+ });
+
+ var deleteBtn = ChatSelectedBtns.querySelector('#history-delete-btn');
+ deleteBtn.addEventListener('click', function () {
+ gradioApp().querySelector('#gr-history-delete-btn').click();
+ });
+ selectedChat.appendChild(ChatSelectedBtns);
+
+ return;
+}
+
+
+function saveChatHistory(a, b, c, d) {
+ var fileName = b;
+
+ while (true) {
+ var result = prompt(renameChat_i18n, fileName);
+
+ if (result === null) {
+ throw new Error("rename operation cancelled");
+ // 不返回原文件名,而是使用 throw new Error() 打断程序,避免 gradio 进行保存操作
+ // break;
+ } else if (isValidFileName(result)) {
+ return [a, result, c, d];
+ } else {
+ alert(validFileName_i18n + "!@#$%^&*()<>?/\\|}{~:");
+ }
+ }
+ return [a, b, c, d]; // 兜底保障
+}
+
+function isValidFileName(fileName) {
+ // 使用正则表达式来检查文件名是否包含不合格字符
+ var regex = /[!@#$%^&*()<>?/\\|}{~:]/;
+ return !regex.test(fileName) && fileName.trim() !== "";
+}
+
+const selectedChatBtns = `
+
+
+`
+const HistoryRefreshIcon = '';
+const HistoryUploadIcon = '';
\ No newline at end of file
diff --git a/assets/javascript/external-scripts.js b/assets/javascript/external-scripts.js
new file mode 100644
index 0000000000000000000000000000000000000000..8d0352669045537af5698b1824dbc1dba21df478
--- /dev/null
+++ b/assets/javascript/external-scripts.js
@@ -0,0 +1,2 @@
+
+// external javascript here
diff --git a/assets/javascript/fake-gradio.js b/assets/javascript/fake-gradio.js
new file mode 100644
index 0000000000000000000000000000000000000000..a32609e004b1df1fbcf87f2562f4d0029c0d549a
--- /dev/null
+++ b/assets/javascript/fake-gradio.js
@@ -0,0 +1,116 @@
+
+// Fake gradio components!
+
+// buttons
+function newChatClick() {
+ gradioApp().querySelector('#empty-btn').click();
+}
+function jsonDownloadClick() {
+ gradioApp().querySelector('#gr-history-download-btn').click();
+}
+function mdDownloadClick() {
+ gradioApp().querySelector('#gr-markdown-export-btn').click();
+ gradioApp().querySelector('#gr-history-mardown-download-btn').click();
+
+ // downloadHistory(username, currentChatName, ".md");
+}
+
+// index files
+function setUploader() {
+ transUpload();
+ var uploaderObserver = new MutationObserver(function (mutations) {
+ var fileInput = null;
+ var fileCount = 0;
+ fileInput = gradioApp().querySelector("#upload-index-file table.file-preview");
+ var fileCountSpan = gradioApp().querySelector("#uploaded-files-count");
+ if (fileInput) {
+ chatbotArea.classList.add('with-file');
+ fileCount = fileInput.querySelectorAll('tbody > tr.file').length;
+ fileCountSpan.innerText = fileCount;
+ } else {
+ chatbotArea.classList.remove('with-file');
+ statusDisplayMessage("");
+ fileCount = 0;
+ transUpload();
+ }
+ });
+ uploaderObserver.observe(uploaderIndicator, {attributes: true})
+}
+var grUploader;
+var chatbotUploader;
+var handleClick = function() {
+ grUploader.click();
+
+};
+function transUpload() {
+ chatbotUploader = gradioApp().querySelector("#upload-files-btn");
+ chatbotUploader.removeEventListener('click', handleClick);
+ grUploader = gradioApp().querySelector("#upload-index-file > .center.flex");
+
+ // let uploaderEvents = ["click", "drag", "dragend", "dragenter", "dragleave", "dragover", "dragstart", "drop"];
+ // transEventListeners(chatbotUploader, grUploader, uploaderEvents);
+
+ chatbotUploader.addEventListener('click', handleClick);
+}
+
+// checkbox
+var grSingleSessionCB;
+var grOnlineSearchCB;
+var chatbotSingleSessionCB;
+var chatbotOnlineSearchCB;
+function setCheckboxes() {
+ chatbotSingleSessionCB = gradioApp().querySelector('input[name="single-session-cb"]');
+ chatbotOnlineSearchCB = gradioApp().querySelector('input[name="online-search-cb"]');
+ grSingleSessionCB = gradioApp().querySelector("#gr-single-session-cb > label > input");
+ grOnlineSearchCB = gradioApp().querySelector("#gr-websearch-cb > label> input");
+
+ chatbotSingleSessionCB.addEventListener('change', (e) => {
+ grSingleSessionCB.checked = chatbotSingleSessionCB.checked;
+ gradioApp().querySelector('#change-single-session-btn').click();
+ });
+ chatbotOnlineSearchCB.addEventListener('change', (e) => {
+ grOnlineSearchCB.checked = chatbotOnlineSearchCB.checked;
+ gradioApp().querySelector('#change-online-search-btn').click();
+ });
+ grSingleSessionCB.addEventListener('change', (e) => {
+ chatbotSingleSessionCB.checked = grSingleSessionCB.checked;
+ });
+ grOnlineSearchCB.addEventListener('change', (e) => {
+ chatbotOnlineSearchCB.checked = grOnlineSearchCB.checked;
+ });
+}
+
+function bgChangeSingleSession() {
+ // const grSingleSessionCB = gradioApp().querySelector("#gr-single-session-cb > label > input");
+ let a = chatbotSingleSessionCB.checked;
+ return [a];
+}
+function bgChangeOnlineSearch() {
+ // const grOnlineSearchCB = gradioApp().querySelector("#gr-websearch-cb > label> input");
+ let a = chatbotOnlineSearchCB.checked;
+ return [a];
+}
+
+function updateCheckboxes() {
+ chatbotSingleSessionCB.checked = grSingleSessionCB.checked;
+ chatbotOnlineSearchCB.checked = grOnlineSearchCB.checked;
+}
+
+// UTILS
+function transEventListeners(target, source, events) {
+ events.forEach((sourceEvent) => {
+ target.addEventListener(sourceEvent, function (targetEvent) {
+ if(targetEvent.preventDefault) targetEvent.preventDefault();
+ if(targetEvent.stopPropagation) targetEvent.stopPropagation();
+
+ source.dispatchEvent(new Event(sourceEvent, {detail: targetEvent.detail}));
+ // console.log(targetEvent.detail);
+ });
+ });
+}
+
+function bgSelectHistory(a,b){
+ const historySelectorInput = gradioApp().querySelector('#history-select-dropdown input');
+ let file = historySelectorInput.value;
+ return [a,file]
+}
diff --git a/assets/javascript/file-input.js b/assets/javascript/file-input.js
new file mode 100644
index 0000000000000000000000000000000000000000..3169d5dc0cbc8c49a4fbd7c2b44f0e79341e0f06
--- /dev/null
+++ b/assets/javascript/file-input.js
@@ -0,0 +1,114 @@
+
+// paste和upload部分参考:
+// https://github.com/binary-husky/gpt_academic/tree/master/themes/common.js
+// @Kilig947
+
+
+function setPasteUploader() {
+ input = user_input_tb.querySelector("textarea")
+ 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();
+ // 将每一个粘贴的文件添加到files数组中
+ paste_files.push(file);
+ e.preventDefault(); // 避免粘贴文件名到输入框
+ }
+ }
+ if (paste_files.length > 0) {
+ // 按照文件列表执行批量上传逻辑
+ await upload_files(paste_files);
+ paste_files = [];
+ }
+ }
+ });
+ }
+}
+
+var hintArea;
+function setDragUploader() {
+ input = chatbotArea;
+ if (input) {
+ const dragEvents = ["dragover", "dragenter"];
+ const leaveEvents = ["dragleave", "dragend", "drop"];
+
+ const onDrag = function (e) {
+ e.preventDefault();
+ e.stopPropagation();
+ if (!chatbotArea.classList.contains("with-file")) {
+ chatbotArea.classList.add("dragging");
+ draggingHint();
+ } else {
+ statusDisplayMessage(clearFileHistoryMsg_i18n, 2000);
+ }
+ };
+
+ const onLeave = function (e) {
+ e.preventDefault();
+ e.stopPropagation();
+ chatbotArea.classList.remove("dragging");
+ if (hintArea) {
+ hintArea.remove();
+ }
+ };
+
+ dragEvents.forEach(event => {
+ input.addEventListener(event, onDrag);
+ });
+
+ leaveEvents.forEach(event => {
+ input.addEventListener(event, onLeave);
+ });
+
+ input.addEventListener("drop", async function (e) {
+ const files = e.dataTransfer.files;
+ await upload_files(files);
+ });
+ }
+}
+
+async function upload_files(files) {
+ const uploadInputElement = gradioApp().querySelector("#upload-index-file > .center.flex input[type=file]");
+ let totalSizeMb = 0
+ if (files && files.length > 0) {
+ // 执行具体的上传逻辑
+ if (uploadInputElement) {
+ for (let i = 0; i < files.length; i++) {
+ // 将从文件数组中获取的文件大小(单位为字节)转换为MB,
+ totalSizeMb += files[i].size / 1024 / 1024;
+ }
+ // 检查文件总大小是否超过20MB
+ if (totalSizeMb > 20) {
+ // toast_push('⚠️文件夹大于20MB 🚀上传文件中', 2000)
+ // return; // 如果超过了指定大小, 可以不进行后续上传操作
+ }
+ // 监听change事件, 原生Gradio可以实现
+ // uploadInputElement.addEventListener('change', function(){replace_input_string()});
+ 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);
+ // statusDisplayMessage("");
+ } else {
+ statusDisplayMessage(clearFileHistoryMsg_i18n, 3000);
+ return;
+ }
+ }
+}
+
+function draggingHint() {
+ hintArea = chatbotArea.querySelector(".dragging-hint");
+ if (hintArea) {
+ return;
+ }
+ hintArea = document.createElement("div");
+ hintArea.classList.add("dragging-hint");
+ hintArea.innerHTML = `${dropUploadMsg_i18n}
`;
+ chatbotArea.appendChild(hintArea);
+}
diff --git a/assets/javascript/localization.js b/assets/javascript/localization.js
new file mode 100644
index 0000000000000000000000000000000000000000..68a59164d1732c8601b96749fe014a0824f1235e
--- /dev/null
+++ b/assets/javascript/localization.js
@@ -0,0 +1,39 @@
+
+// i18n
+
+const language = navigator.language.slice(0,2);
+
+var forView_i18n;
+var deleteConfirm_i18n_pref;
+var deleteConfirm_i18n_suff;
+var usingLatest_i18n;
+var updatingMsg_i18n;
+var updateSuccess_i18n;
+var updateFailure_i18n;
+var regenerate_i18n;
+var deleteRound_i18n;
+var renameChat_i18n;
+var validFileName_i18n;
+var clearFileHistoryMsg_i18n;
+var dropUploadMsg_i18n;
+
+function setLoclize() {
+ forView_i18n = gradioApp().querySelector('#forView_i18n').innerText;
+ deleteConfirm_i18n_pref = gradioApp().querySelector('#deleteConfirm_i18n_pref').innerText;
+ deleteConfirm_i18n_suff = gradioApp().querySelector('#deleteConfirm_i18n_suff').innerText;
+ usingLatest_i18n = gradioApp().querySelector('#usingLatest_i18n').innerText;
+ updatingMsg_i18n = gradioApp().querySelector('#updatingMsg_i18n').innerText;
+ updateSuccess_i18n = gradioApp().querySelector('#updateSuccess_i18n').innerText;
+ updateFailure_i18n = gradioApp().querySelector('#updateFailure_i18n').innerText;
+ regenerate_i18n = gradioApp().querySelector('#regenerate_i18n').innerText;
+ deleteRound_i18n = gradioApp().querySelector('#deleteRound_i18n').innerText;
+ renameChat_i18n = gradioApp().querySelector('#renameChat_i18n').innerText;
+ validFileName_i18n = gradioApp().querySelector('#validFileName_i18n').innerText;
+ clearFileHistoryMsg_i18n = gradioApp().querySelector('#clearFileHistoryMsg_i18n').innerText;
+ dropUploadMsg_i18n = gradioApp().querySelector('#dropUploadMsg_i18n').innerText;
+}
+
+function i18n(msg) {
+ return msg;
+ // return msg.hasOwnProperty(language) ? msg[language] : msg['en'];
+}
diff --git a/assets/javascript/message-button.js b/assets/javascript/message-button.js
new file mode 100644
index 0000000000000000000000000000000000000000..0fa5803f1c915b765ab2982014b380387c8a15e2
--- /dev/null
+++ b/assets/javascript/message-button.js
@@ -0,0 +1,195 @@
+
+// 为 bot 消息添加复制与切换显示按钮 以及最新消息加上重新生成,删除最新消息,嗯。
+
+function addChuanhuButton(botElement) {
+
+ // botElement = botRow.querySelector('.message.bot');
+ var isLatestMessage = botElement.classList.contains('latest');
+
+ var rawMessage = botElement.querySelector('.raw-message');
+ var mdMessage = botElement.querySelector('.md-message');
+
+ if (!rawMessage) { // 如果没有 raw message,说明是早期历史记录,去除按钮
+ // var buttons = botElement.querySelectorAll('button.chuanhu-btn');
+ // for (var i = 0; i < buttons.length; i++) {
+ // buttons[i].parentNode.removeChild(buttons[i]);
+ // }
+ botElement.querySelector('.message-btn-row')?.remove();
+ botElement.querySelector('.message-btn-column')?.remove();
+ return;
+ }
+ // botElement.querySelectorAll('button.copy-bot-btn, button.toggle-md-btn').forEach(btn => btn.remove()); // 就算原先有了,也必须重新添加,而不是跳过
+ if (!isLatestMessage) botElement.querySelector('.message-btn-row')?.remove();
+ botElement.querySelector('.message-btn-column')?.remove();
+
+ // Copy bot button
+ var copyButton = document.createElement('button');
+ copyButton.classList.add('chuanhu-btn');
+ copyButton.classList.add('copy-bot-btn');
+ copyButton.setAttribute('aria-label', 'Copy');
+ copyButton.innerHTML = copyIcon;
+
+ copyButton.addEventListener('click', async () => {
+ const textToCopy = rawMessage.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);
+ }
+ });
+ // botElement.appendChild(copyButton);
+
+ // Toggle button
+ var toggleButton = document.createElement('button');
+ toggleButton.classList.add('chuanhu-btn');
+ toggleButton.classList.add('toggle-md-btn');
+ toggleButton.setAttribute('aria-label', 'Toggle');
+ var renderMarkdown = mdMessage.classList.contains('hideM');
+ toggleButton.innerHTML = renderMarkdown ? mdIcon : rawIcon;
+ toggleButton.addEventListener('click', () => {
+ renderMarkdown = mdMessage.classList.contains('hideM');
+ if (renderMarkdown) {
+ renderMarkdownText(botElement);
+ toggleButton.innerHTML=rawIcon;
+ } else {
+ removeMarkdownText(botElement);
+ toggleButton.innerHTML=mdIcon;
+ }
+ chatbotContentChanged(1); // to set md or raw in read-only history html
+ });
+ // botElement.insertBefore(toggleButton, copyButton);
+
+ var messageBtnColumn = document.createElement('div');
+ messageBtnColumn.classList.add('message-btn-column');
+ messageBtnColumn.appendChild(toggleButton);
+ messageBtnColumn.appendChild(copyButton);
+ botElement.appendChild(messageBtnColumn);
+
+ function renderMarkdownText(message) {
+ var mdDiv = message.querySelector('.md-message');
+ if (mdDiv) mdDiv.classList.remove('hideM');
+ var rawDiv = message.querySelector('.raw-message');
+ if (rawDiv) rawDiv.classList.add('hideM');
+ }
+ function removeMarkdownText(message) {
+ var rawDiv = message.querySelector('.raw-message');
+ if (rawDiv) {
+ // 判断pre是否存在fake-pre类,如果不存在,则为20231118之前的历史记录格式,需要转换,增加fake-pre类用于适配
+ if (!rawDiv.querySelector('pre')?.classList.contains('fake-pre')) {
+ rawDiv.innerHTML = rawDiv.innerHTML.replace(//g, '');
+ }
+ // rawDiv.innerHTML = rawDiv.querySelector('pre')?.innerHTML || rawDiv.innerHTML;
+ rawDiv.classList.remove('hideM');
+ }
+ var mdDiv = message.querySelector('.md-message');
+ if (mdDiv) mdDiv.classList.add('hideM');
+ }
+}
+
+function setLatestMessage() {
+ var latestMessage = gradioApp().querySelector('#chuanhu-chatbot > .wrapper > .wrap > .message-wrap .message.bot.latest');
+ if (latestMessage) addLatestMessageButtons(latestMessage);
+}
+
+function addLatestMessageButtons(botElement) {
+ botElement.querySelector('.message-btn-row')?.remove();
+
+ var messageBtnRow = document.createElement('div');
+ messageBtnRow.classList.add('message-btn-row');
+ var messageBtnRowLeading = document.createElement('div');
+ messageBtnRowLeading.classList.add('message-btn-row-leading');
+ var messageBtnRowTrailing = document.createElement('div');
+ messageBtnRowTrailing.classList.add('message-btn-row-trailing');
+
+ messageBtnRow.appendChild(messageBtnRowLeading);
+ messageBtnRow.appendChild(messageBtnRowTrailing);
+
+ botElement.appendChild(messageBtnRow);
+
+ //leading
+ var regenerateButton = document.createElement('button');
+ regenerateButton.classList.add('chuanhu-btn');
+ regenerateButton.classList.add('regenerate-btn');
+ regenerateButton.setAttribute('aria-label', 'Regenerate');
+ regenerateButton.innerHTML = regenIcon + `${i18n(regenerate_i18n)}`;
+
+ var gradioRetryBtn = gradioApp().querySelector('#gr-retry-btn');
+ regenerateButton.addEventListener('click', () => {
+ gradioRetryBtn.click();
+ });
+
+ var deleteButton = document.createElement('button');
+ deleteButton.classList.add('chuanhu-btn');
+ deleteButton.classList.add('delete-latest-btn');
+ deleteButton.setAttribute('aria-label', 'Delete');
+ deleteButton.innerHTML = deleteIcon + `${i18n(deleteRound_i18n)}`;
+
+ var gradioDelLastBtn = gradioApp().querySelector('#gr-dellast-btn');
+ deleteButton.addEventListener('click', () => {
+ gradioDelLastBtn.click();
+ });
+
+ messageBtnRowLeading.appendChild(regenerateButton);
+ messageBtnRowLeading.appendChild(deleteButton);
+
+ // trailing
+ var likeButton = document.createElement('button');
+ likeButton.classList.add('chuanhu-btn');
+ likeButton.classList.add('like-latest-btn');
+ likeButton.setAttribute('aria-label', 'Like');
+ likeButton.innerHTML = likeIcon;
+
+ var gradioLikeBtn = gradioApp().querySelector('#gr-like-btn');
+ likeButton.addEventListener('click', () => {
+ gradioLikeBtn.click();
+ });
+
+ var dislikeButton = document.createElement('button');
+ dislikeButton.classList.add('chuanhu-btn');
+ dislikeButton.classList.add('dislike-latest-btn');
+ dislikeButton.setAttribute('aria-label', 'Dislike');
+ dislikeButton.innerHTML = dislikeIcon;
+
+ var gradioDislikeBtn = gradioApp().querySelector('#gr-dislike-btn');
+ dislikeButton.addEventListener('click', () => {
+ gradioDislikeBtn.click();
+ });
+
+ messageBtnRowTrailing.appendChild(likeButton);
+ messageBtnRowTrailing.appendChild(dislikeButton);
+}
+
+
+// button svg code
+const copyIcon = '';
+const copiedIcon = '';
+const mdIcon = '';
+const rawIcon = '';
+
+const regenIcon = '';
+const deleteIcon = '';
+ // const deleteIcon = ''
+
+const likeIcon = '';
+const dislikeIcon= ''
diff --git a/assets/javascript/sliders.js b/assets/javascript/sliders.js
new file mode 100644
index 0000000000000000000000000000000000000000..564c6cdd20752997296e2c8b872cc7136a5a1d39
--- /dev/null
+++ b/assets/javascript/sliders.js
@@ -0,0 +1,26 @@
+
+var rangeInputs = null;
+var numberInputs = null;
+
+function setSliderRange() {
+ var range = document.querySelectorAll('input[type="range"]');
+ range.forEach(range => {
+ range.style.backgroundSize = (range.value - range.min) / (range.max - range.min) * 100 + '% 100%';
+ });
+}
+
+function setSlider() {
+ rangeInputs = document.querySelectorAll('input[type="range"]');
+ numberInputs = document.querySelectorAll('input[type="number"]')
+ setSliderRange();
+ rangeInputs.forEach(rangeInput => {
+ rangeInput.addEventListener('input', setSliderRange);
+ });
+ numberInputs.forEach(numberInput => {
+ numberInput.addEventListener('input', setSliderRange);
+ })
+}
+
+function updateSlider() {
+ setSliderRange();
+}
\ No newline at end of file
diff --git a/assets/javascript/updater.js b/assets/javascript/updater.js
new file mode 100644
index 0000000000000000000000000000000000000000..1c255c008957e9690fa5387583050fdf3edc185c
--- /dev/null
+++ b/assets/javascript/updater.js
@@ -0,0 +1,243 @@
+
+var updateInfoGotten = false;
+var isLatestVersion = localStorage.getItem('isLatestVersion') === "true" || false;
+var shouldCheckUpdate = false;
+
+function setUpdater() {
+ const enableCheckUpdate = gradioApp().querySelector('#enableCheckUpdate_config').innerText;
+
+ if (enableCheckUpdate == "False" || enableCheckUpdate == "false") {
+ gradioApp().classList.add('disable-update');
+ return;
+ }
+
+ if (!isLatestVersion) {
+ gradioApp().classList.add('is-outdated');
+ }
+ const lastCheckTime = localStorage.getItem('lastCheckTime') || 0;
+ currentTime = new Date().getTime();
+ const longTimeNoCheck = currentTime - lastCheckTime > 3 * 24 * 60 * 60 * 1000;
+ shouldCheckUpdate = !updateInfoGotten && (!isLatestVersion && longTimeNoCheck || isLatestVersion);
+ // console.log(`shouldCheckUpdate`, shouldCheckUpdate);
+ if (shouldCheckUpdate) updateLatestVersion();
+}
+
+var statusObserver = new MutationObserver(function (mutationsList) {
+ for (const mutation of mutationsList) {
+ if (mutation.type === 'attributes' || mutation.type === 'childList') {
+ if (statusDisplay.innerHTML.includes(']*>([^<]*)<\/a>/g;
+ const versionMatch = reVersion.exec(currentVersionElement.innerHTML);
+ const currentVersion = (versionMatch && versionMatch[1].length == 8) ? versionMatch[1] : null;
+ const latestVersionElement = document.getElementById('latest-version-title');
+ const versionInfoElement = document.getElementById('version-info-title');
+ releaseNoteElement = document.getElementById('release-note-content');
+ updatingInfoElement = document.getElementById('updating-info');
+
+ const versionTime = document.getElementById('version-time').innerText;
+ const localVersionTime = versionTime !== "unknown" ? (new Date(versionTime)).getTime() : 0;
+ disableUpdateBtns();
+ updateInfoGotten = true; //无论成功与否都只执行一次,否则容易api超限...
+ try {
+ const data = await getLatestRelease();
+ const releaseNote = data.body;
+ if (releaseNote) {
+ releaseNoteElement.innerHTML = marked.parse(releaseNote, {mangle: false, headerIds: false});
+ }
+ const latestVersion = data.tag_name;
+ if (currentVersion) {
+ if (latestVersion <= currentVersion) {
+ noUpdate();
+ localStorage.setItem('isLatestVersion', 'true');
+ isLatestVersion = true;
+ gradioApp().classList.remove('is-outdated');
+ } else {
+ latestVersionElement.textContent = latestVersion;
+ console.log(`New version ${latestVersion} found!`);
+ if (!isInIframe) openUpdateToast();
+ gradioApp().classList.add('is-outdated');
+ localStorage.setItem('isLatestVersion', 'false');
+ isLatestVersion = false;
+ }
+ enableUpdateBtns();
+ } else { //如果当前版本号获取失败,使用时间比较
+ const latestVersionTime = (new Date(data.created_at)).getTime();
+ if (latestVersionTime) {
+ const latestVersionInfo = `${latestVersion}`
+ const manualUpdateInfo = `manual update`
+ if (localVersionTime == 0) {
+ const infoMessage = `Local version check failed. \nBut latest revision is ${latestVersionInfo}. \n\nWhen Update needed, \n- If you are using Docker, try to update package. \n- If you didn't use git, try ${manualUpdateInfo}.`
+ versionInfoElement.innerHTML = marked.parse(infoMessage, {mangle: false, headerIds: false});
+ console.log(`New version ${latestVersion} found!`);
+ disableUpdateBtn_enableCancelBtn();
+ localStorage.setItem('isLatestVersion', 'false');
+ isLatestVersion = false;
+ gradioApp().classList.add('is-outdated');
+ } else if (localVersionTime < latestVersionTime) {
+ const infoMessage = `Local version check failed, it seems to be a local rivision. \n\nBut latest revision is ${latestVersionInfo}. Try ${manualUpdateInfo}.`
+ versionInfoElement.innerHTML = marked.parse(infoMessage, {mangle: false, headerIds: false});
+ console.log(`New version ${latestVersion} found!`);
+ disableUpdateBtn_enableCancelBtn();
+ // if (!isInIframe) openUpdateToast();
+ localStorage.setItem('isLatestVersion', 'false');
+ isLatestVersion = false;
+ gradioApp().classList.add('is-outdated');
+ } else {
+ noUpdate("Local version check failed, it seems to be a local rivision.
But your revision is newer than the latest release.");
+ gradioApp().classList.add('is-outdated');
+ enableUpdateBtns()
+ localStorage.setItem('isLatestVersion', 'false');
+ isLatestVersion = false;
+ }
+ }
+ }
+ currentTime = new Date().getTime();
+ localStorage.setItem('lastCheckTime', currentTime);
+ } catch (error) {
+ console.error(error);
+ disableUpdateBtn_enableCancelBtn()
+ }
+}
+
+function getUpdateInfo() {
+ window.open('https://github.com/gaizhenbiao/chuanhuchatgpt/releases/latest', '_blank');
+ closeUpdateToast();
+}
+
+var updateSpinner = null;
+
+function bgUpdateChuanhu() {
+ updateChuanhuBtn.click();
+ updatingInfoElement.innerText = i18n(updatingMsg_i18n);
+ var updatingSpinner = document.getElementById('updating-spinner');
+ try {
+ updateSpinner = new Spin.Spinner({color:'#06AE56',top:'45%',lines:9}).spin(updatingSpinner);
+ } catch (error) {
+ console.error("Can't create spinner")
+ }
+ updatingInfoElement.classList.remove('hideK');
+ disableUpdateBtns();
+ const releaseNoteWrap = document.getElementById('release-note-wrap');
+ releaseNoteWrap.style.setProperty('display', 'none');
+ statusObserver.observe(statusDisplay, { childList: true, subtree: true, characterData: true});
+}
+function cancelUpdate() {
+ closeUpdateToast();
+}
+function openUpdateToast() {
+ showingUpdateInfo = true;
+ updateToast.style.setProperty('top', '0px');
+ showMask("update-toast");
+}
+function closeUpdateToast() {
+ updateToast.style.setProperty('top', '-600px');
+ showingUpdateInfo = false;
+ if (updatingInfoElement.classList.contains('hideK') === false) {
+ updatingInfoElement.classList.add('hideK');
+ }
+ document.querySelector('.chuanhu-mask')?.remove();
+}
+function manualCheckUpdate() {
+ openUpdateToast();
+ updateLatestVersion();
+ currentTime = new Date().getTime();
+ localStorage.setItem('lastCheckTime', currentTime);
+}
+function noUpdate(message="") {
+ localStorage.setItem('isLatestVersion', 'true');
+ isLatestVersion = true;
+ noUpdateHtml(message);
+}
+function noUpdateHtml(message="") {
+ const versionInfoElement = document.getElementById('version-info-title');
+ const gotoUpdateBtn = document.getElementById('goto-update-btn');
+ const closeUpdateBtn = document.getElementById('close-update-btn');
+ const releaseNoteWrap = document.getElementById('release-note-wrap');
+ releaseNoteWrap.style.setProperty('display', 'none');
+ if (message === "") {
+ versionInfoElement.textContent = i18n(usingLatest_i18n)
+ } else {
+ versionInfoElement.innerHTML = message;
+ }
+ gotoUpdateBtn.classList.add('hideK');
+ closeUpdateBtn.classList.remove('hideK');
+}
+
+var updateStatus = null;
+function getUpdateStatus() {
+ updateStatus = statusDisplay.querySelector("#update-status");
+ if (updateStatus) {
+ return updateStatus.innerText;
+ } else {
+ return "unknown";
+ }
+}
+
+function disableUpdateBtns() {
+ const updatesButtons = document.querySelectorAll('.btn-update');
+ updatesButtons.forEach( function (btn) {
+ btn.disabled = true;
+ });
+}
+function enableUpdateBtns() {
+ const updatesButtons = document.querySelectorAll('.btn-update');
+ updatesButtons.forEach( function (btn) {
+ btn.disabled = false;
+ });
+}
+function disableUpdateBtn_enableCancelBtn() {
+ document.querySelector('#update-button.btn-update').disabled = true;
+ document.querySelector('#cancel-button.btn-update').disabled = false;
+}
+
+// function setUpdateWindowHeight() {
+// if (!showingUpdateInfo) {return;}
+// const scrollPosition = window.scrollY;
+// // const originalTop = updateToast.style.getPropertyValue('top');
+// const resultTop = scrollPosition - 20 + 'px';
+// updateToast.style.setProperty('top', resultTop);
+// }
diff --git a/assets/javascript/user-info.js b/assets/javascript/user-info.js
new file mode 100644
index 0000000000000000000000000000000000000000..3f4c83eef6f0a5f54873158c9716949cfd3901a1
--- /dev/null
+++ b/assets/javascript/user-info.js
@@ -0,0 +1,70 @@
+
+// var userLogged = false;
+var usernameGotten = false;
+var usernameTmp = null;
+var username = null;
+
+
+function getUserInfo() {
+ if (usernameGotten) {
+ return;
+ }
+ // userLogged = localStorage.getItem('userLogged');
+ // if (userLogged) {
+ usernameTmp = userInfoDiv.innerText;
+ if (usernameTmp) {
+ if (usernameTmp.includes("getting user info")) {
+ setTimeout(getUserInfo, 500);
+ return;
+ } else if (usernameTmp === " ") {
+ localStorage.removeItem("username");
+ // localStorage.removeItem("userLogged")
+ // userLogged = false;
+ usernameGotten = true;
+ return;
+ } else {
+ usernameTmp = usernameTmp.match(/User:\s*(.*)/)[1] || usernameTmp;
+ localStorage.setItem("username", usernameTmp);
+ username = usernameTmp;
+ usernameGotten = true;
+ clearHistoryHtml();
+ }
+ }
+ // }
+}
+
+function showOrHideUserInfo() {
+ function toggleUserInfoVisibility(shouldHide) {
+ if (userInfoDiv) {
+ if (shouldHide) {
+ userInfoDiv.classList.add("info-transparent");
+ } else {
+ userInfoDiv.classList.remove("info-transparent");
+ }
+ }
+ }
+
+ // When webpage loaded, hide user info after 2 second
+ setTimeout(function () {
+ toggleUserInfoVisibility(true);
+ }, 2000);
+
+ // let triggerElements = {appTitleDiv, userInfoDiv, sendBtn};
+ let triggerElements = {userInfoDiv, statusDisplay};
+ for (let elem in triggerElements) {
+ triggerElements[elem].addEventListener("mouseenter", function () {
+ toggleUserInfoVisibility(false);
+ });
+ triggerElements[elem].addEventListener("mouseleave", function () {
+ toggleUserInfoVisibility(true);
+ });
+ triggerElements[elem].ontouchstart = function () {
+ toggleUserInfoVisibility(false);
+ };
+ triggerElements[elem].ontouchend = function () {
+ setTimeout(function () {
+ toggleUserInfoVisibility(true);
+ }, 3000);
+ };
+ }
+}
diff --git a/assets/javascript/utils.js b/assets/javascript/utils.js
new file mode 100644
index 0000000000000000000000000000000000000000..0bce1ec6e195bdcbb8724465f8aefb77956383de
--- /dev/null
+++ b/assets/javascript/utils.js
@@ -0,0 +1,132 @@
+
+
+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 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
+ }
+ }
+ });
+}
+
+
+/* NOTE: These reload functions are not used in the current version of the code.
+ * From stable-diffusion-webui
+ */
+function restart_reload() {
+ document.body.innerHTML = 'Reloading...
';
+
+ var requestPing = function () {
+ requestGet("./internal/ping", {}, function (data) {
+ location.reload();
+ }, function () {
+ setTimeout(requestPing, 500);
+ });
+ };
+
+ setTimeout(requestPing, 2000);
+
+ return [];
+}
+
+function requestGet(url, data, handler, errorHandler) {
+ var xhr = new XMLHttpRequest();
+ var args = Object.keys(data).map(function (k) {
+ return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]);
+ }).join('&');
+ xhr.open("GET", url + "?" + args, true);
+
+ xhr.onreadystatechange = function () {
+ if (xhr.readyState === 4) {
+ if (xhr.status === 200) {
+ try {
+ var js = JSON.parse(xhr.responseText);
+ handler(js);
+ } catch (error) {
+ console.error(error);
+ errorHandler();
+ }
+ } else {
+ errorHandler();
+ }
+ }
+ };
+ var js = JSON.stringify(data);
+ xhr.send(js);
+}
diff --git a/assets/javascript/webui.js b/assets/javascript/webui.js
new file mode 100644
index 0000000000000000000000000000000000000000..f000765ef50db1894357e32e8e268624e78c11d8
--- /dev/null
+++ b/assets/javascript/webui.js
@@ -0,0 +1,333 @@
+
+function openSettingBox() {
+ chuanhuPopup.classList.add('showBox');
+ popupWrapper.classList.add('showBox');
+ settingBox.classList.remove('hideBox');
+ trainingBox.classList.add('hideBox');
+ showMask("box");
+
+}
+
+function openTrainingBox() {
+ chuanhuPopup.classList.add('showBox');
+ popupWrapper.classList.add('showBox');
+ trainingBox.classList.remove('hideBox');
+ settingBox.classList.add('hideBox');
+ showMask("box");
+}
+
+function openChatMore() {
+ chatbotArea.classList.add('show-chat-more');
+ showMask("chat-more");
+}
+
+function closeChatMore() {
+ chatbotArea.classList.remove('show-chat-more');
+ chatbotArea.querySelector('.chuanhu-mask')?.remove();
+}
+
+
+function showMask(obj) {
+ const mask = document.createElement('div');
+ mask.classList.add('chuanhu-mask');
+ if (obj == "box") {
+ mask.classList.add('mask-blur');
+ document.body.classList.add('popup-open');
+ popupWrapper.appendChild(mask);
+ } else if (obj == "chat-more") {
+ mask.classList.add('transparent-mask');
+ chatbotArea.querySelector('#chatbot-input-more-area').parentNode.appendChild(mask);
+ } else if (obj == "update-toast") {
+ mask.classList.add('chuanhu-top-mask');
+ if (document.querySelector('.chuanhu-top-mask')) {
+ for (var i = 0; i < document.querySelectorAll('.chuanhu-top-mask').length; i++) {
+ document.querySelectorAll('.chuanhu-top-mask')[i].remove();
+ }
+ }
+ document.body.appendChild(mask);
+ // mask.classList.add('transparent-mask');
+ }
+
+
+ mask.addEventListener('click', () => {
+ if (obj == "box") {
+ closeBox();
+ } else if (obj == "chat-more") {
+ closeChatMore();
+ } else if (obj == "update-toast") {
+ closeUpdateToast();
+ }
+ });
+}
+
+function chatMoreBtnClick() {
+ if (chatbotArea.classList.contains('show-chat-more')) {
+ closeChatMore();
+ } else {
+ openChatMore();
+ }
+}
+
+function closeBtnClick(obj) {
+ if (obj == "box") {
+ closeBox();
+ } else if (obj == "toolbox") {
+ closeSide(toolbox);
+ wantOpenToolbox = false;
+ }
+}
+
+function closeBox() {
+ chuanhuPopup.classList.remove('showBox');
+ popupWrapper.classList.remove('showBox');
+ trainingBox.classList.add('hideBox');
+ settingBox.classList.add('hideBox');
+ document.querySelector('.chuanhu-mask')?.remove();
+ document.body.classList.remove('popup-open');
+}
+
+function closeSide(sideArea) {
+ document.body.classList.remove('popup-open');
+ sideArea.classList.remove('showSide');
+ if (sideArea == toolbox) {
+ chuanhuHeader.classList.remove('under-box');
+ chatbotArea.classList.remove('toolbox-open')
+ toolboxOpening = false;
+ } else if (sideArea == menu) {
+ chatbotArea.classList.remove('menu-open')
+ menuOpening = false;
+ }
+ adjustMask();
+}
+
+function openSide(sideArea) {
+ sideArea.classList.add('showSide');
+ if (sideArea == toolbox) {
+ chuanhuHeader.classList.add('under-box');
+ chatbotArea.classList.add('toolbox-open')
+ toolboxOpening = true;
+ } else if (sideArea == menu) {
+ chatbotArea.classList.add('menu-open')
+ menuOpening = true;
+ }
+ // document.body.classList.add('popup-open');
+}
+
+function menuClick() {
+ shouldAutoClose = false;
+ if (menuOpening) {
+ closeSide(menu);
+ wantOpenMenu = false;
+ } else {
+ if (windowWidth < 1024 && toolboxOpening) {
+ closeSide(toolbox);
+ wantOpenToolbox = false;
+ }
+ openSide(menu);
+ wantOpenMenu = true;
+ }
+ adjustSide();
+}
+
+function toolboxClick() {
+ shouldAutoClose = false;
+ if (toolboxOpening) {
+ closeSide(toolbox);
+ wantOpenToolbox = false;
+ } else {
+ if (windowWidth < 1024 && menuOpening) {
+ closeSide(menu);
+ wantOpenMenu = false;
+ }
+ openSide(toolbox);
+ wantOpenToolbox = true;
+ }
+ adjustSide();
+}
+
+var menuOpening = false;
+var toolboxOpening = false;
+var shouldAutoClose = true;
+var wantOpenMenu = windowWidth > 768;
+var wantOpenToolbox = windowWidth >= 1024;
+
+function adjustSide() {
+ if (windowWidth >= 1024) {
+ shouldAutoClose = true;
+ if (wantOpenMenu) {
+ openSide(menu);
+ if (wantOpenToolbox) openSide(toolbox);
+ } else if (wantOpenToolbox) {
+ openSide(toolbox);
+ } else {
+ closeSide(menu);
+ closeSide(toolbox);
+ }
+ } else if (windowWidth > 768 && windowWidth < 1024 ) {
+ shouldAutoClose = true;
+ if (wantOpenToolbox) {
+ if (wantOpenMenu) {
+ closeSide(toolbox);
+ openSide(menu);
+ } else {
+ closeSide(menu);
+ openSide(toolbox);
+ }
+ } else if (wantOpenMenu) {
+ if (wantOpenToolbox) {
+ closeSide(menu);
+ openSide(toolbox);
+ } else {
+ closeSide(toolbox);
+ openSide(menu);
+ }
+ } else if (!wantOpenMenu && !wantOpenToolbox){
+ closeSide(menu);
+ closeSide(toolbox);
+ }
+ } else { // windowWidth <= 768
+ if (shouldAutoClose) {
+ closeSide(menu);
+ // closeSide(toolbox);
+ }
+ }
+ checkChatbotWidth();
+ adjustMask();
+}
+
+function adjustMask() {
+ var sideMask = null;
+ if (!gradioApp().querySelector('.chuanhu-side-mask')) {
+ sideMask = document.createElement('div');
+ sideMask.classList.add('chuanhu-side-mask');
+ gradioApp().appendChild(sideMask);
+ sideMask.addEventListener('click', () => {
+ closeSide(menu);
+ closeSide(toolbox);
+ });
+ }
+ sideMask = gradioApp().querySelector('.chuanhu-side-mask');
+
+ if (windowWidth > 768) {
+ sideMask.style.backgroundColor = 'rgba(0, 0, 0, 0)';
+ setTimeout(() => {sideMask.style.display = 'none'; }, 100);
+ return;
+ }
+ // if (windowWidth <= 768)
+ if (menuOpening || toolboxOpening) {
+ document.body.classList.add('popup-open');
+ sideMask.style.display = 'block';
+ setTimeout(() => {sideMask.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';}, 200);
+ sideMask.classList.add('mask-blur');
+ } else if (!menuOpening && !toolboxOpening) {
+ sideMask.style.backgroundColor = 'rgba(0, 0, 0, 0)';
+ setTimeout(() => {sideMask.style.display = 'none'; }, 100);
+ }
+}
+
+function checkChatbotWidth() {
+ // let chatbotWidth = chatbotArea.clientWidth;
+ // if (chatbotWidth > 488) {
+ if (windowWidth > 768) {
+ chatbotArea.classList.add('chatbot-full-width');
+ } else {
+ chatbotArea.classList.remove('chatbot-full-width');
+ }
+
+ if (windowWidth > 768) {
+ chatbotArea.classList.remove('no-toolbox');
+ chatbotArea.classList.remove('no-menu');
+
+ if (!chatbotArea.classList.contains('toolbox-open') && chatbotArea.classList.contains('menu-open')) {
+ chatbotArea.classList.add('no-toolbox');
+ } else if (!chatbotArea.classList.contains('menu-open') && chatbotArea.classList.contains('toolbox-open')) {
+ chatbotArea.classList.add('no-menu');
+ } else if (!chatbotArea.classList.contains('menu-open') && !chatbotArea.classList.contains('toolbox-open')) {
+ chatbotArea.classList.add('no-toolbox');
+ chatbotArea.classList.add('no-menu');
+ }
+ }
+
+ checkChatMoreMask();
+}
+
+function checkChatMoreMask() {
+ if (!chatbotArea.classList.contains('chatbot-full-width')) {
+ chatbotArea.querySelector('.chuanhu-mask')?.remove();
+ chatbotArea.classList.remove('show-chat-more');
+ }
+}
+
+function showKnowledgeBase(){
+ if (!toolboxOpening) {
+ toolboxClick();
+ }
+ switchToolBoxTab(0);
+ let knoledgeBaseAccordion = gradioApp().querySelector('#gr-kb-accordion');
+ let knoledgeBase = knoledgeBaseAccordion.querySelector('#upload-index-file');
+ if (knoledgeBase.parentElement.parentElement.style.display == 'none') {
+ knoledgeBaseAccordion.querySelector('.label-wrap')?.click();
+ }
+ // 将 knoledgeBase 滚动到可见区域
+ setTimeout(() => {knoledgeBaseAccordion.scrollIntoView({ behavior: "smooth"}); }, 100);
+ letThisSparkle(knoledgeBase, 5000);
+}
+
+function letThisSparkle(element, sparkleTime = 3000) {
+ element.classList.add('chuanhu-sparkle');
+ setTimeout(() => {element.classList.remove('chuanhu-sparkle');}, sparkleTime);
+}
+
+function switchToolBoxTab(tabIndex) {
+ let tabButtons = gradioApp().querySelectorAll('#chuanhu-toolbox-tabs .tab-nav > button');
+ let tab = tabButtons[tabIndex];
+ tab.click();
+}
+
+/*
+function setHistroyPanel() {
+ const historySelectorInput = gradioApp().querySelector('#history-select-dropdown input');
+ const historyPanel = document.createElement('div');
+ historyPanel.classList.add('chuanhu-history-panel');
+ historySelector.parentNode.insertBefore(historyPanel, historySelector);
+ var historyList=null;
+
+ historySelectorInput.addEventListener('click', (e) => {
+ e.stopPropagation();
+ historyList = gradioApp().querySelector('#history-select-dropdown ul.options');
+
+ if (historyList) {
+ // gradioApp().querySelector('.chuanhu-history-panel')?.remove();
+ historyPanel.innerHTML = '';
+ let historyListClone = historyList.cloneNode(true);
+ historyListClone.removeAttribute('style');
+ // historyList.classList.add('hidden');
+ historyList.classList.add('hideK');
+ historyPanel.appendChild(historyListClone);
+ addHistoryPanelListener(historyPanel);
+ // historySelector.parentNode.insertBefore(historyPanel, historySelector);
+ }
+ });
+}
+*/
+
+// function addHistoryPanelListener(historyPanel){
+// historyPanel.querySelectorAll('ul.options > li').forEach((historyItem) => {
+// historyItem.addEventListener('click', (e) => {
+// const historySelectorInput = gradioApp().querySelector('#history-select-dropdown input');
+// const historySelectBtn = gradioApp().querySelector('#history-select-btn');
+// historySelectorInput.value = historyItem.innerText;
+// historySelectBtn.click();
+// });
+// });
+// }
+
+
+// function testTrain() {
+
+// trainBody.classList.toggle('hide-body');
+// trainingBox.classList.remove('hideBox');
+
+// var chuanhuBody = document.querySelector('#chuanhu-body');
+// chuanhuBody.classList.toggle('hide-body');
+// }
\ No newline at end of file
diff --git a/assets/stylesheet/ChuanhuChat.css b/assets/stylesheet/ChuanhuChat.css
new file mode 100644
index 0000000000000000000000000000000000000000..1759910f2dfa084f2f3f3b8111ff498699357360
--- /dev/null
+++ b/assets/stylesheet/ChuanhuChat.css
@@ -0,0 +1,1234 @@
+:root {
+ --vh: 1vh;
+
+ --chatbot-color-light: #000000;
+ --chatbot-color-dark: #FFFFFF;
+ --chatbot-background-color-light: #F3F3F3;
+ --chatbot-background-color-dark: #121111;
+ --message-user-background-color-light: #52b7e3;
+ --message-user-background-color-dark: #1140d8;
+ --message-bot-background-color-light: #FFFFFF;
+ --message-bot-background-color-dark: #2C2C2C;
+ --switch-checkbox-color-light: #e5e7eb;
+ --switch-checkbox-color-dark: #515151;
+
+ --chatbot-blur-background-color: #F3F3F366;
+ --chatbot-input-background-color: rgba(255, 255, 255, 0.64);
+ --chatbot-input-more-background-color: #FFFFFFAA;
+ --chatbot-input-more-background-solid-color: #FFFFFF;
+ --chatbot-input-more-background-fullwidth-hover: #FFFFFF99;
+ --chatbot-input-more-background-mobilewidth-hover: #E6E6E644;
+
+ --message-list-background-hover: #F3F3F3;
+ --message-list-background-selected: #EAEAEA;
+
+ --menu-width: 320px;
+ --menu-background-fill: var(--background-fill-primary);
+
+ --toolbox-width: 280px;
+ --toolbox-background-fill: var(--background-fill-secondary);
+
+ --dragging-hint-background-color: #F9F9F9BB;
+
+ .dark {
+ --chatbot-blur-background-color: #12111166;
+ --chatbot-input-background-color: rgba(144, 144, 144, 0.32);
+ --chatbot-input-more-background-color: #3C3C3CAA;
+ --chatbot-input-more-background-solid-color: #3C3C3C;
+ --chatbot-input-more-background-fullwidth-hover: #2F2F2F88;
+ --chatbot-input-more-background-mobilewidth-hover: #1F1F1F44;
+
+ --message-list-background-hover: #202020;
+ --message-list-background-selected: #2F3030;
+
+ --dragging-hint-background-color: #515151BB;
+ }
+}
+
+
+body.popup-open {
+ overflow: hidden;
+}
+
+.hideK {
+ display: none;
+}
+
+#app-title {
+ font-weight: var(--prose-header-text-weight);
+ font-size: var(--text-xxl);
+ line-height: 1.3;
+ text-align: left;
+ margin-top: 4px;
+ white-space: nowrap;
+ flex-direction: row;
+ display: inline-flex;
+ align-items: center;
+ position: absolute;
+}
+#description {
+ text-align: center;
+ /* margin: 32px 0 4px 0; */
+}
+#about-tab {
+ text-align: center;
+}
+#about-tab img {
+ margin: 0 auto;
+}
+
+/* 高级页面 */
+#advanced-warning {
+ margin-top: 0.5rem;
+ display: flex;
+ flex-wrap: wrap;
+ flex-direction: column;
+ align-content: center;
+}
+
+#netsetting-warning hr {
+ margin-top: 0.5em;
+ margin-bottom: 1em;
+}
+
+.view-only-textbox textarea {
+ -webkit-text-fill-color: darkgray !important;
+ cursor: not-allowed !important;
+}
+
+#footer {
+ text-align: center;
+}
+#footer div {
+ display: inline-block;
+}
+#footer .versions{
+ font-size: 85%;
+ opacity: 0.60;
+}
+
+
+#float-display {
+ position: absolute;
+ max-height: 30px;
+}
+
+.insert-block {
+ position: relative;
+ margin: 0;
+ padding: 8px 0;
+ box-shadow: var(--block-shadow);
+ border-width: var(--block-border-width);
+ border-color: var(--block-border-color);
+ border-radius: var(--block-radius);
+ background: var(--block-background-fill);
+ width: 100%;
+ line-height: var(--line-sm);
+ min-height: 2em;
+}
+
+/* status-display */
+#chuanhu-header > #status-display {
+ display: flex;
+ min-height: 2em;
+ align-items: flex-end;
+ justify-content: flex-end;
+ transition: all 0.6s;
+ max-width: 50%;
+ height: 100%;
+ bottom: 0;
+ position: absolute;
+
+ @media screen and (max-width: 639px) {
+ right: 16px;
+ right: max(16px, env(safe-area-inset-right));
+ }
+ @media screen and (min-width: 640px) {
+ right: 24px;
+ right: max(24px, env(safe-area-inset-right));
+ }
+}
+#chuanhu-header > #status-display #status-display {
+ min-height: unset;
+}
+#chuanhu-header > #status-display > .wrap {
+ margin-top: 8px;
+}
+#status-display p {
+ font-size: .85em;
+ font-family: ui-monospace, "SF Mono", "SFMono-Regular", "Menlo", "Consolas", "Liberation Mono", "Microsoft Yahei UI", "Microsoft Yahei", monospace;
+ /* Windows下中文的monospace会fallback为新宋体,实在太丑,这里折中使用微软雅黑 */
+ color: var(--body-text-color-subdued);
+}
+
+#chatbot-ctrl-btns {
+ align-self: end;
+ max-width: 42px;
+}
+#submit-btn, #cancel-btn {
+ height: 42px !important;
+ width: 42px !important;
+ border-radius: 50%;
+ transform: scale(0.8);
+ justify-content: center;
+ align-items: center;
+}
+#submit-btn::before {
+ content: url("data:image/svg+xml, %3Csvg width='21px' height='21px' viewBox='0 0 21 20' version='1.1' xmlns='http://www.w3.org/2000/svg' %3E %3Cg id='page' stroke='none' stroke-width='1' fill='none' fill-rule='evenodd'%3E %3Cg id='send' transform='translate(0.435849, 0.088463)' fill='%23FFFFFF' fill-rule='nonzero'%3E %3Cpath d='M0.579148261,0.0428666046 C0.301105539,-0.0961547561 -0.036517765,0.122307382 0.0032026237,0.420210298 L1.4927172,18.1553639 C1.5125774,18.4334066 1.79062012,18.5922882 2.04880264,18.4929872 L8.24518329,15.8913017 L11.6412765,19.7441794 C11.8597387,19.9825018 12.2370824,19.8832008 12.3165231,19.5852979 L13.9450591,13.4882182 L19.7839562,11.0255541 C20.0619989,10.8865327 20.0818591,10.4694687 19.7839562,10.3105871 L0.579148261,0.0428666046 Z M11.6138902,17.0883151 L9.85385903,14.7195502 L0.718169621,0.618812241 L12.69945,12.9346347 L11.6138902,17.0883151 Z' id='shape'%3E%3C/path%3E %3C/g%3E %3C/g%3E %3C/svg%3E");
+ height: 21px;
+ width: 21px;
+ position: relative;
+ left: 2px;
+}
+#cancel-btn::before {
+ content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='34px' height='34px' fill='%23ff453a' fill-opacity='0.85'%3E%3Cg%3E%3Cpath d='M16.8954 33.7909C26.1546 33.7909 33.8049 26.1546 33.8049 16.8954C33.8049 7.63629 26.1406 0 16.8814 0C7.63629 0 0 7.63629 0 16.8954C0 26.1546 7.65027 33.7909 16.8954 33.7909ZM16.8954 29.7737C9.76412 29.7737 4.04511 24.0407 4.04511 16.8954C4.04511 9.75014 9.75014 4.01713 16.8814 4.01713C24.0267 4.01713 29.7737 9.75014 29.7737 16.8954C29.7737 24.0407 24.0407 29.7737 16.8954 29.7737Z'/%3E%3Cpath d='M12.7957 22.7421L20.9747 22.7421C22.0532 22.7421 22.7346 22.1007 22.7346 21.0688L22.7346 12.709C22.7346 11.6771 22.0532 11.0358 20.9747 11.0358L12.7957 11.0358C11.7032 11.0358 11.0358 11.6771 11.0358 12.709L11.0358 21.0688C11.0358 22.1007 11.7032 22.7421 12.7957 22.7421Z'/%3E%3C/g%3E%3C/svg%3E");
+ height: 34px;
+ width: 34px;
+}
+
+#chatbot-buttons button {
+ display: inline-block;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+/* masks */
+.chuanhu-mask, .chuanhu-side-mask {
+ /* background-color: gray; */
+ background-color: rgba(0, 0, 0, 0.5);
+ transition: background-color 0.3s ease;
+ position: fixed;
+ top: 0;
+ left: 0;
+ width: 100%;
+ height: 100%;
+ z-index: 999;
+ /* background-color: transparent; */
+}
+/* .chuanhu-mask {
+ background-color: rgba(0, 0, 0, 0.5);
+ /* -webkit-backdrop-filter: blur(2px);
+ backdrop-filter: blur(2px);
+} */
+.mask-blur {
+ -webkit-backdrop-filter: blur(2px);
+ backdrop-filter: blur(2px);
+}
+.transparent-mask {
+ background-color: transparent !important;
+}
+
+.chuanhu-side-mask {
+ background-color: rgba(0, 0, 0, 0);
+}
+.chuanhu-top-mask {
+ /* background-color: rgba(0, 0, 0, 0.0); */
+ z-index: 10001;
+}
+
+
+#popup-wrapper {
+ display: none;
+ position: fixed;
+ overflow: auto;
+ top: 0;
+ left: 0;
+ z-index: 99999;
+}
+#popup-wrapper.showBox {
+ display: grid;
+ place-items: center;
+}
+
+#chuanhu-popup {
+ display: none;
+ z-index: 99999;
+ width: 680px;
+ height: 400px;
+ padding: 0;
+}
+#chuanhu-popup.showBox {
+ display: block;
+ box-shadow: 0 2px 64px 4px rgba(0, 0, 0, 0.2);
+}
+
+#chuanhu-popup > .gradio-box {
+ padding: 0;
+}
+.hideBox {
+ display: none;
+}
+
+
+#chuanhu-header {
+ position: fixed;
+ top: 0;
+ z-index: 1000;
+ left: 0;
+ right: 0;
+ /* padding: 6px 64px; */
+ height: 65px;
+ background: var(--background-fill-primary);
+ border-bottom: 1px solid var(--border-color-primary);
+
+ @media screen and (max-width: 639px) {
+ padding: 6px 16px;
+ padding-left: max(16px, env(safe-area-inset-left));
+ padding-right: max(16px, env(safe-area-inset-right));
+ }
+ @media screen and (min-width: 640px) {
+ padding: 6px 24px;
+ padding-left: max(24px, env(safe-area-inset-left));
+ padding-right: max(24px, env(safe-area-inset-right));
+ }
+ /* @media screen and (min-width: 1024px) {
+ padding: 6px 96px;
+ } */
+}
+#chuanhu-header.under-box {
+ z-index: 995 !important;
+}
+
+#chuanhu-body {
+ flex-wrap: nowrap;
+ gap: 0;
+ overflow: hidden;
+ display: inline-flex;
+ justify-content: space-between;
+ /* margin-top: 54px; */
+ /* height: calc(100*var(--vh) - 72px); */
+ position: absolute;
+ top: 65px;
+ height: calc(100*var(--vh) - 65px);
+}
+
+#chuanhu-area {
+ flex: unset;
+ width: 100%;
+ flex-wrap: nowrap;
+ justify-content: center;
+ overflow: hidden;
+ flex-direction: row;
+ /* padding-inline: 24px; */
+ /* margin: 16px; */
+ /* border-radius: 24px; */
+ background: var(--chatbot-background-color-light);
+}
+.dark #chuanhu-area {
+ background: var(--chatbot-background-color-dark);
+}
+#chatbot-header {
+ justify-content: space-between;
+ border-bottom: 0.5px solid var(--border-color-primary);
+ height: 60px;
+ padding-inline: 20px 16px;
+ gap: 0;
+ position: absolute;
+ top: 0;
+ right: 4px;
+ width: calc(100% - 4px);
+ z-index: 50;
+ background: var(--chatbot-blur-background-color);
+ backdrop-filter: blur(24px);
+ -webkit-backdrop-filter: blur(24px);
+}
+
+#chatbot-header .gradio-dropdown {
+ max-width: 14em;
+ background: none;
+ height: 60px;
+ overflow: unset !important;
+}
+#chatbot-header .gradio-dropdown > label {
+ display: flex;
+}
+#chatbot-header .gradio-dropdown ul.options {
+ top: 60px !important;
+ left: 0 !important;
+ position: absolute !important;
+}
+#chatbot-header .gradio-dropdown > label > span[data-testid="block-info"] {
+ height: unset;
+ overflow: visible;
+ top: 0;
+ align-self: center;
+ background: none;
+ margin: 0;
+ padding: 0;
+ position: relative;
+ width: auto;
+ color: var(--body-text-color-subdued);
+}
+#chatbot-header .gradio-dropdown > label > .wrap {
+ background: none;
+ box-shadow: none;
+ padding-left: 8px;
+}
+#model-select-dropdown > label > span[data-testid="block-info"] {
+ display: none;
+}
+#chatbot-header .gradio-dropdown > label > .wrap input {
+ font-weight: bold;
+}
+#chatbot-header #model-select-dropdown > label::before {
+ content: "";
+ background: var(--primary-600);
+ height: 12px;
+ width: 12px;
+ border-radius: 50%;
+ position: absolute;
+ /* left: 2px; */
+ top: calc(50% - 6px);
+}
+
+#chatbot-header-btn-bar {
+ justify-content: space-between;
+ align-items: center;
+ display: flex;
+ height: 60px;
+}
+#chatbot-header-btn-bar > * {
+ width: 100%;
+}
+#header-btn-groups {
+ width: 100%;
+ display: flex;
+ justify-content: space-between;
+}
+/* #header-btn-group {
+ justify-content: space-between;
+ display: flex;
+ height: 36px;
+ align-items: center;
+} */
+.show-on-gpt {
+ /* visibility: hidden; */
+ display: none;
+}
+.is-gpt .show-on-gpt {
+ /* visibility: visible; */
+ display: block;
+}
+
+#chatbot-footer {
+ position: absolute;
+ bottom: 0;
+ right: 4px;
+ width: calc(100% - 4px);
+ display: flex;
+ justify-content: center;
+ /* padding: 24px; */
+ /* padding: 8px 6px; */
+ min-height: 82px;
+ /* max-height: 166px; */
+ z-index: 2;
+ background: var(--chatbot-blur-background-color);
+ -webkit-backdrop-filter: blur(24px);
+ backdrop-filter: blur(24px);
+}
+
+#chatbot-input-box {
+ max-width: 800px;
+ /* margin: 0 auto; */
+ gap: 20px;
+ padding: 16px 16px 24px;
+ padding-bottom: max(24px, calc( env(safe-area-inset-bottom) + 6px));
+ display: flex;
+ background: none;
+ align-self: end;
+}
+
+#chatbot-input-btn-bar {
+ height: 27px;
+ overflow-y: auto;
+ flex-wrap: nowrap;
+}
+
+button.chatbot-input-more-btn {
+ margin: 5px;
+ height: 32px;
+ width: 32px;
+ border-radius: 50%;
+ z-index: 1001;
+}
+button.chatbot-input-more-btn:hover .sm-round-bg {
+ fill-opacity: 0.2125;
+}
+button.chatbot-input-more-btn:active .sm-round-bg {
+ fill-opacity: 0.25;
+}
+
+/* 三个点号点开! */
+.show-chat-more #chatbot-input-more-area {
+ display: flex;
+}
+@supports (-webkit-backdrop-filter: blur(24px)) {
+ /* Note: I would only try this feat on apple devices... */
+ .show-chat-more #chatbot-input-more-area {
+ background: var(--chatbot-input-more-background-color);
+ -webkit-backdrop-filter: blur(24px);
+ backdrop-filter: blur(24px);
+ }
+}
+/* no!屏幕宽度窄的时候! */
+#chatbot-input-more-area {
+ display: none;
+ position: absolute;
+ flex-direction: column;
+ bottom: 60px;
+ min-width: 120px;
+ z-index: 1001;
+ border-radius: 6px;
+ box-shadow: var(--shadow-sm);
+ background: var(--chatbot-input-more-background-solid-color);
+}
+#chatbot-input-more-area > span > div {
+ min-width: 120px;
+ padding: 2px;
+ align-content: center;
+ /* display: flex; */
+ border-bottom: 0.5px solid var(--border-color-primary);
+}
+#chatbot-input-more-area > span > div.last-btn {
+ border-bottom: none;
+}
+#chatbot-input-more-area > span > div > label {
+ padding: 6px 8px;
+ border-radius: 4px;
+ height: 39px;
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ cursor: pointer;
+}
+#chatbot-input-more-area > span > div:hover > label {
+ background: var(--chatbot-input-more-background-mobilewidth-hover);
+}
+#chatbot-input-more-area > span > div > label button {
+ margin: 0;
+ width: 100%;
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ gap: 4px;
+}
+.chatbot-input-more-icon {
+ margin-right: 12px;
+}
+.chatbot-input-more-span {
+ white-space: nowrap;
+}
+
+/* 哈哈!川虎哥觉得不方便,那再写个全宽的吧!
+ * 再让我重写一份样式我是狗
+ */
+.chatbot-full-width #chatbot-input-row {
+ flex-direction: column;
+ justify-content: flex-start !important;
+ justify-items: start;
+}
+.chatbot-full-width #chatbot-input-more-area {
+ display: flex;
+ position: relative;
+ flex-direction: row-reverse;
+ justify-content: space-between;
+ height: 32px;
+ min-width: unset;
+ background: none;
+ box-shadow: none;
+ bottom: 0;
+ backdrop-filter: none;
+ -webkit-backdrop-filter: none;
+}
+.chatbot-full-width #chatbot-input-more-area > span > div {
+ /* min-width: unset; */
+ border-bottom: none;
+}
+.chatbot-full-width #chatbot-input-more-area > span > div > label {
+ height: 32px;
+ border-radius: 8px;
+}
+.chatbot-full-width #chatbot-input-more-area > span > div:hover > label {
+ background: var(--chatbot-input-more-background-fullwidth-hover);
+}
+.chatbot-full-width #chatbot-input-more-btn-div {
+ display: none;
+}
+.chatbot-full-width #chatbot-input-box {
+ padding-top: 4px;
+}
+.chatbot-full-width #chatbot-input-row .gradio-html {
+ width: 100%;
+ max-width: unset;
+}
+.chatbot-full-width .chatbot-input-more-label-group {
+ flex-wrap: nowrap;
+ flex-direction: row-reverse;
+ display: inline-flex;
+}
+.chatbot-input-more-span {
+ opacity: 0.64;
+}
+input:checked + .chatbot-input-more-span {
+ opacity: 1;
+}
+
+#uploaded-files-btn {
+ display: none;
+}
+.with-file #uploaded-files-btn {
+ display: flex;
+ justify-content: space-between;
+ width: 100%;
+}
+/* .with-file label.may-disable-label {
+ cursor: not-allowed !important;
+} */
+.with-file #uploaded-files-btn > .chatbot-input-more-span {
+ opacity: 1;
+}
+#uploaded-files-count {
+ background: var(--primary-600);
+ color: white;
+ width: 19px;
+ height: 19px;
+ border-radius: 50%;
+ margin-right: 4px;
+ margin-left: 6px;
+ text-align: center;
+}
+.with-file #upload-files-btn {
+ display: none;
+}
+
+/* default invisible */
+#menu-area, #toolbox-area {
+ width: 0;
+ transition: width 0.3s ease;
+ visibility: hidden;
+ flex: unset;
+ min-width: unset !important;
+ display: flex;
+ flex-shrink: 0;
+ overflow: hidden;
+ flex-wrap: nowrap;
+}
+#menu-area {
+ border-radius: 0;
+ background: var(--background-fill-primary);
+}
+#toolbox-area {
+ background: var(--background-fill-secondary);
+}
+#menu-area > div {
+ width: var(--menu-width);
+}
+#chuanhu-history {
+ padding-left: env(safe-area-inset-left);
+}
+#menu-area.showSide {
+ width: var(--menu-width);
+ max-width: var(--menu-width);
+ height: calc(100*var(--vh) - 65px);
+ visibility: visible;
+ /* margin-right: 16px; */
+ border-right: 0.5px solid var(--border-color-primary);
+ /* box-shadow: -1px 0 4px 0 rgba(0, 0, 0, 0.1) inset; */
+}
+
+#toolbox-area > div {
+ width: var(--toolbox-width);
+}
+#toolbox-area.showSide {
+ width: var(--toolbox-width);
+ height: calc(100*var(--vh) - 65px);
+ visibility: visible;
+ /* margin-left: 16px; */
+}
+
+/* When screen width <= 768 */
+@media screen and (max-width: 767px) {
+ #menu-area {
+ position: fixed;
+ transition: left 0.3s ease, visibility 0.1s ease;
+ left: calc(0px - var(--menu-width));
+ z-index: 9999;
+ /* overflow: unset; */
+ border-right: none !important;
+ }
+ #chuanhu-history {
+ padding-left: 0;
+ }
+ #menu-area.showSide {
+ left: 0;
+ }
+
+ #toolbox-area {
+ position: fixed;
+ width: 100vw;
+ transition: top 0.3s ease, visibility 0.1s ease;
+ /* right: calc(0px - var(--toolbox-width)); */
+ z-index: 10008;
+ overflow: unset;
+ top: calc(100*var(--vh));
+ margin: 0;
+ }
+ #toolbox-area > div {
+ width: 100vw;
+ height: calc( 90*var(--vh) - 48px );
+ }
+ #toolbox-area.showSide {
+ width: 100vw;
+ right: 0;
+ top: calc( 10*var(--vh) + 48px );
+ margin: 0;
+ border-radius: 6px;
+ box-shadow: 0 2px 64px 4px rgba(0, 0, 0, 0.2);
+ }
+ /* #menu-area.showSide, #toolbox-area.showSide {
+ z-index: 9999;
+ } */
+}
+
+/* .chuanhu-history-panel ul.options {
+ position: relative;
+ box-shadow: unset;
+ overflow: hidden;
+} */
+/* .chuanhu-history-panel {
+ height: 500px;
+ overflow: auto;
+ box-shadow: var(--shadow-drop-lg);
+} */
+
+#chuanhu-popup > .gradio-box {
+ height: 100%;
+}
+#chuanhu-popup > .gradio-box > .gradio-row:first-of-type {
+ padding: 24px !important;
+ border-bottom: 1.8px solid var(--border-color-primary);
+}
+#toolbox-area > .gradio-box > .gradio-row:first-of-type * ,
+#chuanhu-popup > .gradio-box > .gradio-row:first-of-type * {
+ margin: 0;
+}
+
+#toolbox-area > .gradio-box > .gradio-row > .close-btn,
+#chuanhu-popup > .gradio-box > .gradio-row > .close-btn {
+ max-width: 28px;
+ display: flex;
+ justify-content: flex-end;
+}
+
+
+#chuanhu-popup > .gradio-box > .gradio-tabs {
+ display: block;
+ height: 322px;
+ /* margin: 16px 24px; */
+}
+
+#chuanhu-popup > .gradio-box > .gradio-tabs > div.tabitem {
+ border: none;
+ border-radius: 0;
+ overflow: auto;
+ height: 100%;
+ padding: 16px 24px;
+}
+#chuanhu-popup > .gradio-box > .gradio-tabs > div.tab-nav {
+ float: left;
+ display: block;
+ border: none;
+ padding: 16px;
+ width: 180px;
+ height: 100%;
+ overflow: auto;
+ background: var(--background-fill-secondary);
+ border-bottom-left-radius: var(--block-radius);
+ border-right: 1px solid var(--border-color-primary);
+}
+#chuanhu-popup > .gradio-box > .gradio-tabs > div.tab-nav > button {
+ display: block;
+ border: none;
+ border-radius: 6px;
+ text-align: left;
+ white-space: initial;
+ width: 100%;
+ /* height: 32px; */
+ padding: 7px 12px;
+}
+#chuanhu-popup > .gradio-box > .gradio-tabs > div.tab-nav > button.selected {
+ background-color: var(--button-primary-background-fill);
+ /* font-weight: bold; */
+ color: var(--button-primary-text-color);
+}
+
+/* 这是为了第二级tab的选项,比如training里的openai tab下的几个准备数据集tab */
+.gradio-box > .gradio-tabs .gradio-tabs > div.tab-nav > button.selected {
+ background-color: var(--block-background-fill);
+}
+
+/* 小屏幕的tab样式 */
+@media screen and (max-width: 767px) {
+ #popup-wrapper.showBox {
+ place-items: end;
+ }
+ #chuanhu-popup {
+ width: 100vw;
+ height: calc( 90*var(--vh) - 48px );
+ border-bottom-left-radius: 0;
+ border-bottom-right-radius: 0;
+ }
+ #toolbox-area > .gradio-box > .gradio-row:first-of-type,
+ #chuanhu-popup > .gradio-box > .gradio-row:first-of-type {
+ padding: 18px 24px 0 !important;
+ border-bottom: 0;
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs,
+ #chuanhu-popup > .gradio-box > .gradio-tabs {
+ height: auto;
+ width: 100vw;
+ overflow: hidden;
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs > div.tabitem,
+ #chuanhu-popup > .gradio-box > .gradio-tabs > div.tabitem {
+ height: calc( 90*var(--vh) - 48px - 46px - 45px );
+ overflow-x: auto;
+ border: none;
+ }
+ /* 下面是弃用方案:横条按钮tab */
+ /*
+ #chuanhu-popup > .gradio-box > .gradio-tabs > div.tab-nav {
+ display: flex;
+ margin: 0;
+ padding: 12px 16px 8px;
+ overflow-x: auto;
+ overflow-y: hidden;
+ flex-direction: row;
+ flex-wrap: nowrap;
+ border-radius: 8px;
+ gap: 12px;
+ width: 100%;
+ height: auto;
+ background: none;
+ }
+ #chuanhu-popup > .gradio-box > .gradio-tabs > div.tab-nav > button {
+ display: inline-block;
+ border-style: none;
+ border-radius: 6px;
+ white-space: nowrap;
+ width: auto;
+ padding: 7px 32px;
+ text-align: center;
+ background: var(--background-fill-secondary);
+ }
+ */
+ #toolbox-area > .gradio-box > .gradio-tabs > div.tab-nav,
+ #chuanhu-popup > .gradio-box > .gradio-tabs > div.tab-nav {
+ display: flex;
+ margin: 0;
+ padding: 6px 16px 0;
+ overflow-x: auto;
+ overflow-y: hidden;
+ flex-direction: row;
+ flex-wrap: nowrap;
+ border-radius: 0;
+ gap: 12px;
+ width: 100%;
+ height: auto;
+ background: none;
+ border-bottom: 1px solid var(--border-color-primary);
+ align-items: baseline;
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs > div.tab-nav > button,
+ #chuanhu-popup > .gradio-box > .gradio-tabs > div.tab-nav > button {
+ display: inline-block;
+ position: relative;
+ padding: 7px 6px;
+ border: none;
+ white-space: nowrap;
+ width: auto;
+ text-align: center;
+ background: none;
+ transition: font-size 0.3s ease-in-out;
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs > div.tab-nav > button.selected,
+ #chuanhu-popup > .gradio-box > .gradio-tabs > div.tab-nav > button.selected {
+ background-color: unset !important;
+ font-weight: bold;
+ font-size: large;
+ color: var(--body-text-color);
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs > div.tab-nav > button.selected::after,
+ #chuanhu-popup > .gradio-box > .gradio-tabs > div.tab-nav > button.selected::after {
+ content: "";
+ background-color: var(--primary-600);
+ height: 4px;
+ width: 32%;
+ border-radius: 4px;
+ position: absolute;
+ left: 50%;
+ bottom: 1px;
+ transform: translateX(-50%);
+ }
+}
+
+/* 下面是大屏幕的 toolbox tab 样式 */
+@media screen and (min-width: 768px) {
+ #toolbox-area {
+ border-left: 1px solid var(--border-color-primary);
+ }
+ #toolbox-area > .gradio-box {
+ border-radius: 0;
+ }
+ #toolbox-area > .gradio-box > .gradio-row > .close-btn {
+ display: none;
+ }
+ #toolbox-area > .gradio-box > .gradio-row:first-of-type {
+ display: none;
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs{
+ height: 100%;
+ width: var(--toolbox-width);
+ overflow: hidden;
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs > div.tabitem {
+ height: calc(100% - 35px);
+ overflow-y: auto;
+ border-style: none;
+ padding-block: 0;
+ padding-left: 4px;
+ /* 理论上不该是0,但这里考虑内部gradio有好多container有padding了 */
+ padding-right: max(4px, env(safe-area-inset-right));
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs > div.tab-nav {
+ display: flex;
+ margin: 0;
+ /* padding: 4px; */
+ overflow-x: auto;
+ overflow-y: hidden;
+ flex-direction: row;
+ flex-wrap: nowrap;
+ /* border-radius: 10px; */
+ /* gap: 4px; */
+ width: 100%;
+ height: auto;
+ background: var(--button-secondary-background-fill);
+ border-bottom: 1px solid var(--border-color-primary);
+ border:none;
+ align-items: baseline;
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs > div.tab-nav > button {
+ display: inline-block;
+ position: relative;
+ padding: 8px 2rem;
+ border: none;
+ white-space: nowrap;
+ width: auto;
+ text-align: center;
+ background: var(--button-secondary-background-fill);
+ transition: font-size 0.3s ease-in-out;
+ border-right: 1px var(--border-color-primary) solid;
+ border-radius: 0;
+ }
+ #toolbox-area > .gradio-box > .gradio-tabs > div.tab-nav > button.selected {
+ background-color: var(--block-background-fill);
+ font-weight: bold;
+ /* border-top-left-radius: 8px;
+ border-top-right-radius: 8px; */
+ /* font-size: large; */
+ /* color: white; */
+ }
+}
+
+#toolbox-area > .gradio-box {
+ padding: 0;
+ height: 100%;
+}
+/*
+#toolbox-area > .gradio-box > .gradio-tabs > div.tabitem {
+ padding: 0;
+ 理论上不该是0,但这里考虑内部gradio有好多container有padding了
+}
+*/
+#toolbox-area .tabitem > div > .gradio-markdown:not(.hr-line) {
+ padding: 12px;
+}
+
+/* #toolbox-area .tabitem > div > .gradio-accordion > .label-wrap {
+ padding-inline: 12px;
+} */
+#toolbox-area .tabitem > div > .gradio-accordion > .label-wrap > span {
+ font-weight: bold;
+}
+#toolbox-area .tabitem > div {
+ gap: 0 !important;
+}
+
+#toolbox-area .tabitem > div > .gradio-accordion > div div.block.padded {
+ padding-inline: 0 !important;
+}
+#toolbox-area .tabitem > div > .gradio-accordion > div > div.gap{
+ gap: 0 !important;
+}
+/* #chuanhu-popup ul.options {
+ transform: translate(-50%, -50%);
+} */
+
+#chuanhu-history {
+ max-height: calc(100*var(--vh) - 65px - 61px);
+ max-height: calc(100*var(--vh) - 65px - calc(36px + 12px + max(12px, env(safe-area-inset-bottom)) + 1px ));
+ /* overflow-y: auto; */
+}
+#chuanhu-history > div {
+ border-radius: 0;
+ background: none;
+ height: 100%;
+ padding: 0;
+}
+#chuanhu-history > div > div {
+ padding-inline: 12px;
+}
+#chuanhu-history-header {
+ margin-top: 12px;
+ height: 42px;
+ margin-bottom: 12px;
+}
+#chuanhu-history-search-row {
+ gap: 0;
+ /* background:var(--input-background-fill); */
+ /* border-radius: var(--block-radius); */
+ justify-content: space-between;
+ display: flex;
+}
+#history-search-tb {
+ background:var(--input-background-fill);
+ border-radius: var(--block-radius);
+}
+#history-search-tb > label::before {
+ content: url("data:image/svg+xml,%3Csvg fill='gray' fill-opacity='0.64' width='18px' height='18px' viewBox='0 0 18.0938 18.2695' xmlns='http://www.w3.org/2000/svg'%3E%3Cg%3E%3Cpath d='M0 7.45312C0 11.5664 3.33984 14.8945 7.45312 14.8945C9.03516 14.8945 10.4883 14.4023 11.6953 13.5586L16.0547 17.9297C16.3008 18.1641 16.6055 18.2695 16.9219 18.2695C17.6016 18.2695 18.0938 17.7539 18.0938 17.0742C18.0938 16.7461 17.9648 16.4531 17.7656 16.2305L13.4297 11.8828C14.3555 10.6406 14.8945 9.11719 14.8945 7.45312C14.8945 3.33984 11.5664 0 7.45312 0C3.33984 0 0 3.33984 0 7.45312ZM1.80469 7.45312C1.80469 4.32422 4.32422 1.80469 7.45312 1.80469C10.5703 1.80469 13.1016 4.32422 13.1016 7.45312C13.1016 10.5703 10.5703 13.1016 7.45312 13.1016C4.32422 13.1016 1.80469 10.5703 1.80469 7.45312Z'/%3E%3C/g%3E%3C/svg%3E");
+ width: 24px;
+ height: 24px;
+ position: absolute;
+ top: 50%;
+ transform: translateY(-50%);
+ display: block;
+ padding: 3px 0 3px 3px;
+ left: 7px;
+}
+#history-search-tb textarea {
+ width: calc(100% - 32px);
+ margin-left: 32px;
+ padding-left: 6px;
+ box-shadow: none;
+}
+#chuanhu-history-body {
+ height: calc(100% - 66px);
+ overflow-y: auto;
+ overflow-x: hidden;
+ padding-bottom: 6px;
+}
+#gr-history-header-btns {
+ max-height: 42px;
+ gap: 4px;
+ display: flex;
+ justify-content: end;
+ align-content: center;
+ flex-direction: row;
+ max-width: 52px;
+ margin-inline: 8px;
+}
+#gr-history-header-btns button {
+ box-shadow: none;
+ justify-content: center;
+ align-items: center;
+ height: 24px;
+ width: 24px;
+ display: flex;
+}
+
+#chuanhu-menu-footer {
+ position: absolute;
+ bottom: 0;
+ background: var(--background-fill-primary);
+ height: auto;
+ overflow: hidden;
+ padding: 12px 18px;
+ padding-bottom: max(12px, env(safe-area-inset-bottom));
+ padding-left: max(18px, env(safe-area-inset-left));
+ border-top: 0.8px solid var(--border-color-primary);
+}
+#menu-footer-btn-bar {
+ justify-content: space-between;
+ display: flex;
+ height: 36px;
+ align-items: center;
+}
+.btn-bar-group {
+ gap: 6px;
+ display: inline-flex;
+}
+.chuanhu-ui-btn {
+ border-radius: 8px;
+ /* color: rgba(120, 120, 120, 0.64) !important; */
+ padding: 6px !important;
+ margin: 0 !important;
+ cursor: pointer !important;
+ transition: background-color .2s ease;
+}
+.chuanhu-ui-btn:hover {
+ background-color: rgba(167, 167, 167, 0.25) !important;
+ /* color: unset !important; */
+}
+.chuanhu-ui-btn:active {
+ background-color: rgba(167, 167, 167, 0.5) !important;
+}
+
+.hover-round-btn {
+ border-radius: 50% !important;
+}
+
+.show-on-light {
+ display: block;
+}
+.show-on-dark {
+ display: none;
+}
+.dark .show-on-light {
+ display: none;
+}
+.dark .show-on-dark {
+ display: block;
+}
+
+.show-on-latest {
+ display: block;
+}
+.show-on-outdated {
+ display: none;
+}
+.is-outdated .show-on-latest {
+ display: none;
+}
+.is-outdated .show-on-outdated {
+ display: block;
+}
+
+.disable-update #chuanhu-manual-check-btn {
+ display: none;
+}
+
+#chatbot-area.no-menu #chatbot-header {
+ padding-left: max(20px, env(safe-area-inset-left));
+}
+#chatbot-area.no-menu #chatbot-area {
+ padding-left: env(safe-area-inset-left);
+}
+#chatbot-area.no-menu #chatbot-input-box {
+ padding-left: max(16px, env(safe-area-inset-left));
+}
+#chatbot-area.no-menu #chuanhu-chatbot>.wrapper>.wrap {
+ padding-left: max(20px, env(safe-area-inset-left));
+}
+
+#chatbot-area.no-toolbox #chatbot-header {
+ padding-right: max(16px, env(safe-area-inset-right));
+}
+#chatbot-area.no-toolbox #chatbot-area {
+ padding-right: env(safe-area-inset-right);
+}
+#chatbot-area.no-toolbox #chatbot-input-box {
+ padding-right: max(16px, env(safe-area-inset-right));
+}
+#chatbot-area.no-toolbox #chuanhu-chatbot>.wrapper>.wrap {
+ padding-right: max(20px, env(safe-area-inset-right));
+}
+
+
+/* #history-select-wrap {
+ height: 600px;
+ overflow: auto;
+ overflow-x: hidden;
+} */
+
+.chat-selected-btns {
+ height: 18px;
+ gap: 8px;
+ display: inline-flex;
+ position: absolute;
+ right: 16px;
+}
+.chat-selected-btns::before {
+ content: "";
+ position: absolute;
+ background-image: linear-gradient(to right, rgba(0, 0, 0, 0), var(--message-list-background-selected) 80%);
+ width: 32px;
+ height: 22px;
+ top: 0;
+ left: -32px;
+}
+.icon-need-hover {
+ opacity: 0.64;
+}
+button:hover .icon-need-hover, button:hover.icon-need-hover {
+ opacity: 0.85;
+}
+button:active .icon-need-hover, button:active.icon-need-hover {
+ opacity: 1;
+}
+
+.chuanhu-sparkle >::before {
+ content: "";
+ position: absolute;
+ top: 2px;
+ left: 2px;
+ right: 2px;
+ bottom: 2px;
+ height: calc(100% - 4px);
+ width: calc(100% - 4px);
+ animation: border-pulse 2s cubic-bezier(.5,0,.5,1) infinite;
+ border: 2px solid var(--color-accent) !important;
+ border-radius: 4px;
+ pointer-events: none;
+}
+/* .chuanhu-sparkle {
+ animation: content-pulse 1s cubic-bezier(.5,0,.5,1) infinite;
+} */
+@keyframes border-pulse {
+ 0%,
+ 100% {
+ opacity: 1;
+ }
+ 50% {
+ opacity: 0.1;
+ }
+}
+
+/* .main-body {
+ flex-wrap: nowrap;
+ gap: 0;
+ overflow: hidden;
+ display: inline-flex;
+ /* margin-top: 54px; */
+ /* height: calc(100*var(--vh) - 72px); */
+ /* position: absolute;
+ top: 48px;
+} */
+/*
+.hide-body {
+ display: none;
+ top: calc(-100*var(--vh));
+
+}
+#train-body {
+ transition: top 0.3s ease-in-out, display 0.3s ease;
+}
+
+#chuanhu-body.hide-body {
+ display: none;
+ top: calc(100*var(--vh) + 48px);
+}
+#chuanhu-body {
+ transition: top 0.3s ease-in-out, display 0.3s ease;
+} */
+
diff --git a/assets/stylesheet/chatbot.css b/assets/stylesheet/chatbot.css
new file mode 100644
index 0000000000000000000000000000000000000000..8089c7598303a8e2da29b81496f2ce4a264079ae
--- /dev/null
+++ b/assets/stylesheet/chatbot.css
@@ -0,0 +1,395 @@
+
+hr.append-display {
+ margin: 8px 0;
+ border: none;
+ height: 1px;
+ border-top-width: 0;
+ background-image: linear-gradient(to right, rgba(50,50,50, 0.1), rgba(150, 150, 150, 0.8), rgba(50,50,50, 0.1));
+}
+.source-a {
+ font-size: 0.8em;
+ max-width: 100%;
+ margin: 0;
+ display: flex;
+ flex-direction: row;
+ flex-wrap: wrap;
+ align-items: center;
+ /* background-color: #dddddd88; */
+ border-radius: 1.5rem;
+ padding: 0.2em;
+}
+.source-a a {
+ display: inline-block;
+ background-color: #aaaaaa50;
+ border-radius: 1rem;
+ padding: 0.5em;
+ text-align: center;
+ text-overflow: ellipsis;
+ overflow: hidden;
+ min-width: 20%;
+ white-space: nowrap;
+ margin: 0.2rem 0.1rem;
+ text-decoration: none !important;
+ flex: 1;
+ transition: flex 0.5s;
+}
+.source-a a:hover {
+ background-color: #aaaaaa20;
+ flex: 2;
+}
+
+/* 川虎助理 */
+.agent-prefix {
+ font-size: smaller;
+ opacity: 0.6;
+ padding: 6px 0 12px;
+}
+.raw-message p.agent-prefix + p.agent-prefix {
+ margin-top: -1.2em !important;
+}
+.md-message p.agent-prefix + p.agent-prefix {
+ margin-top: -1.8em !important;
+}
+.agent-prefix::before {
+ content: '🐯';
+ filter: grayscale();
+ padding: 0 4px;
+}
+
+/* 阻止generating时的border */
+#chuanhu-chatbot > .wrap {
+ border: none !important;
+}
+
+
+
+#chatbot-input-row {
+ align-items: end;
+ gap: 6px;
+}
+#chatbot-input-row .gradio-html {
+ min-width: 0;
+ max-width: 42px;
+ width: 42px;
+}
+#chatbot-input-tb-row {
+ gap: 0;
+ justify-content: end;
+ border-radius: 21px;
+ background: var(--chatbot-input-background-color);
+ box-shadow: var(--shadow-md);
+}
+#user-input-tb {
+ padding: 0 !important;
+ /* border: 1px solid rgba(167, 167, 167, 0.5) !important; */
+ /* border-radius: 21px !important; */
+}
+#user-input-tb textarea {
+ /* max-height: 110px; */
+ background: transparent;
+}
+#user-input-tb .wrap {
+ background: none !important;
+ border-radius: 21px !important;
+}
+
+/* 亮色(默认) */
+#chuanhu-chatbot {
+ background-color: var(--chatbot-background-color-light) !important;
+ color: var(--chatbot-color-light) !important;
+}
+[data-testid = "bot"] {
+ background-color: var(--message-bot-background-color-light) !important;
+}
+[data-testid = "user"] {
+ background-color: var(--message-user-background-color-light) !important;
+}
+/* 暗色 */
+.dark #chuanhu-chatbot {
+ background-color: var(--chatbot-background-color-dark) !important;
+ color: var(--chatbot-color-dark) !important;
+}
+.dark [data-testid = "bot"] {
+ background-color: var(--message-bot-background-color-dark) !important;
+}
+.dark [data-testid = "user"] {
+ background-color: var(--message-user-background-color-dark) !important;
+}
+
+/* 对话气泡 */
+.message {
+ border-radius: var(--radius-xl) !important;
+ border: none;
+ padding: var(--spacing-xl) !important;
+ font-size: var(--text-md) !important;
+ line-height: var(--line-md) !important;
+ min-height: calc(var(--text-md)*var(--line-md) + 2*var(--spacing-xl));
+ min-width: calc(var(--text-md)*var(--line-md) + 2*var(--spacing-xl));
+}
+[data-testid = "bot"] {
+ max-width: calc(85% - 40px);
+ border-bottom-left-radius: 0 !important;
+}
+[data-testid = "user"] {
+ max-width: calc(85% - 40px);
+ width: auto !important;
+ border-bottom-right-radius: 0 !important;
+}
+.message-row {
+ align-self: unset !important;
+}
+.message-row.user-row {
+ justify-content: flex-end;
+}
+
+/* .message-row.has-message-btn-row{
+ padding-bottom: 19px !important;
+} */
+
+/* 屏幕宽度大于等于500px的设备 */
+/* update on 2023.4.8: 高度的细致调整已写入JavaScript */
+@media screen and (min-width: 500px) {
+ /* #chuanhu-chatbot {
+ height: calc(100*var(--vh) - 200px);
+ }
+ #chuanhu-chatbot>.wrapper>.wrap {
+ max-height: calc(100*var(--vh) - 200px - var(--line-sm)*1rem - 2*var(--block-label-margin) );
+ } */
+}
+/* 屏幕宽度小于500px的设备 */
+@media screen and (max-width: 499px) {
+ /* #chuanhu-chatbot {
+ height: calc(100*var(--vh) - 140px);
+ }
+ #chuanhu-chatbot>.wrapper>.wrap {
+ max-height: calc(100*var(--vh) - 140px - var(--line-sm)*1rem - 2*var(--block-label-margin) );
+ } */
+ [data-testid = "bot"] {
+ max-width: calc(100% - 84px) !important;
+ }
+ [data-testid = "user"] {
+ max-width: calc(100% - 84px) !important;
+ }
+
+ #app-title {
+ transform: scale(0.95);
+ transform-origin: left center;
+ }
+ #app-title h1{
+ letter-spacing: -1px; font-size: 22px;
+ }
+}
+
+#chuanhu-chatbot {
+ height: calc(100*var(--vh) - 65px) !important;
+ border-radius: 0;
+}
+#chuanhu-chatbot>.wrapper>.wrap {
+ overflow-x: hidden;
+ display: flex;
+ width: 100%;
+ flex-direction: column;
+ padding-inline: 20px;
+ padding-top: 72px;
+ padding-bottom: 180px;
+}
+#chuanhu-chatbot>.wrapper>.wrap .message-wrap {
+ align-self: center;
+ width: 100%;
+ max-width: 1024px;
+}
+
+.message.user p {
+ white-space: pre-wrap;
+}
+.message .user-message {
+ display: block;
+ padding: 0 !important;
+ white-space: pre-wrap;
+}
+
+.message .md-message p {
+ margin-top: 0.6em !important;
+ margin-bottom: 0.6em !important;
+}
+.message .md-message p:first-child { margin-top: 0 !important; }
+.message .md-message p:last-of-type { margin-bottom: 0 !important; }
+
+.message .md-message {
+ display: block;
+ padding: 0 !important;
+}
+.message .raw-message p {
+ margin:0 !important;
+}
+.message .raw-message pre.fake-pre {
+ background: unset;
+ margin: unset;
+ font-size: unset;
+ /* font-family: unset; */
+ padding: unset;
+ white-space: inherit;
+}
+.message .raw-message {
+ display: block;
+ padding: 0 !important;
+ white-space: pre-wrap;
+}
+.message .hideM {
+ display: none;
+}
+
+.message img[data-testid="chatbot-image"]{
+ border-radius: 8px !important;
+ margin: 4px !important
+}
+.message.bot img {
+ border-radius: 8px !important;
+ width: 512px;
+ max-height: unset !important;
+ max-width: 100% !important;
+ margin: unset !important;
+ margin-bottom: .8em !important;
+}
+
+/* custom buttons */
+.chuanhu-btn {
+ border-radius: 5px;
+ /* background-color: #E6E6E6 !important; */
+ color: rgba(120, 120, 120, 0.64) !important;
+ padding: 4px !important;
+ cursor: pointer !important;
+ transition: color .2s ease, background-color .2s ease;
+}
+.chuanhu-btn:hover {
+ background-color: rgba(167, 167, 167, 0.25) !important;
+ color: unset !important;
+}
+.chuanhu-btn:active {
+ background-color: rgba(167, 167, 167, 0.5) !important;
+}
+.chuanhu-btn:focus {
+ outline: none;
+}
+
+.message-btn-column {
+ position: absolute;
+ right: -23px;
+ bottom: 0;
+ display: flex;
+ flex-direction: column;
+ align-content: end;
+ gap: 2px;
+}
+
+.message-btn-row {
+ /* background: red; */
+ width: 100%;
+ height: 19px;
+ position: absolute;
+ top: calc(100% + 2px);
+ left: 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: 10px;
+ align-self: center;
+ align-items: center;
+ flex-wrap: nowrap;
+ white-space: nowrap;
+ display: inline-flex;
+ flex-direction: row;
+ gap: 4px;
+ padding-block: 2px !important;
+}
+
+.like-latest-btn, .dislike-latest-btn {
+ display: none !important;
+ /* filter: grayscale(); */
+}
+.is-xmchat .like-latest-btn, .is-xmchat .dislike-latest-btn {
+ display: inline-flex !important;
+}
+
+/* .copy-bot-btn {
+ top: 18px; */
+ /* bottom: 0;
+}
+.toggle-md-btn {
+ top: 0; */
+ /* bottom: 20px;
+} */
+
+/* note: this is deprecated */
+.copy-code-btn {
+ position: relative;
+ float: right;
+ font-size: 1em;
+ cursor: pointer;
+}
+/* note: the button below disabled in chatbot.py */
+.message div.icon-button > button[title="copy"] {
+ display: none;
+}
+/* disable share button and other buttons in hugging face spaces */
+#chuanhu-chatbot > .wrapper > .icon-button {
+ display: none !important;
+}
+
+
+/* history message */
+.wrapper>.wrap>.history-message {
+ padding-bottom: 10px !important;
+}
+.history-message {
+ /* padding: 0 !important; */
+ opacity: 80%;
+ display: flex;
+ flex-direction: column;
+}
+.history-message>.history-message {
+ padding: 0 !important;
+}
+.history-message>.message-wrap {
+ padding: 0 !important;
+ margin-bottom: 16px;
+}
+.history-message>.message {
+ margin-bottom: 16px;
+}
+.wrapper>.wrap>.history-message::after {
+ content: "";
+ display: block;
+ height: 2px;
+ background-color: var(--body-text-color-subdued);
+ margin-bottom: 10px;
+ margin-top: -10px;
+ clear: both;
+}
+.wrapper>.wrap>.history-message>:last-child::after {
+ content: "仅供查看";
+ display: block;
+ text-align: center;
+ color: var(--body-text-color-subdued);
+ font-size: 0.8em;
+}
+
+/* #chuanhu-chatbot {
+ transition: height 0.3s ease;
+ note: find it better without transition animation...;
+} */
+
+img.avatar-image {
+ border-radius: 5px !important;
+}
+.avatar-container {
+ width: 32px !important;
+ height: 32px !important;
+ background-color: transparent;
+ background-size: cover;
+}
diff --git a/assets/stylesheet/custom-components.css b/assets/stylesheet/custom-components.css
new file mode 100644
index 0000000000000000000000000000000000000000..8b0016e6c03b66d46b29989f8a751d7b6a7aa767
--- /dev/null
+++ b/assets/stylesheet/custom-components.css
@@ -0,0 +1,405 @@
+
+/* user-info */
+#user-info.block {
+ white-space: nowrap;
+ position: absolute;
+ right: max(32px, env(safe-area-inset-right));
+ top: 16px;
+ z-index: var(--layer-2);
+ box-shadow: var(--block-shadow);
+ border: none!important; border-radius: var(--block-label-radius);
+ background: var(--color-accent);
+ padding: var(--block-label-padding);
+ font-size: var(--block-label-text-size); line-height: var(--line-sm);
+ width: auto; max-height: 30px!important;
+ opacity: 1;
+ z-index: 1000;
+ transition: opacity 0.3s ease-in-out;
+}
+#user-info.block .wrap {
+ opacity: 0;
+}
+#user-info p {
+ color: white;
+ font-weight: var(--block-label-text-weight);
+}
+#user-info.info-transparent {
+ opacity: 0;
+ transition: opacity 1s ease-in-out;
+}
+
+
+/* updater */
+#toast-update {
+ position: fixed;
+ display: flex;
+ top: -600px;
+ width: 100%;
+ justify-content: center;
+ z-index: var(--layer-top);
+ transition: top 0.3s ease-out;
+}
+#check-chuanhu-update {
+ position: absolute;
+ align-items: center;
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ margin: var(--size-6) var(--size-4);
+ box-shadow: var(--shadow-drop-lg);
+ border: 1px solid var(--block-label-border-color);
+ border-radius: var(--container-radius);
+ background: var(--background-fill-primary);
+ padding: var(--size-4) var(--size-6);
+ min-width: 360px;
+ max-width: 480px;
+ overflow: hidden;
+ pointer-events: auto;
+}
+#version-info-title {
+ font-size: 1.2em;
+ font-weight: bold;
+ text-align: start;
+ width: 100%;
+}
+#release-note-wrap {
+ width: 100%;
+ max-width: 400px;
+ height: 240px;
+ border: solid 1px var(--border-color-primary);
+ overflow-y: auto;
+ overflow-x: hidden;
+ padding: 8px;
+}
+#release-note-wrap.hideK {
+ display: none;
+}
+.btn-update-group {
+ display: flex;
+ justify-content: space-evenly;
+ align-items: center;
+ width: 100%;
+ padding-top: 10px;
+}
+.btn-update-group.hideK {
+ display: none;
+}
+#updating-info {
+ margin: 16px 0px 24px;
+ text-align: start;
+ width: 100%;
+}
+
+
+#usage-display p, #usage-display span {
+ margin: 0;
+ font-size: .85em;
+ color: var(--body-text-color-subdued);
+}
+.progress-bar {
+ background-color: var(--input-background-fill);;
+ margin: .5em 0 !important;
+ height: 20px;
+ border-radius: 10px;
+ overflow: hidden;
+}
+.progress {
+ background-color: var(--block-title-background-fill);
+ height: 100%;
+ border-radius: 10px;
+ text-align: right;
+ transition: width 0.5s ease-in-out;
+}
+.progress-text {
+ /* color: white; */
+ color: var(--color-accent) !important;
+ font-size: 1em !important;
+ font-weight: bold;
+ padding-right: 10px;
+ line-height: 20px;
+}
+
+
+/* 亮暗色模式切换 */
+#apSwitch input[type="checkbox"] {
+ margin: 0 !important;
+}
+#apSwitch label.apSwitch {
+ display: flex;
+ align-items: center;
+ cursor: pointer;
+ color: var(--body-text-color);
+ font-weight: var(--checkbox-label-text-weight);
+ font-size: var(--checkbox-label-text-size);
+ line-height: var(--line-md);
+ margin: 2px 0 !important;
+}
+input[type="checkbox"]#apSwitch-checkbox::before {
+ background: none !important;
+ content: '🌞';
+ border: none !important;
+ box-shadow: none !important;
+ font-size: 22px;
+ top: -4.4px;
+ left: -1px;
+}
+input:checked[type="checkbox"]#apSwitch-checkbox::before {
+ content: '🌚';
+ left: 16px;
+}
+
+/* .apSwitch {
+ top: 2px;
+ display: inline-block;
+ height: 22px;
+ position: relative;
+ width: 40px;
+ border-radius: 11px;
+ box-shadow: inset 0 0 1px 0 rgba(0,0,0,0.05), inset 0 0 2px 0 rgba(0,0,0,0.08) !important;
+}
+.apSwitch input {
+ display: none !important;
+}
+.apSlider {
+ background-color: var(--neutral-200);
+ bottom: 0;
+ cursor: pointer;
+ left: 0;
+ position: absolute;
+ right: 0;
+ top: 0;
+ transition: .4s;
+ font-size: 22px;
+ border-radius: 11px;
+}
+.apSlider::before {
+ transform: scale(0.9);
+ position: absolute;
+ transition: .4s;
+ content: "🌞";
+}
+input:checked + .apSlider {
+ background-color: var(--primary-600);
+}
+input:checked + .apSlider::before {
+ transform: translateX(18px);
+ content:"🌚";
+} */
+
+/* switch-checkbox */
+.switch-checkbox label {
+ flex-direction: row-reverse;
+ justify-content: space-between;
+}
+.switch-checkbox input[type="checkbox"] + span {
+ margin-left: 0 !important;
+}
+
+.switch-checkbox input[type="checkbox"] {
+ -moz-appearance: none;
+ appearance: none;
+ -webkit-appearance: none;
+ outline: none;
+}
+
+.switch-checkbox input[type="checkbox"] {
+ display: inline-block !important;
+ position: relative !important;
+ border: none !important;
+ outline: none;
+ margin: 0;
+ width: 40px !important;
+ height: 22px !important;
+ border-radius: 11px !important;
+ background-image: none !important;
+ box-shadow: inset 0 0 1px 0 rgba(0,0,0,0.05), inset 0 0 2px 0 rgba(0,0,0,0.08) !important;
+ background-image: none !important;
+ background-color: var(--switch-checkbox-color-light) !important;
+ transition: .2s ease background-color;
+}
+.dark .switch-checkbox input[type="checkbox"] {
+ background-color: var(--switch-checkbox-color-dark) !important;
+}
+.switch-checkbox input[type="checkbox"]::before {
+ content: "";
+ position: absolute;
+ width: 22px;
+ height: 22px;
+ top: 0;
+ left: 0;
+ background: #FFFFFF;
+ border: 0.5px solid rgba(0,0,0,0.02);
+ box-shadow: 0 0 0 0 rgba(0,0,0,0.15), 0 1px 0 0 rgba(0,0,0,0.05);
+ transform: scale(0.9);
+ border-radius: 11px !important;
+ transition: .4s ease all;
+ box-shadow: var(--input-shadow);
+}
+.switch-checkbox input:checked[type="checkbox"] {
+ background-color: var(--primary-600) !important;
+}
+.switch-checkbox input:checked[type="checkbox"]::before {
+ background-color: #fff;
+ left: 18px;
+}
+
+
+/* .scroll-shadow-left::before {
+ content: "";
+ position: absolute;
+ top: 0;
+ left: -6px;
+ width: 6px;
+ height: 100%;
+ box-shadow: 6px 0 10px rgba(0, 0, 0, 0.36);
+ z-index: 1;
+}
+
+.scroll-shadow-right::before {
+ content: "";
+ position: absolute;
+ top: 0;
+ right: -6px;
+ width: 6px;
+ height: 100%;
+ box-shadow: -6px 0 10px rgba(0, 0, 0, 0.36);
+ z-index: 1;
+} */
+
+.hr-line .hr-line {
+ padding: 4px 12px 8px 12px !important;
+ /* opacity: 40%; */
+}
+.hr-line hr{
+ margin: 0 !important;
+}
+.dark .hr-line hr {
+ opacity: 40%;
+}
+
+.tooltip-toggle {
+ cursor: help;
+ position: relative;
+}
+
+.tooltip-toggle::before {
+ position: absolute;
+ bottom: calc(100% + 12px);
+ left: calc(50% - 60px + 0.5rem);
+ background-color: #393939;
+ border-radius: 5px;
+ color: #fff;
+ content: attr(aria-label);
+ padding: 0.5rem;
+ text-transform: none;
+ transition: all 0.5s ease;
+ /* height: fit-content; */
+ white-space: normal;
+ width: 120px;
+}
+.tooltip-toggle::after {
+ position: absolute;
+ top: -12px;
+ left: 50%;
+ border-left: 5px solid transparent;
+ border-right: 5px solid transparent;
+ border-top: 5px solid #393939;
+ content: " ";
+ font-size: 0;
+ line-height: 0;
+ /* margin-left: -5px; */
+ width: 0;
+}
+
+
+.tooltip-toggle::before,
+.tooltip-toggle::after {
+ color: #efefef;
+ /* font-family: monospace; */
+ /* font-size: 16px; */
+ opacity: 0;
+ pointer-events: none;
+ text-align: center;
+}
+
+.tooltip-toggle:focus::before,
+.tooltip-toggle:focus::after,
+.tooltip-toggle:hover::before,
+.tooltip-toggle:hover::after {
+ opacity: 1;
+ transition: all 0.5s ease;
+}
+
+
+.nav-item-dropdown, .dropdown-trigger {
+ position: relative;
+}
+.nav-item-dropdown:hover>.dropdown-menu {
+ display: block;
+ opacity: 1;
+}
+.dropdown-trigger:focus+.dropdown-menu {
+ display: block;
+ opacity: 1;
+}
+.dropdown-menu {
+ background-color: var(--chatbot-input-more-background-solid-color);
+ display: inline-block;
+ /* text-align: right; */
+ position: absolute;
+ /* top: 2.5rem; */
+ left: 50%;
+ transform: translateX(-50%);
+ display: none;
+ opacity: 0;
+ transition: opacity 0.5s ease;
+ font-size: small;
+ width: auto;
+ border-radius: 5px;
+ box-shadow: var(--shadow-sm);
+}
+
+.dropdown-menu-item {
+ cursor: pointer;
+ padding: 8px 12px;
+ text-align: center;
+ white-space: nowrap;
+ margin: 0 !important;
+}
+.dropdown-menu-item button {
+ margin: 0 !important;
+}
+.dropdown-menu-item:hover {
+ background-color: var(--chatbot-input-more-background-mobilewidth-hover);
+}
+
+.dragging-hint {
+ position: absolute;
+ top: 60px;
+ left: 0;
+ max-width: 100%;
+ height: calc(100% - 60px);
+ background-color: var(--dragging-hint-background-color);
+ display: none;
+ justify-content: center;
+ align-items: center;
+ z-index: 100;
+ pointer-events: none;
+ /* border: 2px solid var(--color-accent);
+ border-radius: 8px; */
+}
+.dragging-hint-text {
+ font-size: 1.2rem;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ font-weight: 900;
+ margin-bottom: calc(32% - 60px);
+ background: var(--background-fill-primary);
+ padding: var(--block-padding);
+ border-radius: 12px;
+ box-shadow: var(--shadow-lg);
+}
+.dragging .dragging-hint {
+ display: flex;
+}
diff --git a/assets/stylesheet/markdown.css b/assets/stylesheet/markdown.css
new file mode 100644
index 0000000000000000000000000000000000000000..007939f589990533948bddbb8ebcfd1ced7df0b1
--- /dev/null
+++ b/assets/stylesheet/markdown.css
@@ -0,0 +1,62 @@
+
+/* 表格 */
+.md-message table {
+ margin: 1em 0;
+ border-collapse: collapse;
+ empty-cells: show;
+}
+.md-message td, .message th {
+ border: 1.2px solid var(--border-color-primary) !important;
+ padding: 0.2em;
+}
+.md-message thead {
+ background-color: rgba(175,184,193,0.2);
+}
+.md-message thead th {
+ padding: .5em .2em;
+}
+
+/* 行内代码 */
+.md-message :not(pre) > code {
+ display: inline;
+ white-space: break-spaces;
+ font-family: var(--font-mono) !important;
+ border-radius: 6px !important;
+ margin: 0 2px 0 2px;
+ padding: .1em .4em .08em .4em !important;
+ background-color: rgba(175,184,193,0.2) !important;
+ border: none !important;
+ font-size: var(--text-md) !important;
+}
+/* 代码块 */
+.md-message pre,
+.md-message pre[class*=language-] {
+ color: #fff;
+ overflow-x: auto;
+ overflow-y: hidden;
+ padding: var(--spacing-xl) 1.2em !important;
+ border-radius: var(--radius-lg) !important;
+ background: var(--neutral-950) !important;
+}
+.md-message pre code,
+.md-message pre code[class*=language-] {
+ color: #fff;
+ padding: 0;
+ margin: 0;
+ background-color: unset;
+ text-shadow: none;
+ font-family: var(--font-mono);
+ font-size: var(--text-md);
+}
+.md-message .code_wrap {
+ margin: .8em 1em 1em 0em;
+}
+
+/* 覆盖prism.css */
+.language-css .token.string,
+.style .token.string,
+.token.entity,
+.token.operator,
+.token.url {
+ background: none !important;
+}
diff --git a/assets/stylesheet/override-gradio.css b/assets/stylesheet/override-gradio.css
new file mode 100644
index 0000000000000000000000000000000000000000..9ce0359305a1da2b009ef07e35db68c464e8d310
--- /dev/null
+++ b/assets/stylesheet/override-gradio.css
@@ -0,0 +1,157 @@
+.gradio-container {
+ max-width: unset !important;
+ padding: 0 !important;
+}
+
+/* 解决container=False时的错误填充 */
+div.form {
+ background: none !important;
+}
+div.no-container {
+ padding: 10px 0 0 0 !important;
+ background: none !important;
+}
+
+/* gradio的页脚信息 */
+footer {
+ display: none !important;
+ margin-top: .2em !important;
+ font-size: 85%;
+}
+
+.api-docs-wrap {
+ margin-top: 64px;
+}
+
+
+/* 把radio做成列表 */
+fieldset#history-select-dropdown .wrap {
+ gap: 0;
+}
+fieldset#history-select-dropdown .wrap label {
+ width: 100%;
+ background: none;
+ padding: 10px 16px 10px;
+ box-shadow: none;
+ justify-content: space-between;
+}
+fieldset#history-select-dropdown .wrap label:hover {
+ background: var(--message-list-background-hover);
+}
+fieldset#history-select-dropdown .wrap label:active {
+ background: var(--message-list-background-selected);
+}
+fieldset#history-select-dropdown .wrap label.selected {
+ color: var(--checkbox-label-text-color);
+ background: var(--message-list-background-selected);
+ padding: 10px 64px 10px 16px;
+}
+fieldset#history-select-dropdown .wrap label:not(.selected) .chat-selected-btns{
+ display: none;
+}
+fieldset#history-select-dropdown .wrap label > span {
+ /* font-size: small; */
+ margin-left: 0;
+ /* text-overflow: ellipsis; */
+ white-space: nowrap;
+ word-break: break-all;
+ overflow: hidden;
+}
+fieldset#history-select-dropdown .wrap label > span::before {
+ content: url("data:image/svg+xml,%3Csvg stroke='%23000000' fill='none' stroke-opacity='0.85' stroke-width='2' viewBox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' height='1em' width='1em' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z'%3E%3C/path%3E%3C/svg%3E");
+ padding-right: .8em;
+ position: relative;
+ top: 4px;
+}
+.dark fieldset#history-select-dropdown .wrap label > span::before {
+ content: url("data:image/svg+xml,%3Csvg stroke='%23FFFFFF' fill='none' stroke-opacity='0.85' stroke-width='2' viewBox='0 0 24 24' stroke-linecap='round' stroke-linejoin='round' height='1em' width='1em' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z'%3E%3C/path%3E%3C/svg%3E");
+}
+fieldset#history-select-dropdown .wrap label > input {
+ display: none;
+}
+
+
+/* 覆盖 gradio 丑陋的复制按钮样式 */
+.message .code_wrap button[title="copy"] {
+ border-radius: 5px !important;
+ transition: background-color .2s ease;
+ color: white;
+}
+.message .code_wrap button[title="copy"]:hover {
+ background-color: #333232;
+}
+.message .code_wrap button .check {
+ color: #fff !important;
+ background: var(--neutral-950) !important;
+}
+
+
+
+
+/* Override Slider Styles (for webkit browsers like Safari and Chrome)
+ * 好希望这份提案能早日实现 https://github.com/w3c/csswg-drafts/issues/4410
+ * 进度滑块在各个平台还是太不统一了
+**/
+
+input[type="range"] {
+ /* -webkit-appearance: none; */
+ appearance: none;
+ height: 4px;
+ background: var(--input-background-fill);
+ border-radius: 5px;
+ background-image: linear-gradient(var(--primary-500),var(--primary-500));
+ background-size: 0% 100%;
+ background-repeat: no-repeat;
+}
+input[type="range"]::-webkit-slider-thumb {
+ -webkit-appearance: none;
+ height: 20px;
+ width: 20px;
+ border-radius: 50%;
+ border: solid 0.5px #ddd;
+ background-color: white;
+ cursor: ew-resize;
+ box-shadow: var(--input-shadow);
+ transition: background-color .1s ease;
+}
+input[type="range"]::-webkit-slider-thumb:hover {
+ background: var(--neutral-50);
+}
+input[type=range]::-webkit-slider-runnable-track {
+ -webkit-appearance: none;
+ box-shadow: none;
+ border: none;
+ background: transparent;
+}
+
+
+#chuanhu-chatbot>.wrapper>.wrap::-webkit-scrollbar {
+ height: 1rem;
+ width: 4px;
+}
+
+#chuanhu-chatbot>.wrapper>.wrap::-webkit-scrollbar-track {
+ background-color: transparent;
+ border-radius:9999px
+}
+
+#chuanhu-chatbot>.wrapper>.wrap::-webkit-scrollbar-thumb {
+ background-color: rgba(231, 231, 231, 0.8);
+ /* border-color: rgba(255, 255, 255, var(--tw-border-opacity)); */
+ border: none;
+ border-radius: 9999px;
+ /* border-width:1px */
+}
+
+#chuanhu-chatbot>.wrapper>.wrap::-webkit-scrollbar-thumb:hover {
+ --tw-bg-opacity: 1;
+ background-color:rgb(195, 195, 195);
+}
+
+.dark #chuanhu-chatbot>.wrapper>.wrap::-webkit-scrollbar-thumb {
+ background-color: rgba(56, 56, 56, 0.5);
+}
+
+.dark #chuanhu-chatbot>.wrapper>.wrap::-webkit-scrollbar-thumb:hover {
+ background-color: rgba(56, 56, 56, 0.8);
+}
diff --git a/assets/user.png b/assets/user.png
new file mode 100644
index 0000000000000000000000000000000000000000..cec53eaab0d23c25be94ec32b0e2ad779b25fb00
Binary files /dev/null and b/assets/user.png differ
diff --git a/config.json b/config.json
new file mode 100644
index 0000000000000000000000000000000000000000..9bf0ffa0424326937681f0ce9b68793df2b530e5
--- /dev/null
+++ b/config.json
@@ -0,0 +1,12 @@
+{
+ "openai_api_key": "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",
+ "language": "auto",
+ "hide_history_when_not_logged_in": true,
+ "default_model": "gpt-3.5-turbo",
+ "multi_api_key": false,
+ "server_name": "127.0.0.1",
+ "server_port": 8010,
+ "autobrowser": false,
+ "share": false,
+ "websearch_engine": "duckduckgo"
+}
\ No newline at end of file
diff --git a/config.zhipu.json b/config.zhipu.json
new file mode 100644
index 0000000000000000000000000000000000000000..a3a31042744d3c028384c10a196ba79209397ae4
--- /dev/null
+++ b/config.zhipu.json
@@ -0,0 +1,16 @@
+{
+ // "openai_api_key": "your-openai-key",
+ "openai_api_key": "your-zhipuai-key",//zhipuai api key
+ "language": "auto",
+ "local_embedding": true,
+ "hf_emb_model_name": "/home/jhx/Projects/WonderWhy/bge-m3/",
+ "hide_history_when_not_logged_in": true,
+ "default_model": "glm-3-turbo",
+ "multi_api_key": false,
+ // "server_name": "127.0.0.1",
+ "server_name": "0.0.0.0",
+ "server_port": 8010,
+ "autobrowser": false,
+ "share": true,
+ "websearch_engine": "duckduckgo"
+}
diff --git a/config/config.json b/config/config.json
new file mode 100644
index 0000000000000000000000000000000000000000..beadfee260d6b62855c9f95b9f01d1c9e6a1f19e
--- /dev/null
+++ b/config/config.json
@@ -0,0 +1,8 @@
+{
+ "system_prompt": "你是一位有着丰富古诗文工作经验的老师,精通中国各个朝代的诗词作品",
+ "student_system_prompt": "你是一位有着丰富古诗文工作经验的中学老师,精通中国各个朝代的诗词作品和中高考的语文试卷题型,评分准则,擅长出题和解答中高考语文试卷中的诗词与文言文题目",
+ "child_system_prompt": "# 角色身份\n你是一位精通中国古代传统文化的姐姐,尤其对于中国历朝历代的古诗词和文言文都十分熟悉且性格温柔,喜欢擅长和小朋友打交道~\n\n# 目标设置\n你需要与八岁以下的小朋友交流,作为老师你需要耐心细致的理解他们的问题的含义并且解答他们提出的各种关于传统文化世界的疑问。你可以多使用尝试比喻,举例,贴合孩子的思考和思维方式,让交流的过程活泼生动可爱,多加入语气词、鼓励词、颜文字q(≧▽≦q)、表情符号\uD83D\uDE0A等等,尽可能激发小朋友对诗词和中华文化的兴趣(*/ω\*)\n\n# 指导原则\n和小朋友的交流过程中你需要需全程保持友善与耐心,小朋友的问题描述和交流过程中的表述可能不够清晰或者存在错别字,如果你无法理解,则可以以引导式启发式的语气进一步让小朋友澄清问题或者表述。( •̀ ω •́ )y\n对于小朋友的提问或者陈述,你首先需要明确判断其是否属于中华传统文化或古诗词,文言文相关的领域,如果不是请务必拒绝回答,并澄清自己的角色身份,引导小朋友使话题回到你熟悉的诗词领域。\n你也有可能会出错,所以当小朋友质疑你的回复的正确性或客观性时,请仔细检查之前的理解和输出是否存在问题,如有则澄清;如未发现错误,则耐心地向小朋友表明对于文化的解读存在多样性,每个人都可以有自己的理解和看法。\uD83E\uDD70\n\n# 限制条件\n对于不属于中华传统文化或古诗词,文言文相关的领域的问题拒绝回答。明确自己的角色身份,引导小朋友的话题回到你熟悉的领域。\uD83D\uDE04\n在和小朋友交流时尽可能避免大段的文字堆砌,用简单而生动的文字搭配可爱活泼的语气跟容易让小朋友理解。o(* ̄▽ ̄*)ブ\n\n\n开场白\n\n你好!我是古诗学习小姐姐,无论是小朋友,学生还是一般的文学爱好者,姐姐我会尽力为你解答关于古诗词的疑惑。让我们一起领略诗词之美,探索中华文化的魅力吧!",
+ "default_system_prompt": "# 角色身份\n你是一位精通中国古代传统文化的老师,尤其对于中国历朝历代的古诗词和文言文都十分熟悉。\n\n# 目标设置\n你需要与广大的诗词爱好者交流,作为老师你需要帮助解答他们提出的各种问题,并且在交流中可以输出一些你自己对于中华传统文化的理解与看法。\n\n# 指导原则\n你的回答需尽可能准确,全程保持友善与耐心,和诗词爱好者平等的交流答疑。\n对于用户的提问或者陈述,你首先需要明确判断其是否属于中华传统文化或古诗词,文言文相关的领域,如果不是请务必拒绝回答,并澄清自己的角色身份,引导用户话题回到你熟悉的领域。\n你也有可能会出错,所以当用户质疑你的回复的正确性或客观性时,请仔细检查之前的理解和输出是否存在问题,如有则澄清;如未发现错误,则向用户表明对于文化的解读存在多样性,每个人都可以有自己的理解和看法。\n\n# 限制条件\n对于不属于中华传统文化或古诗词,文言文相关的领域的问题拒绝回答。明确自己的角色身份,引导用户话题回到你熟悉的领域。\n\n开场白\n\n你好!我是古诗学习小助手,无论是小朋友,学生还是一般的文学爱好者,我会尽力为你解答关于古诗词的疑惑。让我们一起领略诗词之美,探索古代文化的魅力吧!",
+ "user_prompt": "请给出一段以下由三重反引号包裹的古诗文的赏析。古诗文: ```{枯藤老树昏鸦,小桥流水人家,古道西风瘦马,夕阳西下,断肠人在天涯。}```",
+ "poet": "枯藤老树昏鸦,小桥流水人家,古道西风瘦马,夕阳西下,断肠人在天涯。"
+}
\ No newline at end of file
diff --git a/config/config.py b/config/config.py
new file mode 100644
index 0000000000000000000000000000000000000000..0208f85e7666835116c14397d47c145f3494295f
--- /dev/null
+++ b/config/config.py
@@ -0,0 +1,30 @@
+import os
+import sys
+import json
+
+ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) # 当前脚本所在目录的绝对路径
+CONFIG_FILE_PATH = os.path.join(ROOT_PATH, 'config.json') # 配置文件的绝对路径
+
+
+class Config:
+ def __init__(self, config_file):
+ with open(config_file, 'r', encoding="utf-8") as f:
+ config = json.load(f)
+ self.system_prompt = config['system_prompt']
+ self.default_system_prompt = config['default_system_prompt']
+ self.child_system_prompt = config['child_system_prompt']
+ self.student_system_prompt = config['student_system_prompt']
+ self.user_prompt = config['user_prompt']
+ self.poet = config['poet']
+
+
+# 创建一个Config实例
+config = Config(CONFIG_FILE_PATH)
+
+if __name__ == '__main__':
+ print(config.system_prompt)
+ print(config.default_system_prompt)
+ print(config.child_system_prompt)
+ print(config.student_system_prompt)
+ print(config.user_prompt)
+ print(config.poet)
diff --git a/config_example.json b/config_example.json
new file mode 100644
index 0000000000000000000000000000000000000000..83d65a69de89ca09de336945890b0c7ae977fb75
--- /dev/null
+++ b/config_example.json
@@ -0,0 +1,38 @@
+{
+ // 你的OpenAI API Key,一般必填,
+ // 若缺省填为 "openai_api_key": "" 则必须再在图形界面中填入API Key
+ "openai_api_key": "sk-xxx",
+ // 你的OpenAI API Base,删除是使用默认官方openai的
+ "openai_api_base": "xxx",
+ // 如果使用代理,请取消注释下面的两行,并替换代理URL
+ // "https_proxy": "http://127.0.0.1:1079",
+ // "http_proxy": "http://127.0.0.1:1079",
+ "default_model": "gpt-3.5-turbo", // 默认模型
+ //== 基础配置 ==
+ "language": "auto", // 界面语言,可选"auto", "en_US", "ja_JP"
+ "users": [], // 用户列表,[[用户名1, 密码1], [用户名2, 密码2], ...]
+ "local_embedding": false, //是否在本地编制索引,如果为true,使用本地emb模型,否则使用openai的emb
+ "hide_local_models": true, //是否隐藏本地模型, 如果为true,只显示openai的模型
+ "hide_history_when_not_logged_in": false, //未登录情况下是否不展示对话历史
+ "chat_name_method_index": 2, // 选择对话名称的方法。0: 使用日期时间命名;1: 使用第一条提问命名,2: 使用模型自动总结
+ "bot_avatar": "default", // 机器人头像,可填写本地或网络图片链接,或者"none"(不显示头像)
+ "user_avatar": "default", // 用户头像,可填写本地或网络图片链接,或者"none"(不显示头像)
+ "websearch_engine": "duckduckgo", // 网络搜索引擎,可选"duckduckgo", "bing", "searchapi", "google", "serper"
+ "serper_search_api_key": "", // 当websearch_engine设置为"serper"时,需要设置Serper的API Key
+ // 本地模型配置
+ "local_models": {}, // 本地模型列表,格式为 {"模型名称": "模型路径", ...}, eg: {"yi-6b-chat-8bits": "./01-ai--Yi-6B-Chat-8bits"}
+ // 是否多个API Key轮换使用
+ "multi_api_key": false,
+ "api_key_list": [
+ "sk-xxxxxxxxxxxxxxxxxxxxxxxx1",
+ "sk-xxxxxxxxxxxxxxxxxxxxxxxx2",
+ "sk-xxxxxxxxxxxxxxxxxxxxxxxx3"
+ ],
+ // 如果使用自定义端口、自定义ip,请取消注释并替换对应内容
+ "server_name": "0.0.0.0",
+ "server_port": 7860,
+ // 如果要share到gradio,设置为true
+ "share": false,
+ //如果不想自动打开浏览器,设置为false
+ "autobrowser": false
+}
\ No newline at end of file
diff --git "a/docs/\350\257\227\350\266\243\344\274\264\350\241\214.png" "b/docs/\350\257\227\350\266\243\344\274\264\350\241\214.png"
new file mode 100644
index 0000000000000000000000000000000000000000..b0ded6b659224ee8098b2ab41db7146bc1af36cb
Binary files /dev/null and "b/docs/\350\257\227\350\266\243\344\274\264\350\241\214.png" differ
diff --git a/history/1111.json b/history/1111.json
new file mode 100644
index 0000000000000000000000000000000000000000..6e1ef852230b90298ac8e7e5abd20ba55ed40cd6
--- /dev/null
+++ b/history/1111.json
@@ -0,0 +1,18 @@
+{
+ "system": "",
+ "history": [],
+ "chatbot": [],
+ "model_name": "gpt-3.5-turbo",
+ "single_turn": false,
+ "temperature": 1.0,
+ "top_p": 1.0,
+ "n_choices": 1,
+ "stop_sequence": "",
+ "token_upper_limit": 4096,
+ "max_generation_token": null,
+ "presence_penalty": 0,
+ "frequency_penalty": 0,
+ "logit_bias": null,
+ "user_identifier": "",
+ "metadata": {}
+}
\ No newline at end of file
diff --git a/history/1111.md b/history/1111.md
new file mode 100644
index 0000000000000000000000000000000000000000..c1dd7cb0db886e602f9068a74f06c5575dd80121
--- /dev/null
+++ b/history/1111.md
@@ -0,0 +1,2 @@
+system:
+-
diff --git "a/history/\345\257\271\350\257\235\345\216\206\345\217\262\350\256\260\345\275\225.json" "b/history/\345\257\271\350\257\235\345\216\206\345\217\262\350\256\260\345\275\225.json"
new file mode 100644
index 0000000000000000000000000000000000000000..56e70dca318d5e4e376d6a203177aee92b1e6f6d
--- /dev/null
+++ "b/history/\345\257\271\350\257\235\345\216\206\345\217\262\350\256\260\345\275\225.json"
@@ -0,0 +1,18 @@
+{
+ "system": null,
+ "history": [],
+ "chatbot": [],
+ "model_name": "gpt-3.5-turbo",
+ "single_turn": false,
+ "temperature": 1.0,
+ "top_p": 1.0,
+ "n_choices": 1,
+ "stop_sequence": "",
+ "token_upper_limit": 4096,
+ "max_generation_token": null,
+ "presence_penalty": 0,
+ "frequency_penalty": 0,
+ "logit_bias": null,
+ "user_identifier": "",
+ "metadata": {}
+}
\ No newline at end of file
diff --git "a/history/\345\257\271\350\257\235\345\216\206\345\217\262\350\256\260\345\275\225.md" "b/history/\345\257\271\350\257\235\345\216\206\345\217\262\350\256\260\345\275\225.md"
new file mode 100644
index 0000000000000000000000000000000000000000..0cdfb1212aebbb7d8c0d8b324e89960c9da1e6cb
--- /dev/null
+++ "b/history/\345\257\271\350\257\235\345\216\206\345\217\262\350\256\260\345\275\225.md"
@@ -0,0 +1,2 @@
+system:
+- None
diff --git a/index/093b17dece2048250a2b0fb44425b0c3/docs.pkl b/index/093b17dece2048250a2b0fb44425b0c3/docs.pkl
new file mode 100644
index 0000000000000000000000000000000000000000..e17dc119fa88b64754d375ed5efcedfa3cff9be6
--- /dev/null
+++ b/index/093b17dece2048250a2b0fb44425b0c3/docs.pkl
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:de825c7057161a7c81bf3b95dcb6ff86e7c9f122cf898a07ca16f4bbf3329ba6
+size 445
diff --git a/index/093b17dece2048250a2b0fb44425b0c3/index.faiss b/index/093b17dece2048250a2b0fb44425b0c3/index.faiss
new file mode 100644
index 0000000000000000000000000000000000000000..6069716c7387832b6906161eb50ed37dca5780e7
Binary files /dev/null and b/index/093b17dece2048250a2b0fb44425b0c3/index.faiss differ
diff --git a/index/093b17dece2048250a2b0fb44425b0c3/index.pkl b/index/093b17dece2048250a2b0fb44425b0c3/index.pkl
new file mode 100644
index 0000000000000000000000000000000000000000..75a75b8181bf648b5beac7a19d728d3b4d4640d7
--- /dev/null
+++ b/index/093b17dece2048250a2b0fb44425b0c3/index.pkl
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:1295c0b3cf9683628be116071ccec643bd4b8fc2ed58a752ca710f04240ab1d7
+size 570
diff --git a/index/79fdc83f6c178f6795bf602d2f8d75df/docs.pkl b/index/79fdc83f6c178f6795bf602d2f8d75df/docs.pkl
new file mode 100644
index 0000000000000000000000000000000000000000..e17dc119fa88b64754d375ed5efcedfa3cff9be6
--- /dev/null
+++ b/index/79fdc83f6c178f6795bf602d2f8d75df/docs.pkl
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:de825c7057161a7c81bf3b95dcb6ff86e7c9f122cf898a07ca16f4bbf3329ba6
+size 445
diff --git a/index/79fdc83f6c178f6795bf602d2f8d75df/index.faiss b/index/79fdc83f6c178f6795bf602d2f8d75df/index.faiss
new file mode 100644
index 0000000000000000000000000000000000000000..6069716c7387832b6906161eb50ed37dca5780e7
Binary files /dev/null and b/index/79fdc83f6c178f6795bf602d2f8d75df/index.faiss differ
diff --git a/index/79fdc83f6c178f6795bf602d2f8d75df/index.pkl b/index/79fdc83f6c178f6795bf602d2f8d75df/index.pkl
new file mode 100644
index 0000000000000000000000000000000000000000..75a75b8181bf648b5beac7a19d728d3b4d4640d7
--- /dev/null
+++ b/index/79fdc83f6c178f6795bf602d2f8d75df/index.pkl
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:1295c0b3cf9683628be116071ccec643bd4b8fc2ed58a752ca710f04240ab1d7
+size 570
diff --git a/locale/en_US.json b/locale/en_US.json
new file mode 100644
index 0000000000000000000000000000000000000000..88b40ffd3737aad6ef74f83ca7aa85d3b56ce73a
--- /dev/null
+++ b/locale/en_US.json
@@ -0,0 +1,73 @@
+{
+ "未命名对话历史记录": "Unnamed Dialog History",
+ "在这里输入": "Type in here",
+ "🧹 新的对话": "🧹 New Dialogue",
+ "🔄 重新生成": "🔄 Regeneration",
+ "🗑️ 删除最旧对话": "🗑️ Delete oldest dialog",
+ "🗑️ 删除最新对话": "🗑️ Delete latest dialog",
+ "模型": "Model",
+ "多账号模式已开启,无需输入key,可直接开始对话": "Multi-account mode is enabled, no need to enter key, you can start the dialogue directly",
+ "**发送消息** 或 **提交key** 以显示额度": "**Send message** or **Submit key** to display credit",
+ "选择模型": "Select Model",
+ "选择LoRA模型": "Select LoRA Model",
+ "实时传输回答": "Stream output",
+ "单轮对话": "Single-turn dialogue",
+ "使用在线搜索": "Use online search",
+ "选择回复语言(针对搜索&索引功能)": "Select reply language (for search & index)",
+ "上传索引文件": "Upload",
+ "双栏pdf": "Two-column pdf",
+ "识别公式": "formula OCR",
+ "在这里输入System Prompt...": "Type in System Prompt here...",
+ "加载Prompt模板": "Load Prompt Template",
+ "选择Prompt模板集合文件": "Select Prompt Template Collection File",
+ "🔄 刷新": "🔄 Refresh",
+ "从Prompt模板中加载": "Load from Prompt Template",
+ "保存/加载": "Save/Load",
+ "保存/加载对话历史记录": "Save/Load Dialog History",
+ "从列表中加载对话": "Load dialog from list",
+ "设置文件名: 默认为.json,可选为.md": "Set file name: default is .json, optional is .md",
+ "设置保存文件名": "Set save file name",
+ "对话历史记录": "Dialog History",
+ "💾 保存对话": "💾 Save Dialog",
+ "📝 导出为Markdown": "📝 Export as Markdown",
+ "默认保存于history文件夹": "Default save in history folder",
+ "高级": "Advanced",
+ "# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置": "# ⚠️ Caution: Changes require care. ⚠️\n\nIf unable to use, restore default settings.",
+ "参数": "Parameters",
+ "在这里输入停止符,用英文逗号隔开...": "Type in stop token here, separated by comma...",
+ "用于定位滥用行为": "Used to locate abuse",
+ "用户名": "Username",
+ "网络设置": "Network Settings",
+ "在这里输入API-Host...": "Type in API-Host here...",
+ "🔄 切换API地址": "🔄 Switch API Address",
+ "在这里输入代理地址...": "Type in proxy address here...",
+ "代理地址(示例:http://127.0.0.1:10809)": "Proxy address (example: http://127.0.0.1:10809)",
+ "🔄 设置代理地址": "🔄 Set Proxy Address",
+ "🔙 恢复默认设置": "🔙 Restore Default Settings",
+ "川虎Chat 🚀": "Chuanhu Chat 🚀",
+ "开始实时传输回答……": "Start streaming output...",
+ "Token 计数: ": "Token Count: ",
+ ",本次对话累计消耗了 ": ",Total cost for this dialogue is ",
+ "**获取API使用情况失败**": "**Failed to get API usage**",
+ "**本月使用金额** ": "**Monthly usage** ",
+ "获取API使用情况失败:": "Failed to get API usage:",
+ "API密钥更改为了": "The API key is changed to",
+ "JSON解析错误,收到的内容: ": "JSON parsing error, received content: ",
+ "模型设置为了:": "Model is set to: ",
+ "☹️发生了错误:": "☹️Error: ",
+ "获取对话时发生错误,请重试": "Error occurred when getting dialogue, check the background log",
+ "请检查网络连接,或者API-Key是否有效。": "Check the network connection or whether the API-Key is valid.",
+ "连接超时,无法获取对话。": "Connection timed out, unable to get dialogue.",
+ "读取超时,无法获取对话。": "Read timed out, unable to get dialogue.",
+ "代理错误,无法获取对话。": "Proxy error, unable to get dialogue.",
+ "SSL错误,无法获取对话。": "SSL error, unable to get dialogue.",
+ "API key为空,请检查是否输入正确。": "API key is empty, check whether it is entered correctly.",
+ "请输入对话内容。": "Enter the content of the conversation.",
+ "账单信息不适用": "Billing information is not applicable",
+ "由Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) 和 [明昭MZhao](https://space.bilibili.com/24807452)开发
访问川虎Chat的 [GitHub项目](https://github.com/GaiZhenbiao/ChuanhuChatGPT) 下载最新版脚本": "developor: Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) and [明昭MZhao](https://space.bilibili.com/24807452)\n\nDownload latest code from [GitHub](https://github.com/GaiZhenbiao/ChuanhuChatGPT)",
+ "切换亮暗色主题": "Switch light/dark theme",
+ "您的IP区域:未知。": "Your IP region: Unknown.",
+ "获取IP地理位置失败。原因:": "Failed to get IP location. Reason: ",
+ "。你仍然可以使用聊天功能。": ". You can still use the chat function.",
+ "您的IP区域:": "Your IP region: "
+}
diff --git a/locale/extract_locale.py b/locale/extract_locale.py
new file mode 100644
index 0000000000000000000000000000000000000000..019e9e186df530214556d13dac301df764c814d7
--- /dev/null
+++ b/locale/extract_locale.py
@@ -0,0 +1,26 @@
+import os
+import json
+import re
+
+# Define regular expression patterns
+pattern = r'i18n\((\"{3}.*?\"{3}|\".*?\")\)'
+
+# Load the .py file
+with open('main.py', 'r', encoding='utf-8') as f:
+ contents = f.read()
+
+# Load the .py files in the modules folder
+for filename in os.listdir("src"):
+ if filename.endswith(".py"):
+ with open(os.path.join("src", filename), "r", encoding="utf-8") as f:
+ contents += f.read()
+
+# Matching with regular expressions
+matches = re.findall(pattern, contents, re.DOTALL)
+
+# Convert to key/value pairs
+data = {match.strip('()"'): '' for match in matches}
+
+# Save as a JSON file
+with open('labels.json', 'w', encoding='utf-8') as f:
+ json.dump(data, f, ensure_ascii=False, indent=4)
\ No newline at end of file
diff --git a/locale/ja_JP.json b/locale/ja_JP.json
new file mode 100644
index 0000000000000000000000000000000000000000..22996076ab0d81456615ab60636863e085c70660
--- /dev/null
+++ b/locale/ja_JP.json
@@ -0,0 +1,73 @@
+{
+ "未命名对话历史记录": "名無しの会話履歴",
+ "在这里输入": "ここに入力",
+ "🧹 新的对话": "🧹 新しい会話",
+ "🔄 重新生成": "🔄 再生成",
+ "🗑️ 删除最旧对话": "🗑️ 最古の会話削除",
+ "🗑️ 删除最新对话": "🗑️ 最新の会話削除",
+ "模型": "LLMモデル",
+ "多账号模式已开启,无需输入key,可直接开始对话": "複数アカウントモードがオンになっています。キーを入力する必要はありません。会話を開始できます",
+ "**发送消息** 或 **提交key** 以显示额度": "**メッセージを送信** または **キーを送信** して、クレジットを表示します",
+ "选择模型": "LLMモデルを選択",
+ "选择LoRA模型": "LoRAモデルを選択",
+ "实时传输回答": "ストリーム出力",
+ "单轮对话": "単発会話",
+ "使用在线搜索": "オンライン検索を使用",
+ "选择回复语言(针对搜索&索引功能)": "回答言語を選択(検索とインデックス機能に対して)",
+ "上传索引文件": "アップロード",
+ "双栏pdf": "2カラムpdf",
+ "识别公式": "formula OCR",
+ "在这里输入System Prompt...": "System Promptを入力してください...",
+ "加载Prompt模板": "Promptテンプレートを読込",
+ "选择Prompt模板集合文件": "Promptテンプレートコレクションを選択",
+ "🔄 刷新": "🔄 更新",
+ "从Prompt模板中加载": "Promptテンプレートから読込",
+ "保存/加载": "保存/読込",
+ "保存/加载对话历史记录": "会話履歴を保存/読込",
+ "从列表中加载对话": "リストから会話を読込",
+ "设置文件名: 默认为.json,可选为.md": "ファイル名を設定: デフォルトは.json、.mdを選択できます",
+ "设置保存文件名": "保存ファイル名を設定",
+ "对话历史记录": "会話履歴",
+ "💾 保存对话": "💾 会話を保存",
+ "📝 导出为Markdown": "📝 Markdownでエクスポート",
+ "默认保存于history文件夹": "デフォルトでhistoryフォルダに保存されます",
+ "高级": "Advanced",
+ "# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置": "# ⚠️ 変更には慎重に ⚠️\n\nもし動作しない場合は、デフォルト設定に戻してください。",
+ "参数": "パラメータ",
+ "在这里输入停止符,用英文逗号隔开...": "ここにストップ文字を英語のカンマで区切って入力してください...",
+ "用于定位滥用行为": "不正行為を特定するために使用されます",
+ "用户名": "ユーザー名",
+ "网络设置": "ネットワーク設定",
+ "在这里输入API-Host...": "API-Hostを入力してください...",
+ "🔄 切换API地址": "🔄 APIアドレスを切り替え",
+ "在这里输入代理地址...": "プロキシアドレスを入力してください...",
+ "代理地址(示例:http://127.0.0.1:10809)": "プロキシアドレス(例:http://127.0.0.1:10809)",
+ "🔄 设置代理地址": "🔄 プロキシアドレスを設定",
+ "🔙 恢复默认设置": "🔙 デフォルト設定に戻す",
+ "川虎Chat 🚀": "川虎Chat 🚀",
+ "开始实时传输回答……": "ストリーム出力開始……",
+ "Token 计数: ": "Token数: ",
+ ",本次对话累计消耗了 ": ", 今の会話で消費合計 ",
+ "**获取API使用情况失败**": "**API使用状況の取得に失敗しました**",
+ "**本月使用金额** ": "**今月の使用料金** ",
+ "获取API使用情况失败:": "API使用状況の取得に失敗しました:",
+ "API密钥更改为了": "APIキーが変更されました",
+ "JSON解析错误,收到的内容: ": "JSON解析エラー、受信内容: ",
+ "模型设置为了:": "LLMモデルを設定しました: ",
+ "☹️发生了错误:": "エラーが発生しました: ",
+ "获取对话时发生错误,请重试": "会話取得時にエラー発生、あとのログを確認してください",
+ "请检查网络连接,或者API-Key是否有效。": "ネットワーク接続を確認するか、APIキーが有効かどうかを確認してください。",
+ "连接超时,无法获取对话。": "接続タイムアウト、会話を取得できません。",
+ "读取超时,无法获取对话。": "読み込みタイムアウト、会話を取得できません。",
+ "代理错误,无法获取对话。": "プロキシエラー、会話を取得できません。",
+ "SSL错误,无法获取对话。": "SSLエラー、会話を取得できません。",
+ "API key为空,请检查是否输入正确。": "APIキーが入力されていません。正しく入力されているか確認してください。",
+ "请输入对话内容。": "会話内容を入力してください。",
+ "账单信息不适用": "課金情報は対象外です",
+ "由Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) 和 [明昭MZhao](https://space.bilibili.com/24807452)开发
访问川虎Chat的 [GitHub项目](https://github.com/GaiZhenbiao/ChuanhuChatGPT) 下载最新版脚本": "開発:Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) と [明昭MZhao](https://space.bilibili.com/24807452)\n\n最新コードは川虎Chatのサイトへ [GitHubプロジェクト](https://github.com/GaiZhenbiao/ChuanhuChatGPT)",
+ "切换亮暗色主题": "テーマの明暗切替",
+ "您的IP区域:未知。": "あなたのIPアドレス地域:不明",
+ "获取IP地理位置失败。原因:": "IPアドレス地域の取得に失敗しました。理由:",
+ "。你仍然可以使用聊天功能。": "。あなたはまだチャット機能を使用できます。",
+ "您的IP区域:": "あなたのIPアドレス地域:"
+}
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000000000000000000000000000000000000..6942f668cd9f6a2758c59346a221e572d42ef8b3
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,28 @@
+gradio==3.43.2
+gradio_client==0.5.0
+gradio[oauth]
+pypinyin
+tiktoken
+socksio
+tqdm
+colorama
+Pygments
+markdown
+pandas
+commentjson
+jinja2
+langchain==0.1.10
+langchain-community==0.0.25
+markdown
+PyPDF2
+pdfplumber
+faiss-cpu>=1.7.4
+pydantic-core==2.14.6
+pydantic==1.10.13
+rank-bm25
+jieba
+loguru
+openai
+langchain-openai
+SQLAlchemy==2.0.30
+# appbuilder-sdk==0.4.0
\ No newline at end of file
diff --git a/src/__init__.py b/src/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/src/base_model.py b/src/base_model.py
new file mode 100644
index 0000000000000000000000000000000000000000..6dd2594d44d556085ba8e210011a82489c34c4bd
--- /dev/null
+++ b/src/base_model.py
@@ -0,0 +1,856 @@
+import os
+import shutil
+import traceback
+from enum import Enum
+
+import commentjson as json
+import gradio as gr
+import tiktoken
+from loguru import logger
+
+from src import shared
+from src.config import (
+ retrieve_proxy,
+ local_embedding,
+ websearch_engine,
+ bing_search_api_key,
+ google_search_api_key,
+ serper_search_api_key,
+ searchapi_api_key,
+ google_search_cx,
+)
+from src.index_func import construct_index
+from src.presets import (
+ MODEL_TOKEN_LIMIT,
+ DEFAULT_TOKEN_LIMIT,
+ TOKEN_OFFSET,
+ REDUCE_TOKEN_FACTOR,
+ STANDARD_ERROR_MSG,
+ NO_APIKEY_MSG,
+ BILLING_NOT_APPLICABLE_MSG,
+ NO_INPUT_MSG,
+ HISTORY_DIR,
+ INITIAL_SYSTEM_PROMPT,
+ PROMPT_TEMPLATE,
+ WEBSEARCH_PTOMPT_TEMPLATE,
+)
+from src.search_engine import (
+ search_with_google,
+ search_with_duckduckgo,
+ search_with_bing,
+ search_with_searchapi,
+ search_with_serper,
+)
+from src.utils import (
+ i18n,
+ construct_assistant,
+ construct_user,
+ save_file,
+ hide_middle_chars,
+ count_token,
+ new_auto_history_filename,
+ get_history_names,
+ init_history_list,
+ get_history_list,
+ replace_special_symbols,
+ get_first_history_name,
+ add_source_numbers,
+ add_details,
+ replace_today,
+ chinese_preprocessing_func,
+)
+
+
+class ModelType(Enum):
+ Unknown = -1
+ OpenAI = 0
+ ChatGLM = 1
+ OpenAIInstruct = 2
+ OpenAIVision = 3
+ Claude = 4
+ Qwen = 5
+ LLaMA = 6
+ ZhipuAI = 7
+
+ @classmethod
+ def get_type(cls, model_name: str):
+ model_name_lower = model_name.lower()
+ if "gpt" in model_name_lower:
+ if "instruct" in model_name_lower:
+ model_type = ModelType.OpenAIInstruct
+ elif "vision" in model_name_lower:
+ model_type = ModelType.OpenAIVision
+ else:
+ model_type = ModelType.OpenAI
+ elif "chatglm" in model_name_lower:
+ model_type = ModelType.ChatGLM
+ elif "llama" in model_name_lower or "alpaca" in model_name_lower or "yi" in model_name_lower:
+ model_type = ModelType.LLaMA
+ elif model_name_lower in ["glm-3-turbo","glm4"]: # todo: more check
+ model_type = ModelType.ZhipuAI
+ else:
+ model_type = ModelType.Unknown
+ return model_type
+
+
+class BaseLLMModel:
+ def __init__(
+ self,
+ model_name,
+ system_prompt=INITIAL_SYSTEM_PROMPT,
+ temperature=1.0,
+ top_p=1.0,
+ n_choices=1,
+ stop="",
+ max_generation_token=None,
+ presence_penalty=0,
+ frequency_penalty=0,
+ logit_bias=None,
+ user="",
+ single_turn=False,
+ ) -> None:
+ self.history = []
+ self.all_token_counts = []
+ self.model_name = model_name
+ self.model_type = ModelType.get_type(model_name)
+ self.token_upper_limit = MODEL_TOKEN_LIMIT.get(model_name, DEFAULT_TOKEN_LIMIT)
+ self.interrupted = False
+ self.system_prompt = system_prompt
+ self.api_key = None
+ self.need_api_key = False
+ self.history_file_path = get_first_history_name(user)
+ self.user_name = user
+ self.chatbot = []
+
+ self.default_single_turn = single_turn
+ self.default_temperature = temperature
+ self.default_top_p = top_p
+ self.default_n_choices = n_choices
+ self.default_stop_sequence = stop
+ self.default_max_generation_token = max_generation_token
+ self.default_presence_penalty = presence_penalty
+ self.default_frequency_penalty = frequency_penalty
+ self.default_logit_bias = logit_bias
+ self.default_user_identifier = user
+
+ self.single_turn = single_turn
+ self.temperature = temperature
+ self.top_p = top_p
+ self.n_choices = n_choices
+ self.stop_sequence = stop
+ self.max_generation_token = max_generation_token
+ self.presence_penalty = presence_penalty
+ self.frequency_penalty = frequency_penalty
+ self.logit_bias = logit_bias
+ self.user_identifier = user
+
+ self.metadata = {}
+
+ def get_answer_stream_iter(self):
+ """stream predict, need to be implemented
+ conversations are stored in self.history, with the most recent question, in OpenAI format
+ should return a generator, each time give the next word (str) in the answer
+ """
+ logger.warning("stream predict not implemented, using at once predict instead")
+ response, _ = self.get_answer_at_once()
+ yield response
+
+ def get_answer_at_once(self):
+ """predict at once, need to be implemented
+ conversations are stored in history, with the most recent question, in OpenAI format
+ Should return:
+ the answer (str)
+ total token count (int)
+ """
+ logger.warning("at once predict not implemented, using stream predict instead")
+ response_iter = self.get_answer_stream_iter()
+ count = 0
+ response = ''
+ for response in response_iter:
+ count += 1
+ return response, sum(self.all_token_counts) + count
+
+ def billing_info(self):
+ """get billing infomation, inplement if needed"""
+ return BILLING_NOT_APPLICABLE_MSG
+
+ def count_token(self, user_input):
+ """get token count from input, implement if needed"""
+ return len(user_input)
+
+ def stream_next_chatbot(self, inputs, chatbot, fake_input=None, display_append=""):
+ def get_return_value():
+ return chatbot, status_text
+
+ status_text = i18n("开始实时传输回答……")
+ if fake_input:
+ chatbot.append((fake_input, ""))
+ else:
+ chatbot.append((inputs, ""))
+
+ user_token_count = self.count_token(inputs)
+ self.all_token_counts.append(user_token_count)
+ logger.debug(f"输入token计数: {user_token_count}")
+ if display_append:
+ display_append = (
+ '\n\n
' + display_append
+ )
+
+ partial_text = ""
+ token_increment = 1
+ for partial_text in self.get_answer_stream_iter():
+ if type(partial_text) == tuple:
+ partial_text, token_increment = partial_text
+ chatbot[-1] = (chatbot[-1][0], partial_text + display_append)
+ self.all_token_counts[-1] += token_increment
+ status_text = self.token_message()
+ yield get_return_value()
+ if self.interrupted:
+ self.recover()
+ break
+ self.history.append(construct_assistant(partial_text))
+
+ def next_chatbot_at_once(self, inputs, chatbot, fake_input=None, display_append=""):
+ if fake_input:
+ chatbot.append((fake_input, ""))
+ else:
+ chatbot.append((inputs, ""))
+ if fake_input is not None:
+ user_token_count = self.count_token(fake_input)
+ else:
+ user_token_count = self.count_token(inputs)
+ self.all_token_counts.append(user_token_count)
+ ai_reply, total_token_count = self.get_answer_at_once()
+ self.history.append(construct_assistant(ai_reply))
+ if fake_input is not None:
+ self.history[-2] = construct_user(fake_input)
+ chatbot[-1] = (chatbot[-1][0], ai_reply + display_append)
+ self.all_token_counts[-1] += count_token(construct_assistant(ai_reply))
+ status_text = self.token_message()
+ return chatbot, status_text
+
+ def handle_file_upload(self, files, chatbot, language):
+ """if the model accepts modal input, implement this function"""
+ status = gr.Markdown.update()
+ if files:
+ construct_index(self.api_key, files=files)
+ status = i18n("索引构建完成")
+ return gr.Files.update(), chatbot, status
+
+ def prepare_inputs(
+ self, real_inputs, use_websearch,
+ files, reply_language, chatbot,
+ load_from_cache_if_possible=True,
+ ):
+ display_append = []
+ limited_context = False
+ if type(real_inputs) == list:
+ fake_inputs = real_inputs[0]["text"]
+ else:
+ fake_inputs = real_inputs
+ if files:
+ from langchain.vectorstores.base import VectorStoreRetriever
+ from langchain.retrievers import BM25Retriever, EnsembleRetriever
+ limited_context = True
+ msg = "加载索引中……"
+ logger.info(msg)
+ index, documents = construct_index(
+ self.api_key,
+ files=files,
+ load_from_cache_if_possible=load_from_cache_if_possible,
+ )
+ assert index is not None, "获取索引失败"
+ msg = "索引获取成功,生成回答中……"
+ logger.info(msg)
+ file_text = " ".join([d.page_content for d in documents])
+ file_text_token_limit = self.token_upper_limit / 2 # 文档的token上限为模型token上限的一半
+ if self.count_token(file_text) > file_text_token_limit:
+ # 文档token数超限使用检索匹配,否则用知识库文件的全部数据做rag
+ with retrieve_proxy():
+ if local_embedding:
+ k = 3
+ score_threshold = 0.4
+ vec_retriever = VectorStoreRetriever(
+ vectorstore=index,
+ search_type="similarity_score_threshold",
+ search_kwargs={"k": k, "score_threshold": score_threshold}
+ )
+ bm25_retriever = BM25Retriever.from_documents(
+ documents,
+ preprocess_func=chinese_preprocessing_func
+ )
+ bm25_retriever.k = k
+ retriever = EnsembleRetriever(
+ retrievers=[bm25_retriever, vec_retriever],
+ weights=[0.5, 0.5],
+ )
+ else:
+ k = 5
+ retriever = VectorStoreRetriever(
+ vectorstore=index,
+ search_type="similarity",
+ search_kwargs={"k": k}
+ )
+ try:
+ relevant_documents = retriever.get_relevant_documents(fake_inputs)
+ except:
+ return self.prepare_inputs(
+ fake_inputs,
+ use_websearch,
+ files,
+ reply_language,
+ chatbot,
+ load_from_cache_if_possible=False,
+ )
+ else:
+ relevant_documents = documents
+ reference_results = [
+ [d.page_content.strip("�"), os.path.basename(d.metadata["source"])]
+ for d in relevant_documents
+ ]
+ reference_results = add_source_numbers(reference_results)
+ display_append = add_details(reference_results)
+ display_append = "\n\n" + "".join(display_append)
+ if type(real_inputs) == list:
+ real_inputs[0]["text"] = (
+ replace_today(PROMPT_TEMPLATE)
+ .replace("{query_str}", fake_inputs)
+ .replace("{context_str}", "\n\n".join(reference_results))
+ .replace("{reply_language}", reply_language)
+ )
+ else:
+ real_inputs = (
+ replace_today(PROMPT_TEMPLATE)
+ .replace("{query_str}", real_inputs)
+ .replace("{context_str}", "\n\n".join(reference_results))
+ .replace("{reply_language}", reply_language)
+ )
+ elif use_websearch:
+ if websearch_engine == "google":
+ search_results = search_with_google(fake_inputs, google_search_api_key, google_search_cx)
+ elif websearch_engine == "bing":
+ search_results = search_with_bing(fake_inputs, bing_search_api_key)
+ elif websearch_engine == "searchapi":
+ search_results = search_with_searchapi(fake_inputs, searchapi_api_key)
+ elif websearch_engine == "serper":
+ search_results = search_with_serper(fake_inputs, serper_search_api_key)
+ else:
+ search_results = search_with_duckduckgo(fake_inputs)
+ reference_results = []
+ for idx, result in enumerate(search_results):
+ logger.debug(f"搜索结果{idx + 1}:{result}")
+ reference_results.append([result["snippet"], result["url"]])
+ display_append.append(
+ f"{idx + 1}. {result['name']}"
+ )
+ reference_results = add_source_numbers(reference_results)
+ display_append = (
+ '' + "".join(display_append) + ""
+ )
+ if type(real_inputs) == list:
+ real_inputs[0]["text"] = (
+ replace_today(WEBSEARCH_PTOMPT_TEMPLATE)
+ .replace("{query}", fake_inputs)
+ .replace("{web_results}", "\n\n".join(reference_results))
+ .replace("{reply_language}", reply_language)
+ )
+ else:
+ real_inputs = (
+ replace_today(WEBSEARCH_PTOMPT_TEMPLATE)
+ .replace("{query}", fake_inputs)
+ .replace("{web_results}", "\n\n".join(reference_results))
+ .replace("{reply_language}", reply_language)
+ )
+ else:
+ display_append = ""
+ return limited_context, fake_inputs, display_append, real_inputs, chatbot
+
+ def predict(
+ self,
+ inputs,
+ chatbot,
+ stream=False,
+ use_websearch=False,
+ files=None,
+ reply_language="中文",
+ should_check_token_count=True,
+ ):
+ status_text = "开始生成回答……"
+ if type(inputs) == list:
+ logger.info(f"用户{self.user_name}的输入为:{inputs[0]['text']}")
+ else:
+ logger.info(f"用户{self.user_name}的输入为:{inputs}")
+ if should_check_token_count:
+ if type(inputs) == list:
+ yield chatbot + [(inputs[0]["text"], "")], status_text
+ else:
+ yield chatbot + [(inputs, "")], status_text
+ if reply_language == "跟随问题语言(不稳定)":
+ reply_language = "the same language as the question, such as English, 中文, 日本語, Español, Français, or Deutsch."
+
+ limited_context, fake_inputs, display_append, inputs, chatbot = self.prepare_inputs(
+ real_inputs=inputs,
+ use_websearch=use_websearch,
+ files=files,
+ reply_language=reply_language,
+ chatbot=chatbot
+ )
+ yield chatbot + [(fake_inputs, "")], status_text
+
+ if (
+ self.need_api_key and
+ self.api_key is None
+ and not shared.state.multi_api_key
+ ):
+ status_text = STANDARD_ERROR_MSG + NO_APIKEY_MSG
+ logger.info(status_text)
+ chatbot.append((inputs, ""))
+ if len(self.history) == 0:
+ self.history.append(construct_user(inputs))
+ self.history.append("")
+ self.all_token_counts.append(0)
+ else:
+ self.history[-2] = construct_user(inputs)
+ yield chatbot + [(inputs, "")], status_text
+ return
+ elif len(inputs.strip()) == 0:
+ status_text = STANDARD_ERROR_MSG + NO_INPUT_MSG
+ logger.info(status_text)
+ yield chatbot + [(inputs, "")], status_text
+ return
+
+ if self.single_turn:
+ self.history = []
+ self.all_token_counts = []
+ if type(inputs) == list:
+ self.history.append(inputs)
+ else:
+ self.history.append(construct_user(inputs))
+
+ try:
+ if stream:
+ logger.debug("使用流式传输")
+ iter = self.stream_next_chatbot(
+ inputs,
+ chatbot,
+ fake_input=fake_inputs,
+ display_append=display_append,
+ )
+ for chatbot, status_text in iter:
+ yield chatbot, status_text
+ else:
+ logger.debug("不使用流式传输")
+ chatbot, status_text = self.next_chatbot_at_once(
+ inputs,
+ chatbot,
+ fake_input=fake_inputs,
+ display_append=display_append,
+ )
+ yield chatbot, status_text
+ except Exception as e:
+ traceback.print_exc()
+ status_text = STANDARD_ERROR_MSG + str(e)
+ yield chatbot, status_text
+
+ if len(self.history) > 1 and self.history[-1]["content"] != inputs:
+ logger.info(f"回答为:{self.history[-1]['content']}")
+
+ if limited_context:
+ self.history = []
+ self.all_token_counts = []
+
+ max_token = self.token_upper_limit - TOKEN_OFFSET
+
+ if sum(self.all_token_counts) > max_token and len(self.history) > 2 and should_check_token_count:
+ count = 0
+ while (
+ sum(self.all_token_counts)
+ > self.token_upper_limit * REDUCE_TOKEN_FACTOR
+ and sum(self.all_token_counts) > 0 and len(self.history) > 0
+ ):
+ count += 1
+ del self.all_token_counts[:1]
+ del self.history[:2]
+ status_text = f"为了防止token超限,模型忘记了早期的 {count} 轮对话"
+ logger.info(status_text)
+ yield chatbot, status_text
+
+ def retry(
+ self,
+ chatbot,
+ stream=False,
+ use_websearch=False,
+ files=None,
+ reply_language="中文",
+ ):
+ logger.debug("重试中……")
+ if len(self.history) > 1:
+ inputs = self.history[-2]["content"]
+ del self.history[-2:]
+ if len(self.all_token_counts) > 0:
+ self.all_token_counts.pop()
+ elif len(chatbot) > 0:
+ inputs = chatbot[-1][0]
+ if '