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 + + +

PoetryChat🖼️

+ + + +[![License Apache 2.0](https://img.shields.io/badge/license-mit-blue.svg)](LICENSE) +[![python_version](https://img.shields.io/badge/Python-3.10%2B-green.svg)](requirements.txt) + +

Description

+ +  Powered by Large Language Models, PoetryChat ... + + + + + +

Demonstration

+ +![诗趣伴行](./docs/诗趣伴行.png) + +  You can easily and directly experience the our demo online on `HuggingFace` now. Click here for Online Experience 👉 [PoetryChat - a Hugging Face Space by Tsumugii](https://huggingface.co/spaces/Tsumugii/PoetryChat) + + + +

Todo

+ +- [ ] Complete the Gradio Interface and UI design +- [ ] Add team members brief introduction +- [ ] Add a gif demonstration +- [ ] Deploy the demo on HuggingFace +- [ ] RAG layer +- [ ] LLM Agent layer +- [ ] Application layer + + + + + + + +

Quick Start

+ +
+

Installation

+ + +  First of all, please make sure that you have already installed `conda` as Python runtime environment. And `miniconda` is strongly recommended. + +  1. create a virtual `conda` environment for the demo 😆 + +```bash +$ conda create -n poetrychat python==3.10 # poetrychat is the name of your environment +$ conda activate poetrychat +``` + +  2. Install essential `requirements` by run the following command in the `CLI` 😊 + +```bash +$ git clone https://github.com/Antony-Zhang/PoetryChat && cd PoetryChat && git checkout poetryChat2.0 +$ pip install -r requirements.txt +``` + +
+

Preparation

+ + +  1. open `.env.example` and fill your own `API Keys` in the **corresponding place** if you want to use certain LLM, then **rename** the file into `.env` + +``` +OPENAI_API_KEY = "" +OPENAI_URL_BASE = "" +``` + +  2. xxx + + + + + + + + + + + +

References

+ +1. [Gradio Official Documents](https://www.gradio.app/) +2. [LIC·2024 语言与智能技术竞赛_飞桨大赛-飞桨AI Studio星河社区](https://aistudio.baidu.com/competition/detail/1171/0/introduction) +3. [PoetryChat: 一个面向不同年龄段的交互式LLM古诗学习助手](https://github.com/Antony-Zhang/PoetryChat) +4. [Tsumugii24/WonderWhy (github.com)](https://github.com/Tsumugii24/WonderWhy) + + + + + + + +

Acknowledgements

+ +  ***I would like to express my sincere gratitude to my teammates for their efforts and supports throughout the development of this project. Their expertise and insightful feedback played a crucial role in shaping the direction of the project.*** + +- 姜舒凡 [@Tsumugii24](https://github.com/Tsumugii24) + +- 朱嘉辉 [@jiaohui](https://github.com/jiaohuix) + +- 陈思州 [@jjyaoao](https://github.com/jjyaoao) + + + + + +

Contact

+ +Feel free to open GitHub issues or directly send me a mail if you have any questions about this project. + diff --git a/app.py b/app.py new file mode 100644 index 0000000000000000000000000000000000000000..909af53630ce68818d9272b1b407049ee6007821 --- /dev/null +++ b/app.py @@ -0,0 +1,935 @@ +# -*- coding: utf-8 -*- +""" +@author:XuMing(xuming624@qq.com) +@description: +""" +import gradio as gr +from loguru import logger +# import appbuilder +import time +import os + +from src.config import ( + http_proxy, + hide_history_when_not_logged_in, + chat_name_method_index, + my_api_key, + multi_api_key, + server_name, + server_port, + share, + config_file, + api_host, + authflag, + dockerflag, + show_api_billing, + latex_delimiters_set, + user_avatar, + bot_avatar, + autobrowser, + update_doc_config, +) +from src.gradio_patch import reg_patch +from src.models import get_model +from src.overwrites import ( + postprocess, + postprocess_chat_messages, + reload_javascript, + get_html, +) +from src.presets import ( + MODEL_ALIASES, + MODELS, + HISTORY_NAME_METHODS, + small_and_beautiful_theme, + CONCURRENT_COUNT, + TITLE, + HIDE_MY_KEY, + DEFAULT_MODEL, + REPLY_LANGUAGES, + INITIAL_SYSTEM_PROMPT, + ENABLE_STREAMING_OPTION, + DESCRIPTION, + favicon_path, + API_HOST, + HISTORY_DIR, + assets_path, +) +from src.utils import ( + delete_chat_history, + filter_history, + get_history_list, + auto_name_chat_history, + get_template_dropdown, + rename_chat_history, + init_history_list, + get_first_history_name, + setup_wizard, + auth_from_conf, + get_geoip, + get_template_names, + load_template, + get_history_names, + reset, + predict, + interrupt, + retry, + i18n, + dislike, + toggle_like_btn_visibility, + set_key, + set_single_turn, + hide_middle_chars, + set_system_prompt, + start_outputing, + set_token_upper_limit, + set_temperature, + set_user_identifier, + set_top_p, + delete_first_conversation, + delete_last_conversation, + set_n_choices, + set_logit_bias, + load_chat_history, + end_outputing, + set_max_tokens, + reset_default, + reset_textbox, + set_stop_sequence, + set_presence_penalty, + set_frequency_penalty, + upload_chat_history, + export_markdown, + billing_info, + get_template_content, + like, + transfer_input, + handle_file_upload, + handle_summarize_index, +) + +reg_patch() + +gr.Chatbot._postprocess_chat_messages = postprocess_chat_messages +gr.Chatbot.postprocess = postprocess + +# 设置环境变量和默认应用ID +os.environ["APPBUILDER_TOKEN"] = "bce-v3/ALTAK-QwuihwYsMjA5jBiIBVfJP/51f962e086efb6f3a2332414360552bae5f3958d" +APPBUILDER_APPID_DEFAULT = "2ef66c07-b4ac-4fb7-adf9-a76b5c80b2b5" +APPBUILDER_APPID_CHILD = "c2de7a91-e17e-4d31-becf-b5d014156de7" +APPBUILDER_APPID_STUDENT = "93ea3085-0e79-40f0-8e3d-f47381af427a" + +def on_mode_change(mode): + return f"已选择模式:{mode}" + +# def generate_image_from_text(prompt, width=1024, height=1024, image_num=1): +# import appbuilder +# os.environ["APPBUILDER_TOKEN"] = "bce-v3/ALTAK-QwuihwYsMjA5jBiIBVfJP/51f962e086efb6f3a2332414360552bae5f3958d" +# text2Image = appbuilder.Text2Image() +# content_data = {"prompt": prompt, "width": width, "height": height, "image_num": image_num} +# msg = appbuilder.Message(content_data) +# out = text2Image.run(msg) +# return out.content['img_urls'] + +# def generate_image(prompt): +# img_urls = generate_image_from_text(prompt) +# return img_urls[0] # 假设只生成一张图片 + +def generate_local_image(prompt): + # 模拟生成图片并保存到本地路径 + # 在实际应用中,你需要调用你的图像生成函数并保存图像 + # 这里我们假设生成的图像保存在 `generated_image.png` + local_image_path = "assets/generate_image.png" + + # 模拟生成图片保存 + # 这里你可以替换为实际的图像生成逻辑 + # from PIL import Image, ImageDraw, ImageFont + # image = Image.new('RGB', (1024, 1024), color = (73, 109, 137)) + # d = ImageDraw.Draw(image) + # d.text((10,10), prompt, fill=(255,255,0)) + # image.save(local_image_path) + time.sleep(6) + return local_image_path + +def respond(query, app_selection, chat_history): + # 根据用户选择设置应用ID + if app_selection == "成人模式": + app_id = APPBUILDER_APPID_DEFAULT + elif app_selection == "儿童模式": + app_id = APPBUILDER_APPID_CHILD + elif app_selection == "学生模式": + app_id = APPBUILDER_APPID_STUDENT + + # 初始化应用 + # builder = appbuilder.AppBuilderClient(app_id) + + # 创建会话ID + # conversation_id = builder.create_conversation() + + # 执行对话 + # msg = builder.run(conversation_id, query) + + # chat_history.append((query, msg.content.answer)) + time.sleep(2) + return "", chat_history + +# # 创建独立的模式切换模块 +# def mode_switch_ui(): +# with gr.Tab(label=i18n("切换模式")): +# gr.Markdown(i18n("# 选择运行模式 ⚙️"), elem_id="mode-selection-info") +# with gr.Accordion(i18n("模式切换"), open=True): +# mode_selection = gr.Radio( +# choices=["默认模式", "成人模式", "儿童模式", "学生模式"], +# label=i18n("运行模式"), +# value="默认模式" +# ) +# return mode_selection + + + +with gr.Blocks(theme=small_and_beautiful_theme) as demo: + user_name = gr.Textbox("", visible=False) + # 激活/logout路由 + logout_hidden_btn = gr.LogoutButton(visible=False) + promptTemplates = gr.State(load_template(get_template_names()[0], mode=2)) + user_question = gr.State("") + assert type(my_api_key) == str + user_api_key = gr.State(my_api_key) + current_model = gr.State() + # mode_selection = mode_switch_ui() + + topic = gr.State(i18n("未命名对话历史记录")) + + with gr.Row(elem_id="chuanhu-header"): + gr.HTML(get_html("header_title.html").format( + app_title=TITLE), elem_id="app-title") + status_display = gr.Markdown(get_geoip, elem_id="status-display") + with gr.Row(elem_id="float-display"): + user_info = gr.Markdown( + value="getting user info...", elem_id="user-info") + + with gr.Row(equal_height=True, elem_id="chuanhu-body"): + + with gr.Column(elem_id="menu-area"): + with gr.Column(elem_id="chuanhu-history"): + with gr.Box(): + with gr.Row(elem_id="chuanhu-history-header"): + with gr.Row(elem_id="chuanhu-history-search-row"): + with gr.Column(min_width=150, scale=2): + historySearchTextbox = gr.Textbox(show_label=False, container=False, placeholder=i18n( + "搜索(支持正则)..."), lines=1, elem_id="history-search-tb") + with gr.Column(min_width=52, scale=1, elem_id="gr-history-header-btns"): + uploadFileBtn = gr.UploadButton( + interactive=True, label="", file_types=[".json"], elem_id="gr-history-upload-btn") + historyRefreshBtn = gr.Button("", elem_id="gr-history-refresh-btn") + + with gr.Row(elem_id="chuanhu-history-body"): + with gr.Column(scale=6, elem_id="history-select-wrap"): + historySelectList = gr.Radio( + label=i18n("从列表中加载对话"), + choices=get_history_names(), + value=get_first_history_name(), + # multiselect=False, + container=False, + elem_id="history-select-dropdown" + ) + with gr.Row(visible=False): + with gr.Column(min_width=42, scale=1): + historyDeleteBtn = gr.Button( + "🗑️", elem_id="gr-history-delete-btn") + with gr.Column(min_width=42, scale=1): + historyDownloadBtn = gr.Button( + "⏬", elem_id="gr-history-download-btn") + with gr.Column(min_width=42, scale=1): + historyMarkdownDownloadBtn = gr.Button( + "⤵️", elem_id="gr-history-mardown-download-btn") + with gr.Row(visible=False): + with gr.Column(scale=6): + saveFileName = gr.Textbox( + show_label=True, + placeholder=i18n("设置文件名: 默认为.json,可选为.md"), + label=i18n("设置保存文件名"), + value=i18n("对话历史记录"), + elem_classes="no-container" + # container=False, + ) + with gr.Column(scale=1): + renameHistoryBtn = gr.Button( + i18n("💾 保存对话"), elem_id="gr-history-save-btn") + exportMarkdownBtn = gr.Button( + i18n("📝 导出为 Markdown"), elem_id="gr-markdown-export-btn") + + with gr.Column(elem_id="chuanhu-menu-footer"): + with gr.Row(elem_id="chuanhu-func-nav"): + gr.HTML(get_html("func_nav.html")) + # gr.HTML(get_html("footer.html").format(versions=versions_html()), elem_id="footer") + # gr.Markdown(CHUANHU_DESCRIPTION, elem_id="chuanhu-author") + + with gr.Column(elem_id="chuanhu-area", scale=5): + with gr.Column(elem_id="chatbot-area"): + with gr.Row(elem_id="chatbot-header"): + # 获取模型的别名列表 + MODEL_ALIASES_LIST = list(MODEL_ALIASES.values()) + DEFAULT_MODEL_ALIAS = MODEL_ALIASES["gpt-3.5-turbo"] + + model_select_dropdown = gr.Dropdown( + label=i18n("选择模型"), choices=MODEL_ALIASES_LIST, multiselect=False, value=DEFAULT_MODEL_ALIAS, + interactive=True, + show_label=False, container=False, elem_id="model-select-dropdown" + ) + lora_select_dropdown = gr.Dropdown( + label=i18n("选择LoRA模型"), choices=[], multiselect=False, interactive=True, + container=False, visible=False, + ) + gr.HTML(get_html("chatbot_header_btn.html").format( + json_label=i18n("历史记录(JSON)"), + md_label=i18n("导出为 Markdown") + ), elem_id="chatbot-header-btn-bar") + with gr.Row(): + chatbot = gr.Chatbot( + label="ChatGPT", + elem_id="chuanhu-chatbot", + latex_delimiters=latex_delimiters_set, + sanitize_html=False, + # height=700, + show_label=False, + avatar_images=[user_avatar, bot_avatar], + show_share_button=False, + ) + with gr.Row(elem_id="chatbot-footer"): + with gr.Box(elem_id="chatbot-input-box"): + with gr.Row(elem_id="chatbot-input-row"): + gr.HTML(get_html("chatbot_more.html").format( + single_turn_label=i18n("单轮对话"), + websearch_label=i18n("在线搜索"), + upload_file_label=i18n("上传文件"), + uploaded_files_label=i18n("知识库文件"), + uploaded_files_tip=i18n("在工具箱中管理知识库文件") + )) + with gr.Row(elem_id="chatbot-input-tb-row"): + with gr.Column(min_width=225, scale=12): + user_input = gr.Textbox( + elem_id="user-input-tb", + show_label=False, + placeholder=i18n("在这里输入"), + elem_classes="no-container", + max_lines=5, + # container=False + ) + with gr.Column(min_width=42, scale=1, elem_id="chatbot-ctrl-btns"): + submitBtn = gr.Button( + value="", variant="primary", elem_id="submit-btn") + cancelBtn = gr.Button( + value="", variant="secondary", visible=False, elem_id="cancel-btn") + # Note: Buttons below are set invisible in UI. But they are used in JS. + with gr.Row(elem_id="chatbot-buttons", visible=False): + with gr.Column(min_width=120, scale=1): + emptyBtn = gr.Button( + i18n("🧹 新的对话"), elem_id="empty-btn" + ) + with gr.Column(min_width=120, scale=1): + retryBtn = gr.Button( + i18n("🔄 重新生成"), elem_id="gr-retry-btn") + with gr.Column(min_width=120, scale=1): + delFirstBtn = gr.Button(i18n("🗑️ 删除最旧对话")) + with gr.Column(min_width=120, scale=1): + delLastBtn = gr.Button( + i18n("🗑️ 删除最新对话"), elem_id="gr-dellast-btn") + with gr.Row(visible=False) as like_dislike_area: + with gr.Column(min_width=20, scale=1): + likeBtn = gr.Button( + "👍", elem_id="gr-like-btn") + with gr.Column(min_width=20, scale=1): + dislikeBtn = gr.Button( + "👎", elem_id="gr-dislike-btn") + + with gr.Column(elem_id="toolbox-area", scale=1): + # For CSS setting, there is an extra box. Don't remove it. + with gr.Box(elem_id="chuanhu-toolbox"): + with gr.Row(): + gr.Markdown("## " + i18n("工具箱")) + gr.HTML(get_html("close_btn.html").format( + obj="toolbox"), elem_classes="close-btn") + with gr.Tabs(elem_id="chuanhu-toolbox-tabs"): + with gr.Tab(label=i18n("主题")): + with gr.Accordion(label=i18n("模型"), open=not HIDE_MY_KEY, visible=not HIDE_MY_KEY): + keyTxt = gr.Textbox( + show_label=True, + placeholder=f"Your API-key...", + value=hide_middle_chars(user_api_key.value), + type="password", + visible=not HIDE_MY_KEY, + label="API-Key", + elem_id="api-key" + ) + if multi_api_key: + usageTxt = gr.Markdown(i18n( + "多账号模式已开启,无需输入key,可直接开始对话"), elem_id="usage-display", + elem_classes="insert-block", visible=show_api_billing) + else: + usageTxt = gr.Markdown(i18n( + "**发送消息** 或 **提交key** 以显示额度"), elem_id="usage-display", + elem_classes="insert-block", visible=show_api_billing) + gr.Markdown("---", elem_classes="hr-line", visible=not HIDE_MY_KEY) + with gr.Accordion(label="讨论主题展示", open=True): + systemPromptTxt = gr.Textbox( + show_label=True, + placeholder=i18n("在这里输入System Prompt..."), + label="古诗词", + value=INITIAL_SYSTEM_PROMPT, + lines=8 + ) + retain_system_prompt_checkbox = gr.Checkbox( + label=i18n("新建对话保留当前讨论主题"), value=False, visible=True, + elem_classes="switch-checkbox") + with gr.Accordion(label=i18n("加载自定义讨论主题"), open=False): + with gr.Column(): + with gr.Row(): + with gr.Column(scale=6): + templateFileSelectDropdown = gr.Dropdown( + label=i18n("选择Prompt模板集合文件"), + choices=get_template_names(), + multiselect=False, + value=get_template_names()[0], + container=False, + ) + with gr.Column(scale=1): + templateRefreshBtn = gr.Button( + i18n("🔄 刷新")) + with gr.Row(): + with gr.Column(): + templateSelectDropdown = gr.Dropdown( + label=i18n("从Prompt模板中加载"), + choices=load_template( + get_template_names()[ + 0], mode=1 + ), + multiselect=False, + container=False, + ) + gr.Markdown("---", elem_classes="hr-line") + with gr.Accordion(label=i18n("知识库"), open=True, elem_id="gr-kb-accordion", visible=True): + use_websearch_checkbox = gr.Checkbox(label=i18n( + "使用在线搜索"), value=False, elem_classes="switch-checkbox", elem_id="gr-websearch-cb", + visible=False) + index_files = gr.Files(label=i18n( + "上传"), type="file", + file_types=[".pdf", ".docx", ".pptx", ".epub", ".xlsx", ".txt", "text", "image"], + elem_id="upload-index-file") + two_column = gr.Checkbox(label=i18n( + "双栏pdf"), value=False) + summarize_btn = gr.Button(i18n("总结"), visible=False) + + with gr.Tab(label=i18n("模式")): # 将标题修改为“模式” + gr.Markdown(i18n("# 选择运行模式 ⚙️"), + elem_id="mode-selection-info") + with gr.Accordion(i18n("模式切换"), open=True): + mode_selection = gr.Radio(choices=["默认模式", "成人模式", "儿童模式", "学生模式"], label=i18n("运行模式"), value="默认模式") + submit_button = gr.Button(i18n("确认选择")) + result = gr.Textbox(label="选择结果") + submit_button.click(on_mode_change, inputs=mode_selection, outputs=result) + gr.Markdown("### 智能图片生成") + # 添加文本输入框用于输入生成图片的文本 + image_output = gr.Image(label="古诗文意象图") + text_input = gr.Textbox(label="你的描述") + generate_button = gr.Button("生成图片") + + # 绑定按钮点击事件 + # generate_button.click(generate_image, inputs=text_input, outputs=image_output) + # 做一个假本地返回效果 + generate_button.click(generate_local_image, inputs=text_input, outputs=image_output) + + + # with gr.Tab(label=i18n("参数")): + # gr.Markdown(i18n("# ⚠️ 务必谨慎更改 ⚠️"), + # elem_id="advanced-warning") + # with gr.Accordion(i18n("参数"), open=True): + temperature_slider = gr.Slider( + minimum=-0, + maximum=2.0, + value=1.0, + step=0.1, + interactive=True, + label="temperature", + visible=False + ) + top_p_slider = gr.Slider( + minimum=-0, + maximum=1.0, + value=1.0, + step=0.05, + interactive=True, + label="top-p", + visible=False + ) + n_choices_slider = gr.Slider( + minimum=1, + maximum=10, + value=1, + step=1, + interactive=True, + label="n choices", + visible=False + ) + stop_sequence_txt = gr.Textbox( + show_label=True, + placeholder=i18n("停止符,用英文逗号隔开..."), + label="stop", + value="", + lines=1, + visible=False + ) + max_context_length_slider = gr.Slider( + minimum=1, + maximum=32768, + value=2000, + step=1, + interactive=True, + label="max context", + visible=False + ) + max_generation_slider = gr.Slider( + minimum=1, + maximum=32768, + value=1000, + step=1, + interactive=True, + label="max generations", + visible=False + ) + presence_penalty_slider = gr.Slider( + minimum=-2.0, + maximum=2.0, + value=0.0, + step=0.01, + interactive=True, + label="presence penalty", + visible=False + ) + frequency_penalty_slider = gr.Slider( + minimum=-2.0, + maximum=2.0, + value=0.0, + step=0.01, + interactive=True, + label="frequency penalty", + visible=False + ) + logit_bias_txt = gr.Textbox( + show_label=True, + placeholder=f"word:likelihood", + label="logit bias", + value="", + lines=1, + visible=False + ) + user_identifier_txt = gr.Textbox( + show_label=True, + placeholder=i18n("用于定位滥用行为"), + label=i18n("用户标识符"), + value=user_name.value, + lines=1, + visible=False + ) + with gr.Tab(label=i18n("关于")): + gr.Markdown("#### " + i18n("PoetryChat Github地址")) + gr.Markdown(DESCRIPTION) + + with gr.Row(elem_id="popup-wrapper"): + with gr.Box(elem_id="chuanhu-popup"): + with gr.Box(elem_id="chuanhu-setting"): + with gr.Row(): + gr.Markdown("## " + i18n("设置")) + gr.HTML(get_html("close_btn.html").format( + obj="box"), elem_classes="close-btn") + with gr.Tabs(elem_id="chuanhu-setting-tabs"): + with gr.Tab(label=i18n("高级")): + gr.HTML(get_html("appearance_switcher.html").format( + label=i18n("切换亮暗色主题")), elem_classes="insert-block", visible=False) + use_streaming_checkbox = gr.Checkbox( + label=i18n("实时传输回答"), value=True, visible=ENABLE_STREAMING_OPTION, + elem_classes="switch-checkbox" + ) + language_select_dropdown = gr.Dropdown( + label=i18n("选择回复语言(针对搜索&索引功能)"), + choices=REPLY_LANGUAGES, + multiselect=False, + value=REPLY_LANGUAGES[0], + visible=False, + ) + name_chat_method = gr.Dropdown( + label=i18n("对话命名方式"), + choices=HISTORY_NAME_METHODS, + multiselect=False, + interactive=True, + value=HISTORY_NAME_METHODS[chat_name_method_index], + ) + single_turn_checkbox = gr.Checkbox(label=i18n( + "单轮对话"), value=False, elem_classes="switch-checkbox", elem_id="gr-single-session-cb", + visible=False) + # checkUpdateBtn = gr.Button(i18n("🔄 检查更新..."), visible=check_update) + logout_btn = gr.Button(i18n("退出用户"), variant="primary", visible=authflag) + + with gr.Tab(i18n("网络")): + gr.Markdown( + i18n("⚠️ 为保证API-Key安全,请在配置文件`config.json`中修改网络设置"), + elem_id="netsetting-warning") + default_btn = gr.Button(i18n("🔙 恢复默认网络设置")) + # 网络代理 + proxyTxt = gr.Textbox( + show_label=True, + placeholder=i18n("未设置代理..."), + label=i18n("代理地址"), + value=http_proxy, + lines=1, + interactive=False, + # container=False, + elem_classes="view-only-textbox no-container", + ) + # changeProxyBtn = gr.Button(i18n("🔄 设置代理地址")) + + # 优先展示自定义的api_host + apihostTxt = gr.Textbox( + show_label=True, + placeholder="api.openai.com", + label="OpenAI API-Host", + value=api_host or API_HOST, + lines=1, + interactive=False, + # container=False, + elem_classes="view-only-textbox no-container", + ) + + with gr.Tab(label=i18n("关于"), elem_id="about-tab"): + gr.Markdown("# " + i18n("PoetryChat")) + gr.Markdown(DESCRIPTION, elem_id="description") + + with gr.Box(elem_id="web-config", visible=False): + gr.HTML(get_html('web_config.html').format( + enableCheckUpdate_config=False, + hideHistoryWhenNotLoggedIn_config=hide_history_when_not_logged_in, + forView_i18n=i18n("仅供查看"), + deleteConfirm_i18n_pref=i18n("你真的要删除 "), + deleteConfirm_i18n_suff=i18n(" 吗?"), + usingLatest_i18n=i18n("您使用的就是最新版!"), + updatingMsg_i18n=i18n("正在尝试更新..."), + updateSuccess_i18n=i18n("更新成功,请重启本程序"), + updateFailure_i18n=i18n( + "更新失败,请尝试[手动更新](https://github.com/shibing624/chatgpt-webui/"), + regenerate_i18n=i18n("重新生成"), + deleteRound_i18n=i18n("删除这轮问答"), + renameChat_i18n=i18n("重命名该对话"), + validFileName_i18n=i18n("请输入有效的文件名,不要包含以下特殊字符:"), + clearFileHistoryMsg_i18n=i18n("⚠️请先删除知识库中的历史文件,再尝试上传!"), + dropUploadMsg_i18n=i18n("释放文件以上传"), + )) + with gr.Box(elem_id="fake-gradio-components", visible=False): + changeSingleSessionBtn = gr.Button( + visible=False, elem_classes="invisible-btn", elem_id="change-single-session-btn") + changeOnlineSearchBtn = gr.Button( + visible=False, elem_classes="invisible-btn", elem_id="change-online-search-btn") + historySelectBtn = gr.Button( + visible=False, elem_classes="invisible-btn", elem_id="history-select-btn") # Not used + + + def create_greeting(request: gr.Request): + if hasattr(request, "username") and request.username: + logger.info(f"Get User Name: {request.username}") + user_info, user_name = gr.Markdown.update( + value=f"User: {request.username}"), request.username + else: + user_info, user_name = gr.Markdown.update( + value=f"", visible=False), "" + current_model = get_model( + model_name=MODELS[DEFAULT_MODEL], access_key=my_api_key, user_name=user_name)[0] + if not hide_history_when_not_logged_in or user_name: + loaded_stuff = current_model.auto_load() + else: + loaded_stuff = [gr.update(), gr.update(), gr.Chatbot.update(label=MODELS[DEFAULT_MODEL]), + current_model.single_turn, current_model.temperature, current_model.top_p, + current_model.n_choices, current_model.stop_sequence, current_model.token_upper_limit, + current_model.max_generation_token, current_model.presence_penalty, + current_model.frequency_penalty, current_model.logit_bias, current_model.user_identifier] + return user_info, user_name, current_model, toggle_like_btn_visibility( + DEFAULT_MODEL), *loaded_stuff, init_history_list(user_name, prepend=current_model.history_file_path[:-5]) + + + demo.load(create_greeting, inputs=None, outputs=[ + user_info, user_name, current_model, like_dislike_area, saveFileName, systemPromptTxt, chatbot, + single_turn_checkbox, temperature_slider, top_p_slider, n_choices_slider, stop_sequence_txt, + max_context_length_slider, max_generation_slider, presence_penalty_slider, frequency_penalty_slider, + logit_bias_txt, user_identifier_txt, historySelectList], api_name="load") + chatgpt_predict_args = dict( + fn=predict, + inputs=[ + current_model, + user_question, + chatbot, + use_streaming_checkbox, + use_websearch_checkbox, + index_files, + language_select_dropdown, + ], + outputs=[chatbot, status_display], + show_progress=True, + ) + + start_outputing_args = dict( + fn=start_outputing, + inputs=[], + outputs=[submitBtn, cancelBtn], + show_progress=True, + ) + + end_outputing_args = dict( + fn=end_outputing, inputs=[], outputs=[submitBtn, cancelBtn] + ) + + reset_textbox_args = dict( + fn=reset_textbox, inputs=[], outputs=[user_input] + ) + + transfer_input_args = dict( + fn=transfer_input, inputs=[user_input], outputs=[ + user_question, user_input, submitBtn, cancelBtn], show_progress=True + ) + + get_usage_args = dict( + fn=billing_info, inputs=[current_model], outputs=[ + usageTxt], show_progress=False + ) + + load_history_from_file_args = dict( + fn=load_chat_history, + inputs=[current_model, historySelectList], + outputs=[saveFileName, systemPromptTxt, chatbot, single_turn_checkbox, temperature_slider, top_p_slider, + n_choices_slider, stop_sequence_txt, max_context_length_slider, max_generation_slider, + presence_penalty_slider, frequency_penalty_slider, logit_bias_txt, user_identifier_txt], + ) + + refresh_history_args = dict( + fn=get_history_list, inputs=[user_name], outputs=[historySelectList] + ) + + auto_name_chat_history_args = dict( + fn=auto_name_chat_history, + inputs=[current_model, name_chat_method, user_question, chatbot, single_turn_checkbox], + outputs=[historySelectList], + show_progress=False, + ) + + # Chatbot + cancelBtn.click(interrupt, [current_model], []) + + user_input.submit( + **transfer_input_args).then( + **chatgpt_predict_args).then( + **end_outputing_args).then( + **auto_name_chat_history_args) + user_input.submit(**get_usage_args) + + submitBtn.click(**transfer_input_args).then( + **chatgpt_predict_args, api_name="predict").then( + **end_outputing_args).then( + **auto_name_chat_history_args) + submitBtn.click(**get_usage_args) + index_files.upload(handle_file_upload, [current_model, index_files, chatbot, language_select_dropdown], [ + index_files, chatbot, status_display]) + summarize_btn.click(handle_summarize_index, [ + current_model, index_files, chatbot, language_select_dropdown], [chatbot, status_display]) + emptyBtn.click( + reset, + inputs=[current_model, retain_system_prompt_checkbox], + outputs=[chatbot, status_display, historySelectList, systemPromptTxt, single_turn_checkbox, temperature_slider, + top_p_slider, n_choices_slider, stop_sequence_txt, max_context_length_slider, max_generation_slider, + presence_penalty_slider, frequency_penalty_slider, logit_bias_txt, user_identifier_txt], + show_progress=True, + _js='(a,b)=>{return clearChatbot(a,b);}', + ) + + retryBtn.click(**start_outputing_args).then( + retry, + [ + current_model, + chatbot, + use_streaming_checkbox, + use_websearch_checkbox, + index_files, + language_select_dropdown, + ], + [chatbot, status_display], + show_progress=True, + ).then(**end_outputing_args) + retryBtn.click(**get_usage_args) + + delFirstBtn.click( + delete_first_conversation, + [current_model], + [status_display], + ) + + delLastBtn.click( + delete_last_conversation, + [current_model, chatbot], + [chatbot, status_display], + show_progress=False + ) + + likeBtn.click( + like, + [current_model], + [status_display], + show_progress=False + ) + + dislikeBtn.click( + dislike, + [current_model], + [status_display], + show_progress=False + ) + two_column.change(update_doc_config, [two_column], None) + + # LLM Models + keyTxt.change(set_key, [current_model, keyTxt], [ + user_api_key, status_display], api_name="set_key").then(**get_usage_args) + keyTxt.submit(**get_usage_args) + single_turn_checkbox.change( + set_single_turn, [current_model, single_turn_checkbox], None, show_progress=False) + model_select_dropdown.change(get_model, + [model_select_dropdown, lora_select_dropdown, user_api_key, temperature_slider, + top_p_slider, systemPromptTxt, user_name, current_model], [ + current_model, status_display, chatbot, lora_select_dropdown, user_api_key, + keyTxt], show_progress=True, api_name="get_model") + model_select_dropdown.change(toggle_like_btn_visibility, [model_select_dropdown], [ + like_dislike_area], show_progress=False) + lora_select_dropdown.change(get_model, + [model_select_dropdown, lora_select_dropdown, user_api_key, temperature_slider, + top_p_slider, systemPromptTxt, user_name, current_model], + [current_model, status_display, chatbot], show_progress=True) + + # Template + systemPromptTxt.change(set_system_prompt, [ + current_model, systemPromptTxt], None) + templateRefreshBtn.click(get_template_dropdown, None, [ + templateFileSelectDropdown]) + templateFileSelectDropdown.input( + load_template, + [templateFileSelectDropdown], + [promptTemplates, templateSelectDropdown], + show_progress=True, + ) + templateSelectDropdown.change( + get_template_content, + [promptTemplates, templateSelectDropdown, systemPromptTxt], + [systemPromptTxt], + show_progress=True, + ) + + # S&L + renameHistoryBtn.click( + rename_chat_history, + [current_model, saveFileName, chatbot], + [historySelectList], + show_progress=True, + _js='(a,b,c,d)=>{return saveChatHistory(a,b,c,d);}' + ) + exportMarkdownBtn.click( + export_markdown, + [current_model, saveFileName, chatbot], + [], + show_progress=True, + ) + historyRefreshBtn.click(**refresh_history_args) + historyDeleteBtn.click(delete_chat_history, [current_model, historySelectList], + [status_display, historySelectList, chatbot], + _js='(a,b,c)=>{return showConfirmationDialog(a, b, c);}').then( + reset, + inputs=[current_model, retain_system_prompt_checkbox], + outputs=[chatbot, status_display, historySelectList, systemPromptTxt], + show_progress=True, + _js='(a,b)=>{return clearChatbot(a,b);}', + ) + historySelectList.input(**load_history_from_file_args) + uploadFileBtn.upload(upload_chat_history, [current_model, uploadFileBtn], [ + saveFileName, systemPromptTxt, chatbot, single_turn_checkbox, temperature_slider, top_p_slider, + n_choices_slider, stop_sequence_txt, max_context_length_slider, max_generation_slider, presence_penalty_slider, + frequency_penalty_slider, logit_bias_txt, user_identifier_txt]).then(**refresh_history_args) + historyDownloadBtn.click(None, [ + user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b,".json");}') + historyMarkdownDownloadBtn.click(None, [ + user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b,".md");}') + historySearchTextbox.input( + filter_history, + [user_name, historySearchTextbox], + [historySelectList] + ) + + # Advanced + temperature_slider.input( + set_temperature, [current_model, temperature_slider], None, show_progress=False) + top_p_slider.input(set_top_p, [current_model, top_p_slider], None, show_progress=False) + n_choices_slider.input( + set_n_choices, [current_model, n_choices_slider], None, show_progress=False) + stop_sequence_txt.input( + set_stop_sequence, [current_model, stop_sequence_txt], None, show_progress=False) + max_context_length_slider.input( + set_token_upper_limit, [current_model, max_context_length_slider], None, show_progress=False) + max_generation_slider.input( + set_max_tokens, [current_model, max_generation_slider], None, show_progress=False) + presence_penalty_slider.input( + set_presence_penalty, [current_model, presence_penalty_slider], None, show_progress=False) + frequency_penalty_slider.input( + set_frequency_penalty, [current_model, frequency_penalty_slider], None, show_progress=False) + logit_bias_txt.input( + set_logit_bias, [current_model, logit_bias_txt], None, show_progress=False) + user_identifier_txt.input(set_user_identifier, [ + current_model, user_identifier_txt], None, show_progress=False) + + default_btn.click( + reset_default, [], [apihostTxt, proxyTxt, status_display], show_progress=True + ) + + # Invisible elements + changeSingleSessionBtn.click( + fn=lambda value: gr.Checkbox.update(value=value), + inputs=[single_turn_checkbox], + outputs=[single_turn_checkbox], + _js='(a)=>{return bgChangeSingleSession(a);}' + ) + changeOnlineSearchBtn.click( + fn=lambda value: gr.Checkbox.update(value=value), + inputs=[use_websearch_checkbox], + outputs=[use_websearch_checkbox], + _js='(a)=>{return bgChangeOnlineSearch(a);}' + ) + historySelectBtn.click( # This is an experimental feature... Not actually used. + fn=load_chat_history, + inputs=[current_model, historySelectList], + outputs=[saveFileName, systemPromptTxt, chatbot, single_turn_checkbox, temperature_slider, top_p_slider, + n_choices_slider, stop_sequence_txt, max_context_length_slider, max_generation_slider, + presence_penalty_slider, frequency_penalty_slider, logit_bias_txt, user_identifier_txt], + _js='(a,b)=>{return bgSelectHistory(a,b);}' + ) + logout_btn.click( + fn=None, + inputs=[], + outputs=[], + _js='self.location="/logout"' + ) + +demo.title = TITLE + +if __name__ == "__main__": + reload_javascript() + setup_wizard() + demo.queue(concurrency_count=CONCURRENT_COUNT).launch( + allowed_paths=[HISTORY_DIR, assets_path], + server_name=server_name, + server_port=server_port, + share=share, + blocked_paths=[config_file], + auth=auth_from_conf if authflag else None, + favicon_path=favicon_path, + inbrowser=autobrowser and not dockerflag, + ) diff --git a/assets/chatbot.png b/assets/chatbot.png new file mode 100644 index 0000000000000000000000000000000000000000..7b9e59211a3c1f4f82b81c536434c40bf46a4624 Binary files /dev/null and b/assets/chatbot.png differ diff --git a/assets/favicon.ico b/assets/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..7b9e59211a3c1f4f82b81c536434c40bf46a4624 Binary files /dev/null and b/assets/favicon.ico differ diff --git a/assets/generate_image.png b/assets/generate_image.png new file mode 100644 index 0000000000000000000000000000000000000000..ed261c9231038a5579a56907065411966def076e --- /dev/null +++ b/assets/generate_image.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0252771bc3b39b07a0a0d76ac6ed307bb3c702c001bff0df499d1c0f2c98a4ab +size 1586595 diff --git a/assets/html/appearance_switcher.html b/assets/html/appearance_switcher.html new file mode 100644 index 0000000000000000000000000000000000000000..5fbf3b09b1c39de75c400514c9d0d81c807ea6bd --- /dev/null +++ b/assets/html/appearance_switcher.html @@ -0,0 +1,6 @@ +
+ +
diff --git a/assets/html/billing_info.html b/assets/html/billing_info.html new file mode 100644 index 0000000000000000000000000000000000000000..71abcc802da3c70716919c1a4738ac077c47bf01 --- /dev/null +++ b/assets/html/billing_info.html @@ -0,0 +1,9 @@ +{label} +
+
+ {usage_percent}% +
+
+
+ ${rounded_usage}${usage_limit} +
\ No newline at end of file diff --git a/assets/html/chatbot_header_btn.html b/assets/html/chatbot_header_btn.html new file mode 100644 index 0000000000000000000000000000000000000000..b9095b31522ebc61dd7015a7d54fa531c8f694d9 --- /dev/null +++ b/assets/html/chatbot_header_btn.html @@ -0,0 +1,72 @@ +
+
+ + + +
+ +
+ + + + + +
+
\ No newline at end of file diff --git a/assets/html/chatbot_more.html b/assets/html/chatbot_more.html new file mode 100644 index 0000000000000000000000000000000000000000..6d9e74df458def83cc96ac4cc9c3f8a3c8929394 --- /dev/null +++ b/assets/html/chatbot_more.html @@ -0,0 +1,72 @@ +
+
+ +
+ +
+ +
+ +
+
+ + +
+ +
+
+
+ + +
+ +
+
\ No newline at end of file diff --git a/assets/html/close_btn.html b/assets/html/close_btn.html new file mode 100644 index 0000000000000000000000000000000000000000..fa011b0c2bc56fe511b0a1794d618ef3e44586dd --- /dev/null +++ b/assets/html/close_btn.html @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/assets/html/footer.html b/assets/html/footer.html new file mode 100644 index 0000000000000000000000000000000000000000..bca27bb8066dfab5cc0acf7be349a514de5f9a58 --- /dev/null +++ b/assets/html/footer.html @@ -0,0 +1 @@ +
{versions}
diff --git a/assets/html/func_nav.html b/assets/html/func_nav.html new file mode 100644 index 0000000000000000000000000000000000000000..34d132d6919b282cc23bcfee7dba61d69bd18cc4 --- /dev/null +++ b/assets/html/func_nav.html @@ -0,0 +1,78 @@ + \ No newline at end of file diff --git a/assets/html/header_title.html b/assets/html/header_title.html new file mode 100644 index 0000000000000000000000000000000000000000..dbba9326d5186d5be1270379995a53ff2002c6c7 --- /dev/null +++ b/assets/html/header_title.html @@ -0,0 +1,11 @@ +
+ +
+
+
{app_title}
+
\ No newline at end of file diff --git a/assets/html/update.html b/assets/html/update.html new file mode 100644 index 0000000000000000000000000000000000000000..6f005e11a0a11f441ff2c56d05b98402c640a53f --- /dev/null +++ b/assets/html/update.html @@ -0,0 +1,29 @@ +
+
+

+ {current_version} + {version_time} +

+

+ Latest Version: getting latest version... +

+

+ Getting update... +

+
+
+
+ Getting Release Note... +
+
+
+ + +
+
+ + +
+
+
\ No newline at end of file diff --git a/assets/html/web_config.html b/assets/html/web_config.html new file mode 100644 index 0000000000000000000000000000000000000000..6e153b4222129520addd99a7aec463962aa92088 --- /dev/null +++ b/assets/html/web_config.html @@ -0,0 +1,23 @@ +
+ +
+ {enableCheckUpdate_config} + {hideHistoryWhenNotLoggedIn_config} +
+ +
+ {forView_i18n} + {deleteConfirm_i18n_pref} + {deleteConfirm_i18n_suff} + {usingLatest_i18n} + {updatingMsg_i18n} + {updateSuccess_i18n} + {updateFailure_i18n} + {regenerate_i18n} + {deleteRound_i18n} + {renameChat_i18n} + {validFileName_i18n} + {clearFileHistoryMsg_i18n} + {dropUploadMsg_i18n} +
+
\ No newline at end of file diff --git a/assets/icon.png b/assets/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..7b9e59211a3c1f4f82b81c536434c40bf46a4624 Binary files /dev/null and b/assets/icon.png differ diff --git a/assets/javascript/ChuanhuChat.js b/assets/javascript/ChuanhuChat.js new file mode 100644 index 0000000000000000000000000000000000000000..d7078498b409e6926205a6c539d02985c05f313a --- /dev/null +++ b/assets/javascript/ChuanhuChat.js @@ -0,0 +1,463 @@ + +// ChuanhuChat core javascript + +const MAX_HISTORY_LENGTH = 32; + +var key_down_history = []; +var currentIndex = -1; + +var gradioContainer = null; +var user_input_ta = null; +var user_input_tb = null; +var userInfoDiv = null; +var appTitleDiv = null; +var chatbotArea = null; +var chatbot = null; +var chatbotIndicator = null; +var uploaderIndicator = null; +var chatListIndicator = null; +var chatbotWrap = null; +var apSwitch = null; +var messageBotDivs = null; +var loginUserForm = null; +var logginUser = null; +var updateToast = null; +var sendBtn = null; +var cancelBtn = null; +var sliders = null; +var updateChuanhuBtn = null; +var statusDisplay = null; + +var historySelector = null; +var chuanhuPopup = null; +var settingBox = null; +var trainingBox = null; +var popupWrapper = null; +var chuanhuHeader = null; +var menu = null; +var toolbox = null; +// var trainBody = null; + +var isInIframe = (window.self !== window.top); +var currentTime = new Date().getTime(); + +let windowWidth = window.innerWidth; // 初始窗口宽度 + +function addInit() { + var needInit = {chatbotIndicator, uploaderIndicator}; + + chatbotIndicator = gradioApp().querySelector('#chuanhu-chatbot > div.wrap'); + uploaderIndicator = gradioApp().querySelector('#upload-index-file > div[data-testid="block-label"]'); + chatListIndicator = gradioApp().querySelector('#history-select-dropdown > div.wrap'); + + for (let elem in needInit) { + if (needInit[elem] == null) { + // addInited = false; + return false; + } + } + + chatbotObserver.observe(chatbotIndicator, { attributes: true, childList: true, subtree: true }); + chatListObserver.observe(chatListIndicator, { attributes: true }); + setUploader(); + setPasteUploader(); + setDragUploader(); + return true; +} + +function initialize() { + gradioObserver.observe(gradioApp(), { childList: true, subtree: true }); + + loginUserForm = gradioApp().querySelector(".gradio-container > .main > .wrap > .panel > .form") + gradioContainer = gradioApp().querySelector(".gradio-container"); + user_input_tb = gradioApp().getElementById('user-input-tb'); + userInfoDiv = gradioApp().getElementById("user-info"); + appTitleDiv = gradioApp().getElementById("app-title"); + chatbotArea = gradioApp().querySelector('#chatbot-area'); + chatbot = gradioApp().querySelector('#chuanhu-chatbot'); + chatbotWrap = gradioApp().querySelector('#chuanhu-chatbot > .wrapper > .wrap'); + apSwitch = gradioApp().querySelector('.apSwitch input[type="checkbox"]'); + updateToast = gradioApp().querySelector("#toast-update"); + sendBtn = gradioApp().getElementById("submit-btn"); + cancelBtn = gradioApp().getElementById("cancel-btn"); + sliders = gradioApp().querySelectorAll('input[type="range"]'); + updateChuanhuBtn = gradioApp().getElementById("update-chuanhu-btn"); + statusDisplay = gradioApp().querySelector('#status-display'); + + historySelector = gradioApp().querySelector('#history-select-dropdown'); + chuanhuPopup = gradioApp().querySelector('#chuanhu-popup'); + settingBox = gradioApp().querySelector('#chuanhu-setting'); + // trainingBox = gradioApp().querySelector('#chuanhu-training'); + popupWrapper = gradioApp().querySelector('#popup-wrapper'); + chuanhuHeader = gradioApp().querySelector('#chuanhu-header'); + menu = gradioApp().querySelector('#menu-area'); + toolbox = gradioApp().querySelector('#toolbox-area'); + // trainBody = gradioApp().querySelector('#train-body'); + + // if (loginUserForm) { + // localStorage.setItem("userLogged", true); + // userLogged = true; + // } + + adjustDarkMode(); + adjustSide(); + setChatList(); + setChatListHeader(); + setLoclize(); + selectHistory(); + // setChatbotHeight(); + setPopupBoxPosition(); + setSlider(); + setCheckboxes(); + setAutocomplete(); + checkModel(); + + settingBox.classList.add('hideBox'); + // trainingBox.classList.add('hideBox'); + + if (!historyLoaded) loadHistoryHtml(); + if (!usernameGotten) getUserInfo(); + + setUpdater(); + + setChatbotScroll(); + setTimeout(showOrHideUserInfo(), 2000); + + // setHistroyPanel(); + // trainBody.classList.add('hide-body'); + + + + return true; +} + +function gradioApp() { + const elems = document.getElementsByTagName('gradio-app'); + const elem = elems.length == 0 ? document : elems[0]; + + if (elem !== document) { + elem.getElementById = function(id) { + return document.getElementById(id); + }; + } + return elem.shadowRoot ? elem.shadowRoot : elem; +} + +function showConfirmationDialog(a, file, c) { + if (file != "") { + var result = confirm(i18n(deleteConfirm_i18n_pref) + file + i18n(deleteConfirm_i18n_suff)); + if (result) { + return [a, file, c]; + } + } + return [a, "CANCELED", c]; +} + +function selectHistory() { + user_input_ta = user_input_tb.querySelector("textarea"); + if (user_input_ta) { + disableSendBtn(); + // 在 textarea 上监听 keydown 事件 + user_input_ta.addEventListener("keydown", function (event) { + var value = user_input_ta.value.trim(); + // 判断按下的是否为方向键 + if (event.code === 'ArrowUp' || event.code === 'ArrowDown') { + // 如果按下的是方向键,且输入框中有内容,且历史记录中没有该内容,则不执行操作 + if (value && key_down_history.indexOf(value) === -1) + return; + // 对于需要响应的动作,阻止默认行为。 + event.preventDefault(); + var length = key_down_history.length; + if (length === 0) { + currentIndex = -1; // 如果历史记录为空,直接将当前选中的记录重置 + return; + } + if (currentIndex === -1) { + currentIndex = length; + } + if (event.code === 'ArrowUp' && currentIndex > 0) { + currentIndex--; + user_input_ta.value = key_down_history[currentIndex]; + } else if (event.code === 'ArrowDown' && currentIndex < length - 1) { + currentIndex++; + user_input_ta.value = key_down_history[currentIndex]; + } + user_input_ta.selectionStart = user_input_ta.value.length; + user_input_ta.selectionEnd = user_input_ta.value.length; + const input_event = new InputEvent("input", { bubbles: true, cancelable: true }); + user_input_ta.dispatchEvent(input_event); + } else if (event.code === "Enter") { + if (value) { + currentIndex = -1; + if (key_down_history.indexOf(value) === -1) { + key_down_history.push(value); + if (key_down_history.length > MAX_HISTORY_LENGTH) { + key_down_history.shift(); + } + } + } + } + }); + } +} + +function disableSendBtn() { + sendBtn.disabled = user_input_ta.value.trim() === ''; + user_input_ta.addEventListener('input', () => { + sendBtn.disabled = user_input_ta.value.trim() === ''; + }); +} + +function checkModel() { + const model = gradioApp().querySelector('#model-select-dropdown input'); + var modelValue = model.value; + checkGPT(); + checkXMChat(); + function checkGPT() { + modelValue = model.value; + if (modelValue.toLowerCase().includes('gpt')) { + gradioApp().querySelector('#header-btn-groups').classList.add('is-gpt'); + } else { + gradioApp().querySelector('#header-btn-groups').classList.remove('is-gpt'); + } + // console.log('gpt model checked') + } + function checkXMChat() { + modelValue = model.value; + if (modelValue.includes('xmchat')) { + chatbotArea.classList.add('is-xmchat'); + } else { + chatbotArea.classList.remove('is-xmchat'); + } + } + + model.addEventListener('blur', ()=>{ + setTimeout(()=>{ + checkGPT(); + checkXMChat(); + }, 100); + }); +} + +function toggleDarkMode(isEnabled) { + if (isEnabled) { + document.body.classList.add("dark"); + document.querySelector('meta[name="theme-color"]').setAttribute('content', '#171717'); + document.body.style.setProperty("background-color", "var(--neutral-950)", "important"); + } else { + document.body.classList.remove("dark"); + document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff'); + document.body.style.backgroundColor = ""; + } +} +function adjustDarkMode() { + const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)"); + apSwitch.checked = darkModeQuery.matches; + toggleDarkMode(darkModeQuery.matches); + darkModeQuery.addEventListener("change", (e) => { + apSwitch.checked = e.matches; + toggleDarkMode(e.matches); + }); + apSwitch.addEventListener("change", (e) => { + toggleDarkMode(e.target.checked); + }); +} +function btnToggleDarkMode() { + apSwitch.checked = !apSwitch.checked; + toggleDarkMode(apSwitch.checked); +} + +function setScrollShadow() { + const toolboxScroll = toolbox.querySelector('#toolbox-area > .gradio-box > .gradio-tabs > div.tab-nav'); + const toolboxTabs = toolboxScroll.querySelectorAll('button'); + let toolboxScrollWidth = 0; + toolboxTabs.forEach((tab) => { + toolboxScrollWidth += tab.offsetWidth; // 获取按钮宽度并累加 + }); + function adjustScrollShadow() { + if (toolboxScroll.scrollLeft > 0) { + toolboxScroll.classList.add('scroll-shadow-left'); + } else { + toolboxScroll.classList.remove('scroll-shadow-left'); + } + + if (toolboxScroll.scrollLeft + toolboxScroll.clientWidth < toolboxScrollWidth) { + toolboxScroll.classList.add('scroll-shadow-right'); + } else { + toolboxScroll.classList.remove('scroll-shadow-right'); + } + } + toolboxScroll.addEventListener('scroll', () => { + adjustScrollShadow(); + }); + // no, I failed to make shadow appear on the top layer... +} + +function setPopupBoxPosition() { + const screenWidth = window.innerWidth; + const screenHeight = window.innerHeight; + popupWrapper.style.height = `${screenHeight}px`; + popupWrapper.style.width = `${screenWidth}px`; + // const popupBoxWidth = 680; + // const popupBoxHeight = 400; + // chuanhuPopup.style.left = `${(screenWidth - popupBoxWidth) / 2}px`; + // chuanhuPopup.style.top = `${(screenHeight - popupBoxHeight) / 2}px`; +} + +function updateVH() { + const vh = window.innerHeight * 0.01; + document.documentElement.style.setProperty('--vh', `${vh}px`); +} + +function setChatbotHeight() { + return; + const screenWidth = window.innerWidth; + const statusDisplay = document.querySelector('#status-display'); + const statusDisplayHeight = statusDisplay ? statusDisplay.offsetHeight : 0; + const vh = window.innerHeight * 0.01; + document.documentElement.style.setProperty('--vh', `${vh}px`); + if (isInIframe) { + chatbot.style.height = `700px`; + chatbotWrap.style.maxHeight = `calc(700px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))` + } else { + if (screenWidth <= 320) { + chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px)`; + chatbotWrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`; + } else if (screenWidth <= 499) { + chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px)`; + chatbotWrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`; + } else { + chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px)`; + chatbotWrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`; + } + } +} +function setChatbotScroll() { + var scrollHeight = chatbotWrap.scrollHeight; + chatbotWrap.scrollTo(0,scrollHeight) +} + +function setAutocomplete() { + // 避免API Key被当成密码导致的模型下拉框被当成用户名而引发的浏览器自动填充行为 + const apiKeyInput = gradioApp().querySelector("#api-key input"); + apiKeyInput.setAttribute("autocomplete", "new-password"); +} + +function clearChatbot(a, b) { + clearHistoryHtml(); + // clearMessageRows(); + return [a, b] +} + +function chatbotContentChanged(attempt = 1, force = false) { + for (var i = 0; i < attempt; i++) { + setTimeout(() => { + // clearMessageRows(); + saveHistoryHtml(); + disableSendBtn(); + updateSlider(); + updateCheckboxes(); + bindFancyBox(); + + gradioApp().querySelectorAll('#chuanhu-chatbot .message-wrap .message.bot').forEach(addChuanhuButton); + + if (chatbotIndicator.classList.contains('hide')) { // generation finished + setLatestMessage(); + setChatList(); + } + + if (!chatbotIndicator.classList.contains('translucent')) { // message deleted + var checkLatestAdded = setInterval(() => { + var latestMessageNow = gradioApp().querySelector('#chuanhu-chatbot > .wrapper > .wrap > .message-wrap .message.bot.latest'); + if (latestMessageNow && latestMessageNow.querySelector('.message-btn-row')) { + clearInterval(checkLatestAdded); + } else { + setLatestMessage(); + } + }, 200); + } + + + }, i === 0 ? 0 : 200); + } + // 理论上是不需要多次尝试执行的,可惜gradio的bug导致message可能没有渲染完毕,所以尝试500ms后再次执行 +} + +var chatbotObserver = new MutationObserver(() => { + chatbotContentChanged(1); + if (chatbotIndicator.classList.contains('hide')) { + // setLatestMessage(); + chatbotContentChanged(2); + } + if (!chatbotIndicator.classList.contains('translucent')) { + chatbotContentChanged(2); + } + +}); + +var chatListObserver = new MutationObserver(() => { + setChatList(); +}); + +// 监视页面内部 DOM 变动 +var gradioObserver = new MutationObserver(function (mutations) { + for (var i = 0; i < mutations.length; i++) { + if (mutations[i].addedNodes.length) { + if (addInit()) { + gradioObserver.disconnect(); + return; + } + } + } +}); + +// 监视页面变化 +window.addEventListener("DOMContentLoaded", function () { + // const ga = document.getElementsByTagName("gradio-app"); + updateVH(); + windowWidth = window.innerWidth; + gradioApp().addEventListener("render", initialize); + isInIframe = (window.self !== window.top); + historyLoaded = false; +}); +window.addEventListener('resize', ()=>{ + // setChatbotHeight(); + updateVH(); + windowWidth = window.innerWidth; + setPopupBoxPosition(); + adjustSide(); +}); +window.addEventListener('orientationchange', (event) => { + updateVH(); + windowWidth = window.innerWidth; + setPopupBoxPosition(); + adjustSide(); +}); +window.addEventListener('scroll', ()=>{setPopupBoxPosition();}); +window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", adjustDarkMode); + +// console suprise +var styleTitle1 = ` +font-size: 16px; +font-family: ui-monospace, monospace; +color: #06AE56; +` +var styleDesc1 = ` +font-size: 12px; +font-family: ui-monospace, monospace; +` +function makeML(str) { + let l = new String(str) + l = l.substring(l.indexOf("/*") + 3, l.lastIndexOf("*/")) + return l +} +let ChuanhuInfo = function () { + /* chatgpt-webui - GUI for ChatGPT API */ +} +let description = ` +© 2023 shibing624 +GitHub repository: [https://github.com/shibing624/chatgpt-webui]\n +Enjoy our project!\n +` +console.log(`%c${makeML(ChuanhuInfo)}`,styleTitle1); +console.log(`%c${description}`, styleDesc1); diff --git a/assets/javascript/Kelpy-Codos.js b/assets/javascript/Kelpy-Codos.js new file mode 100644 index 0000000000000000000000000000000000000000..cfbaeedb4f371dfb5fe157db545b364046fca3e1 --- /dev/null +++ b/assets/javascript/Kelpy-Codos.js @@ -0,0 +1,76 @@ +// ==UserScript== +// @name Kelpy Codos +// @namespace https://github.com/Keldos-Li/Kelpy-Codos +// @version 1.0.5 +// @author Keldos; https://keldos.me/ +// @description Add copy button to PRE tags before CODE tag, for Chuanhu ChatGPT especially. +// Based on Chuanhu ChatGPT version: ac04408 (2023-3-22) +// @license GPL-3.0 +// @grant none +// ==/UserScript== + +(function () { + 'use strict'; + + function addCopyButton(pre) { + var code = pre.querySelector('code'); + if (!code) { + return; // 如果没有找到 元素,则不添加按钮 + } + 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 '
' in inputs: + inputs = inputs.split('
')[1] + inputs = inputs.split("
")[0] + elif len(self.history) == 1: + inputs = self.history[-1]["content"] + del self.history[-1] + else: + yield chatbot, f"{STANDARD_ERROR_MSG}上下文是空的" + return + + iter = self.predict( + inputs, + chatbot, + stream=stream, + use_websearch=use_websearch, + files=files, + reply_language=reply_language, + ) + for x in iter: + yield x + logger.debug("重试完毕") + + def interrupt(self): + self.interrupted = True + + def recover(self): + self.interrupted = False + + def set_token_upper_limit(self, new_upper_limit): + self.token_upper_limit = new_upper_limit + logger.info(f"token上限设置为{new_upper_limit}") + self.auto_save() + + def set_temperature(self, new_temperature): + self.temperature = new_temperature + self.auto_save() + + def set_top_p(self, new_top_p): + self.top_p = new_top_p + self.auto_save() + + def set_n_choices(self, new_n_choices): + self.n_choices = new_n_choices + self.auto_save() + + def set_stop_sequence(self, new_stop_sequence: str): + new_stop_sequence = new_stop_sequence.split(",") + self.stop_sequence = new_stop_sequence + self.auto_save() + + def set_max_tokens(self, new_max_tokens): + self.max_generation_token = new_max_tokens + self.auto_save() + + def set_presence_penalty(self, new_presence_penalty): + self.presence_penalty = new_presence_penalty + self.auto_save() + + def set_frequency_penalty(self, new_frequency_penalty): + self.frequency_penalty = new_frequency_penalty + self.auto_save() + + def set_logit_bias(self, logit_bias): + self.logit_bias = logit_bias + self.auto_save() + + def encoded_logit_bias(self): + if self.logit_bias is None: + return {} + logit_bias = self.logit_bias.split() + bias_map = {} + encoding = tiktoken.get_encoding("cl100k_base") + for line in logit_bias: + word, bias_amount = line.split(":") + if word: + for token in encoding.encode(word): + bias_map[token] = float(bias_amount) + return bias_map + + def set_user_identifier(self, new_user_identifier): + self.user_identifier = new_user_identifier + self.auto_save() + + def set_system_prompt(self, new_system_prompt): + self.system_prompt = new_system_prompt + self.auto_save() + + def set_key(self, new_access_key): + self.api_key = new_access_key.strip() + msg = i18n("API密钥更改为了") + hide_middle_chars(self.api_key) + logger.info(msg) + return self.api_key, msg + + def set_single_turn(self, new_single_turn): + self.single_turn = new_single_turn + self.auto_save() + + def reset(self, remain_system_prompt=False): + self.history = [] + self.all_token_counts = [] + self.interrupted = False + self.history_file_path = new_auto_history_filename(self.user_name) + history_name = self.history_file_path[:-5] + choices = get_history_names(self.user_name) + if history_name not in choices: + choices.insert(0, history_name) + system_prompt = self.system_prompt if remain_system_prompt else "" + + self.single_turn = self.default_single_turn + self.temperature = self.default_temperature + self.top_p = self.default_top_p + self.n_choices = self.default_n_choices + self.stop_sequence = self.default_stop_sequence + self.max_generation_token = self.default_max_generation_token + self.presence_penalty = self.default_presence_penalty + self.frequency_penalty = self.default_frequency_penalty + self.logit_bias = self.default_logit_bias + self.user_identifier = self.default_user_identifier + + return ( + [], + self.token_message([0]), + gr.Radio.update(choices=choices, value=history_name), + system_prompt, + self.single_turn, + self.temperature, + self.top_p, + self.n_choices, + self.stop_sequence, + self.token_upper_limit, + self.max_generation_token, + self.presence_penalty, + self.frequency_penalty, + self.logit_bias, + self.user_identifier, + ) + + def delete_first_conversation(self): + if self.history: + del self.history[:2] + del self.all_token_counts[:1] + return self.token_message() + + def delete_last_conversation(self, chatbot): + if len(chatbot) > 0 and STANDARD_ERROR_MSG in chatbot[-1][1]: + msg = "由于包含报错信息,只删除chatbot记录" + chatbot = chatbot[:-1] + return chatbot, self.history + if len(self.history) > 0: + self.history = self.history[:-2] + if len(chatbot) > 0: + msg = "删除了一组chatbot对话" + chatbot = chatbot[:-1] + if len(self.all_token_counts) > 0: + msg = "删除了一组对话的token计数记录" + self.all_token_counts.pop() + msg = "删除了一组对话" + self.chatbot = chatbot + self.auto_save(chatbot) + return chatbot, msg + + def token_message(self, token_lst=None): + if token_lst is None: + token_lst = self.all_token_counts + token_sum = 0 + for i in range(len(token_lst)): + token_sum += sum(token_lst[: i + 1]) + return ( + i18n("Token 计数: ") + + f"{sum(token_lst)}" + + i18n(",本次对话累计消耗了 ") + + f"{token_sum} tokens" + ) + + def rename_chat_history(self, filename, chatbot): + if filename == "": + return gr.update() + if not filename.endswith(".json"): + filename += ".json" + self.delete_chat_history(self.history_file_path) + # 命名重复检测 + repeat_file_index = 2 + full_path = os.path.join(HISTORY_DIR, self.user_name, filename) + while os.path.exists(full_path): + full_path = os.path.join( + HISTORY_DIR, self.user_name, f"{repeat_file_index}_{filename}" + ) + repeat_file_index += 1 + filename = os.path.basename(full_path) + + self.history_file_path = filename + save_file(filename, self, chatbot) + return init_history_list(self.user_name) + + def auto_name_chat_history( + self, name_chat_method, user_question, chatbot, single_turn_checkbox + ): + if len(self.history) == 2 and not single_turn_checkbox: + user_question = self.history[0]["content"] + if type(user_question) == list: + user_question = user_question[0]["text"] + filename = replace_special_symbols(user_question)[:16] + ".json" + return self.rename_chat_history(filename, chatbot) + else: + return gr.update() + + def auto_save(self, chatbot=None): + if chatbot is not None: + save_file(self.history_file_path, self, chatbot) + + def export_markdown(self, filename, chatbot): + if filename == "": + return + if not filename.endswith(".md"): + filename += ".md" + save_file(filename, self, chatbot) + + def load_chat_history(self, new_history_file_path=None): + logger.debug(f"{self.user_name} 加载对话历史中……") + if new_history_file_path is not None: + if type(new_history_file_path) != str: + # copy file from new_history_file_path.name to os.path.join(HISTORY_DIR, self.user_name) + new_history_file_path = new_history_file_path.name + shutil.copyfile( + new_history_file_path, + os.path.join( + HISTORY_DIR, + self.user_name, + os.path.basename(new_history_file_path), + ), + ) + self.history_file_path = os.path.basename(new_history_file_path) + else: + self.history_file_path = new_history_file_path + try: + if self.history_file_path == os.path.basename(self.history_file_path): + history_file_path = os.path.join( + HISTORY_DIR, self.user_name, self.history_file_path + ) + else: + history_file_path = self.history_file_path + if not self.history_file_path.endswith(".json"): + history_file_path += ".json" + saved_json = {} + if os.path.exists(history_file_path): + with open(history_file_path, "r", encoding="utf-8") as f: + saved_json = json.load(f) + try: + if type(saved_json["history"][0]) == str: + logger.info("历史记录格式为旧版,正在转换……") + new_history = [] + for index, item in enumerate(saved_json["history"]): + if index % 2 == 0: + new_history.append(construct_user(item)) + else: + new_history.append(construct_assistant(item)) + saved_json["history"] = new_history + logger.info(new_history) + except: + pass + if len(saved_json["chatbot"]) < len(saved_json["history"]) // 2: + logger.info("Trimming corrupted history...") + saved_json["history"] = saved_json["history"][-len(saved_json["chatbot"]):] + logger.info(f"Trimmed history: {saved_json['history']}") + logger.debug(f"{self.user_name} 加载对话历史完毕") + self.history = saved_json["history"] + self.single_turn = saved_json.get("single_turn", self.single_turn) + self.temperature = saved_json.get("temperature", self.temperature) + self.top_p = saved_json.get("top_p", self.top_p) + self.n_choices = saved_json.get("n_choices", self.n_choices) + self.stop_sequence = list(saved_json.get("stop_sequence", self.stop_sequence)) + self.token_upper_limit = saved_json.get( + "token_upper_limit", self.token_upper_limit + ) + self.max_generation_token = saved_json.get( + "max_generation_token", self.max_generation_token + ) + self.presence_penalty = saved_json.get( + "presence_penalty", self.presence_penalty + ) + self.frequency_penalty = saved_json.get( + "frequency_penalty", self.frequency_penalty + ) + self.logit_bias = saved_json.get("logit_bias", self.logit_bias) + self.user_identifier = saved_json.get("user_identifier", self.user_name) + self.metadata = saved_json.get("metadata", self.metadata) + self.chatbot = saved_json["chatbot"] + return ( + os.path.basename(self.history_file_path)[:-5], + saved_json["system"], + saved_json["chatbot"], + self.single_turn, + self.temperature, + self.top_p, + self.n_choices, + ",".join(self.stop_sequence), + self.token_upper_limit, + self.max_generation_token, + self.presence_penalty, + self.frequency_penalty, + self.logit_bias, + self.user_identifier, + ) + except: + # 没有对话历史或者对话历史解析失败 + logger.info(f"没有找到对话历史记录 {self.history_file_path}") + self.reset() + return ( + os.path.basename(self.history_file_path), + "", + [], + self.single_turn, + self.temperature, + self.top_p, + self.n_choices, + ",".join(self.stop_sequence), + self.token_upper_limit, + self.max_generation_token, + self.presence_penalty, + self.frequency_penalty, + self.logit_bias, + self.user_identifier, + ) + + def delete_chat_history(self, filename): + if filename == "CANCELED": + return gr.update(), gr.update(), gr.update() + if filename == "": + return i18n("你没有选择任何对话历史"), gr.update(), gr.update() + if filename and not filename.endswith(".json"): + filename += ".json" + if filename == os.path.basename(filename): + history_file_path = os.path.join(HISTORY_DIR, self.user_name, filename) + else: + history_file_path = filename + md_history_file_path = history_file_path[:-5] + ".md" + try: + os.remove(history_file_path) + os.remove(md_history_file_path) + return i18n("删除对话历史成功"), get_history_list(self.user_name), [] + except: + logger.info(f"删除对话历史失败 {history_file_path}") + return ( + i18n("对话历史") + filename + i18n("已经被删除啦"), + get_history_list(self.user_name), + [], + ) + + def auto_load(self): + self.history_file_path = new_auto_history_filename(self.user_name) + return self.load_chat_history() + + def like(self): + """like the last response, implement if needed""" + return gr.update() + + def dislike(self): + """dislike the last response, implement if needed""" + return gr.update() + + def deinitialize(self): + """deinitialize the model, implement if needed""" + pass diff --git a/src/chatglm.py b/src/chatglm.py new file mode 100644 index 0000000000000000000000000000000000000000..5c79c63402aa8be97bd6506ad3f99d44501cf413 --- /dev/null +++ b/src/chatglm.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- +""" +@author:XuMing(xuming624@qq.com) +@description: +""" +import platform + +from loguru import logger + +from src.base_model import BaseLLMModel +from src.presets import LOCAL_MODELS + + +class ChatGLMClient(BaseLLMModel): + def __init__(self, model_name, user_name=""): + super().__init__(model_name=model_name, user=user_name) + import torch + from transformers import AutoModel, AutoTokenizer + system_name = platform.system() + logger.info(f"Loading model from {model_name}") + if model_name in LOCAL_MODELS: + model_path = LOCAL_MODELS[model_name] + else: + model_path = model_name + self.CHATGLM_TOKENIZER = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) + quantified = False + if "int4" in model_name: + quantified = True + model = AutoModel.from_pretrained(model_path, trust_remote_code=True, device_map='auto', torch_dtype='auto') + if torch.cuda.is_available(): + logger.info("CUDA is available, using CUDA") + model = model.half().cuda() + # mps加速还存在一些问题,暂时不使用 + elif system_name == "Darwin" and model_path is not None and not quantified: + logger.info("Running on macOS, using MPS") + # running on macOS and model already downloaded + model = model.half().to("mps") + else: + logger.info("GPU is not available, using CPU") + model = model.float() + model = model.eval() + logger.info(f"Model loaded from {model_path}") + self.CHATGLM_MODEL = model + + def _get_glm3_style_input(self): + history = self.history + query = history.pop()["content"] + return history, query + + def _get_glm2_style_input(self): + history = [x["content"] for x in self.history] + query = history.pop() + logger.debug(f"{history}") + assert len(history) % 2 == 0, f"History should be even length. current history is: {history}" + history = [[history[i], history[i + 1]] + for i in range(0, len(history), 2)] + return history, query + + def _get_glm_style_input(self): + if "glm2" in self.model_name: + return self._get_glm2_style_input() + else: + return self._get_glm3_style_input() + + def get_answer_at_once(self): + history, query = self._get_glm_style_input() + logger.debug(f"{history}") + response, _ = self.CHATGLM_MODEL.chat( + self.CHATGLM_TOKENIZER, query, history=history) + return response, len(response) + + def get_answer_stream_iter(self): + history, query = self._get_glm_style_input() + logger.debug(f"{history}") + for response, history in self.CHATGLM_MODEL.stream_chat( + self.CHATGLM_TOKENIZER, + query, + history, + max_length=self.token_upper_limit, + top_p=self.top_p, + temperature=self.temperature, + ): + yield response diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000000000000000000000000000000000000..6105ccf05aef46cec5e350cc52567fd2d433dd09 --- /dev/null +++ b/src/config.py @@ -0,0 +1,206 @@ +import os +import sys +from collections import defaultdict +from contextlib import contextmanager + +import commentjson as json +from loguru import logger + +from src import shared, presets + +pwd_path = os.path.abspath(os.path.dirname(__file__)) + +# 添加一个统一的config文件 +config_file = os.path.join(pwd_path, "../config.json") +config = {} +if os.path.exists(config_file): + with open(config_file, "r", encoding='utf-8') as f: + config = json.load(f) +if config: + logger.info(f"加载配置文件成功, config: {config}") +language = config.get("language", "") or os.environ.get("LANGUAGE", "auto") + +hide_history_when_not_logged_in = config.get("hide_history_when_not_logged_in", False) +show_api_billing = config.get("show_api_billing", False) +# 选择对话名称的方法。0: 使用日期时间命名;1: 使用第一条提问命名,2: 使用模型自动总结 +chat_name_method_index = config.get("chat_name_method_index", 2) + +hide_local_models = config.get("hide_local_models", False) +if hide_local_models: + presets.MODELS = presets.ONLINE_MODELS + logger.info(f"已设置隐藏本地模型,可用模型:{presets.MODELS}") +else: + local_models = config.get("local_models", None) + if local_models: + presets.LOCAL_MODELS = local_models + logger.info(f"已设置本地模型:{local_models}") + presets.MODELS = presets.ONLINE_MODELS + list(presets.LOCAL_MODELS.keys()) +if "available_models" in config: + presets.MODELS = config["available_models"] + logger.info(f"已设置可用模型:{config['available_models']}") + +# 处理docker if we are running in Docker +dockerflag = config.get("dockerflag", False) + +xmchat_api_key = config.get("xmchat_api_key", "") +minimax_api_key = config.get("minimax_api_key", "") +minimax_group_id = config.get("minimax_group_id", "") + +usage_limit = config.get("usage_limit", 120) + +# 多账户机制 +multi_api_key = config.get("multi_api_key", False) # 是否开启多账户机制 +if multi_api_key: + api_key_list = config.get("api_key_list", []) + if len(api_key_list) == 0: + logger.error("多账号模式已开启,但api_key_list为空,请检查config.json") + sys.exit(1) + shared.state.set_api_key_queue(api_key_list) + +auth_list = config.get("users", []) # 实际上是使用者的列表 +authflag = len(auth_list) > 0 # 是否开启认证的状态值,改为判断auth_list长度 + +# 处理自定义的api_host,优先读环境变量的配置,如果存在则自动装配 +api_host = config.get("openai_api_base", None) +if api_host is not None: + shared.state.set_api_host(api_host) + logger.info(f"OpenAI API Base set to: {shared.state.openai_api_base}") +# 处理 api-key 以及 允许的用户列表 +my_api_key = config.get("openai_api_key", os.environ.get("OPENAI_API_KEY", "")) + +@contextmanager +def retrieve_openai_api(api_key=None): + if api_key is None: + yield my_api_key + else: + yield api_key + + +# 处理代理: +http_proxy = config.get("http_proxy", "") +https_proxy = config.get("https_proxy", "") +http_proxy = os.environ.get("HTTP_PROXY", http_proxy) +https_proxy = os.environ.get("HTTPS_PROXY", https_proxy) + +# 重置系统变量,在不需要设置的时候不设置环境变量,以免引起全局代理报错 +os.environ["HTTP_PROXY"] = "" +os.environ["HTTPS_PROXY"] = "" + +local_embedding = config.get("local_embedding", False) # 是否使用本地embedding +chunk_size = config.get("chunk_size", 500) +chunk_overlap = config.get("chunk_overlap", 50) +hf_emb_model_name = config.get("hf_emb_model_name", "shibing624/text2vec-base-multilingual") + + +@contextmanager +def retrieve_proxy(proxy=None): + """ + 1, 如果proxy = NONE,设置环境变量,并返回最新设置的代理 + 2,如果proxy != NONE,更新当前的代理配置,但是不更新环境变量 + """ + global http_proxy, https_proxy + if proxy is not None: + http_proxy = proxy + https_proxy = proxy + yield http_proxy, https_proxy + else: + old_var = os.environ["HTTP_PROXY"], os.environ["HTTPS_PROXY"] + os.environ["HTTP_PROXY"] = http_proxy + os.environ["HTTPS_PROXY"] = https_proxy + yield http_proxy, https_proxy # return new proxy + + # return old proxy + os.environ["HTTP_PROXY"], os.environ["HTTPS_PROXY"] = old_var + + +# 处理latex options +user_latex_option = config.get("latex_option", "default") +if user_latex_option == "default": + latex_delimiters_set = [ + {"left": "$$", "right": "$$", "display": True}, + {"left": "$", "right": "$", "display": False}, + {"left": "\\(", "right": "\\)", "display": False}, + {"left": "\\[", "right": "\\]", "display": True}, + ] +elif user_latex_option == "strict": + latex_delimiters_set = [ + {"left": "$$", "right": "$$", "display": True}, + {"left": "\\(", "right": "\\)", "display": False}, + {"left": "\\[", "right": "\\]", "display": True}, + ] +elif user_latex_option == "all": + latex_delimiters_set = [ + {"left": "$$", "right": "$$", "display": True}, + {"left": "$", "right": "$", "display": False}, + {"left": "\\(", "right": "\\)", "display": False}, + {"left": "\\[", "right": "\\]", "display": True}, + {"left": "\\begin{equation}", "right": "\\end{equation}", "display": True}, + {"left": "\\begin{align}", "right": "\\end{align}", "display": True}, + {"left": "\\begin{alignat}", "right": "\\end{alignat}", "display": True}, + {"left": "\\begin{gather}", "right": "\\end{gather}", "display": True}, + {"left": "\\begin{CD}", "right": "\\end{CD}", "display": True}, + ] +elif user_latex_option == "disabled": + latex_delimiters_set = [] +else: + latex_delimiters_set = [ + {"left": "$$", "right": "$$", "display": True}, + {"left": "$", "right": "$", "display": False}, + {"left": "\\(", "right": "\\)", "display": False}, + {"left": "\\[", "right": "\\]", "display": True}, + ] + +# 处理advance docs +advance_docs = defaultdict(lambda: defaultdict(dict)) +advance_docs.update(config.get("advance_docs", {})) + + +def update_doc_config(two_column_pdf): + global advance_docs + advance_docs["pdf"]["two_column"] = two_column_pdf + + +# 处理gradio.launch参数 +server_name = config.get("server_name", None) +server_port = config.get("server_port", None) +if server_name is None: + if dockerflag: + server_name = "0.0.0.0" + else: + server_name = "127.0.0.1" +if server_port is None: + if dockerflag: + server_port = 7860 + +assert server_port is None or type(server_port) == int, "要求port设置为int类型" + +# 设置默认model +default_model = config.get("default_model", "") +try: + presets.DEFAULT_MODEL = presets.MODELS.index(default_model) +except ValueError: + logger.error("你填写的默认模型" + default_model + "不存在!请从下面的列表中挑一个填写:" + str(presets.MODELS)) + +share = config.get("share", False) + +# avatar +bot_avatar = config.get("bot_avatar", "default") +user_avatar = config.get("user_avatar", "default") +if bot_avatar == "" or bot_avatar == "none" or bot_avatar is None: + bot_avatar = None +elif bot_avatar == "default": + bot_avatar = os.path.join(pwd_path, "../assets/chatbot.png") +if user_avatar == "" or user_avatar == "none" or user_avatar is None: + user_avatar = None +elif user_avatar == "default": + user_avatar = os.path.join(pwd_path, "../assets/user.png") + +websearch_engine = config.get("websearch_engine", "duckduckgo") +# 设置websearch engine api key +bing_search_api_key = config.get("bing_search_api_key", "") or os.environ.get("BING_SEARCH_API_KEY", "") +google_search_api_key = config.get("google_search_api_key", "") or os.environ.get("GOOGLE_SEARCH_API_KEY", "") +google_search_cx = config.get("google_search_cx", "") or os.environ.get("GOOGLE_SEARCH_CX", "") +serper_search_api_key = config.get("serper_search_api_key", "") or os.environ.get("SERPER_SEARCH_API_KEY", "") +searchapi_api_key = config.get("searchapi_api_key", "") or os.environ.get("SEARCHAPI_API_KEY", "") + +autobrowser = config.get("autobrowser", True) diff --git a/src/gradio_patch.py b/src/gradio_patch.py new file mode 100644 index 0000000000000000000000000000000000000000..1d39de76515c2678d537760d7813ceaf75831f52 --- /dev/null +++ b/src/gradio_patch.py @@ -0,0 +1,118 @@ +# -*- coding: utf-8 -*- +""" +@author:XuMing(xuming624@qq.com) +@description: +""" +import os + +import fastapi +import gradio +from fastapi.responses import RedirectResponse +from gradio.oauth import MOCKED_OAUTH_TOKEN + +OAUTH_CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID") +OAUTH_CLIENT_SECRET = os.environ.get("OAUTH_CLIENT_SECRET") +OAUTH_SCOPES = os.environ.get("OAUTH_SCOPES") +OPENID_PROVIDER_URL = os.environ.get("OPENID_PROVIDER_URL") + + +def _add_oauth_routes(app: fastapi.FastAPI) -> None: + """Add OAuth routes to the FastAPI app (login, callback handler and logout).""" + try: + from authlib.integrations.starlette_client import OAuth + except ImportError as e: + raise ImportError( + "Cannot initialize OAuth to due a missing library. Please run `pip install gradio[oauth]` or add " + "`gradio[oauth]` to your requirements.txt file in order to install the required dependencies." + ) from e + + # Check environment variables + msg = ( + "OAuth is required but {} environment variable is not set. Make sure you've enabled OAuth in your Space by" + " setting `hf_oauth: true` in the Space metadata." + ) + if OAUTH_CLIENT_ID is None: + raise ValueError(msg.format("OAUTH_CLIENT_ID")) + if OAUTH_CLIENT_SECRET is None: + raise ValueError(msg.format("OAUTH_CLIENT_SECRET")) + if OAUTH_SCOPES is None: + raise ValueError(msg.format("OAUTH_SCOPES")) + if OPENID_PROVIDER_URL is None: + raise ValueError(msg.format("OPENID_PROVIDER_URL")) + + # Register OAuth server + oauth = OAuth() + oauth.register( + name="huggingface", + client_id=OAUTH_CLIENT_ID, + client_secret=OAUTH_CLIENT_SECRET, + client_kwargs={"scope": OAUTH_SCOPES}, + server_metadata_url=OPENID_PROVIDER_URL + "/.well-known/openid-configuration", + ) + + # Define OAuth routes + @app.get("/login/huggingface") + async def oauth_login(request: fastapi.Request): + """Endpoint that redirects to HF OAuth page.""" + redirect_uri = str(request.url_for("oauth_redirect_callback")) + if ".hf.space" in redirect_uri: + # In Space, FastAPI redirect as http but we want https + redirect_uri = redirect_uri.replace("http://", "https://") + return await oauth.huggingface.authorize_redirect(request, redirect_uri) + + @app.get("/login/callback") + async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse: + """Endpoint that handles the OAuth callback.""" + token = await oauth.huggingface.authorize_access_token(request) + request.session["oauth_profile"] = token["userinfo"] + request.session["oauth_token"] = token + return RedirectResponse("/") + + @app.get("/logout") + async def oauth_logout(request: fastapi.Request) -> RedirectResponse: + """Endpoint that logs out the user (e.g. delete cookie session).""" + request.session.pop("oauth_profile", None) + request.session.pop("oauth_token", None) + # 清除cookie并跳转到首页 + response = RedirectResponse(url="/", status_code=302) + response.delete_cookie(key=f"access-token") + response.delete_cookie(key=f"access-token-unsecure") + return response + + +def _add_mocked_oauth_routes(app: fastapi.FastAPI) -> None: + """Add fake oauth routes if Gradio is run locally and OAuth is enabled. + + Clicking on a gr.LoginButton will have the same behavior as in a Space (i.e. gets redirected in a new tab) but + instead of authenticating with HF, a mocked user profile is added to the session. + """ + + # Define OAuth routes + @app.get("/login/huggingface") + async def oauth_login(request: fastapi.Request): + """Fake endpoint that redirects to HF OAuth page.""" + return RedirectResponse("/login/callback") + + @app.get("/login/callback") + async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse: + """Endpoint that handles the OAuth callback.""" + request.session["oauth_profile"] = MOCKED_OAUTH_TOKEN["userinfo"] + request.session["oauth_token"] = MOCKED_OAUTH_TOKEN + return RedirectResponse("/") + + @app.get("/logout") + async def oauth_logout(request: fastapi.Request) -> RedirectResponse: + """Endpoint that logs out the user (e.g. delete cookie session).""" + request.session.pop("oauth_profile", None) + request.session.pop("oauth_token", None) + # 清除cookie并跳转到首页 + response = RedirectResponse(url="/", status_code=302) + response.delete_cookie(key=f"access-token") + response.delete_cookie(key=f"access-token-unsecure") + return response + + +def reg_patch(): + gradio.oauth._add_mocked_oauth_routes = _add_mocked_oauth_routes + gradio.oauth._add_oauth_routes = _add_oauth_routes + print("覆盖gradio.oauth /logout路由") diff --git a/src/index/79fdc83f6c178f6795bf602d2f8d75df/docs.pkl b/src/index/79fdc83f6c178f6795bf602d2f8d75df/docs.pkl new file mode 100644 index 0000000000000000000000000000000000000000..f64320f2f2453a82b6bc77b0cda7bd3c3ac2fdd3 --- /dev/null +++ b/src/index/79fdc83f6c178f6795bf602d2f8d75df/docs.pkl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b0d1dd0f395614cc860d5003f43828fcdcb6a29e5db039ba892e708b1658cbc +size 747 diff --git a/src/index/79fdc83f6c178f6795bf602d2f8d75df/index.faiss b/src/index/79fdc83f6c178f6795bf602d2f8d75df/index.faiss new file mode 100644 index 0000000000000000000000000000000000000000..3f16a43d23f84d4e2b10701be70d2436b3c2de97 Binary files /dev/null and b/src/index/79fdc83f6c178f6795bf602d2f8d75df/index.faiss differ diff --git a/src/index/79fdc83f6c178f6795bf602d2f8d75df/index.pkl b/src/index/79fdc83f6c178f6795bf602d2f8d75df/index.pkl new file mode 100644 index 0000000000000000000000000000000000000000..92bba28f05b7878c3b8c220b9e6294c209757fb7 --- /dev/null +++ b/src/index/79fdc83f6c178f6795bf602d2f8d75df/index.pkl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:93489114a9478cb5436cc3654e44e53b3593921d82f00bce9f7a8e3080184fbe +size 872 diff --git a/src/index_func.py b/src/index_func.py new file mode 100644 index 0000000000000000000000000000000000000000..d37aa8fc30bc4c8c6f588de7d3e73e83ec121128 --- /dev/null +++ b/src/index_func.py @@ -0,0 +1,226 @@ +import os +import re +from typing import List, Optional, Any + +from langchain.schema import Document +from langchain.text_splitter import RecursiveCharacterTextSplitter +from loguru import logger +from tqdm import tqdm + +from src.config import local_embedding, retrieve_proxy, chunk_overlap, chunk_size, hf_emb_model_name +from src import shared +from src.utils import excel_to_string, get_files_hash, load_pkl, save_pkl + +pwd_path = os.path.abspath(os.path.dirname(__file__)) + + +class ChineseRecursiveTextSplitter(RecursiveCharacterTextSplitter): + """Recursive text splitter for Chinese text. + copy from: https://github.com/chatchat-space/Langchain-Chatchat/tree/master + """ + + def __init__( + self, + separators: Optional[List[str]] = None, + keep_separator: bool = True, + is_separator_regex: bool = True, + **kwargs: Any, + ) -> None: + """Create a new TextSplitter.""" + super().__init__(keep_separator=keep_separator, **kwargs) + self._separators = separators or [ + "\n\n", + "\n", + "。|!|?", + "\.\s|\!\s|\?\s", + ";|;\s", + ",|,\s" + ] + self._is_separator_regex = is_separator_regex + + @staticmethod + def _split_text_with_regex_from_end( + text: str, separator: str, keep_separator: bool + ) -> List[str]: + # Now that we have the separator, split the text + if separator: + if keep_separator: + # The parentheses in the pattern keep the delimiters in the result. + _splits = re.split(f"({separator})", text) + splits = ["".join(i) for i in zip(_splits[0::2], _splits[1::2])] + if len(_splits) % 2 == 1: + splits += _splits[-1:] + else: + splits = re.split(separator, text) + else: + splits = list(text) + return [s for s in splits if s != ""] + + def _split_text(self, text: str, separators: List[str]) -> List[str]: + """Split incoming text and return chunks.""" + final_chunks = [] + # Get appropriate separator to use + separator = separators[-1] + new_separators = [] + for i, _s in enumerate(separators): + _separator = _s if self._is_separator_regex else re.escape(_s) + if _s == "": + separator = _s + break + if re.search(_separator, text): + separator = _s + new_separators = separators[i + 1:] + break + + _separator = separator if self._is_separator_regex else re.escape(separator) + splits = self._split_text_with_regex_from_end(text, _separator, self._keep_separator) + + # Now go merging things, recursively splitting longer texts. + _good_splits = [] + _separator = "" if self._keep_separator else separator + for s in splits: + if self._length_function(s) < self._chunk_size: + _good_splits.append(s) + else: + if _good_splits: + merged_text = self._merge_splits(_good_splits, _separator) + final_chunks.extend(merged_text) + _good_splits = [] + if not new_separators: + final_chunks.append(s) + else: + other_info = self._split_text(s, new_separators) + final_chunks.extend(other_info) + if _good_splits: + merged_text = self._merge_splits(_good_splits, _separator) + final_chunks.extend(merged_text) + return [re.sub(r"\n{2,}", "\n", chunk.strip()) for chunk in final_chunks if chunk.strip() != ""] + + +def get_documents(file_paths): + text_splitter = ChineseRecursiveTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) + + documents = [] + logger.debug("Loading documents...") + logger.debug(f"file_paths: {file_paths}") + for file in file_paths: + filepath = file.name + filename = os.path.basename(filepath) + file_type = os.path.splitext(filename)[1] + logger.info(f"loading file: {filename}") + texts = None + try: + if file_type == ".pdf": + import PyPDF2 + logger.debug("Loading PDF...") + try: + from src.pdf_func import parse_pdf + from src.config import advance_docs + + two_column = advance_docs["pdf"].get("two_column", False) + pdftext = parse_pdf(filepath, two_column).text + except: + pdftext = "" + with open(filepath, "rb") as pdfFileObj: + pdfReader = PyPDF2.PdfReader(pdfFileObj) + for page in tqdm(pdfReader.pages): + pdftext += page.extract_text() + texts = [Document(page_content=pdftext, + metadata={"source": filepath})] + elif file_type == ".docx": + logger.debug("Loading Word...") + from langchain.document_loaders import UnstructuredWordDocumentLoader + loader = UnstructuredWordDocumentLoader(filepath) + texts = loader.load() + elif file_type == ".pptx": + logger.debug("Loading PowerPoint...") + from langchain.document_loaders import UnstructuredPowerPointLoader + loader = UnstructuredPowerPointLoader(filepath) + texts = loader.load() + elif file_type == ".epub": + logger.debug("Loading EPUB...") + from langchain.document_loaders import UnstructuredEPubLoader + loader = UnstructuredEPubLoader(filepath) + texts = loader.load() + elif file_type == ".xlsx": + logger.debug("Loading Excel...") + text_list = excel_to_string(filepath) + texts = [] + for elem in text_list: + texts.append(Document(page_content=elem, + metadata={"source": filepath})) + else: + logger.debug("Loading text file...") + from langchain_community.document_loaders import TextLoader + loader = TextLoader(filepath, "utf8") + texts = loader.load() + logger.debug(f"text size: {len(texts)}, text top3: {texts[:3]}") + except Exception as e: + logger.error(f"Error loading file: {filename}, {e}") + + if texts is not None: + texts = text_splitter.split_documents(texts) + documents.extend(texts) + logger.debug(f"Documents loaded. documents size: {len(documents)}, top3: {documents[:3]}") + return documents + + +def construct_index(api_key, files, load_from_cache_if_possible=True): + from langchain_community.vectorstores import FAISS + from langchain.embeddings.huggingface import HuggingFaceEmbeddings + + if api_key: + os.environ["OPENAI_API_KEY"] = api_key + else: + os.environ["OPENAI_API_KEY"] = "sk-xxxxxxx" + + index_name = get_files_hash(files) + index_dir = os.path.join(pwd_path, 'index') + index_path = os.path.join(index_dir, index_name) + doc_file = os.path.join(index_path, 'docs.pkl') + + if local_embedding: + embeddings = HuggingFaceEmbeddings(model_name=hf_emb_model_name) + else: + from langchain_community.embeddings import OpenAIEmbeddings + if os.environ.get("OPENAI_API_TYPE", "openai") == "openai": + embeddings = OpenAIEmbeddings( + openai_api_base=shared.state.openai_api_base, + openai_api_key=os.environ.get("OPENAI_EMBEDDING_API_KEY", api_key) + ) + else: + embeddings = OpenAIEmbeddings( + deployment=os.environ["AZURE_EMBEDDING_DEPLOYMENT_NAME"], + openai_api_key=os.environ["AZURE_OPENAI_API_KEY"], + model=os.environ["AZURE_EMBEDDING_MODEL_NAME"], + openai_api_base=os.environ["AZURE_OPENAI_API_BASE_URL"], + openai_api_type="azure" + ) + + # 确保索引路径存在 + os.makedirs(index_dir, exist_ok=True) + + if os.path.exists(index_path) and load_from_cache_if_possible: + try: + logger.info("找到了缓存的索引文件,加载中……") + index = FAISS.load_local(index_path, embeddings) + documents = load_pkl(doc_file) + return index, documents + except (FileNotFoundError, RuntimeError) as e: + logger.error(f"加载缓存的索引文件失败,重新构建索引…… 错误: {e}") + + try: + documents = get_documents(files) + logger.info("构建索引中……") + with retrieve_proxy(): + index = FAISS.from_documents(documents, embeddings) + logger.debug("索引构建完成!") + os.makedirs(index_path, exist_ok=True) + index.save_local(index_path) + logger.debug("索引已保存至本地!") + save_pkl(documents, doc_file) + logger.debug("索引文档已保存至本地!") + return index, documents + except Exception as e: + logger.error(f"索引构建失败!错误: {e}") + return None \ No newline at end of file diff --git a/src/llama.py b/src/llama.py new file mode 100644 index 0000000000000000000000000000000000000000..c310feaca9ce9dfb420c06e8865792c4a3c982da --- /dev/null +++ b/src/llama.py @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- +""" +@author:XuMing(xuming624@qq.com) +@description: + +int8 gptq model need: pip install optimum auto-gptq +""" + +from loguru import logger + +from src.base_model import BaseLLMModel +from src.presets import LOCAL_MODELS + + +class LLaMAClient(BaseLLMModel): + def __init__(self, model_name, user_name=""): + super().__init__(model_name=model_name, user=user_name) + from transformers import AutoModelForCausalLM, AutoTokenizer + self.max_generation_token = 1000 + logger.info(f"Loading model from {model_name}") + if model_name in LOCAL_MODELS: + model_path = LOCAL_MODELS[model_name] + else: + model_path = model_name + self.tokenizer = AutoTokenizer.from_pretrained(model_path, legacy=True, use_fast=False) + self.model = AutoModelForCausalLM.from_pretrained(model_path, device_map='auto', torch_dtype='auto').eval() + logger.info(f"Model loaded from {model_path}") + self.stop_str = self.tokenizer.eos_token or "" + + def _get_chat_input(self): + messages = [] + logger.debug(f"{self.history}") + for conv in self.history: + if conv["role"] == "system": + messages.append({'role': 'system', 'content': conv["content"]}) + elif conv["role"] == "user": + messages.append({'role': 'user', 'content': conv["content"]}) + else: + messages.append({'role': 'assistant', 'content': conv["content"]}) + input_ids = self.tokenizer.apply_chat_template( + conversation=messages, + tokenize=True, + add_generation_prompt=True, + return_tensors='pt' + ) + + return input_ids.to(self.model.device) + + def get_answer_at_once(self): + input_ids = self._get_chat_input() + output_ids = self.model.generate( + input_ids, + max_new_tokens=self.max_generation_token, + top_p=self.top_p, + temperature=self.temperature, + ) + response = self.tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True) + + return response, len(response) + + def get_answer_stream_iter(self): + from transformers import TextIteratorStreamer + from threading import Thread + input_ids = self._get_chat_input() + streamer = TextIteratorStreamer( + self.tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True + ) + thread = Thread( + target=self.model.generate, + kwargs={"input_ids": input_ids, + "max_new_tokens": self.max_generation_token, + "top_p": self.top_p, + "temperature": self.temperature, + "streamer": streamer} + ) + thread.start() + generated_text = "" + for new_text in streamer: + stop = False + pos = new_text.find(self.stop_str) + if pos != -1: + new_text = new_text[:pos] + stop = True + generated_text += new_text + yield generated_text + if stop: + break diff --git a/src/models.py b/src/models.py new file mode 100644 index 0000000000000000000000000000000000000000..c6b1e521e8c12639c8e28ee99266e3c3d20726ca --- /dev/null +++ b/src/models.py @@ -0,0 +1,106 @@ +# -*- coding: utf-8 -*- +""" +Get model client from model name +""" + +import json + +import colorama +import gradio as gr +from loguru import logger + +from src import config +from src.base_model import ModelType +from src.utils import ( + hide_middle_chars, + i18n, +) + + +def get_model( + model_name, + lora_model_path=None, + access_key=None, + temperature=None, + top_p=None, + system_prompt=None, + user_name="", + original_model=None, +): + msg = i18n("模型设置为了:") + f" {model_name}" + model_type = ModelType.get_type(model_name) + lora_choices = ["No LoRA"] + if model_type != ModelType.OpenAI: + config.local_embedding = True + model = original_model + chatbot = gr.Chatbot.update(label=model_name) + try: + if model_type == ModelType.OpenAI: + logger.info(f"正在加载OpenAI模型: {model_name}") + from src.openai_client import OpenAIClient + model = OpenAIClient( + model_name=model_name, + api_key=access_key, + system_prompt=system_prompt, + user_name=user_name, + ) + elif model_type == ModelType.OpenAIVision: + logger.info(f"正在加载OpenAI Vision模型: {model_name}") + from src.openai_client import OpenAIVisionClient + model = OpenAIVisionClient(model_name, api_key=access_key, user_name=user_name) + elif model_type == ModelType.ChatGLM: + logger.info(f"正在加载ChatGLM模型: {model_name}") + from src.chatglm import ChatGLMClient + model = ChatGLMClient(model_name, user_name=user_name) + elif model_type == ModelType.LLaMA: + logger.info(f"正在加载LLaMA模型: {model_name}") + from src.llama import LLaMAClient + model = LLaMAClient(model_name, user_name=user_name) + elif model_type == ModelType.ZhipuAI: # todo: fix zhipu bug + logger.info(f"正在加载ZhipuAI模型: {model_name}") + from src.zhipu_client import ZhipuAIClient + model = ZhipuAIClient( + model_name=model_name, + api_key=access_key, + system_prompt=system_prompt, + user_name=user_name, + ) + elif model_type == ModelType.Unknown: + raise ValueError(f"未知模型: {model_name}") + except Exception as e: + logger.error(e) + logger.info(msg) + presudo_key = hide_middle_chars(access_key) + if original_model is not None and model is not None: + model.history = original_model.history + model.history_file_path = original_model.history_file_path + return model, msg, chatbot, gr.Dropdown.update(choices=lora_choices, visible=False), access_key, presudo_key + + +if __name__ == "__main__": + with open("../config.json", "r") as f: + openai_api_key = json.load(f)["openai_api_key"] + print('key:', openai_api_key) + client = get_model(model_name="gpt-3.5-turbo", access_key=openai_api_key)[0] + chatbot = [] + stream = False + # 测试账单功能 + logger.info(colorama.Back.GREEN + "测试账单功能" + colorama.Back.RESET) + logger.info(client.billing_info()) + # 测试问答 + logger.info(colorama.Back.GREEN + "测试问答" + colorama.Back.RESET) + question = "巴黎是中国的首都吗?" + for i in client.predict(inputs=question, chatbot=chatbot, stream=stream): + logger.info(i) + logger.info(f"测试问答后history : {client.history}") + # 测试记忆力 + logger.info(colorama.Back.GREEN + "测试记忆力" + colorama.Back.RESET) + question = "我刚刚问了你什么问题?" + for i in client.predict(inputs=question, chatbot=chatbot, stream=stream): + logger.info(i) + logger.info(f"测试记忆力后history : {client.history}") + # 测试重试功能 + logger.info(colorama.Back.GREEN + "测试重试功能" + colorama.Back.RESET) + for i in client.retry(chatbot=chatbot, stream=stream): + logger.info(i) + logger.info(f"重试后history : {client.history}") diff --git a/src/openai_client.py b/src/openai_client.py new file mode 100644 index 0000000000000000000000000000000000000000..291035b8bb3a69a9551963da47688547e0e37d05 --- /dev/null +++ b/src/openai_client.py @@ -0,0 +1,553 @@ +# -*- coding: utf-8 -*- +""" +@author:XuMing(xuming624@qq.com) +@description: +""" + +import base64 +import datetime +import json +import os +from io import BytesIO + +import gradio as gr +import requests +from PIL import Image +from loguru import logger + +from src import shared, config +from src.base_model import BaseLLMModel +from src.presets import ( + INITIAL_SYSTEM_PROMPT, + TIMEOUT_ALL, + TIMEOUT_STREAMING, + STANDARD_ERROR_MSG, + CONNECTION_TIMEOUT_MSG, + READ_TIMEOUT_MSG, + ERROR_RETRIEVE_MSG, + GENERAL_ERROR_MSG, + CHAT_COMPLETION_URL, + SUMMARY_CHAT_SYSTEM_PROMPT +) +from src.utils import ( + count_token, + construct_system, + construct_user, + get_last_day_of_month, + i18n, + replace_special_symbols, +) + + +def decode_chat_response(response): + try: + error_msg = "" + for chunk in response.iter_lines(): + if chunk: + chunk = chunk.decode() + chunk_length = len(chunk) + try: + chunk = json.loads(chunk[6:]) + except: + logger.error(i18n("JSON解析错误,收到的内容: ") + f"{chunk}") + error_msg += chunk + continue + try: + if chunk_length > 6 and "delta" in chunk["choices"][0]: + if "finish_reason" in chunk["choices"][0]: + finish_reason = chunk["choices"][0]["finish_reason"] + else: + finish_reason = chunk["finish_reason"] + if finish_reason == "stop": + break + try: + yield chunk["choices"][0]["delta"].get("content", "") + except Exception as e: + logger.error(f"Error: {e}") + continue + except Exception as ee: + logger.error(f"ERROR: {chunk}, {ee}") + continue + if error_msg and not error_msg.endswith("[DONE]"): + raise Exception(error_msg) + except GeneratorExit as ge: + raise ValueError(f"GeneratorExit: {ge}") + except Exception as e: + raise Exception(f"Error in generate: {str(e)}") + + +class OpenAIClient(BaseLLMModel): + def __init__( + self, + model_name, + api_key, + system_prompt=INITIAL_SYSTEM_PROMPT, + temperature=1.0, + top_p=1.0, + user_name="", + ) -> None: + super().__init__( + model_name=model_name, + temperature=temperature, + top_p=top_p, + system_prompt=system_prompt, + user=user_name, + ) + self.api_key = api_key + self.need_api_key = True + self._refresh_header() + + def get_answer_stream_iter(self): + if not self.api_key: + raise ValueError("API key is not set") + response = self._get_response(stream=True) + if response is not None: + stream_iter = decode_chat_response(response) + partial_text = "" + for chunk in stream_iter: + partial_text += chunk + yield partial_text + else: + yield STANDARD_ERROR_MSG + GENERAL_ERROR_MSG + + def get_answer_at_once(self): + if not self.api_key: + raise ValueError("API key is not set") + response = self._get_response() + response = json.loads(response.text) + content = response["choices"][0]["message"]["content"] + total_token_count = response["usage"]["total_tokens"] + return content, total_token_count + + def count_token(self, user_input): + input_token_count = count_token(construct_user(user_input)) + if self.system_prompt is not None and len(self.all_token_counts) == 0: + system_prompt_token_count = count_token( + construct_system(self.system_prompt) + ) + return input_token_count + system_prompt_token_count + return input_token_count + + def billing_info(self): + try: + curr_time = datetime.datetime.now() + last_day_of_month = get_last_day_of_month( + curr_time).strftime("%Y-%m-%d") + first_day_of_month = curr_time.replace(day=1).strftime("%Y-%m-%d") + usage_url = f"{shared.state.usage_api_url}?start_date={first_day_of_month}&end_date={last_day_of_month}" + try: + usage_data = self._get_billing_data(usage_url) + except Exception as e: + logger.warning(f"获取API使用情况失败:" + str(e)) + return i18n("**获取API使用情况失败**") + rounded_usage = "{:.5f}".format(usage_data["total_usage"] / 100) + return i18n("**本月使用金额** ") + f"\u3000 ${rounded_usage}" + except requests.exceptions.ConnectTimeout: + status_text = ( + STANDARD_ERROR_MSG + CONNECTION_TIMEOUT_MSG + ERROR_RETRIEVE_MSG + ) + return status_text + except requests.exceptions.ReadTimeout: + status_text = STANDARD_ERROR_MSG + READ_TIMEOUT_MSG + ERROR_RETRIEVE_MSG + return status_text + except Exception as e: + import traceback + traceback.print_exc() + logger.error(i18n("获取API使用情况失败:") + str(e)) + return STANDARD_ERROR_MSG + ERROR_RETRIEVE_MSG + + def set_token_upper_limit(self, new_upper_limit): + pass + + @shared.state.switching_api_key # 在不开启多账号模式的时候,这个装饰器不会起作用 + def _get_response(self, stream=False): + openai_api_key = self.api_key + system_prompt = self.system_prompt + history = self.history + # logger.debug(f"{history}") + headers = { + "Authorization": f"Bearer {openai_api_key}", + "Content-Type": "application/json", + } + + if system_prompt is not None: + history = [construct_system(system_prompt), *history] + + payload = { + "model": self.model_name, + "messages": history, + "temperature": self.temperature, + "top_p": self.top_p, + "n": self.n_choices, + "stream": stream, + "presence_penalty": self.presence_penalty, + "frequency_penalty": self.frequency_penalty, + } + + if self.max_generation_token is not None: + payload["max_tokens"] = self.max_generation_token + if self.stop_sequence is not None: + payload["stop"] = self.stop_sequence + if self.logit_bias is not None: + payload["logit_bias"] = self.logit_bias + if self.user_identifier: + payload["user"] = self.user_identifier + + if stream: + timeout = TIMEOUT_STREAMING + else: + timeout = TIMEOUT_ALL + + # 如果有自定义的api-host,使用自定义host发送请求,否则使用默认设置发送请求 + if shared.state.chat_completion_url != CHAT_COMPLETION_URL: + logger.debug(f"使用自定义API URL: {shared.state.chat_completion_url}") + + with config.retrieve_proxy(): + try: + response = requests.post( + shared.state.chat_completion_url, + headers=headers, + json=payload, + stream=stream, + timeout=timeout, + ) + except Exception as e: + logger.error(f"Error: {e}") + response = None + return response + + def _refresh_header(self): + self.headers = { + "Authorization": f"Bearer {self.api_key}", + "Content-Type": "application/json", + } + + def _get_billing_data(self, billing_url): + with config.retrieve_proxy(): + response = requests.get( + billing_url, + headers=self.headers, + timeout=TIMEOUT_ALL, + ) + + if response.status_code == 200: + data = response.json() + return data + else: + raise Exception( + f"API request failed with status code {response.status_code}: {response.text}" + ) + + def set_key(self, new_access_key): + ret = super().set_key(new_access_key) + self._refresh_header() + return ret + + def _single_query_at_once(self, history, temperature=1.0): + timeout = TIMEOUT_ALL + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {self.api_key}", + "temperature": f"{temperature}", + } + payload = { + "model": self.model_name, + "messages": history, + } + # 如果有自定义的api-host,使用自定义host发送请求,否则使用默认设置发送请求 + if shared.state.chat_completion_url != CHAT_COMPLETION_URL: + logger.debug(f"使用自定义API URL: {shared.state.chat_completion_url}") + + with config.retrieve_proxy(): + response = requests.post( + shared.state.chat_completion_url, + headers=headers, + json=payload, + stream=False, + timeout=timeout, + ) + + return response + + def auto_name_chat_history(self, name_chat_method, user_question, chatbot, single_turn_checkbox): + if len(self.history) == 2 and not single_turn_checkbox and not config.hide_history_when_not_logged_in: + user_question = self.history[0]["content"] + if name_chat_method == i18n("模型自动总结(消耗tokens)"): + ai_answer = self.history[1]["content"] + try: + history = [ + {"role": "system", "content": SUMMARY_CHAT_SYSTEM_PROMPT}, + {"role": "user", + "content": f"Please write a title based on the following conversation:\n---\nUser: {user_question}\nAssistant: {ai_answer}"} + ] + response = self._single_query_at_once(history, temperature=0.0) + response = json.loads(response.text) + content = response["choices"][0]["message"]["content"] + filename = replace_special_symbols(content) + ".json" + except Exception as e: + logger.info(f"自动命名失败。{e}") + filename = replace_special_symbols(user_question)[:16] + ".json" + return self.rename_chat_history(filename, chatbot) + elif name_chat_method == i18n("第一条提问"): + filename = replace_special_symbols(user_question)[:16] + ".json" + return self.rename_chat_history(filename, chatbot) + else: + return gr.update() + else: + return gr.update() + + +class OpenAIVisionClient(BaseLLMModel): + def __init__( + self, + model_name, + api_key, + system_prompt=INITIAL_SYSTEM_PROMPT, + temperature=1.0, + top_p=1.0, + user_name="" + ) -> None: + super().__init__( + model_name=model_name, + temperature=temperature, + top_p=top_p, + system_prompt=system_prompt, + user=user_name + ) + self.api_key = api_key + self.need_api_key = True + self.max_generation_token = 4096 + self.images = [] + self._refresh_header() + + def get_answer_stream_iter(self): + response = self._get_response(stream=True) + if response is not None: + stream_iter = decode_chat_response(response) + partial_text = "" + for chunk in stream_iter: + partial_text += chunk + yield partial_text + else: + yield STANDARD_ERROR_MSG + GENERAL_ERROR_MSG + + def get_answer_at_once(self): + response = self._get_response() + response = json.loads(response.text) + content = response["choices"][0]["message"]["content"] + total_token_count = response["usage"]["total_tokens"] + return content, total_token_count + + def try_read_image(self, filepath): + def is_image_file(filepath): + # 判断文件是否为图片 + valid_image_extensions = [ + ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff"] + file_extension = os.path.splitext(filepath)[1].lower() + return file_extension in valid_image_extensions + + def image_to_base64(image_path): + # 打开并加载图片 + img = Image.open(image_path) + + # 获取图片的宽度和高度 + width, height = img.size + + # 计算压缩比例,以确保最长边小于4096像素 + max_dimension = 2048 + scale_ratio = min(max_dimension / width, max_dimension / height) + + if scale_ratio < 1: + # 按压缩比例调整图片大小 + new_width = int(width * scale_ratio) + new_height = int(height * scale_ratio) + img = img.resize((new_width, new_height), Image.LANCZOS) + + # 将图片转换为jpg格式的二进制数据 + buffer = BytesIO() + if img.mode == "RGBA": + img = img.convert("RGB") + img.save(buffer, format='JPEG') + binary_image = buffer.getvalue() + + # 对二进制数据进行Base64编码 + base64_image = base64.b64encode(binary_image).decode('utf-8') + + return base64_image + + if is_image_file(filepath): + logger.info(f"读取图片文件: {filepath}") + base64_image = image_to_base64(filepath) + self.images.append({ + "path": filepath, + "base64": base64_image, + }) + + def handle_file_upload(self, files, chatbot, language): + """if the model accepts multi modal input, implement this function""" + if files: + for file in files: + if file.name: + self.try_read_image(file.name) + if self.images is not None: + chatbot = chatbot + [([image["path"] for image in self.images], None)] + return None, chatbot, None + + def prepare_inputs(self, real_inputs, use_websearch, files, reply_language, chatbot, + load_from_cache_if_possible=True): + fake_inputs = real_inputs + display_append = "" + limited_context = False + return limited_context, fake_inputs, display_append, real_inputs, chatbot + + def count_token(self, user_input): + input_token_count = count_token(construct_user(user_input)) + if self.system_prompt is not None and len(self.all_token_counts) == 0: + system_prompt_token_count = count_token( + construct_system(self.system_prompt) + ) + return input_token_count + system_prompt_token_count + return input_token_count + + def billing_info(self): + try: + curr_time = datetime.datetime.now() + last_day_of_month = get_last_day_of_month( + curr_time).strftime("%Y-%m-%d") + first_day_of_month = curr_time.replace(day=1).strftime("%Y-%m-%d") + usage_url = f"{shared.state.usage_api_url}?start_date={first_day_of_month}&end_date={last_day_of_month}" + try: + usage_data = self._get_billing_data(usage_url) + except Exception as e: + logger.warning(f"获取API使用情况失败:" + str(e)) + return i18n("**获取API使用情况失败**") + rounded_usage = "{:.5f}".format(usage_data["total_usage"] / 100) + return i18n("**本月使用金额** ") + f"\u3000 ${rounded_usage}" + except requests.exceptions.ConnectTimeout: + status_text = ( + STANDARD_ERROR_MSG + CONNECTION_TIMEOUT_MSG + ERROR_RETRIEVE_MSG + ) + return status_text + except requests.exceptions.ReadTimeout: + status_text = STANDARD_ERROR_MSG + READ_TIMEOUT_MSG + ERROR_RETRIEVE_MSG + return status_text + except Exception as e: + import traceback + traceback.print_exc() + logger.error(i18n("获取API使用情况失败:") + str(e)) + return STANDARD_ERROR_MSG + ERROR_RETRIEVE_MSG + + @shared.state.switching_api_key # 在不开启多账号模式的时候,这个装饰器不会起作用 + def _get_response(self, stream=False): + openai_api_key = self.api_key + system_prompt = self.system_prompt + history = self.history + if self.images: + self.history[-1]["content"] = [ + {"type": "text", "text": self.history[-1]["content"]}, + *[{"type": "image_url", "image_url": "data:image/jpeg;base64," + image["base64"]} for image in + self.images] + ] + self.images = [] + # logger.debug(colorama.Fore.YELLOW + f"{history}" + colorama.Fore.RESET) + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {openai_api_key}", + } + + if system_prompt is not None: + history = [construct_system(system_prompt), *history] + + payload = { + "model": self.model_name, + "messages": history, + "temperature": self.temperature, + "top_p": self.top_p, + "n": self.n_choices, + "stream": stream, + "presence_penalty": self.presence_penalty, + "frequency_penalty": self.frequency_penalty, + "max_tokens": 4096 + } + + if self.stop_sequence is not None: + payload["stop"] = self.stop_sequence + if self.logit_bias is not None: + payload["logit_bias"] = self.encoded_logit_bias() + if self.user_identifier: + payload["user"] = self.user_identifier + + if stream: + timeout = TIMEOUT_STREAMING + else: + timeout = TIMEOUT_ALL + + # 如果有自定义的api-host,使用自定义host发送请求,否则使用默认设置发送请求 + if shared.state.chat_completion_url != CHAT_COMPLETION_URL: + logger.debug(f"使用自定义API URL: {shared.state.chat_completion_url}") + + with config.retrieve_proxy(): + try: + response = requests.post( + shared.state.chat_completion_url, + headers=headers, + json=payload, + stream=stream, + timeout=timeout, + ) + except: + return None + return response + + def _refresh_header(self): + self.headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {self.api_key}", + } + + def _get_billing_data(self, billing_url): + with config.retrieve_proxy(): + response = requests.get( + billing_url, + headers=self.headers, + timeout=TIMEOUT_ALL, + ) + + if response.status_code == 200: + data = response.json() + return data + else: + raise Exception( + f"API request failed with status code {response.status_code}: {response.text}" + ) + + def set_key(self, new_access_key): + ret = super().set_key(new_access_key) + self._refresh_header() + return ret + + def _single_query_at_once(self, history, temperature=1.0): + timeout = TIMEOUT_ALL + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {self.api_key}", + "temperature": f"{temperature}", + } + payload = { + "model": self.model_name, + "messages": history, + } + # 如果有自定义的api-host,使用自定义host发送请求,否则使用默认设置发送请求 + if shared.state.chat_completion_url != CHAT_COMPLETION_URL: + logger.debug(f"使用自定义API URL: {shared.state.chat_completion_url}") + + with config.retrieve_proxy(): + response = requests.post( + shared.state.chat_completion_url, + headers=headers, + json=payload, + stream=False, + timeout=timeout, + ) + + return response diff --git a/src/overwrites.py b/src/overwrites.py new file mode 100644 index 0000000000000000000000000000000000000000..ed97703b299ce647d6e177b34a302feffc5dc1b5 --- /dev/null +++ b/src/overwrites.py @@ -0,0 +1,180 @@ +import os +from collections import namedtuple + +import gradio as gr +from gradio.utils import validate_url +from gradio_client import utils as client_utils + +from src.presets import chuanhu_path, assets_path +from src.utils import convert_bot_before_marked, convert_user_before_marked + + +def postprocess( + self, + y, +): + """ + Parameters: + y: List of lists representing the message and response pairs. Each message and response should be a string, which may be in Markdown format. It can also be a tuple whose first element is a string filepath or URL to an image/video/audio, and second (optional) element is the alt text, in which case the media file is displayed. It can also be None, in which case that message is not displayed. + Returns: + List of lists representing the message and response. Each message and response will be a string of HTML, or a dictionary with media information. Or None if the message is not to be displayed. + """ + if y is None: + return [] + processed_messages = [] + for message_pair in y: + assert isinstance( + message_pair, (tuple, list) + ), f"Expected a list of lists or list of tuples. Received: {message_pair}" + assert ( + len(message_pair) == 2 + ), f"Expected a list of lists of length 2 or list of tuples of length 2. Received: {message_pair}" + + processed_messages.append( + [ + self._postprocess_chat_messages(message_pair[0], "user"), + self._postprocess_chat_messages(message_pair[1], "bot"), + ] + ) + return processed_messages + + +def postprocess_chat_messages( + self, chat_message, role: str +): + if chat_message is None: + return None + elif isinstance(chat_message, (tuple, list)): + file_uri = chat_message[0] + if validate_url(file_uri): + filepath = file_uri + else: + filepath = self.make_temp_copy_if_needed(file_uri) + + mime_type = client_utils.get_mimetype(filepath) + return { + "name": filepath, + "mime_type": mime_type, + "alt_text": chat_message[1] if len(chat_message) > 1 else None, + "data": None, # These last two fields are filled in by the frontend + "is_file": True, + } + elif isinstance(chat_message, str): + # chat_message = inspect.cleandoc(chat_message) + # escape html spaces + # chat_message = chat_message.replace(" ", " ") + if role == "bot": + chat_message = convert_bot_before_marked(chat_message) + elif role == "user": + chat_message = convert_user_before_marked(chat_message) + return chat_message + else: + raise ValueError(f"Invalid message for Chatbot component: {chat_message}") + + +def add_classes_to_gradio_component(comp): + """ + this adds gradio-* to the component for css styling (ie gradio-button to gr.Button), as well as some others + code from stable-diffusion-webui + """ + + comp.elem_classes = [f"gradio-{comp.get_block_name()}", *(comp.elem_classes or [])] + + if getattr(comp, 'multiselect', False): + comp.elem_classes.append('multiselect') + + +def IOComponent_init(self, *args, **kwargs): + res = original_IOComponent_init(self, *args, **kwargs) + add_classes_to_gradio_component(self) + + return res + + +original_IOComponent_init = gr.components.IOComponent.__init__ +gr.components.IOComponent.__init__ = IOComponent_init + + +def BlockContext_init(self, *args, **kwargs): + res = original_BlockContext_init(self, *args, **kwargs) + add_classes_to_gradio_component(self) + + return res + + +original_BlockContext_init = gr.blocks.BlockContext.__init__ +gr.blocks.BlockContext.__init__ = BlockContext_init + + +def get_html(filename): + path = os.path.join(chuanhu_path, "assets", "html", filename) + if os.path.exists(path): + with open(path, encoding="utf8") as file: + return file.read() + return "" + + +def webpath(fn): + if fn.startswith(assets_path): + web_path = os.path.relpath(fn, chuanhu_path).replace('\\', '/') + else: + web_path = os.path.abspath(fn) + return f'file={web_path}?{os.path.getmtime(fn)}' + + +ScriptFile = namedtuple("ScriptFile", ["basedir", "filename", "path"]) + + +def list_scripts(scriptdirname, extension): + scripts_list = [] + scripts_dir = os.path.join(chuanhu_path, "assets", scriptdirname) + if os.path.exists(scripts_dir): + for filename in sorted(os.listdir(scripts_dir)): + scripts_list.append(ScriptFile(assets_path, filename, os.path.join(scripts_dir, filename))) + scripts_list = [x for x in scripts_list if + os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)] + return scripts_list + + +def javascript_html(): + head = "" + for script in list_scripts("javascript", ".js"): + head += f'\n' + for script in list_scripts("javascript", ".mjs"): + head += f'\n' + return head + + +def css_html(): + head = "" + for cssfile in list_scripts("stylesheet", ".css"): + head += f'' + return head + + +def reload_javascript(): + js = javascript_html() + js += '' + js += '' + js += '' + + meta = """ + + + + + + """ + css = css_html() + + def template_response(*args, **kwargs): + res = GradioTemplateResponseOriginal(*args, **kwargs) + res.body = res.body.replace(b'', f'{meta}{js}'.encode("utf8")) + res.body = res.body.replace(b'', f'{css}'.encode("utf8")) + res.init_headers() + return res + + gr.routes.templates.TemplateResponse = template_response + + +GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse diff --git a/src/pdf_func.py b/src/pdf_func.py new file mode 100644 index 0000000000000000000000000000000000000000..8f37dea88071e0073692fb19202dd773937bf520 --- /dev/null +++ b/src/pdf_func.py @@ -0,0 +1,163 @@ +from types import SimpleNamespace + +import pdfplumber +from langchain.docstore.document import Document + + +def prepare_table_config(crop_page): + """Prepare table查找边界, 要求page为原始page + + From https://github.com/jsvine/pdfplumber/issues/242 + """ + page = crop_page.root_page # root/parent + cs = page.curves + page.edges + + def curves_to_edges(): + """See https://github.com/jsvine/pdfplumber/issues/127""" + edges = [] + for c in cs: + edges += pdfplumber.utils.rect_to_edges(c) + return edges + + edges = curves_to_edges() + return { + "vertical_strategy": "explicit", + "horizontal_strategy": "explicit", + "explicit_vertical_lines": edges, + "explicit_horizontal_lines": edges, + "intersection_y_tolerance": 10, + } + + +def get_text_outside_table(crop_page): + ts = prepare_table_config(crop_page) + if len(ts["explicit_vertical_lines"]) == 0 or len(ts["explicit_horizontal_lines"]) == 0: + return crop_page + + ### Get the bounding boxes of the tables on the page. + bboxes = [table.bbox for table in crop_page.root_page.find_tables(table_settings=ts)] + + def not_within_bboxes(obj): + """Check if the object is in any of the table's bbox.""" + + def obj_in_bbox(_bbox): + """See https://github.com/jsvine/pdfplumber/blob/stable/pdfplumber/table.py#L404""" + v_mid = (obj["top"] + obj["bottom"]) / 2 + h_mid = (obj["x0"] + obj["x1"]) / 2 + x0, top, x1, bottom = _bbox + return (h_mid >= x0) and (h_mid < x1) and (v_mid >= top) and (v_mid < bottom) + + return not any(obj_in_bbox(__bbox) for __bbox in bboxes) + + return crop_page.filter(not_within_bboxes) + + +# 请使用 LaTeX 表达公式,行内公式以 $ 包裹,行间公式以 $$ 包裹 + +extract_words = lambda page: page.extract_words(keep_blank_chars=True, y_tolerance=0, x_tolerance=1, + extra_attrs=["fontname", "size", "object_type"]) + + +# dict_keys(['text', 'x0', 'x1', 'top', 'doctop', 'bottom', 'upright', 'direction', 'fontname', 'size']) + +def get_title_with_cropped_page(first_page): + title = [] # 处理标题 + x0, top, x1, bottom = first_page.bbox # 获取页面边框 + + for word in extract_words(first_page): + word = SimpleNamespace(**word) + + if word.size >= 14: + title.append(word.text) + title_bottom = word.bottom + elif word.text == "Abstract": # 获取页面abstract + top = word.top + + user_info = [i["text"] for i in extract_words(first_page.within_bbox((x0, title_bottom, x1, top)))] + # 裁剪掉上半部分, within_bbox: full_included; crop: partial_included + return title, user_info, first_page.within_bbox((x0, top, x1, bottom)) + + +def get_column_cropped_pages(pages, two_column=True): + new_pages = [] + for page in pages: + if two_column: + left = page.within_bbox((0, 0, page.width / 2, page.height), relative=True) + right = page.within_bbox((page.width / 2, 0, page.width, page.height), relative=True) + new_pages.append(left) + new_pages.append(right) + else: + new_pages.append(page) + + return new_pages + + +def parse_pdf(filename, two_column=True): + with pdfplumber.open(filename) as pdf: + title, user_info, first_page = get_title_with_cropped_page(pdf.pages[0]) + new_pages = get_column_cropped_pages([first_page] + pdf.pages[1:], two_column) + + chapters = [] + # tuple (chapter_name, [pageid] (start,stop), chapter_text) + create_chapter = lambda page_start, name_top, name_bottom: SimpleNamespace( + name=[], + name_top=name_top, + name_bottom=name_bottom, + record_chapter_name=True, + + page_start=page_start, + page_stop=None, + + text=[], + ) + cur_chapter = None + + # 按页遍历PDF文档 + for idx, page in enumerate(new_pages): + page = get_text_outside_table(page) + + # 按行遍历页面文本 + for word in extract_words(page): + word = SimpleNamespace(**word) + + # 检查行文本是否以12号字体打印,如果是,则将其作为新章节开始 + if word.size >= 11: # 出现chapter name + if cur_chapter is None: + cur_chapter = create_chapter(page.page_number, word.top, word.bottom) + elif not cur_chapter.record_chapter_name or ( + cur_chapter.name_bottom != cur_chapter.name_bottom and cur_chapter.name_top != cur_chapter.name_top): + # 不再继续写chapter name + cur_chapter.page_stop = page.page_number # stop id + chapters.append(cur_chapter) + # 重置当前chapter信息 + cur_chapter = create_chapter(page.page_number, word.top, word.bottom) + + # print(word.size, word.top, word.bottom, word.text) + cur_chapter.name.append(word.text) + else: + cur_chapter.record_chapter_name = False # chapter name 结束 + cur_chapter.text.append(word.text) + else: + # 处理最后一个章节 + cur_chapter.page_stop = page.page_number # stop id + chapters.append(cur_chapter) + + for i in chapters: + print(f"section: {i.name} pages:{i.page_start, i.page_stop} word-count:{len(i.text)}") + print(" ".join(i.text)) + + title = " ".join(title) + user_info = " ".join(user_info) + text = f"Article Title: {title}, Information:{user_info}\n" + for idx, chapter in enumerate(chapters): + chapter.name = " ".join(chapter.name) + text += f"The {idx}th Chapter {chapter.name}: " + " ".join(chapter.text) + "\n" + + return Document(page_content=text, metadata={"title": title}) + + +if __name__ == '__main__': + # Test code + z = parse_pdf("./build/test.pdf") + print(z["user_info"]) + print(z["title"]) diff --git a/src/presets.py b/src/presets.py new file mode 100644 index 0000000000000000000000000000000000000000..f1ee1e258258273867d2388aeb214492929819ae --- /dev/null +++ b/src/presets.py @@ -0,0 +1,281 @@ +# -*- coding:utf-8 -*- + +import locale +import os + +import commentjson as json +import gradio as gr +from config.config import config + +pwd_path = os.path.abspath(os.path.dirname(__file__)) + + +class I18nAuto: + def __init__(self): + language = os.environ.get("LANGUAGE", "auto") + if language == "auto": + language = locale.getdefaultlocale()[0] # get the language code of the system (e.g. zh_CN) + self.language_map = {} + file_path = os.path.join(pwd_path, f'../locale/{language}.json') + self.file_is_exists = os.path.isfile(file_path) + if self.file_is_exists: + with open(file_path, "r", encoding="utf-8") as f: + self.language_map.update(json.load(f)) + + def __call__(self, key): + if self.file_is_exists and key in self.language_map: + return self.language_map[key] + else: + return key + + +i18n = I18nAuto() # internationalization + +# ChatGPT 设置 +# INITIAL_SYSTEM_PROMPT = "You are a helpful assistant." +POETRY_THEME_INFO = """静夜思 +唐·李白 +床前明月光,疑是地上霜。 +举头望明月,低头思故乡。""" +# INITIAL_SYSTEM_PROMPT = config.default_system_prompt +INITIAL_SYSTEM_PROMPT = config.child_system_prompt +CHILD_SYSTEM_PROMPT = config.child_system_prompt +STUDENT_SYSTEM_PROMPT = config.student_system_prompt + +API_HOST = "api.openai.com" +OPENAI_API_BASE = "https://api.openai.com/v1" +CHAT_COMPLETION_URL = "https://api.openai.com/v1/chat/completions" +IMAGES_COMPLETION_URL = "https://api.openai.com/v1/images/generations" +COMPLETION_URL = "https://api.openai.com/v1/completions" +BALANCE_API_URL = "https://api.openai.com/dashboard/billing/credit_grants" +USAGE_API_URL = "https://api.openai.com/dashboard/billing/usage" +HISTORY_DIR = os.path.join(pwd_path, '../history') +TEMPLATES_DIR = os.path.join(pwd_path, '../templates') + +# assert文件 +chuanhu_path = os.path.dirname(pwd_path) +assets_path = os.path.join(pwd_path, "../assets") +favicon_path = os.path.join(pwd_path, "../assets/favicon.ico") + +# 错误信息 +STANDARD_ERROR_MSG = i18n("☹️发生了错误:") # 错误信息的标准前缀 +GENERAL_ERROR_MSG = i18n("获取对话时发生错误,请重试") +GENERATE_ERROR_MSG = i18n("生成时发生错误,请重试") +ERROR_RETRIEVE_MSG = i18n("请检查网络连接,或者API-Key是否有效。") +CONNECTION_TIMEOUT_MSG = i18n("连接超时,无法获取对话。") # 连接超时 +READ_TIMEOUT_MSG = i18n("读取超时,无法获取对话。") # 读取超时 +PROXY_ERROR_MSG = i18n("代理错误,无法获取对话。") # 代理错误 +SSL_ERROR_PROMPT = i18n("SSL错误,无法获取对话。") # SSL 错误 +NO_APIKEY_MSG = i18n("API key为空,请检查是否输入正确。") # API key 长度不足 51 位 +NO_INPUT_MSG = i18n("请输入对话内容。") # 未输入对话内容 +BILLING_NOT_APPLICABLE_MSG = i18n("账单信息不适用") # 本地运行的模型返回的账单信息 + +TIMEOUT_STREAMING = 60 # 流式对话时的超时时间 +TIMEOUT_ALL = 120 # 非流式对话时的超时时间 +ENABLE_STREAMING_OPTION = True # 是否启用选择选择是否实时显示回答的勾选框 +HIDE_MY_KEY = True # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True +CONCURRENT_COUNT = 100 # 允许同时使用的用户数量 + +SIM_K = 5 +INDEX_QUERY_TEMPRATURE = 1.0 + +TITLE = i18n("PoetryChat 📝") + +DESCRIPTION = i18n("GitHub: [Antony-Zhang/PoetryChat](https://github.com/Antony-Zhang/PoetryChat)") + +ONLINE_MODELS = [ + "gpt-3.5-turbo", + "gpt-3.5-turbo-16k", + "gpt-3.5-turbo-1106", + "gpt-4", + "gpt-4-32k", + "gpt-4-1106-preview", + "gpt-4-turbo-preview", + "gpt-4-vision-preview", + + # zhipuai chatglm + "glm-3-turbo", + "glm4" + +] + +# 定义实际模型名称和别名的字典 +MODEL_ALIASES = { + # "gpt-3.5-turbo": "AppBuilder-SDK", + "gpt-3.5-turbo": "gpt-3.5-turbo", + "gpt-3.5-turbo-16k": "gpt-3.5-turbo-16k", + "gpt-3.5-turbo-1106": "gpt-3.5-turbo-1106", + "gpt-4": "gpt-4", + "gpt-4-32k": "gpt-4-32k", + "gpt-4-1106-preview": "gpt-4-1106-preview", + "gpt-4-turbo-preview": "gpt-4-turbo-preview", + "gpt-4-vision-preview": "gpt-4-vision-preview", +} + +MODEL_TOKEN_LIMIT = { + "gpt-3.5-turbo": 4096, + "gpt-3.5-turbo-16k": 16384, + "gpt-3.5-turbo-1106": 16384, + "gpt-4": 8192, + "gpt-4-32k": 32768, + "gpt-4-1106-preview": 128000, + "gpt-4-turbo-preview": 128000, + "gpt-4-vision-preview": 128000, +} + +LOCAL_MODELS = { + "chatglm3-6b": "THUDM/chatglm3-6b", + "llama-2-7b-chat": "TheBloke/Llama-2-7B-Chat-GPTQ", + "yi-6b-chat-8bits": "01-ai/Yi-6B-Chat-8bits", + "yi-6b-chat": "01-ai/Yi-6B-Chat", +} + +MODELS = ONLINE_MODELS + list(LOCAL_MODELS.keys()) +DEFAULT_MODEL = 0 + +os.makedirs(HISTORY_DIR, exist_ok=True) + +TOKEN_OFFSET = 1000 # 模型的token上限减去这个值,得到软上限。到达软上限之后,自动尝试减少token占用。 +DEFAULT_TOKEN_LIMIT = 3000 # 默认的token上限 +REDUCE_TOKEN_FACTOR = 0.5 # 与模型token上限想乘,得到目标token数。减少token占用时,将token占用减少到目标token数以下。 + +REPLY_LANGUAGES = [ + "简体中文", + "繁體中文", + "English", + "日本語", + "Español", + "Français", + "Deutsch", + "跟随问题语言(不稳定)" +] + +HISTORY_NAME_METHODS = [ + i18n("根据日期时间"), + i18n("第一条提问"), + i18n("模型自动总结(消耗tokens)"), +] +WEBSEARCH_PTOMPT_TEMPLATE = """\ +Web search results: + +{web_results} +Current date: {current_date} + +Instructions: Using the provided web search results, write a comprehensive reply to the given query. Make sure to cite results using [citation:x] notation after the reference, where x is a number. If the provided search results refer to multiple subjects with the same name, write separate answers for each subject. +Query: {query} +Reply in {reply_language} +""" + +PROMPT_TEMPLATE = """\ +Context information is below. +--------------------- +{context_str} +--------------------- +Current date: {current_date}. +Using the provided context information, write a comprehensive reply to the given query. +Make sure to cite results using [number] notation after the reference. +If the provided context information refer to multiple subjects with the same name, write separate answers for each subject. +Use prior knowledge only if the given context didn't provide enough information. +Answer the question: {query_str} +Reply in {reply_language} +""" + +REFINE_TEMPLATE = """\ +The original question is as follows: {query_str} +We have provided an existing answer: {existing_answer} +We have the opportunity to refine the existing answer +(only if needed) with some more context below. +------------ +{context_msg} +------------ +Given the new context, refine the original answer to better +Reply in {reply_language} +If the context isn't useful, return the original answer. +""" + +SUMMARIZE_PROMPT = """Write a concise summary of the following: + +{text} + +CONCISE SUMMARY IN 中文:""" + +SUMMARY_CHAT_SYSTEM_PROMPT = """\ +Please summarize the following conversation for a chat topic. +No more than 16 characters. +No special characters. +Punctuation mark is banned. +Not including '.' ':' '?' '!' '“' '*' '<' '>'. +Reply in user's language. +""" + +ALREADY_CONVERTED_MARK = "" +START_OF_OUTPUT_MARK = "" +END_OF_OUTPUT_MARK = "" + +small_and_beautiful_theme = gr.themes.Soft( + primary_hue=gr.themes.Color( + c50="#EBFAF2", + c100="#CFF3E1", + c200="#A8EAC8", + c300="#77DEA9", + c400="#3FD086", + c500="#02C160", + c600="#06AE56", + c700="#05974E", + c800="#057F45", + c900="#04673D", + c950="#2E5541", + name="small_and_beautiful", + ), + secondary_hue=gr.themes.Color( + c50="#576b95", + c100="#576b95", + c200="#576b95", + c300="#576b95", + c400="#576b95", + c500="#576b95", + c600="#576b95", + c700="#576b95", + c800="#576b95", + c900="#576b95", + c950="#576b95", + ), + neutral_hue=gr.themes.Color( + name="gray", + c50="#f6f7f8", + # c100="#f3f4f6", + c100="#F2F2F2", + c200="#e5e7eb", + c300="#d1d5db", + c400="#B2B2B2", + c500="#808080", + c600="#636363", + c700="#515151", + c800="#393939", + # c900="#272727", + c900="#2B2B2B", + c950="#171717", + ), + radius_size=gr.themes.sizes.radius_sm, +).set( + # button_primary_background_fill="*primary_500", + button_primary_background_fill_dark="*primary_600", + # button_primary_background_fill_hover="*primary_400", + # button_primary_border_color="*primary_500", + button_primary_border_color_dark="*primary_600", + button_primary_text_color="wihte", + button_primary_text_color_dark="white", + button_secondary_background_fill="*neutral_100", + button_secondary_background_fill_hover="*neutral_50", + button_secondary_background_fill_dark="*neutral_900", + button_secondary_text_color="*neutral_800", + button_secondary_text_color_dark="white", + # background_fill_primary="#F7F7F7", + # background_fill_primary_dark="#1F1F1F", + # block_title_text_color="*primary_500", + block_title_background_fill_dark="*primary_900", + block_label_background_fill_dark="*primary_900", + input_background_fill="#F6F6F6", + # chatbot_code_background_color="*neutral_950", + chatbot_code_background_color_dark="*neutral_950", +) diff --git a/src/search_engine.py b/src/search_engine.py new file mode 100644 index 0000000000000000000000000000000000000000..855c71d48a6c17c55982e10e55c7dd9848894167 --- /dev/null +++ b/src/search_engine.py @@ -0,0 +1,241 @@ +# -*- coding: utf-8 -*- +""" +@author:XuMing(xuming624@qq.com) +@description: +""" +import json +from itertools import islice + +import requests +from fastapi import HTTPException +from loguru import logger + +# Search engine related. You don't really need to change this. +BING_SEARCH_V7_ENDPOINT = "https://api.bing.microsoft.com/v7.0/search" +BING_MKT = "en-US" +GOOGLE_SEARCH_ENDPOINT = "https://customsearch.googleapis.com/customsearch/v1" +SERPER_SEARCH_ENDPOINT = "https://google.serper.dev/search" +SEARCHAPI_SEARCH_ENDPOINT = "https://www.searchapi.io/api/v1/search" +# Specify the number of references from the search engine you want to use. +# 8 is usually a good number. +REFERENCE_COUNT = 8 + +# Specify the default timeout for the search engine. If the search engine +# does not respond within this time, we will return an error. +DEFAULT_SEARCH_ENGINE_TIMEOUT = 5 + + +def search_with_bing(query: str, subscription_key: str): + """ + Search with bing and return the contexts. + """ + params = {"q": query, "mkt": BING_MKT} + response = requests.get( + BING_SEARCH_V7_ENDPOINT, + headers={"Ocp-Apim-Subscription-Key": subscription_key}, + params=params, + timeout=DEFAULT_SEARCH_ENGINE_TIMEOUT, + ) + if not response.ok: + logger.error(f"{response.status_code} {response.text}") + raise HTTPException(response.status_code, "Search engine error.") + json_content = response.json() + try: + contexts = json_content["webPages"]["value"][:REFERENCE_COUNT] + except KeyError: + logger.error(f"Error encountered: {json_content}") + return [] + return contexts + + +def search_with_google(query: str, subscription_key: str, cx: str): + """ + Search with google and return the contexts. + """ + params = { + "key": subscription_key, + "cx": cx, + "q": query, + "num": REFERENCE_COUNT, + } + response = requests.get( + GOOGLE_SEARCH_ENDPOINT, params=params, timeout=DEFAULT_SEARCH_ENGINE_TIMEOUT + ) + if not response.ok: + logger.error(f"{response.status_code} {response.text}") + raise HTTPException(response.status_code, "Search engine error.") + json_content = response.json() + try: + contexts = json_content["items"][:REFERENCE_COUNT] + except KeyError: + logger.error(f"Error encountered: {json_content}") + return [] + return contexts + + +def search_with_serper(query: str, subscription_key: str): + """ + Search with serper and return the contexts. + """ + payload = json.dumps({ + "q": query, + "num": ( + REFERENCE_COUNT + if REFERENCE_COUNT % 10 == 0 + else (REFERENCE_COUNT // 10 + 1) * 10 + ), + }) + headers = {"X-API-KEY": subscription_key, "Content-Type": "application/json"} + logger.info( + f"{payload} {headers} {subscription_key} {query} {SERPER_SEARCH_ENDPOINT}" + ) + response = requests.post( + SERPER_SEARCH_ENDPOINT, + headers=headers, + data=payload, + timeout=DEFAULT_SEARCH_ENGINE_TIMEOUT, + ) + if not response.ok: + logger.error(f"{response.status_code} {response.text}") + raise HTTPException(response.status_code, "Search engine error.") + json_content = response.json() + try: + # convert to the same format as bing/google + contexts = [] + if json_content.get("knowledgeGraph"): + url = json_content["knowledgeGraph"].get("descriptionUrl") or json_content["knowledgeGraph"].get("website") + snippet = json_content["knowledgeGraph"].get("description") + if url and snippet: + contexts.append({ + "name": json_content["knowledgeGraph"].get("title", ""), + "url": url, + "snippet": snippet + }) + if json_content.get("answerBox"): + url = json_content["answerBox"].get("url") + snippet = json_content["answerBox"].get("snippet") or json_content["answerBox"].get("answer") + if url and snippet: + contexts.append({ + "name": json_content["answerBox"].get("title", ""), + "url": url, + "snippet": snippet + }) + contexts += [ + {"name": c["title"], "url": c["link"], "snippet": c.get("snippet", "")} + for c in json_content["organic"] + ] + return contexts[:REFERENCE_COUNT] + except KeyError: + logger.error(f"Error encountered: {json_content}") + return [] + + +def search_with_searchapi(query: str, subscription_key: str): + """ + Search with SearchApi.io and return the contexts. + """ + payload = { + "q": query, + "engine": "google", + "num": ( + REFERENCE_COUNT + if REFERENCE_COUNT % 10 == 0 + else (REFERENCE_COUNT // 10 + 1) * 10 + ), + } + headers = {"Authorization": f"Bearer {subscription_key}", "Content-Type": "application/json"} + logger.info( + f"{payload} {headers} {subscription_key} {query} {SEARCHAPI_SEARCH_ENDPOINT}" + ) + response = requests.get( + SEARCHAPI_SEARCH_ENDPOINT, + headers=headers, + params=payload, + timeout=30, + ) + if not response.ok: + logger.error(f"{response.status_code} {response.text}") + raise HTTPException(response.status_code, "Search engine error.") + json_content = response.json() + try: + # convert to the same format as bing/google + contexts = [] + + if json_content.get("answer_box"): + if json_content["answer_box"].get("organic_result"): + title = json_content["answer_box"].get("organic_result").get("title", "") + url = json_content["answer_box"].get("organic_result").get("link", "") + if json_content["answer_box"].get("type") == "population_graph": + title = json_content["answer_box"].get("place", "") + url = json_content["answer_box"].get("explore_more_link", "") + + title = json_content["answer_box"].get("title", "") + url = json_content["answer_box"].get("link") + snippet = json_content["answer_box"].get("answer") or json_content["answer_box"].get("snippet") + + if url and snippet: + contexts.append({ + "name": title, + "url": url, + "snippet": snippet + }) + + if json_content.get("knowledge_graph"): + if json_content["knowledge_graph"].get("source"): + url = json_content["knowledge_graph"].get("source").get("link", "") + + url = json_content["knowledge_graph"].get("website", "") + snippet = json_content["knowledge_graph"].get("description") + + if url and snippet: + contexts.append({ + "name": json_content["knowledge_graph"].get("title", ""), + "url": url, + "snippet": snippet + }) + + contexts += [ + {"name": c["title"], "url": c["link"], "snippet": c.get("snippet", "")} + for c in json_content["organic_results"] + ] + + if json_content.get("related_questions"): + for question in json_content["related_questions"]: + if question.get("source"): + url = question.get("source").get("link", "") + else: + url = "" + + snippet = question.get("answer", "") + + if url and snippet: + contexts.append({ + "name": question.get("question", ""), + "url": url, + "snippet": snippet + }) + + return contexts[:REFERENCE_COUNT] + except KeyError: + logger.error(f"Error encountered: {json_content}") + return [] + + +def search_with_duckduckgo(query: str): + """ + Search with DuckDuckGo and return the contexts. + """ + try: + from duckduckgo_search import DDGS + except ImportError: + raise ImportError("Please install duckduckgo-search to use this search engine.") + contexts = [] + with DDGS() as ddgs: + ddgs_gen = ddgs.text(query, backend="lite") + for r in islice(ddgs_gen, REFERENCE_COUNT): + contexts.append({ + "name": r['title'], + "url": r['href'], + "snippet": r['body'] + }) + return contexts diff --git a/src/shared.py b/src/shared.py new file mode 100644 index 0000000000000000000000000000000000000000..2440439dc8a9691f03e7b514cad1de805e71f96a --- /dev/null +++ b/src/shared.py @@ -0,0 +1,65 @@ +import os +import queue + +from src.presets import OPENAI_API_BASE, CHAT_COMPLETION_URL, BALANCE_API_URL, USAGE_API_URL, API_HOST, IMAGES_COMPLETION_URL + + +class State: + interrupted = False + multi_api_key = False + chat_completion_url = CHAT_COMPLETION_URL + balance_api_url = BALANCE_API_URL + usage_api_url = USAGE_API_URL + openai_api_base = OPENAI_API_BASE + images_completion_url = IMAGES_COMPLETION_URL + + def interrupt(self): + self.interrupted = True + + def recover(self): + self.interrupted = False + + def set_api_host(self, api_host: str): + api_host = api_host.rstrip("/") + if not api_host.startswith("http"): + api_host = f"https://{api_host}" + if api_host.endswith("/v1"): + api_host = api_host[:-3] + self.chat_completion_url = f"{api_host}/v1/chat/completions" + self.images_completion_url = f"{api_host}/v1/images/generations" + self.openai_api_base = f"{api_host}/v1" + self.balance_api_url = f"{api_host}/dashboard/billing/credit_grants" + self.usage_api_url = f"{api_host}/dashboard/billing/usage" + + def reset_api_host(self): + self.chat_completion_url = CHAT_COMPLETION_URL + self.images_completion_url = IMAGES_COMPLETION_URL + self.balance_api_url = BALANCE_API_URL + self.usage_api_url = USAGE_API_URL + return API_HOST + + def reset_all(self): + self.interrupted = False + self.chat_completion_url = CHAT_COMPLETION_URL + + def set_api_key_queue(self, api_key_list): + self.multi_api_key = True + self.api_key_queue = queue.Queue() + for api_key in api_key_list: + self.api_key_queue.put(api_key) + + def switching_api_key(self, func): + if not hasattr(self, "api_key_queue"): + return func + + def wrapped(*args, **kwargs): + api_key = self.api_key_queue.get() + args[0].api_key = api_key + ret = func(*args, **kwargs) + self.api_key_queue.put(api_key) + return ret + + return wrapped + + +state = State() diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..ca0ad3f94d67313e0fbd95e6e0de29331050b176 --- /dev/null +++ b/src/utils.py @@ -0,0 +1,1385 @@ +# -*- coding:utf-8 -*- + +import csv +import datetime +import getpass +import hashlib +import html +import json +import os +import pickle +import re +import threading +from enum import Enum +from typing import List, Union +from typing import TYPE_CHECKING + +import colorama +import gradio as gr +import pandas as pd +import requests +import tiktoken +from loguru import logger +from markdown import markdown +from pygments import highlight +from pygments.formatters import HtmlFormatter +from pygments.lexers import get_lexer_by_name +from pypinyin import lazy_pinyin + +from src.config import retrieve_proxy, hide_history_when_not_logged_in, config_file +from src.presets import ALREADY_CONVERTED_MARK, HISTORY_DIR, TEMPLATES_DIR, i18n, LOCAL_MODELS, ONLINE_MODELS +from src.shared import state + +if TYPE_CHECKING: + from typing import TypedDict + + + class DataframeData(TypedDict): + headers: List[str] + data: List[List[Union[str, int, bool]]] + + +def predict(current_model, *args): + if current_model: + iter = current_model.predict(*args) + for i in iter: + yield i + + +def billing_info(current_model): + if current_model: + return current_model.billing_info() + + +def set_key(current_model, *args): + return current_model.set_key(*args) + + +def load_chat_history(current_model, *args): + return current_model.load_chat_history(*args) + + +def delete_chat_history(current_model, *args): + return current_model.delete_chat_history(*args) + + +def interrupt(current_model, *args): + return current_model.interrupt(*args) + + +def reset(current_model, *args): + if current_model: + return current_model.reset(*args) + + +def retry(current_model, *args): + iter = current_model.retry(*args) + for i in iter: + yield i + + +def delete_first_conversation(current_model, *args): + return current_model.delete_first_conversation(*args) + + +def delete_last_conversation(current_model, *args): + return current_model.delete_last_conversation(*args) + + +def set_system_prompt(current_model, *args): + return current_model.set_system_prompt(*args) + + +def rename_chat_history(current_model, *args): + return current_model.rename_chat_history(*args) + + +def auto_name_chat_history(current_model, *args): + if current_model: + return current_model.auto_name_chat_history(*args) + + +def export_markdown(current_model, *args): + return current_model.export_markdown(*args) + + +def upload_chat_history(current_model, *args): + return current_model.load_chat_history(*args) + + +def set_token_upper_limit(current_model, *args): + return current_model.set_token_upper_limit(*args) + + +def set_temperature(current_model, *args): + current_model.set_temperature(*args) + + +def set_top_p(current_model, *args): + current_model.set_top_p(*args) + + +def set_n_choices(current_model, *args): + current_model.set_n_choices(*args) + + +def set_stop_sequence(current_model, *args): + current_model.set_stop_sequence(*args) + + +def set_max_tokens(current_model, *args): + current_model.set_max_tokens(*args) + + +def set_presence_penalty(current_model, *args): + current_model.set_presence_penalty(*args) + + +def set_frequency_penalty(current_model, *args): + current_model.set_frequency_penalty(*args) + + +def set_logit_bias(current_model, *args): + current_model.set_logit_bias(*args) + + +def set_user_identifier(current_model, *args): + current_model.set_user_identifier(*args) + + +def set_single_turn(current_model, *args): + current_model.set_single_turn(*args) + + +def handle_file_upload(current_model, *args): + return current_model.handle_file_upload(*args) + + +def handle_summarize_index(current_model, *args): + return current_model.summarize_index(*args) + + +def like(current_model, *args): + return current_model.like(*args) + + +def dislike(current_model, *args): + return current_model.dislike(*args) + + +def count_token(input_str): + encoding = tiktoken.get_encoding("cl100k_base") + if type(input_str) == dict: + input_str = f"role: {input_str['role']}, content: {input_str['content']}" + length = len(encoding.encode(input_str)) + return length + + +def markdown_to_html_with_syntax_highlight(md_str): # deprecated + def replacer(match): + lang = match.group(1) or "text" + code = match.group(2) + + try: + lexer = get_lexer_by_name(lang, stripall=True) + except ValueError: + lexer = get_lexer_by_name("text", stripall=True) + + formatter = HtmlFormatter() + highlighted_code = highlight(code, lexer, formatter) + + return f'
{highlighted_code}
' + + code_block_pattern = r"```(\w+)?\n([\s\S]+?)\n```" + md_str = re.sub(code_block_pattern, replacer, md_str, flags=re.MULTILINE) + + html_str = markdown(md_str) + return html_str + + +def normalize_markdown(md_text: str) -> str: # deprecated + lines = md_text.split("\n") + normalized_lines = [] + inside_list = False + + for i, line in enumerate(lines): + if re.match(r"^(\d+\.|-|\*|\+)\s", line.strip()): + if not inside_list and i > 0 and lines[i - 1].strip() != "": + normalized_lines.append("") + inside_list = True + normalized_lines.append(line) + elif inside_list and line.strip() == "": + if i < len(lines) - 1 and not re.match( + r"^(\d+\.|-|\*|\+)\s", lines[i + 1].strip() + ): + normalized_lines.append(line) + continue + else: + inside_list = False + normalized_lines.append(line) + + return "\n".join(normalized_lines) + + +def convert_mdtext(md_text): # deprecated + code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL) + inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL) + code_blocks = code_block_pattern.findall(md_text) + non_code_parts = code_block_pattern.split(md_text)[::2] + + result = [] + raw = f'
{html.escape(md_text)}
' + for non_code, code in zip(non_code_parts, code_blocks + [""]): + if non_code.strip(): + non_code = normalize_markdown(non_code) + result.append(markdown(non_code, extensions=["tables"])) + if code.strip(): + # _, code = detect_language(code) # 暂时去除代码高亮功能,因为在大段代码的情况下会出现问题 + # code = code.replace("\n\n", "\n") # 暂时去除代码中的空行,因为在大段代码的情况下会出现问题 + code = f"\n```{code}\n\n```" + code = markdown_to_html_with_syntax_highlight(code) + result.append(code) + result = "".join(result) + output = f'
{result}
' + output += raw + output += ALREADY_CONVERTED_MARK + return output + + +def clip_rawtext(chat_message, need_escape=True): + # first, clip hr line + hr_pattern = r'\n\n
(.*?)' + hr_match = re.search(hr_pattern, chat_message, re.DOTALL) + message_clipped = chat_message[: hr_match.start()] if hr_match else chat_message + # second, avoid agent-prefix being escaped + agent_prefix_pattern = ( + r'(.*?)' + ) + # agent_matches = re.findall(agent_prefix_pattern, message_clipped) + agent_parts = re.split(agent_prefix_pattern, message_clipped, flags=re.DOTALL) + final_message = "" + for i, part in enumerate(agent_parts): + if i % 2 == 0: + if part != "" and part != "\n": + final_message += ( + f'
{escape_markdown(part)}
' + if need_escape + else f'
{part}
' + ) + else: + part = part.replace(' data-fancybox="gallery"', '') + final_message += part + return final_message + + +def convert_bot_before_marked(chat_message): + """ + 注意不能给输出加缩进, 否则会被marked解析成代码块 + """ + if '
' in chat_message: + return chat_message + else: + raw = f'
{clip_rawtext(chat_message)}
' + # really_raw = f'{START_OF_OUTPUT_MARK}
{clip_rawtext(chat_message, need_escape=False)}\n
{END_OF_OUTPUT_MARK}' + + code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL) + code_blocks = code_block_pattern.findall(chat_message) + non_code_parts = code_block_pattern.split(chat_message)[::2] + result = [] + for non_code, code in zip(non_code_parts, code_blocks + [""]): + if non_code.strip(): + result.append(non_code) + if code.strip(): + code = f"\n```{code}\n```" + result.append(code) + result = "".join(result) + md = f'
\n\n{result}\n
' + return raw + md + + +def convert_user_before_marked(chat_message): + if '
' in chat_message: + return chat_message + else: + return f'
{escape_markdown(chat_message)}
' + + +def escape_markdown(text): + """ + Escape Markdown special characters to HTML-safe equivalents. + """ + escape_chars = { + # ' ': ' ', + "_": "_", + "*": "*", + "[": "[", + "]": "]", + "(": "(", + ")": ")", + "{": "{", + "}": "}", + "#": "#", + "+": "+", + "-": "-", + ".": ".", + "!": "!", + "`": "`", + ">": ">", + "<": "<", + "|": "|", + "$": "$", + ":": ":", + "\n": "
", + } + text = text.replace(" ", "    ") + return "".join(escape_chars.get(c, c) for c in text) + + +def convert_asis(userinput): # deprecated + return ( + f'

{html.escape(userinput)}

' + + ALREADY_CONVERTED_MARK + ) + + +def detect_converted_mark(userinput): # deprecated + try: + if userinput.endswith(ALREADY_CONVERTED_MARK): + return True + else: + return False + except: + return True + + +def detect_language(code): # deprecated + if code.startswith("\n"): + first_line = "" + else: + first_line = code.strip().split("\n", 1)[0] + language = first_line.lower() if first_line else "" + code_without_language = code[len(first_line):].lstrip() if first_line else code + return language, code_without_language + + +def construct_text(role, text): + return {"role": role, "content": text} + + +def construct_user(text): + return construct_text("user", text) + + +def construct_system(text): + return construct_text("system", text) + + +def construct_assistant(text): + return construct_text("assistant", text) + + +def save_file(filename, model, chatbot): + system = model.system_prompt + history = model.history + user_name = model.user_name + os.makedirs(os.path.join(HISTORY_DIR, user_name), exist_ok=True) + if filename is None: + filename = new_auto_history_filename(user_name) + if filename.endswith(".md"): + filename = filename[:-3] + if not filename.endswith(".json") and not filename.endswith(".md"): + filename += ".json" + if filename == ".json": + raise Exception("文件名不能为空") + + json_s = { + "system": system, + "history": history, + "chatbot": chatbot, + "model_name": model.model_name, + "single_turn": model.single_turn, + "temperature": model.temperature, + "top_p": model.top_p, + "n_choices": model.n_choices, + "stop_sequence": model.stop_sequence, + "token_upper_limit": model.token_upper_limit, + "max_generation_token": model.max_generation_token, + "presence_penalty": model.presence_penalty, + "frequency_penalty": model.frequency_penalty, + "logit_bias": model.logit_bias, + "user_identifier": model.user_identifier, + "metadata": model.metadata, + } + if not filename == os.path.basename(filename): + history_file_path = filename + else: + history_file_path = os.path.join(HISTORY_DIR, user_name, filename) + + with open(history_file_path, "w", encoding="utf-8") as f: + json.dump(json_s, f, ensure_ascii=False, indent=4) + + filename = os.path.basename(filename) + filename_md = filename[:-5] + ".md" + md_s = f"system: \n- {system} \n" + for data in history: + md_s += f"\n{data['role']}: \n- {data['content']} \n" + with open( + os.path.join(HISTORY_DIR, user_name, filename_md), "w", encoding="utf8" + ) as f: + f.write(md_s) + return os.path.join(HISTORY_DIR, user_name, filename) + + +def sorted_by_pinyin(list): + return sorted(list, key=lambda char: lazy_pinyin(char)[0][0]) + + +def sorted_by_last_modified_time(list, dir): + return sorted( + list, key=lambda char: os.path.getctime(os.path.join(dir, char)), reverse=True + ) + + +def get_file_names_by_type(dir, filetypes=[".json"]): + os.makedirs(dir, exist_ok=True) + files = [] + for type in filetypes: + files += [f for f in os.listdir(dir) if f.endswith(type)] + return files + + +def get_file_names_by_pinyin(dir, filetypes=[".json"]): + files = get_file_names_by_type(dir, filetypes) + if files != [""]: + files = sorted_by_pinyin(files) + logger.debug(f"files are:{files}") + return files + + +def get_file_names_dropdown_by_pinyin(dir, filetypes=[".json"]): + files = get_file_names_by_pinyin(dir, filetypes) + return gr.Dropdown.update(choices=files) + + +def get_file_names_by_last_modified_time(dir, filetypes=[".json"]): + files = get_file_names_by_type(dir, filetypes) + if files != [""]: + files = sorted_by_last_modified_time(files, dir) + logger.debug(f"files are:{files}") + return files + + +def get_history_names(user_name=""): + logger.debug(f"从用户 {user_name} 中获取历史记录文件名列表") + if user_name == "" and hide_history_when_not_logged_in: + return [] + else: + history_files = get_file_names_by_last_modified_time( + os.path.join(HISTORY_DIR, user_name) + ) + history_files = [f[: f.rfind(".")] for f in history_files] + return history_files + + +def get_first_history_name(user_name=""): + history_names = get_history_names(user_name) + return history_names[0] if history_names else "" + + +def get_history_list(user_name=""): + history_names = get_history_names(user_name) + return gr.Radio.update(choices=history_names) + + +def init_history_list(user_name="", prepend=""): + history_names = get_history_names(user_name) + if prepend and prepend not in history_names: + history_names.insert(0, prepend) + return gr.Radio.update( + choices=history_names, value=history_names[0] if history_names else "" + ) + + +def filter_history(user_name, keyword): + history_names = get_history_names(user_name) + try: + history_names = [name for name in history_names if re.search(keyword, name)] + return gr.update(choices=history_names) + except: + return gr.update(choices=history_names) + + +def load_template(filename, mode=0): + logger.debug(f"加载模板文件{filename},模式为{mode}(0为返回字典和下拉菜单,1为返回下拉菜单,2为返回字典)") + lines = [] + if filename.endswith(".json"): + with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as f: + lines = json.load(f) + lines = [[i["act"], i["prompt"]] for i in lines] + else: + with open( + os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8" + ) as csvfile: + reader = csv.reader(csvfile) + lines = list(reader) + lines = lines[1:] + if mode == 1: + return sorted_by_pinyin([row[0] for row in lines]) + elif mode == 2: + return {row[0]: row[1] for row in lines} + else: + choices = sorted_by_pinyin([row[0] for row in lines]) + return {row[0]: row[1] for row in lines}, gr.Dropdown.update(choices=choices) + + +def get_template_names(): + logger.debug("获取模板文件名列表") + return get_file_names_by_pinyin(TEMPLATES_DIR, filetypes=[".csv", "json"]) + + +def get_template_dropdown(): + logger.debug("获取模板下拉菜单") + template_names = get_template_names() + return gr.Dropdown.update(choices=template_names) + + +def get_template_content(templates, selection, original_system_prompt): + logger.debug(f"应用模板中,选择为{selection},原始系统提示为{original_system_prompt}") + try: + return templates[selection] + except: + return original_system_prompt + + +def reset_textbox(): + logger.debug("重置文本框") + return gr.update(value="") + + +def reset_default(): + default_host = state.reset_api_host() + retrieve_proxy("") + return gr.update(value=default_host), gr.update(value=""), "API-Host 和代理已重置" + + +def change_api_host(host): + state.set_api_host(host) + msg = f"API-Host更改为了{host}" + logger.info(msg) + return msg + + +def change_proxy(proxy): + retrieve_proxy(proxy) + os.environ["HTTPS_PROXY"] = proxy + msg = f"代理更改为了{proxy}" + logger.info(msg) + return msg + + +def hide_middle_chars(s): + if s is None: + return "" + if len(s) <= 8: + return s + else: + head = s[:4] + tail = s[-4:] + hidden = "*" * (len(s) - 8) + return head + hidden + tail + + +def submit_key(key): + key = key.strip() + msg = f"API密钥更改为了{hide_middle_chars(key)}" + logger.info(msg) + return key, msg + + +def replace_today(prompt): + today = datetime.datetime.today().strftime("%Y-%m-%d") + return prompt.replace("{current_date}", today) + + +SERVER_GEO_IP_MSG = None +FETCHING_IP = False + + +def get_geoip(): + global SERVER_GEO_IP_MSG, FETCHING_IP + + # 如果已经获取了IP信息,则直接返回 + if SERVER_GEO_IP_MSG is not None: + return SERVER_GEO_IP_MSG + + # 如果正在获取IP信息,则返回等待消息 + if FETCHING_IP: + return i18n("IP地址信息正在获取中,请稍候...") + + # 定义一个内部函数用于在新线程中执行IP信息的获取 + def fetch_ip(): + global SERVER_GEO_IP_MSG, FETCHING_IP + try: + with retrieve_proxy(): + response = requests.get("https://ipapi.co/json/", timeout=5) + data = response.json() + except: + data = {"error": True, "reason": "连接ipapi失败"} + if "error" in data.keys(): + logger.warning(f"无法获取IP地址信息。\n{data}") + SERVER_GEO_IP_MSG = i18n("你可以使用聊天功能。") + else: + country = data["country_name"] + if country == "China": + SERVER_GEO_IP_MSG = "**您的IP区域:中国。**" + else: + SERVER_GEO_IP_MSG = i18n("您的IP区域:") + f"{country}。" + logger.info(SERVER_GEO_IP_MSG) + FETCHING_IP = False + + # 设置正在获取IP信息的标志 + FETCHING_IP = True + + # 启动一个新线程来获取IP信息 + thread = threading.Thread(target=fetch_ip) + thread.start() + + # 返回一个默认消息,真正的IP信息将由新线程更新 + return i18n("正在获取IP地址信息,请稍候...") + + +def find_n(lst, max_num): + n = len(lst) + total = sum(lst) + + if total < max_num: + return n + + for i in range(len(lst)): + if total - lst[i] < max_num: + return n - i - 1 + total = total - lst[i] + return 1 + + +def start_outputing(): + logger.debug("显示取消按钮,隐藏发送按钮") + return gr.Button.update(visible=False), gr.Button.update(visible=True) + + +def end_outputing(): + return ( + gr.Button.update(visible=True), + gr.Button.update(visible=False), + ) + + +def cancel_outputing(): + logger.info("中止输出……") + state.interrupt() + + +def transfer_input(inputs): + # 一次性返回,降低延迟 + textbox = reset_textbox() + outputing = start_outputing() + return ( + inputs, + gr.update(value=""), + gr.Button.update(visible=False), + gr.Button.update(visible=True), + ) + + +def update_chuanhu(): + return gr.Markdown.update(value=i18n("done")) + + +def add_source_numbers(lst, source_name="Source", use_source=True): + if use_source: + return [ + f'[{idx + 1}]\t "{item[0]}"\n{source_name}: {item[1]}' + for idx, item in enumerate(lst) + ] + else: + return [f'[{idx + 1}]\t "{item}"' for idx, item in enumerate(lst)] + + +def add_details(lst): + nodes = [] + for index, txt in enumerate(lst): + brief = txt[:25].replace("\n", "") + nodes.append(f"
{brief}...

{txt}

") + return nodes + + +def sheet_to_string(sheet, sheet_name=None): + result = [] + for index, row in sheet.iterrows(): + row_string = "" + for column in sheet.columns: + row_string += f"{column}: {row[column]}, " + row_string = row_string.rstrip(", ") + row_string += "." + result.append(row_string) + return result + + +def excel_to_string(file_path): + # 读取Excel文件中的所有工作表 + excel_file = pd.read_excel(file_path, engine="openpyxl", sheet_name=None) + + # 初始化结果字符串 + result = [] + + # 遍历每一个工作表 + for sheet_name, sheet_data in excel_file.items(): + # 处理当前工作表并添加到结果字符串 + result += sheet_to_string(sheet_data, sheet_name=sheet_name) + + return result + + +def get_last_day_of_month(any_day): + # The day 28 exists in every month. 4 days later, it's always next month + next_month = any_day.replace(day=28) + datetime.timedelta(days=4) + # subtracting the number of the current day brings us back one month + return next_month - datetime.timedelta(days=next_month.day) + + +def get_model_source(model_name, alternative_source): + if model_name == "gpt2-medium": + return "https://huggingface.co/gpt2-medium" + + +def refresh_ui_elements_on_load(current_model, selected_model_name, user_name): + current_model.set_user_identifier(user_name) + return toggle_like_btn_visibility(selected_model_name), *current_model.auto_load() + + +def toggle_like_btn_visibility(selected_model_name): + if selected_model_name == "xmchat": + return gr.update(visible=True) + else: + return gr.update(visible=False) + + +def get_corresponding_file_type_by_model_name(selected_model_name): + if selected_model_name in ["xmchat", "GPT4 Vision"]: + return ["image"] + else: + return [".pdf", ".docx", ".pptx", ".epub", ".xlsx", ".txt", "text"] + + +def new_auto_history_filename(username): + latest_file = get_first_history_name(username) + if latest_file: + with open(os.path.join(HISTORY_DIR, username, latest_file + ".json"), + "r", + encoding="utf-8", + ) as f: + if len(f.read()) == 0: + return latest_file + now = i18n("新对话 ") + datetime.datetime.now().strftime("%m-%d %H-%M") + return f"{now}.json" + + +def get_history_filepath(username): + dirname = os.path.join(HISTORY_DIR, username) + os.makedirs(dirname, exist_ok=True) + latest_file = get_first_history_name(username) + if not latest_file: + latest_file = new_auto_history_filename(username) + + latest_file = os.path.join(dirname, latest_file) + return latest_file + + +def beautify_err_msg(err_msg): + if "insufficient_quota" in err_msg: + return i18n("剩余配额不足") + if "The model `gpt-4` does not exist" in err_msg: + return i18n("你没有权限访问 GPT4") + if "Resource not found" in err_msg: + return i18n("请查看 config_example.json,配置 Azure OpenAI") + return err_msg + + +def auth_from_conf(username, password): + try: + with open(config_file, encoding="utf-8") as f: + conf = json.load(f) + usernames, passwords = [i[0] for i in conf["users"]], [ + i[1] for i in conf["users"] + ] + if username in usernames: + if passwords[usernames.index(username)] == password: + return True + return False + except: + return False + + +def get_files_hash(file_src=None, file_paths=None): + if file_src: + file_paths = [x.name for x in file_src] + file_paths.sort(key=lambda x: os.path.basename(x)) + + md5_hash = hashlib.md5() + for file_path in file_paths: + with open(file_path, "rb") as f: + while chunk := f.read(8192): + md5_hash.update(chunk) + + return md5_hash.hexdigest() + + +def myprint(**args): + print(args) + + +def replace_special_symbols(string, replace_string=" "): + # 定义正则表达式,匹配所有特殊符号 + pattern = r"[!@#$%^&*()<>?/\|}{~:]" + + new_string = re.sub(pattern, replace_string, string) + + return new_string + + +class ConfigType(Enum): + Bool = 1 + String = 2 + Password = 3 + Number = 4 + ListOfStrings = 5 + + +class ConfigItem: + def __init__(self, key, name, default=None, type=ConfigType.String) -> None: + self.key = key + self.name = name + self.default = default + self.type = type + + +def generate_prompt_string(config_item): + if config_item.default is not None: + return ( + i18n("请输入 ") + + colorama.Fore.GREEN + + i18n(config_item.name) + + colorama.Style.RESET_ALL + + i18n(",默认为 ") + + colorama.Fore.GREEN + + str(config_item.default) + + colorama.Style.RESET_ALL + + i18n(":") + ) + else: + return ( + i18n("请输入 ") + + colorama.Fore.GREEN + + i18n(config_item.name) + + colorama.Style.RESET_ALL + + i18n(":") + ) + + +def generate_result_string(config_item, config_value): + return ( + i18n("你设置了 ") + + colorama.Fore.CYAN + + i18n(config_item.name) + + colorama.Style.RESET_ALL + + i18n(" 为: ") + + config_value + ) + + +class SetupWizard: + def __init__(self, file_path=config_file) -> None: + self.config = {} + self.file_path = file_path + language = input( + '请问是否需要更改语言?可选:"auto", "zh_CN", "en_US", "ja_JP", "ko_KR", "sv_SE", "ru_RU", "vi_VN"\nChange the language? Options: "auto", "zh_CN", "en_US", "ja_JP", "ko_KR", "sv_SE", "ru_RU", "vi_VN"\n目前正在使用中文(zh_CN)\nCurrently using Chinese(zh_CN)\n如果需要,请输入你想用的语言的代码:\nIf you need, please enter the code of the language you want to use:') + if language.lower() in ["auto", "zh_cn", "en_us", "ja_jp", "ko_kr", "sv_se", "ru_ru", "vi_vn"]: + i18n.change_language(language) + else: + print( + "你没有输入有效的语言代码,将使用默认语言中文(zh_CN)\nYou did not enter a valid language code, the default language Chinese(zh_CN) will be used.") + print(i18n("正在进行首次设置,请按照提示进行配置,配置将会被保存在") + + " config.json " + + i18n("中。") + ) + print( + i18n("在") + + " example_config.json " + + i18n("中,包含了可用设置项及其简要说明。") + ) + print(i18n("现在开始进行交互式配置。碰到不知道该怎么办的设置项时,请直接按回车键跳过,程序会自动选择合适的默认值。") + ) + + def set(self, config_items: List[ConfigItem], prompt: str): + """Ask for a settings key + Returns: + Bool: Set or aborted + """ + print(colorama.Fore.YELLOW + i18n(prompt) + colorama.Style.RESET_ALL) + choice = input(i18n("输入 Yes(y) 或 No(n),默认No:")) + if choice.lower() in ["y", "yes"]: + for config_item in config_items: + if config_item.type == ConfigType.Password: + config_value = getpass.getpass(generate_prompt_string(config_item)) + print( + colorama.Fore.CYAN + + i18n(config_item.name) + + colorama.Style.RESET_ALL + + ": " + + hide_middle_chars(config_value) + ) + self.config[config_item.key] = config_value + elif config_item.type == ConfigType.String: + config_value = input(generate_prompt_string(config_item)) + print(generate_result_string(config_item, config_value)) + self.config[config_item.key] = config_value + elif config_item.type == ConfigType.Number: + config_value = input(generate_prompt_string(config_item)) + print(generate_result_string(config_item, config_value)) + try: + self.config[config_item.key] = int(config_value) + except: + print("输入的不是数字,将使用默认值。") + elif config_item.type == ConfigType.ListOfStrings: + # read one string at a time + config_value = [] + while True: + config_value_item = input( + generate_prompt_string(config_item) + i18n(",输入空行结束:") + ) + if config_value_item == "": + break + config_value.append(config_value_item) + print(generate_result_string(config_item, ", ".join(config_value))) + self.config[config_item.key] = config_value + elif config_item.type == ConfigType.Bool: + self.config[config_item.key] = True + return True + elif choice.lower() in ["n", "no"]: + for config_item in config_items: + print( + i18n("你选择了不设置 ") + + colorama.Fore.RED + + i18n(config_item.name) + + colorama.Style.RESET_ALL + + i18n("。") + ) + if config_item.default is not None: + self.config[config_item.key] = config_item.default + if type == ConfigType.Bool: + return True + return False + + def set_users(self): + # 询问设置用户账户 + choice = input(colorama.Fore.YELLOW + i18n( + "是否设置用户账户?设置后,用户需要登陆才可访问。输入 Yes(y) 或 No(n),默认No:") + colorama.Style.RESET_ALL) + if choice.lower() in ["y", "yes"]: + users = [] + while True: + username = input(i18n("请先输入用户名,输入空行结束添加用户:")) + if username == "": + break + password = getpass.getpass(i18n("请输入密码:")) + users.append([username, password]) + self.config["users"] = users + return True + else: + print(i18n("你选择了不设置用户账户。")) + return False + + def __setitem__(self, setting_key: str, value): + self.config[setting_key] = value + + def __getitem__(self, setting_key: str): + return self.config[setting_key] + + def save(self): + with open(self.file_path, "w", encoding="utf-8") as f: + json.dump(self.config, f, ensure_ascii=False, indent=4) + + +def setup_wizard(): + if not os.path.exists(config_file): + wizard = SetupWizard() + flag = False + # 设置openai_api_key。 + flag = wizard.set( + [ConfigItem("openai_api_key", "OpenAI API Key", type=ConfigType.Password)], + "是否设置默认 OpenAI API Key?如果设置,软件启动时会自动加载该API Key,无需在 UI 中手动输入。如果不设置,可以在软件启动后手动输入 API Key。", + ) + if not flag: + flag = wizard.set( + [ + ConfigItem( + "openai_api_key", "OpenAI API Key", type=ConfigType.Password + ) + ], + "如果不设置,将无法使用GPT模型和知识库在线索引功能。如果不设置此选项,您必须每次手动输入API Key。如果不设置,将自动启用本地编制索引的功能,可与本地模型配合使用。请问要设置默认 OpenAI API Key 吗?", + ) + if not flag: + wizard["local_embedding"] = True + # 设置openai_api_base + wizard.set( + [ConfigItem("openai_api_base", "OpenAI API Base", type=ConfigType.String)], + "是否设置默认 OpenAI API Base?如果你在使用第三方API或者CloudFlare Workers等来中转OpenAI API,可以在这里设置。", + ) + # 设置http_proxy + flag = wizard.set( + [ConfigItem("http_proxy", "HTTP 代理", type=ConfigType.String)], + "是否设置默认 HTTP 代理?这可以透过代理使用OpenAI API。", + ) + if flag: + wizard["https_proxy"] = wizard["http_proxy"] + # 设置多 API Key 切换 + flag = wizard.set( + [ConfigItem("api_key_list", "API Key 列表", type=ConfigType.ListOfStrings)], + "是否设置多 API Key 切换?如果设置,将在多个API Key之间切换使用。", + ) + if flag: + wizard["multi_api_key"] = True + # 设置local_embedding + wizard.set( + [ConfigItem("local_embedding", "本地编制索引", type=ConfigType.Bool)], + "是否在本地编制知识库索引?如果是,可以在使用本地模型时离线使用知识库,否则使用OpenAI服务来编制索引(需要OpenAI API Key)。请确保你的电脑有至少16GB内存。本地索引模型需要从互联网下载。", + ) + print( + colorama.Back.GREEN + i18n("现在开始设置其他在线模型的API Key") + colorama.Style.RESET_ALL + ) + # Google Palm + wizard.set( + [ + ConfigItem( + "google_palm_api_key", + "Google Palm API Key", + type=ConfigType.Password, + ) + ], + "是否设置默认 Google Palm API 密钥?如果设置,软件启动时会自动加载该API Key,无需在 UI 中手动输入。如果不设置,可以在软件启动后手动输入 API Key。", + ) + # XMChat + wizard.set( + [ConfigItem("xmchat_api_key", "XMChat API Key", type=ConfigType.Password)], + "是否设置默认 XMChat API 密钥?如果设置,软件启动时会自动加载该API Key,无需在 UI 中手动输入。如果不设置,可以在软件启动后手动输入 API Key。", + ) + # MiniMax + wizard.set( + [ + ConfigItem( + "minimax_api_key", "MiniMax API Key", type=ConfigType.Password + ), + ConfigItem( + "minimax_group_id", "MiniMax Group ID", type=ConfigType.Password + ), + ], + "是否设置默认 MiniMax API 密钥和 Group ID?如果设置,软件启动时会自动加载该API Key,无需在 UI 中手动输入。如果不设置,将无法使用 MiniMax 模型。", + ) + # Midjourney + wizard.set( + [ + ConfigItem( + "midjourney_proxy_api_base", + i18n("你的") + "https://github.com/novicezk/midjourney-proxy" + i18n("代理地址"), + type=ConfigType.String, + ), + ConfigItem( + "midjourney_proxy_api_secret", + "MidJourney Proxy API Secret(用于鉴权访问 api,可选)", + type=ConfigType.Password, + ), + ConfigItem( + "midjourney_discord_proxy_url", + "MidJourney Discord Proxy URL(用于对生成对图进行反代,可选)", + type=ConfigType.String, + ), + ConfigItem( + "midjourney_temp_folder", + "你的 MidJourney 临时文件夹,用于存放生成的图片,填空则关闭自动下载切图(直接显示MJ的四宫格图)", + type=ConfigType.String, + default="files", + ), + ], + "是否设置 Midjourney ?如果设置,软件启动时会自动加载该API Key,无需在 UI 中手动输入。如果不设置,将无法使用 Midjourney 模型。", + ) + # Spark + wizard.set( + [ + ConfigItem("spark_appid", "讯飞星火 App ID", type=ConfigType.Password), + ConfigItem( + "spark_api_secret", "讯飞星火 API Secret", type=ConfigType.Password + ), + ConfigItem("spark_api_key", "讯飞星火 API Key", type=ConfigType.Password), + ], + "是否设置讯飞星火?如果设置,软件启动时会自动加载该API Key,无需在 UI 中手动输入。如果不设置,将无法使用 讯飞星火 模型。请注意不要搞混App ID和API Secret。", + ) + # Cloude + wizard.set( + [ + ConfigItem( + "cloude_api_secret", "Cloude API Secret", type=ConfigType.Password + ), + ], + "是否设置Cloude API?如果设置,软件启动时会自动加载该API Key,无需在 UI 中手动输入。如果不设置,将无法使用 Cloude 模型。", + ) + # 文心一言 + wizard.set( + [ + ConfigItem( + "ernie_api_key", "百度云中的文心一言 API Key", type=ConfigType.Password + ), + ConfigItem( + "ernie_secret_key", "百度云中的文心一言 Secret Key", type=ConfigType.Password + ), + ], + "是否设置文心一言?如果设置,软件启动时会自动加载该API Key,无需在 UI 中手动输入。如果不设置,将无法使用 文心一言 模型。", + ) + # Azure OpenAI + wizard.set( + [ + ConfigItem( + "azure_openai_api_key", + "Azure OpenAI API Key", + type=ConfigType.Password, + ), + ConfigItem( + "azure_openai_api_base_url", + "Azure OpenAI API Base URL", + type=ConfigType.String, + ), + ConfigItem( + "azure_openai_api_version", + "Azure OpenAI API Version", + type=ConfigType.String, + ), + ConfigItem( + "azure_deployment_name", + "Azure OpenAI Chat 模型 Deployment 名称", + type=ConfigType.String, + ), + ConfigItem( + "azure_embedding_deployment_name", + "Azure OpenAI Embedding 模型 Deployment 名称", + type=ConfigType.String, + ), + ConfigItem( + "azure_embedding_model_name", + "Azure OpenAI Embedding 模型名称", + type=ConfigType.String, + ), + ], + "是否设置 Azure OpenAI?如果设置,软件启动时会自动加载该API Key,无需在 UI 中手动输入。如果不设置,将无法使用 Azure OpenAI 模型。", + ) + print( + colorama.Back.GREEN + i18n("现在开始进行软件功能设置") + colorama.Style.RESET_ALL + ) + # 用户列表 + wizard.set_users() + # 未登录情况下是否不展示对话历史 + wizard.set( + [ + ConfigItem( + "hide_history_when_not_logged_in", + "未登录情况下是否不展示对话历史", + type=ConfigType.Bool, + ) + ], + "是否设置未登录情况下是否不展示对话历史?如果设置,未登录情况下将不展示对话历史。", + ) + # 默认模型 + wizard.set( + [ + ConfigItem( + "default_model", + "默认模型", + type=ConfigType.String, + default="gpt-3.5-turbo", + ) + ], + "是否更改默认模型?如果设置,软件启动时会自动加载该模型,无需在 UI 中手动选择。目前的默认模型为 GPT3.5 Turbo。可选的在线模型有:" + + "\n" + + "\n".join(ONLINE_MODELS) + + "\n" + + "可选的本地模型为:" + + "\n" + + "\n".join(LOCAL_MODELS), + ) + # 是否启用自动加载 + wizard.set( + [ + ConfigItem( + "hide_history_when_not_logged_in", + "是否不展示对话历史", + type=ConfigType.Bool, + default=False, + ) + ], + "未设置用户名/密码情况下是否不展示对话历史?", + ) + # 如何自动命名对话历史 + wizard.set( + [ + ConfigItem( + "chat_name_method_index", + "自动命名对话历史的方式(0: 使用日期时间命名;1: 使用第一条提问命名,2: 使用模型自动总结。)", + type=ConfigType.Number, + default=2, + ) + ], + "是否选择自动命名对话历史的方式?", + ) + # 头像 + wizard.set( + [ + ConfigItem( + "bot_avatar", + "机器人头像", + type=ConfigType.String, + default="default", + ), + ConfigItem( + "user_avatar", + "用户头像", + type=ConfigType.String, + default="default", + ), + ], + '是否设置机器人头像和用户头像?可填写本地或网络图片链接,或者"none"(不显示头像)。', + ) + # 川虎助理 + wizard.set( + [ + ConfigItem( + "default_chuanhu_assistant_model", + "川虎助理使用的模型", + type=ConfigType.String, + default="gpt-4", + ), + ConfigItem( + "GOOGLE_CSE_ID", + "谷歌搜索引擎ID(获取方式请看 https://stackoverflow.com/questions/37083058/programmatically-searching-google-in-python-using-custom-search)", + type=ConfigType.String, + ), + ConfigItem( + "GOOGLE_API_KEY", + "谷歌API Key(获取方式请看 https://stackoverflow.com/questions/37083058/programmatically-searching-google-in-python-using-custom-search)", + type=ConfigType.String, + ), + ConfigItem( + "WOLFRAM_ALPHA_APPID", + "Wolfram Alpha API Key(获取方式请看 https://products.wolframalpha.com/api/)", + type=ConfigType.String, + ), + ConfigItem( + "SERPAPI_API_KEY", + "SerpAPI API Key(获取方式请看 https://serpapi.com/)", + type=ConfigType.String, + ), + ], + "是否设置川虎助理?如果不设置,仍可设置川虎助理。如果设置,可以使用川虎助理Pro模式。", + ) + # 文档处理与显示 + wizard.set( + [ + ConfigItem( + "latex_option", + "LaTeX 公式渲染策略", + type=ConfigType.String, + default="default", + ) + ], + '是否设置文档处理与显示?可选的 LaTeX 公式渲染策略有:"default", "strict", "all"或者"disabled"。', + ) + # 是否隐藏API Key输入框 + wizard.set( + [ + ConfigItem( + "hide_my_key", + "是否隐藏API Key输入框", + type=ConfigType.Bool, + default=False, + ) + ], + "是否隐藏API Key输入框?如果设置,将不会在 UI 中显示API Key输入框。", + ) + # 是否指定可用模型列表 + wizard.set( + [ + ConfigItem( + "available_models", + "可用模型列表", + type=ConfigType.ListOfStrings, + ) + ], + "是否指定可用模型列表?如果设置,将只会在 UI 中显示指定的模型。默认展示所有模型。可用的模型有:" + + "\n".join(ONLINE_MODELS) + + "\n".join(LOCAL_MODELS), + ) + # 添加模型到列表 + wizard.set( + [ + ConfigItem( + "extra_models", + "额外模型列表", + type=ConfigType.ListOfStrings, + ) + ], + "是否添加模型到列表?例如,训练好的GPT模型可以添加到列表中。可以在UI中自动添加模型到列表。", + ) + # 分享 + wizard.set( + [ + ConfigItem( + "server_name", + "服务器地址,例如设置为 0.0.0.0 则可以通过公网访问(如果你用公网IP)", + type=ConfigType.String, + ), + ConfigItem( + "server_port", + "服务器端口", + type=ConfigType.Number, + default=7860, + ), + ], + "是否配置运行地址和端口?(不建议设置)", + ) + wizard.set( + [ + ConfigItem( + "share", + "是否通过gradio分享?", + type=ConfigType.Bool, + default=False, + ) + ], + "是否通过gradio分享?可以通过公网访问。", + ) + wizard.save() + print(colorama.Back.GREEN + i18n("设置完成。现在请重启本程序。") + colorama.Style.RESET_ALL) + exit() + + +def save_pkl(data, file_path): + with open(file_path, 'wb') as f: + pickle.dump(data, f) + + +def load_pkl(file_path): + with open(file_path, 'rb') as f: + data = pickle.load(f) + return data + + +def chinese_preprocessing_func(text: str) -> List[str]: + import jieba + jieba.setLogLevel('ERROR') + return jieba.lcut(text) diff --git a/src/zhipu_client.py b/src/zhipu_client.py new file mode 100644 index 0000000000000000000000000000000000000000..3d8fd3543a246bb84de096332f4e44c420ea782b --- /dev/null +++ b/src/zhipu_client.py @@ -0,0 +1,225 @@ + +import base64 +import datetime +import json +import os +from io import BytesIO + +import gradio as gr +import requests +from PIL import Image +from loguru import logger +from zhipuai import ZhipuAI + + +from src import shared, config +from src.base_model import BaseLLMModel +from src.presets import ( + INITIAL_SYSTEM_PROMPT, + TIMEOUT_ALL, + TIMEOUT_STREAMING, + STANDARD_ERROR_MSG, + CONNECTION_TIMEOUT_MSG, + READ_TIMEOUT_MSG, + ERROR_RETRIEVE_MSG, + GENERAL_ERROR_MSG, + CHAT_COMPLETION_URL, + SUMMARY_CHAT_SYSTEM_PROMPT +) +from src.openai_client import OpenAIClient + +from src.utils import ( + count_token, + construct_system, + construct_user, + get_last_day_of_month, + i18n, + replace_special_symbols, +) + + + + +def decode_chat_response(response): + try: + error_msg = "" + for chunk in response: + if chunk: + # chunk = chunk.decode() + chunk = chunk.choices[0].delta + chunk_length = len(chunk.content) + try: + if chunk_length > 1 and chunk!="": + try: + yield chunk.content + except Exception as e: + logger.error(f"Error xxx: {e}") + continue + except Exception as ee: + logger.error(f"ERROR: {chunk}, {ee}") + continue + if error_msg and not error_msg.endswith("[DONE]"): + raise Exception(error_msg) + except GeneratorExit as ge: + raise ValueError(f"GeneratorExit: {ge}") + except Exception as e: + raise Exception(f"Error in generate: {str(e)}") + + + +class ZhipuAIClient(OpenAIClient): + def __init__( + self, + model_name, + api_key, + system_prompt=INITIAL_SYSTEM_PROMPT, + temperature=1.0, + top_p=1.0, + user_name="", + ) -> None: + super().__init__( + api_key = api_key, + model_name=model_name, + temperature=temperature, + top_p=top_p, + system_prompt=system_prompt, + # user=user_name, + ) + self.api_key = api_key + self.need_api_key = True + self._refresh_header() + self.client = None + # self.user_name = user_name + logger.info(f"user name: {user_name}") + + def get_answer_stream_iter(self): + if not self.api_key: + raise ValueError("API key is not set") + response = self._get_response(stream=True) + if response is not None: + stream_iter = decode_chat_response(response) + partial_text = "" + for chunk in stream_iter: + partial_text += chunk + yield partial_text + else: + yield STANDARD_ERROR_MSG + GENERAL_ERROR_MSG + + # def get_answer_at_once(self): + # if not self.api_key: + # raise ValueError("API key is not set") + # response = self._get_response() + # response = json.loads(response.text) + # content = response["choices"][0]["message"]["content"] + # total_token_count = response["usage"]["total_tokens"] + # return content, total_token_count + + + @shared.state.switching_api_key # 在不开启多账号模式的时候,这个装饰器不会起作用 + def _get_response(self, stream=False): + zhipuai_api_key = self.api_key + system_prompt = self.system_prompt + history = self.history + # logger.debug(f"{history}") + # headers = { + # "Authorization": f"Bearer {zhipuai_api_key}", + # "Content-Type": "application/json", + # } + + if system_prompt is not None: + history = [construct_system(system_prompt), *history] + + payload = { + "model": self.model_name, + "messages": history, + "temperature": self.temperature, + "top_p": self.top_p, + "n": self.n_choices, + "stream": stream, + "presence_penalty": self.presence_penalty, + "frequency_penalty": self.frequency_penalty, + } + + if self.max_generation_token is not None: + payload["max_tokens"] = self.max_generation_token + if self.stop_sequence is not None: + payload["stop"] = self.stop_sequence + if self.logit_bias is not None: + payload["logit_bias"] = self.logit_bias + if self.user_identifier: + payload["user"] = self.user_identifier + + if stream: + timeout = TIMEOUT_STREAMING + else: + timeout = TIMEOUT_ALL + + # 如果有自定义的api-host,使用自定义host发送请求,否则使用默认设置发送请求 + # if shared.state.chat_completion_url != CHAT_COMPLETION_URL: + # logger.debug(f"使用自定义API URL: {shared.state.chat_completion_url}") + + # with config.retrieve_proxy(): + # try: + # response = requests.post( + # shared.state.chat_completion_url, + # headers=headers, + # json=payload, + # stream=stream, + # timeout=timeout, + # ) + # except Exception as e: + # logger.error(f"Error: {e}") + # response = None + # return response + + if self.client is None: + self.client = ZhipuAI(api_key = zhipuai_api_key) + + response = self.client.chat.completions.create( + model=self.model_name, + # model="glm-3-turbo", + messages=history, + temperature=self.temperature, + top_p= self.top_p, + stream= stream, + ) + + + # "n": self.n_choices, + # "stream": stream, + # "presence_penalty": self.presence_penalty, + # "frequency_penalty": self.frequency_penalty, + + return response + + + # todo: fix bug + def billing_info(self): + status_text = "获取API使用情况失败,未更新ZhipuAI代价代码。" + return status_text + # try: + # curr_time = datetime.datetime.now() + # last_day_of_month = get_last_day_of_month( + # curr_time).strftime("%Y-%m-%d") + # first_day_of_month = curr_time.replace(day=1).strftime("%Y-%m-%d") + # usage_url = f"{shared.state.usage_api_url}?start_date={first_day_of_month}&end_date={last_day_of_month}" + # try: + # usage_data = self._get_billing_data(usage_url) + # except Exception as e: + # logger.warning(f"获取API使用情况失败:" + str(e)) + # return i18n("**获取API使用情况失败**") + # rounded_usage = "{:.5f}".format(usage_data["total_usage"] / 100) + # return i18n("**本月使用金额** ") + f"\u3000 ${rounded_usage}" + # except requests.exceptions.ConnectTimeout: + # status_text = ( + # STANDARD_ERROR_MSG + CONNECTION_TIMEOUT_MSG + ERROR_RETRIEVE_MSG + # ) + # return status_text + # except requests.exceptions.ReadTimeout: + # status_text = STANDARD_ERROR_MSG + READ_TIMEOUT_MSG + ERROR_RETRIEVE_MSG + # return status_text + # except Exception as e: + # import traceback + # traceback.print_exc() + # logger.error(i18n("获取API使用情况失败:") + str(e)) + # return STANDARD_ERROR_MSG + ERROR_RETRIEVE_MSG \ No newline at end of file diff --git a/sunny.py b/sunny.py new file mode 100644 index 0000000000000000000000000000000000000000..572eaecef947611a3857261639269f1d4cd7831c --- /dev/null +++ b/sunny.py @@ -0,0 +1,431 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +""" + ngrok.cc 内网穿透服务 Python 版 + 本程序仅适用于ngrok.cc 使用前请先在 https://ngrok.cc 注册账号. + Linux 系统一般自带Python 可以直接运行 + 赋予权限 chmod 755 sunny.py + 直接运行 ./sunny.py --clientid=xxxxxxxxxxxxxxxx + 命令行模式执行 python sunny.py --clientid=xxxxxx 即可运行 + 感谢 hauntek 提供的 python-ngrok 原版程序 +""" +import getopt +import socket +import ssl +import json +import struct +import random +import sys +import time +import logging +import threading + +python_version = sys.version_info >= (3, 0) +if not python_version: + reload(sys) + sys.setdefaultencoding('utf8') + +options = { + 'clientid':'', +} + +def usage(): + print( + ' -h help \n' \ + ' -a clientid xxxxxxxxxxxxxxxx\n' \ + ) + sys.exit() + +try: + opts, args = getopt.getopt(sys.argv[1:], "h:c:", ['help', "clientid="]) +except getopt.GetoptError: + usage() + +if len(opts) == 0: + print( + '使用说明\n' \ + '在命令行模式运行 python sunny.py --clientid=xxxxxxxx\n' \ + '如果是多个隧道换成 python sunny.py --clientid=xxxxxxxx,xxxxxxxx\n' \ + '请登录 https://ngrok.cc 获取 clientid\n' \ + ) + +for option, value in opts: + if option in ['-h', '--help']: + usage() + if option in ['-c', '--clientid']: + options['clientid'] = value + +if options['clientid'] == '': + if not python_version: + input_clientid = raw_input('请输入clientid:') + else: + input_clientid = str(input('请输入clientid:')) + if input_clientid != '': + options['clientid'] = input_clientid + else: + sys.exit() + +Tunnels = list() # 全局渠道赋值 + +reqIdaddr = dict() +localaddr = dict() + +# ngrok.cc 添加到渠道队列 +def ngrok_adds(Tunnel): + global Tunnels + for tunnelinfo in Tunnel: + if tunnelinfo.get('proto'): + if tunnelinfo.get('proto').get('http'): + protocol = 'http' + if tunnelinfo.get('proto').get('https'): + protocol = 'https' + if tunnelinfo.get('proto').get('tcp'): + protocol = 'tcp' + + proto = tunnelinfo['proto'][protocol].split(':') # 127.0.0.1:80 拆分成数组 + if proto[0] == '': + proto[0] = '127.0.0.1' + if proto[1] == '' or proto[1] == 0: + proto[1] = 80 + + body = dict() + body['protocol'] = protocol + body['hostname'] = tunnelinfo['hostname'] + body['subdomain'] = tunnelinfo['subdomain'] + body['httpauth'] = tunnelinfo['httpauth'] + body['rport'] = tunnelinfo['remoteport'] + body['lhost'] = str(proto[0]) + body['lport'] = int(proto[1]) + Tunnels.append(body) # 加入渠道队列 + +# ngrok.cc 获取服务器设置 +def ngrok_auth(options): + host = 'www.ngrok.cc' + port = 443 + try: + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + ssl_client = ssl.wrap_socket(client, ssl_version=ssl.PROTOCOL_TLSv1_2) # ssl.PROTOCOL_TLSv1_2 + ssl_client.connect((host, port)) + except Exception: + print('连接认证服务器: https://www.ngrok.cc 错误.') + time.sleep(10) + sys.exit() + + header = "POST " + "/api/clientid/clientid/%s" + " HTTP/1.1" + "\r\n" + header += "Content-Type: text/html" + "\r\n" + header += "Host: %s" + "\r\n" + header += "\r\n" + buf = header % (options, host) + ssl_client.sendall(buf.encode('utf-8')) # 发送请求头 + + fd = ssl_client.makefile('rb', 0) + body = bytes() + while True: + line = fd.readline().decode('utf-8') + if line == "\n" or line == "\r\n": + chunk_size = int(fd.readline(), 16) + if chunk_size > 0: + body = fd.read(chunk_size).decode('utf-8') + break + + ssl_client.close() + + authData = json.loads(body) + if authData['status'] != 200: + print('认证错误:%s, ErrorCode:%s' % (authData['msg'], authData['status'])) + time.sleep(10) + sys.exit() + + print('认证成功,正在连接服务器...') + # 设置映射隧道,支持多渠道[客户端id] + ngrok_adds(authData['data']) + proto = authData['server'].split(':') + return proto + +print('欢迎使用内网穿透 python-ngrok v1.42\r\nCtrl+C 退出') +serverArr = ngrok_auth(options['clientid']) +host = str(serverArr[0]) # Ngrok服务器地址 +port = int(serverArr[1]) # 端口 +bufsize = 1024 # 吞吐量 + +mainsocket = 0 + +ClientId = '' +pingtime = 0 + +def getloacladdr(Tunnels, Url): + protocol = Url[0:Url.find(':')] + hostname = Url[Url.find('//') + 2:] + subdomain = hostname[0:hostname.find('.')] + rport = Url[Url.rfind(':') + 1:] + + for tunnelinfo in Tunnels: + if tunnelinfo.get('protocol') == protocol: + if tunnelinfo.get('protocol') in ['http', 'https']: + if tunnelinfo.get('hostname') == hostname: + return tunnelinfo + if tunnelinfo.get('subdomain') == subdomain: + return tunnelinfo + if tunnelinfo.get('protocol') == 'tcp': + if tunnelinfo.get('rport') == int(rport): + return tunnelinfo + + return dict() + +def dnsopen(host): + try: + ip = socket.gethostbyname(host) + except socket.error: + return False + + return ip + +def connectremote(host, port): + try: + host = socket.gethostbyname(host) + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + ssl_client = ssl.wrap_socket(client, ssl_version=ssl.PROTOCOL_SSLv23) + ssl_client.connect((host, port)) + ssl_client.setblocking(1) + logger = logging.getLogger('%s:%d' % ('Conn', ssl_client.fileno())) + logger.debug('New connection to: %s:%d' % (host, port)) + except socket.error: + return False + + return ssl_client + +def connectlocal(localhost, localport): + try: + localhost = socket.gethostbyname(localhost) + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client.connect((localhost, localport)) + client.setblocking(1) + logger = logging.getLogger('%s:%d' % ('Conn', client.fileno())) + logger.debug('New connection to: %s:%d' % (localhost, localport)) + except socket.error: + return False + + return client + +def NgrokAuth(): + Payload = dict() + Payload['ClientId'] = '' + Payload['OS'] = 'darwin' + Payload['Arch'] = 'amd64' + Payload['Version'] = '2' + Payload['MmVersion'] = '2.1' + Payload['User'] = 'user' + Payload['Password'] = '' + body = dict() + body['Type'] = 'Auth' + body['Payload'] = Payload + buffer = json.dumps(body) + return(buffer) + +def ReqTunnel(ReqId, Protocol, Hostname, Subdomain, HttpAuth, RemotePort): + Payload = dict() + Payload['ReqId'] = ReqId + Payload['Protocol'] = Protocol + Payload['Hostname'] = Hostname + Payload['Subdomain'] = Subdomain + Payload['HttpAuth'] = HttpAuth + Payload['RemotePort'] = RemotePort + body = dict() + body['Type'] = 'ReqTunnel' + body['Payload'] = Payload + buffer = json.dumps(body) + return(buffer) + +def RegProxy(ClientId): + Payload = dict() + Payload['ClientId'] = ClientId + body = dict() + body['Type'] = 'RegProxy' + body['Payload'] = Payload + buffer = json.dumps(body) + return(buffer) + +def Ping(): + Payload = dict() + body = dict() + body['Type'] = 'Ping' + body['Payload'] = Payload + buffer = json.dumps(body) + return(buffer) + +def lentobyte(len): + return struct.pack(' 0: + if not recvbuf: + recvbuf = recvbut + else: + recvbuf += recvbut + + if type == 1 or (type == 2 and linkstate == 1): + lenbyte = tolen(recvbuf[0:8]) + if len(recvbuf) >= (8 + lenbyte): + buf = recvbuf[8:lenbyte + 8].decode('utf-8') + logger = logging.getLogger('%s:%d' % ('Recv', sock.fileno())) + logger.debug('Reading message with length: %d' % len(buf)) + logger.debug('Read message: %s' % buf) + js = json.loads(buf) + if type == 1: + if js['Type'] == 'ReqProxy': + newsock = connectremote(host, port) + if newsock: + thread = threading.Thread(target = HKClient, args = (newsock, 0, 2)) + thread.setDaemon(True) + thread.start() + if js['Type'] == 'AuthResp': + ClientId = js['Payload']['ClientId'] + logger = logging.getLogger('%s' % 'client') + logger.debug('Authenticated with server, client id: %s' % ClientId) + sendpack(sock, Ping()) + pingtime = time.time() + for info in Tunnels: + reqid = getRandChar(8) + sendpack(sock, ReqTunnel(reqid, info['protocol'], info['hostname'], info['subdomain'], info['httpauth'], info['rport'])) + reqIdaddr[reqid] = (info['lhost'], info['lport']) + if js['Type'] == 'NewTunnel': + if js['Payload']['Error'] != '': + logger = logging.getLogger('%s' % 'client') + logger.error('Server failed to allocate tunnel: %s' % js['Payload']['Error']) + print('隧道建立失败: %s' % js['Payload']['Error']) + time.sleep(30) + else: + logger = logging.getLogger('%s' % 'client') + logger.debug('Tunnel established at %s' % js['Payload']['Url']) + print('隧道建立成功: %s' % js['Payload']['Url']) # 注册成功 + localaddr[js['Payload']['Url']] = reqIdaddr[js['Payload']['ReqId']] + if type == 2: + if js['Type'] == 'StartProxy': + localhost, localport = localaddr[js['Payload']['Url']] + + newsock = connectlocal(localhost, localport) + if newsock: + thread = threading.Thread(target = HKClient, args = (newsock, 0, 3, sock)) + thread.setDaemon(True) + thread.start() + tosock = newsock + linkstate = 2 + else: + body = 'Web服务错误
隧道 %s 无效
无法连接到%s. 此端口尚未提供Web服务
' + html = body % (js['Payload']['Url'], localhost + ':' + str(localport)) + header = "HTTP/1.0 502 Bad Gateway" + "\r\n" + header += "Content-Type: text/html" + "\r\n" + header += "Content-Length: %d" + "\r\n" + header += "\r\n" + "%s" + buf = header % (len(html.encode('utf-8')), html) + sendbuf(sock, buf.encode('utf-8')) + + if len(recvbuf) == (8 + lenbyte): + recvbuf = bytes() + else: + recvbuf = recvbuf[8 + lenbyte:] + + if type == 3 or (type == 2 and linkstate == 2): + sendbuf(tosock, recvbuf) + recvbuf = bytes() + + except socket.error: + break + + if type == 1: + mainsocket = False + if type == 3: + try: + tosock.shutdown(socket.SHUT_WR) + except socket.error: + tosock.close() + + logger = logging.getLogger('%s:%d' % ('Close', sock.fileno())) + logger.debug('Closing') + sock.close() + +# 客户端程序初始化 +if __name__ == '__main__': + logging.basicConfig(level=logging.INFO, format='[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s', datefmt='%Y/%m/%d %H:%M:%S') + logger = logging.getLogger('%s' % 'client') + logger.debug('python-ngrok v1.5') + while True: + try: + # 检测控制连接是否连接. + if mainsocket == False: + ip = dnsopen(host) + if ip == False: + logger = logging.getLogger('%s' % 'client') + logger.debug('update dns') + print('连接ngrok服务器失败.') + time.sleep(10) + continue + mainsocket = connectremote(ip, port) + if mainsocket == False: + logger = logging.getLogger('%s' % 'client') + logger.debug('connect failed...!') + print('连接ngrok服务器失败.') + time.sleep(10) + continue + thread = threading.Thread(target = HKClient, args = (mainsocket, 0, 1)) + thread.setDaemon(True) + thread.start() + + # 发送心跳 + if pingtime + 20 < time.time() and pingtime != 0: + sendpack(mainsocket, Ping()) + pingtime = time.time() + + time.sleep(1) + + except socket.error: + pingtime = 0 + except KeyboardInterrupt: + sys.exit() \ No newline at end of file diff --git "a/templates/\347\273\217\345\205\270\345\224\220\350\257\227.json" "b/templates/\347\273\217\345\205\270\345\224\220\350\257\227.json" new file mode 100644 index 0000000000000000000000000000000000000000..82345b4dcf0610266fddd290d0c55e020330c0b4 --- /dev/null +++ "b/templates/\347\273\217\345\205\270\345\224\220\350\257\227.json" @@ -0,0 +1,38 @@ +[ + { + "act": "静夜思 李白", + "prompt": "静夜思\n唐·李白\n床前明月光,疑是地上霜。\n举头望明月,低头思故乡。" + }, + { + "act": "春望 杜甫", + "prompt": "春望\n唐·杜甫\n国破山河在,城春草木深。\n感时花溅泪,恨别鸟惊心。\n烽火连三月,家书抵万金。\n白头搔更短,浑欲不胜簪。" + }, + { + "act": "望庐山瀑布 李白", + "prompt": "望庐山瀑布\n唐·李白\n日照香炉生紫烟,遥看瀑布挂前川。\n飞流直下三千尺,疑是银河落九天。" + }, + { + "act": "将进酒 李白", + "prompt": "将进酒\n唐·李白\n君不见黄河之水天上来,奔流到海不复回。\n君不见高堂明镜悲白发,朝如青丝暮成雪。\n人生得意须尽欢,莫使金樽空对月。\n天生我材必有用,千金散尽还复来。\n烹羊宰牛且为乐,会须一饮三百杯。\n岑夫子,丹丘生,将进酒,杯莫停。\n与君歌一曲,请君为我倾耳听。\n钟鼓馔玉不足贵,但愿长醉不复醒。\n古来圣贤皆寂寞,惟有饮者留其名。\n陈王昔时宴平乐,斗酒十千恣欢谑。\n主人何为言少钱,径须沽取对君酌。\n五花马,千金裘,呼儿将出换美酒,与尔同销万古愁。" + }, + { + "act": "春晓 孟浩然", + "prompt": "春晓\n唐·孟浩然\n春眠不觉晓,处处闻啼鸟。\n夜来风雨声,花落知多少。" + }, + { + "act": "早发白帝城 李白", + "prompt": "早发白帝城\n唐·李白\n朝辞白帝彩云间,千里江陵一日还。\n两岸猿声啼不住,轻舟已过万重山。" + }, + { + "act": "登鹳雀楼 王之涣", + "prompt": "登鹳雀楼\n唐·王之涣\n白日依山尽,黄河入海流。\n欲穷千里目,更上一层楼。" + }, + { + "act": "赠汪伦 李白", + "prompt": "赠汪伦\n唐·李白\n李白乘舟将欲行,忽闻岸上踏歌声。\n桃花潭水深千尺,不及汪伦送我情。" + }, + { + "act": "登高 杜甫", + "prompt": "登高\n唐·杜甫\n风急天高猿啸哀,渚清沙白鸟飞回。\n无边落木萧萧下,不尽长江滚滚来。\n万里悲秋常作客,百年多病独登台。\n艰难苦恨繁霜鬓,潦倒新停浊酒杯。" + } +] diff --git "a/templates/\347\273\217\345\205\270\345\256\213\350\257\215.json" "b/templates/\347\273\217\345\205\270\345\256\213\350\257\215.json" new file mode 100644 index 0000000000000000000000000000000000000000..60d5396c03d4eeb3bf7a16d2764260b4a4f1e744 --- /dev/null +++ "b/templates/\347\273\217\345\205\270\345\256\213\350\257\215.json" @@ -0,0 +1,38 @@ +[ + { + "act": "水调歌头·明月几时有 苏轼", + "prompt": "水调歌头·明月几时有\n宋·苏轼\n丙辰中秋,欢饮达旦,大醉,作此篇,兼怀子由。\n明月几时有?把酒问青天。\n不知天上宫阙,今夕是何年。\n我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n起舞弄清影,何似在人间。\n转朱阁,低绮户,照无眠。\n不应有恨,何事长向别时圆?\n人有悲欢离合,月有阴晴圆缺,此事古难全。\n但愿人长久,千里共婵娟。" + }, + { + "act": "青玉案·元夕 辛弃疾", + "prompt": "青玉案·元夕\n宋·辛弃疾\n东风夜放花千树,更吹落,星如雨。\n宝马雕车香满路,凤萧声动,玉壶光转,一夜鱼龙舞。\n蛾儿雪柳黄金缕,笑语盈盈暗香去。\n众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。" + }, + { + "act": "如梦令·常记溪亭日暮 李清照", + "prompt": "如梦令·常记溪亭日暮\n宋·李清照\n常记溪亭日暮,沉醉不知归路。\n兴尽晚回舟,误入藕花深处。\n争渡,争渡,惊起一滩鸥鹭。" + }, + { + "act": "声声慢·寻寻觅觅 李清照", + "prompt": "声声慢·寻寻觅觅\n宋·李清照\n寻寻觅觅,冷冷清清,凄凄惨惨戚戚。\n乍暖还寒时候,最难将息。\n三杯两盏淡酒,怎敌他、晚来风急?\n雁过也,正伤心,却是旧时相识。\n满地黄花堆积,憔悴损,如今有谁堪摘?\n守着窗儿,独自怎生得黑!\n梧桐更兼细雨,到黄昏、点点滴滴。\n这次第,怎一个愁字了得!" + }, + { + "act": "雨霖铃·寒蝉凄切 柳永", + "prompt": "雨霖铃·寒蝉凄切\n宋·柳永\n寒蝉凄切,对长亭晚,骤雨初歇。\n都门帐饮无绪,留恋处,兰舟催发。\n执手相看泪眼,竟无语凝噎。\n念去去,千里烟波,暮霭沉沉楚天阔。\n多情自古伤离别,更那堪,冷落清秋节!\n今宵酒醒何处?杨柳岸,晓风残月。" + }, + { + "act": "蝶恋花·庭院深深深几许 柳永", + "prompt": "蝶恋花·庭院深深深几许\n宋·柳永\n庭院深深深几许,杨柳堆烟,帘幕无重数。\n玉勒雕鞍游冶处,楼高不见章台路。\n雨横风狂三月暮,门掩黄昏,无计留春住。\n泪眼问花花不语,乱红飞过秋千去。" + }, + { + "act": "望海潮·东南形胜 柳永", + "prompt": "望海潮·东南形胜\n宋·柳永\n东南形胜,三吴都会,钱塘自古繁华。\n烟柳画桥,风帘翠幕,参差十万人家。\n云树绕堤沙,怒涛卷霜雪,天堑无涯。\n市列珠翠,户盈罗绮,竞豪奢。\n重湖叠巘清嘉,有三秋桂子,十里荷花。\n羌管弄晴,菱歌泛夜,傥能醉否?\n急管繁弦,乐极哀来,月儿如钩。\n寂寞箫鼓,闻说双溪艳曲,也传不到耳。\n算只有殷勤,画檐蛛网,尽日惹飞絮。" + }, + { + "act": "八声甘州·对潇潇暮雨洒江天 柳永", + "prompt": "八声甘州·对潇潇暮雨洒江天\n宋·柳永\n对潇潇暮雨洒江天,一番洗清秋。\n渐霜风凄紧,关河冷落,残照当楼。\n是处红衰翠减,苒苒物华休。\n惟有长江水,无语东流。\n不忍登高临远,望故乡渺邈,归思难收。\n叹年来踪迹,何事苦淹留?\n想佳人、妆楼颙望,误几回、天际识归舟。\n争知我,倚栏杆处,正恁凝愁!" + }, + { + "act": "念奴娇·赤壁怀古 苏轼", + "prompt": "念奴娇·赤壁怀古\n宋·苏轼\n大江东去,浪淘尽,千古风流人物。\n故垒西边,人道是,三国周郎赤壁。\n乱石穿空,惊涛拍岸,卷起千堆雪。\n江山如画,一时多少豪杰。\n遥想公瑾当年,小乔初嫁了,雄姿英发。\n羽扇纶巾,谈笑间,樯橹灰飞烟灭。\n故国神游,多情应笑我,早生华发。\n人生如梦,一尊还酹江月。" + } +] diff --git "a/templates/\347\273\217\345\205\270\346\226\207\350\250\200\346\226\207.json" "b/templates/\347\273\217\345\205\270\346\226\207\350\250\200\346\226\207.json" new file mode 100644 index 0000000000000000000000000000000000000000..82345b4dcf0610266fddd290d0c55e020330c0b4 --- /dev/null +++ "b/templates/\347\273\217\345\205\270\346\226\207\350\250\200\346\226\207.json" @@ -0,0 +1,38 @@ +[ + { + "act": "静夜思 李白", + "prompt": "静夜思\n唐·李白\n床前明月光,疑是地上霜。\n举头望明月,低头思故乡。" + }, + { + "act": "春望 杜甫", + "prompt": "春望\n唐·杜甫\n国破山河在,城春草木深。\n感时花溅泪,恨别鸟惊心。\n烽火连三月,家书抵万金。\n白头搔更短,浑欲不胜簪。" + }, + { + "act": "望庐山瀑布 李白", + "prompt": "望庐山瀑布\n唐·李白\n日照香炉生紫烟,遥看瀑布挂前川。\n飞流直下三千尺,疑是银河落九天。" + }, + { + "act": "将进酒 李白", + "prompt": "将进酒\n唐·李白\n君不见黄河之水天上来,奔流到海不复回。\n君不见高堂明镜悲白发,朝如青丝暮成雪。\n人生得意须尽欢,莫使金樽空对月。\n天生我材必有用,千金散尽还复来。\n烹羊宰牛且为乐,会须一饮三百杯。\n岑夫子,丹丘生,将进酒,杯莫停。\n与君歌一曲,请君为我倾耳听。\n钟鼓馔玉不足贵,但愿长醉不复醒。\n古来圣贤皆寂寞,惟有饮者留其名。\n陈王昔时宴平乐,斗酒十千恣欢谑。\n主人何为言少钱,径须沽取对君酌。\n五花马,千金裘,呼儿将出换美酒,与尔同销万古愁。" + }, + { + "act": "春晓 孟浩然", + "prompt": "春晓\n唐·孟浩然\n春眠不觉晓,处处闻啼鸟。\n夜来风雨声,花落知多少。" + }, + { + "act": "早发白帝城 李白", + "prompt": "早发白帝城\n唐·李白\n朝辞白帝彩云间,千里江陵一日还。\n两岸猿声啼不住,轻舟已过万重山。" + }, + { + "act": "登鹳雀楼 王之涣", + "prompt": "登鹳雀楼\n唐·王之涣\n白日依山尽,黄河入海流。\n欲穷千里目,更上一层楼。" + }, + { + "act": "赠汪伦 李白", + "prompt": "赠汪伦\n唐·李白\n李白乘舟将欲行,忽闻岸上踏歌声。\n桃花潭水深千尺,不及汪伦送我情。" + }, + { + "act": "登高 杜甫", + "prompt": "登高\n唐·杜甫\n风急天高猿啸哀,渚清沙白鸟飞回。\n无边落木萧萧下,不尽长江滚滚来。\n万里悲秋常作客,百年多病独登台。\n艰难苦恨繁霜鬓,潦倒新停浊酒杯。" + } +]