Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,412 +1,409 @@
|
|
| 1 |
-
import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
|
| 2 |
-
|
| 3 |
-
help_menu_description = \
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
</br></br
|
| 7 |
-
|
| 8 |
-
</br></br
|
| 9 |
-
</br></br
|
| 10 |
-
</br></br
|
| 11 |
-
</br></br
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
from
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
gr.
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
with gr.Row(
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
with gr.Row():
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
opt =
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
with gr.Column(scale=
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
}
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
#
|
| 220 |
-
ret.update({
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
ret =
|
| 262 |
-
ret.update({
|
| 263 |
-
ret
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
cancel_handles.append(
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
click_handle =
|
| 302 |
-
cancel_handles.append(click_handle)
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
#
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
demo.load(
|
| 378 |
-
demo.load(
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
#
|
| 403 |
-
#
|
| 404 |
-
#
|
| 405 |
-
#
|
| 406 |
-
#
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
if __name__ == "__main__":
|
| 412 |
-
main()
|
|
|
|
| 1 |
+
import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
|
| 2 |
+
|
| 3 |
+
help_menu_description = \
|
| 4 |
+
"""
|
| 5 |
+
</br></br>普通对话使用说明: 1. 输入问题; 2. 点击提交
|
| 6 |
+
</br></br>基础功能区使用说明: 1. 输入文本; 2. 点击任意基础功能区按钮
|
| 7 |
+
</br></br>函数插件区使用说明: 1. 输入路径/问题, 或者上传文件; 2. 点击任意函数插件区按钮
|
| 8 |
+
</br></br>虚空终端使用说明: 点击虚空终端, 然后根据提示输入指令, 再次点击虚空终端
|
| 9 |
+
</br></br>如何保存对话: 点击保存当前的对话按钮
|
| 10 |
+
</br></br>如何语音对话: 请阅读Wiki
|
| 11 |
+
</br></br>如何临时更换API_KEY: 在输入区输入临时API_KEY后提交(网页刷新后失效)"""
|
| 12 |
+
|
| 13 |
+
def main():
|
| 14 |
+
import subprocess, sys
|
| 15 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'https://public.agent-matrix.com/publish/gradio-3.32.8-py3-none-any.whl'])
|
| 16 |
+
import gradio as gr
|
| 17 |
+
if gr.__version__ not in ['3.32.8']:
|
| 18 |
+
raise ModuleNotFoundError("使用项目内置Gradio获取最优体验! 请运行 `pip install -r requirements.txt` 指令安装内置Gradio及其他依赖, 详情信息见requirements.txt.")
|
| 19 |
+
from request_llms.bridge_all import predict
|
| 20 |
+
from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, load_chat_cookies, DummyWith
|
| 21 |
+
# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址
|
| 22 |
+
proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION = get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION')
|
| 23 |
+
CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = get_conf('CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT')
|
| 24 |
+
ENABLE_AUDIO, AUTO_CLEAR_TXT, PATH_LOGGING, AVAIL_THEMES, THEME, ADD_WAIFU = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT', 'PATH_LOGGING', 'AVAIL_THEMES', 'THEME', 'ADD_WAIFU')
|
| 25 |
+
DARK_MODE, NUM_CUSTOM_BASIC_BTN, SSL_KEYFILE, SSL_CERTFILE = get_conf('DARK_MODE', 'NUM_CUSTOM_BASIC_BTN', 'SSL_KEYFILE', 'SSL_CERTFILE')
|
| 26 |
+
INIT_SYS_PROMPT = get_conf('INIT_SYS_PROMPT')
|
| 27 |
+
|
| 28 |
+
# 如果WEB_PORT是-1, 则随机选取WEB端口
|
| 29 |
+
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
|
| 30 |
+
from check_proxy import get_current_version
|
| 31 |
+
from themes.theme import adjust_theme, advanced_css, theme_declaration, js_code_clear, js_code_reset, js_code_show_or_hide, js_code_show_or_hide_group2
|
| 32 |
+
from themes.theme import js_code_for_css_changing, js_code_for_toggle_darkmode, js_code_for_persistent_cookie_init
|
| 33 |
+
from themes.theme import load_dynamic_theme, to_cookie_str, from_cookie_str, init_cookie
|
| 34 |
+
title_html = f"<h1 align=\"center\">GPT 学术优化 {get_current_version()}</h1>{theme_declaration}"
|
| 35 |
+
|
| 36 |
+
# 问询记录, python 版本建议3.9+(越新越好)
|
| 37 |
+
import logging, uuid
|
| 38 |
+
os.makedirs(PATH_LOGGING, exist_ok=True)
|
| 39 |
+
try:logging.basicConfig(filename=f"{PATH_LOGGING}/chat_secrets.log", level=logging.INFO, encoding="utf-8", format="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
|
| 40 |
+
except:logging.basicConfig(filename=f"{PATH_LOGGING}/chat_secrets.log", level=logging.INFO, format="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
|
| 41 |
+
# Disable logging output from the 'httpx' logger
|
| 42 |
+
logging.getLogger("httpx").setLevel(logging.WARNING)
|
| 43 |
+
print(f"所有问询记录将自动保存在本地目录./{PATH_LOGGING}/chat_secrets.log, 请注意自我隐私保护哦!")
|
| 44 |
+
|
| 45 |
+
# 一些普通功能模块
|
| 46 |
+
from core_functional import get_core_functions
|
| 47 |
+
functional = get_core_functions()
|
| 48 |
+
|
| 49 |
+
# 高级函数插件
|
| 50 |
+
from crazy_functional import get_crazy_functions
|
| 51 |
+
DEFAULT_FN_GROUPS = get_conf('DEFAULT_FN_GROUPS')
|
| 52 |
+
plugins = get_crazy_functions()
|
| 53 |
+
all_plugin_groups = list(set([g for _, plugin in plugins.items() for g in plugin['Group'].split('|')]))
|
| 54 |
+
match_group = lambda tags, groups: any([g in groups for g in tags.split('|')])
|
| 55 |
+
|
| 56 |
+
# 处理markdown文本格式的转变
|
| 57 |
+
gr.Chatbot.postprocess = format_io
|
| 58 |
+
|
| 59 |
+
# 做一些外观色彩上的调整
|
| 60 |
+
set_theme = adjust_theme()
|
| 61 |
+
|
| 62 |
+
# 代理与自动更新
|
| 63 |
+
from check_proxy import check_proxy, auto_update, warm_up_modules
|
| 64 |
+
proxy_info = check_proxy(proxies)
|
| 65 |
+
|
| 66 |
+
gr_L1 = lambda: gr.Row().style()
|
| 67 |
+
gr_L2 = lambda scale, elem_id: gr.Column(scale=scale, elem_id=elem_id, min_width=400)
|
| 68 |
+
if LAYOUT == "TOP-DOWN":
|
| 69 |
+
gr_L1 = lambda: DummyWith()
|
| 70 |
+
gr_L2 = lambda scale, elem_id: gr.Row()
|
| 71 |
+
CHATBOT_HEIGHT /= 2
|
| 72 |
+
|
| 73 |
+
cancel_handles = []
|
| 74 |
+
customize_btns = {}
|
| 75 |
+
predefined_btns = {}
|
| 76 |
+
with gr.Blocks(title="GPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
|
| 77 |
+
gr.HTML(title_html)
|
| 78 |
+
gr.HTML('''<center>使用时,先在输入框填入API-KEY然后回车。支持任意数量的OpenAI的密钥和API2D的密钥共存,例如输入"OpenAI密钥1,API2D密钥2",然后提交,即可同时使用两种模型接口。</center>''')
|
| 79 |
+
secret_css, dark_mode, py_pickle_cookie = gr.Textbox(visible=False), gr.Textbox(DARK_MODE, visible=False), gr.Textbox(visible=False)
|
| 80 |
+
cookies = gr.State(load_chat_cookies())
|
| 81 |
+
with gr_L1():
|
| 82 |
+
with gr_L2(scale=2, elem_id="gpt-chat"):
|
| 83 |
+
chatbot = gr.Chatbot(label=f"当前模型:{LLM_MODEL}", elem_id="gpt-chatbot")
|
| 84 |
+
if LAYOUT == "TOP-DOWN": chatbot.style(height=CHATBOT_HEIGHT)
|
| 85 |
+
history = gr.State([])
|
| 86 |
+
with gr_L2(scale=1, elem_id="gpt-panel"):
|
| 87 |
+
with gr.Accordion("输入区", open=True, elem_id="input-panel") as area_input_primary:
|
| 88 |
+
with gr.Row():
|
| 89 |
+
txt = gr.Textbox(show_label=False, lines=2, placeholder="输入问题或API密钥,输入多个密钥时,用英文逗号间隔。支持多个OpenAI密钥共存。").style(container=False)
|
| 90 |
+
with gr.Row():
|
| 91 |
+
submitBtn = gr.Button("提交", elem_id="elem_submit", variant="primary")
|
| 92 |
+
with gr.Row():
|
| 93 |
+
resetBtn = gr.Button("重置", elem_id="elem_reset", variant="secondary"); resetBtn.style(size="sm")
|
| 94 |
+
stopBtn = gr.Button("停止", elem_id="elem_stop", variant="secondary"); stopBtn.style(size="sm")
|
| 95 |
+
clearBtn = gr.Button("清除", elem_id="elem_clear", variant="secondary", visible=False); clearBtn.style(size="sm")
|
| 96 |
+
if ENABLE_AUDIO:
|
| 97 |
+
with gr.Row():
|
| 98 |
+
audio_mic = gr.Audio(source="microphone", type="numpy", elem_id="elem_audio", streaming=True, show_label=False).style(container=False)
|
| 99 |
+
with gr.Row():
|
| 100 |
+
status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}", elem_id="state-panel")
|
| 101 |
+
|
| 102 |
+
with gr.Accordion("基础功能区", open=True, elem_id="basic-panel") as area_basic_fn:
|
| 103 |
+
with gr.Row():
|
| 104 |
+
for k in range(NUM_CUSTOM_BASIC_BTN):
|
| 105 |
+
customize_btn = gr.Button("自定义按钮" + str(k+1), visible=False, variant="secondary", info_str=f'基础功能区: 自定义按钮')
|
| 106 |
+
customize_btn.style(size="sm")
|
| 107 |
+
customize_btns.update({"自定义按钮" + str(k+1): customize_btn})
|
| 108 |
+
for k in functional:
|
| 109 |
+
if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
|
| 110 |
+
variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
|
| 111 |
+
functional[k]["Button"] = gr.Button(k, variant=variant, info_str=f'基础功能区: {k}')
|
| 112 |
+
functional[k]["Button"].style(size="sm")
|
| 113 |
+
predefined_btns.update({k: functional[k]["Button"]})
|
| 114 |
+
with gr.Accordion("函数插件区", open=True, elem_id="plugin-panel") as area_crazy_fn:
|
| 115 |
+
with gr.Row():
|
| 116 |
+
gr.Markdown("插件可读取“输入区”文本/路径作为参数(上传文件自动修正路径)")
|
| 117 |
+
with gr.Row(elem_id="input-plugin-group"):
|
| 118 |
+
plugin_group_sel = gr.Dropdown(choices=all_plugin_groups, label='', show_label=False, value=DEFAULT_FN_GROUPS,
|
| 119 |
+
multiselect=True, interactive=True, elem_classes='normal_mut_select').style(container=False)
|
| 120 |
+
with gr.Row():
|
| 121 |
+
for k, plugin in plugins.items():
|
| 122 |
+
if not plugin.get("AsButton", True): continue
|
| 123 |
+
visible = True if match_group(plugin['Group'], DEFAULT_FN_GROUPS) else False
|
| 124 |
+
variant = plugins[k]["Color"] if "Color" in plugin else "secondary"
|
| 125 |
+
info = plugins[k].get("Info", k)
|
| 126 |
+
plugin['Button'] = plugins[k]['Button'] = gr.Button(k, variant=variant,
|
| 127 |
+
visible=visible, info_str=f'函数插件区: {info}').style(size="sm")
|
| 128 |
+
with gr.Row():
|
| 129 |
+
with gr.Accordion("更多函数插件", open=True):
|
| 130 |
+
dropdown_fn_list = []
|
| 131 |
+
for k, plugin in plugins.items():
|
| 132 |
+
if not match_group(plugin['Group'], DEFAULT_FN_GROUPS): continue
|
| 133 |
+
if not plugin.get("AsButton", True): dropdown_fn_list.append(k) # 排除已经是按钮的插件
|
| 134 |
+
elif plugin.get('AdvancedArgs', False): dropdown_fn_list.append(k) # 对于需要高级参数的插件,亦在下拉菜单中显示
|
| 135 |
+
with gr.Row():
|
| 136 |
+
dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="", show_label=False).style(container=False)
|
| 137 |
+
with gr.Row():
|
| 138 |
+
plugin_advanced_arg = gr.Textbox(show_label=True, label="高级参数输入区", visible=False,
|
| 139 |
+
placeholder="这里是特殊函数插件的高级参数输入区").style(container=False)
|
| 140 |
+
with gr.Row():
|
| 141 |
+
switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary").style(size="sm")
|
| 142 |
+
with gr.Row():
|
| 143 |
+
with gr.Accordion("点击展开“文件下载区”。", open=False) as area_file_up:
|
| 144 |
+
file_upload = gr.Files(label="任何文件, 推荐上传压缩文件(zip, tar)", file_count="multiple", elem_id="elem_upload")
|
| 145 |
+
|
| 146 |
+
with gr.Floating(init_x="0%", init_y="0%", visible=True, width=None, drag="forbidden", elem_id="tooltip"):
|
| 147 |
+
with gr.Row():
|
| 148 |
+
with gr.Tab("上传文件", elem_id="interact-panel"):
|
| 149 |
+
gr.Markdown("请上传本地文件/压缩包供“函数插件区”功能调用。请注意: 上传文件后会自动把输入区修改为相应路径。")
|
| 150 |
+
file_upload_2 = gr.Files(label="任何文件, 推荐上传压缩文件(zip, tar)", file_count="multiple", elem_id="elem_upload_float")
|
| 151 |
+
|
| 152 |
+
with gr.Tab("更换模型", elem_id="interact-panel"):
|
| 153 |
+
md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
|
| 154 |
+
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
|
| 155 |
+
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
|
| 156 |
+
max_length_sl = gr.Slider(minimum=256, maximum=1024*32, value=4096, step=128, interactive=True, label="Local LLM MaxLength",)
|
| 157 |
+
system_prompt = gr.Textbox(show_label=True, lines=2, placeholder=f"System Prompt", label="System prompt", value=INIT_SYS_PROMPT)
|
| 158 |
+
|
| 159 |
+
with gr.Tab("界面外观", elem_id="interact-panel"):
|
| 160 |
+
theme_dropdown = gr.Dropdown(AVAIL_THEMES, value=THEME, label="更换UI主题").style(container=False)
|
| 161 |
+
checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "浮动输入区", "输入清除键", "插件参数区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区", elem_id='cbs').style(container=False)
|
| 162 |
+
opt = ["自定义菜单"]
|
| 163 |
+
value=[]
|
| 164 |
+
if ADD_WAIFU: opt += ["添加Live2D形象"]; value += ["添加Live2D形象"]
|
| 165 |
+
checkboxes_2 = gr.CheckboxGroup(opt, value=value, label="显示/隐藏自定义菜单", elem_id='cbsc').style(container=False)
|
| 166 |
+
dark_mode_btn = gr.Button("切换界面明暗 ☀", variant="secondary").style(size="sm")
|
| 167 |
+
dark_mode_btn.click(None, None, None, _js=js_code_for_toggle_darkmode)
|
| 168 |
+
with gr.Tab("帮助", elem_id="interact-panel"):
|
| 169 |
+
gr.Markdown(help_menu_description)
|
| 170 |
+
|
| 171 |
+
with gr.Floating(init_x="20%", init_y="50%", visible=False, width="40%", drag="top") as area_input_secondary:
|
| 172 |
+
with gr.Accordion("浮动输入区", open=True, elem_id="input-panel2"):
|
| 173 |
+
with gr.Row() as row:
|
| 174 |
+
row.style(equal_height=True)
|
| 175 |
+
with gr.Column(scale=10):
|
| 176 |
+
txt2 = gr.Textbox(show_label=False, placeholder="Input question here.",
|
| 177 |
+
elem_id='user_input_float', lines=8, label="输入区2").style(container=False)
|
| 178 |
+
with gr.Column(scale=1, min_width=40):
|
| 179 |
+
submitBtn2 = gr.Button("提交", variant="primary"); submitBtn2.style(size="sm")
|
| 180 |
+
resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn2.style(size="sm")
|
| 181 |
+
stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn2.style(size="sm")
|
| 182 |
+
clearBtn2 = gr.Button("清除", elem_id="elem_clear2", variant="secondary", visible=False); clearBtn2.style(size="sm")
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
with gr.Floating(init_x="20%", init_y="50%", visible=False, width="40%", drag="top") as area_customize:
|
| 186 |
+
with gr.Accordion("自定义菜单", open=True, elem_id="edit-panel"):
|
| 187 |
+
with gr.Row() as row:
|
| 188 |
+
with gr.Column(scale=10):
|
| 189 |
+
AVAIL_BTN = [btn for btn in customize_btns.keys()] + [k for k in functional]
|
| 190 |
+
basic_btn_dropdown = gr.Dropdown(AVAIL_BTN, value="自定义按钮1", label="选择一个需要自定义基础功能区按钮").style(container=False)
|
| 191 |
+
basic_fn_title = gr.Textbox(show_label=False, placeholder="输入新按钮名称", lines=1).style(container=False)
|
| 192 |
+
basic_fn_prefix = gr.Textbox(show_label=False, placeholder="输入新提示前缀", lines=4).style(container=False)
|
| 193 |
+
basic_fn_suffix = gr.Textbox(show_label=False, placeholder="输入新提示后缀", lines=4).style(container=False)
|
| 194 |
+
with gr.Column(scale=1, min_width=70):
|
| 195 |
+
basic_fn_confirm = gr.Button("确认并保存", variant="primary"); basic_fn_confirm.style(size="sm")
|
| 196 |
+
basic_fn_clean = gr.Button("恢复默认", variant="primary"); basic_fn_clean.style(size="sm")
|
| 197 |
+
def assign_btn(persistent_cookie_, cookies_, basic_btn_dropdown_, basic_fn_title, basic_fn_prefix, basic_fn_suffix, clean_up=False):
|
| 198 |
+
ret = {}
|
| 199 |
+
# 读取之前的自定义按钮
|
| 200 |
+
customize_fn_overwrite_ = cookies_['customize_fn_overwrite']
|
| 201 |
+
# 更新新的自定义按钮
|
| 202 |
+
customize_fn_overwrite_.update({
|
| 203 |
+
basic_btn_dropdown_:
|
| 204 |
+
{
|
| 205 |
+
"Title":basic_fn_title,
|
| 206 |
+
"Prefix":basic_fn_prefix,
|
| 207 |
+
"Suffix":basic_fn_suffix,
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
)
|
| 211 |
+
if clean_up:
|
| 212 |
+
customize_fn_overwrite_ = {}
|
| 213 |
+
cookies_.update(customize_fn_overwrite_) # 更新cookie
|
| 214 |
+
visible = (not clean_up) and (basic_fn_title != "")
|
| 215 |
+
if basic_btn_dropdown_ in customize_btns:
|
| 216 |
+
# 是自定义按钮,不是预定义按钮
|
| 217 |
+
ret.update({customize_btns[basic_btn_dropdown_]: gr.update(visible=visible, value=basic_fn_title)})
|
| 218 |
+
else:
|
| 219 |
+
# 是预定义按钮
|
| 220 |
+
ret.update({predefined_btns[basic_btn_dropdown_]: gr.update(visible=visible, value=basic_fn_title)})
|
| 221 |
+
ret.update({cookies: cookies_})
|
| 222 |
+
try: persistent_cookie_ = from_cookie_str(persistent_cookie_) # persistent cookie to dict
|
| 223 |
+
except: persistent_cookie_ = {}
|
| 224 |
+
persistent_cookie_["custom_bnt"] = customize_fn_overwrite_ # dict update new value
|
| 225 |
+
persistent_cookie_ = to_cookie_str(persistent_cookie_) # persistent cookie to dict
|
| 226 |
+
ret.update({py_pickle_cookie: persistent_cookie_}) # write persistent cookie
|
| 227 |
+
return ret
|
| 228 |
+
|
| 229 |
+
# update btn
|
| 230 |
+
h = basic_fn_confirm.click(assign_btn, [py_pickle_cookie, cookies, basic_btn_dropdown, basic_fn_title, basic_fn_prefix, basic_fn_suffix],
|
| 231 |
+
[py_pickle_cookie, cookies, *customize_btns.values(), *predefined_btns.values()])
|
| 232 |
+
h.then(None, [py_pickle_cookie], None, _js="""(py_pickle_cookie)=>{setCookie("py_pickle_cookie", py_pickle_cookie, 365);}""")
|
| 233 |
+
# clean up btn
|
| 234 |
+
h2 = basic_fn_clean.click(assign_btn, [py_pickle_cookie, cookies, basic_btn_dropdown, basic_fn_title, basic_fn_prefix, basic_fn_suffix, gr.State(True)],
|
| 235 |
+
[py_pickle_cookie, cookies, *customize_btns.values(), *predefined_btns.values()])
|
| 236 |
+
h2.then(None, [py_pickle_cookie], None, _js="""(py_pickle_cookie)=>{setCookie("py_pickle_cookie", py_pickle_cookie, 365);}""")
|
| 237 |
+
|
| 238 |
+
def persistent_cookie_reload(persistent_cookie_, cookies_):
|
| 239 |
+
ret = {}
|
| 240 |
+
for k in customize_btns:
|
| 241 |
+
ret.update({customize_btns[k]: gr.update(visible=False, value="")})
|
| 242 |
+
|
| 243 |
+
try: persistent_cookie_ = from_cookie_str(persistent_cookie_) # persistent cookie to dict
|
| 244 |
+
except: return ret
|
| 245 |
+
|
| 246 |
+
customize_fn_overwrite_ = persistent_cookie_.get("custom_bnt", {})
|
| 247 |
+
cookies_['customize_fn_overwrite'] = customize_fn_overwrite_
|
| 248 |
+
ret.update({cookies: cookies_})
|
| 249 |
+
|
| 250 |
+
for k,v in persistent_cookie_["custom_bnt"].items():
|
| 251 |
+
if v['Title'] == "": continue
|
| 252 |
+
if k in customize_btns: ret.update({customize_btns[k]: gr.update(visible=True, value=v['Title'])})
|
| 253 |
+
else: ret.update({predefined_btns[k]: gr.update(visible=True, value=v['Title'])})
|
| 254 |
+
return ret
|
| 255 |
+
|
| 256 |
+
# 功能区显示开关与功能区的互动
|
| 257 |
+
def fn_area_visibility(a):
|
| 258 |
+
ret = {}
|
| 259 |
+
ret.update({area_input_primary: gr.update(visible=("浮动输入区" not in a))})
|
| 260 |
+
ret.update({area_input_secondary: gr.update(visible=("浮动输入区" in a))})
|
| 261 |
+
ret.update({plugin_advanced_arg: gr.update(visible=("插件参数区" in a))})
|
| 262 |
+
if "浮动输入区" in a: ret.update({txt: gr.update(value="")})
|
| 263 |
+
return ret
|
| 264 |
+
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, plugin_advanced_arg] )
|
| 265 |
+
checkboxes.select(None, [checkboxes], None, _js=js_code_show_or_hide)
|
| 266 |
+
|
| 267 |
+
# 功能区显示开关与功能区的互动
|
| 268 |
+
def fn_area_visibility_2(a):
|
| 269 |
+
ret = {}
|
| 270 |
+
ret.update({area_customize: gr.update(visible=("自定义菜单" in a))})
|
| 271 |
+
return ret
|
| 272 |
+
checkboxes_2.select(fn_area_visibility_2, [checkboxes_2], [area_customize] )
|
| 273 |
+
checkboxes_2.select(None, [checkboxes_2], None, _js=js_code_show_or_hide_group2)
|
| 274 |
+
|
| 275 |
+
# 整理反复出现的控件句柄组合
|
| 276 |
+
input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg]
|
| 277 |
+
output_combo = [cookies, chatbot, history, status]
|
| 278 |
+
predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True)], outputs=output_combo)
|
| 279 |
+
# 提交按钮、重置按钮
|
| 280 |
+
cancel_handles.append(txt.submit(**predict_args))
|
| 281 |
+
cancel_handles.append(txt2.submit(**predict_args))
|
| 282 |
+
cancel_handles.append(submitBtn.click(**predict_args))
|
| 283 |
+
cancel_handles.append(submitBtn2.click(**predict_args))
|
| 284 |
+
resetBtn.click(None, None, [chatbot, history, status], _js=js_code_reset) # 先在前端快速清除chatbot&status
|
| 285 |
+
resetBtn2.click(None, None, [chatbot, history, status], _js=js_code_reset) # 先在前端快速清除chatbot&status
|
| 286 |
+
resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, status]) # 再在后端清除history
|
| 287 |
+
resetBtn2.click(lambda: ([], [], "已重置"), None, [chatbot, history, status]) # 再在后端清除history
|
| 288 |
+
clearBtn.click(None, None, [txt, txt2], _js=js_code_clear)
|
| 289 |
+
clearBtn2.click(None, None, [txt, txt2], _js=js_code_clear)
|
| 290 |
+
if AUTO_CLEAR_TXT:
|
| 291 |
+
submitBtn.click(None, None, [txt, txt2], _js=js_code_clear)
|
| 292 |
+
submitBtn2.click(None, None, [txt, txt2], _js=js_code_clear)
|
| 293 |
+
txt.submit(None, None, [txt, txt2], _js=js_code_clear)
|
| 294 |
+
txt2.submit(None, None, [txt, txt2], _js=js_code_clear)
|
| 295 |
+
# 基础功能区的回调函数注册
|
| 296 |
+
for k in functional:
|
| 297 |
+
if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
|
| 298 |
+
click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo)
|
| 299 |
+
cancel_handles.append(click_handle)
|
| 300 |
+
for btn in customize_btns.values():
|
| 301 |
+
click_handle = btn.click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(btn.value)], outputs=output_combo)
|
| 302 |
+
cancel_handles.append(click_handle)
|
| 303 |
+
# 文件上传区,接收文件后与chatbot的互动
|
| 304 |
+
file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt, txt2, checkboxes, cookies], [chatbot, txt, txt2, cookies]).then(None, None, None, _js=r"()=>{toast_push('上传完毕 ...'); cancel_loading_status();}")
|
| 305 |
+
file_upload_2.upload(on_file_uploaded, [file_upload_2, chatbot, txt, txt2, checkboxes, cookies], [chatbot, txt, txt2, cookies]).then(None, None, None, _js=r"()=>{toast_push('上传完毕 ...'); cancel_loading_status();}")
|
| 306 |
+
# 函数插件-固定按钮区
|
| 307 |
+
for k in plugins:
|
| 308 |
+
if not plugins[k].get("AsButton", True): continue
|
| 309 |
+
click_handle = plugins[k]["Button"].click(ArgsGeneralWrapper(plugins[k]["Function"]), [*input_combo], output_combo)
|
| 310 |
+
click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
|
| 311 |
+
cancel_handles.append(click_handle)
|
| 312 |
+
# 函数插件-下拉菜单与随变按钮的互动
|
| 313 |
+
def on_dropdown_changed(k):
|
| 314 |
+
variant = plugins[k]["Color"] if "Color" in plugins[k] else "secondary"
|
| 315 |
+
info = plugins[k].get("Info", k)
|
| 316 |
+
ret = {switchy_bt: gr.update(value=k, variant=variant, info_str=f'函数插件区: {info}')}
|
| 317 |
+
if plugins[k].get("AdvancedArgs", False): # 是否唤起高级插件参数区
|
| 318 |
+
ret.update({plugin_advanced_arg: gr.update(visible=True, label=f"插件[{k}]的高级参数说明:" + plugins[k].get("ArgsReminder", [f"没有提供高级参数功能说明"]))})
|
| 319 |
+
else:
|
| 320 |
+
ret.update({plugin_advanced_arg: gr.update(visible=False, label=f"插件[{k}]不需要高级参数。")})
|
| 321 |
+
return ret
|
| 322 |
+
dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt, plugin_advanced_arg] )
|
| 323 |
+
|
| 324 |
+
def on_md_dropdown_changed(k):
|
| 325 |
+
return {chatbot: gr.update(label="当前模型:"+k)}
|
| 326 |
+
md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
|
| 327 |
+
|
| 328 |
+
def on_theme_dropdown_changed(theme, secret_css):
|
| 329 |
+
adjust_theme, css_part1, _, adjust_dynamic_theme = load_dynamic_theme(theme)
|
| 330 |
+
if adjust_dynamic_theme:
|
| 331 |
+
css_part2 = adjust_dynamic_theme._get_theme_css()
|
| 332 |
+
else:
|
| 333 |
+
css_part2 = adjust_theme()._get_theme_css()
|
| 334 |
+
return css_part2 + css_part1
|
| 335 |
+
|
| 336 |
+
theme_handle = theme_dropdown.select(on_theme_dropdown_changed, [theme_dropdown, secret_css], [secret_css])
|
| 337 |
+
theme_handle.then(
|
| 338 |
+
None,
|
| 339 |
+
[secret_css],
|
| 340 |
+
None,
|
| 341 |
+
_js=js_code_for_css_changing
|
| 342 |
+
)
|
| 343 |
+
# 随变按钮的回调函数注册
|
| 344 |
+
def route(request: gr.Request, k, *args, **kwargs):
|
| 345 |
+
if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
|
| 346 |
+
yield from ArgsGeneralWrapper(plugins[k]["Function"])(request, *args, **kwargs)
|
| 347 |
+
click_handle = switchy_bt.click(route,[switchy_bt, *input_combo], output_combo)
|
| 348 |
+
click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
|
| 349 |
+
cancel_handles.append(click_handle)
|
| 350 |
+
# 终止按钮的回调函数注册
|
| 351 |
+
stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
|
| 352 |
+
stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
|
| 353 |
+
plugins_as_btn = {name:plugin for name, plugin in plugins.items() if plugin.get('Button', None)}
|
| 354 |
+
def on_group_change(group_list):
|
| 355 |
+
btn_list = []
|
| 356 |
+
fns_list = []
|
| 357 |
+
if not group_list: # 处理特殊情况:没有选择任何插件组
|
| 358 |
+
return [*[plugin['Button'].update(visible=False) for _, plugin in plugins_as_btn.items()], gr.Dropdown.update(choices=[])]
|
| 359 |
+
for k, plugin in plugins.items():
|
| 360 |
+
if plugin.get("AsButton", True):
|
| 361 |
+
btn_list.append(plugin['Button'].update(visible=match_group(plugin['Group'], group_list))) # 刷新按钮
|
| 362 |
+
if plugin.get('AdvancedArgs', False): dropdown_fn_list.append(k) # 对于需要高级参数的插件,亦在下拉菜单中显示
|
| 363 |
+
elif match_group(plugin['Group'], group_list): fns_list.append(k) # 刷新下拉列表
|
| 364 |
+
return [*btn_list, gr.Dropdown.update(choices=fns_list)]
|
| 365 |
+
plugin_group_sel.select(fn=on_group_change, inputs=[plugin_group_sel], outputs=[*[plugin['Button'] for name, plugin in plugins_as_btn.items()], dropdown])
|
| 366 |
+
if ENABLE_AUDIO:
|
| 367 |
+
from crazy_functions.live_audio.audio_io import RealtimeAudioDistribution
|
| 368 |
+
rad = RealtimeAudioDistribution()
|
| 369 |
+
def deal_audio(audio, cookies):
|
| 370 |
+
rad.feed(cookies['uuid'].hex, audio)
|
| 371 |
+
audio_mic.stream(deal_audio, inputs=[audio_mic, cookies])
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
demo.load(init_cookie, inputs=[cookies], outputs=[cookies])
|
| 375 |
+
demo.load(persistent_cookie_reload, inputs = [py_pickle_cookie, cookies],
|
| 376 |
+
outputs = [py_pickle_cookie, cookies, *customize_btns.values(), *predefined_btns.values()], _js=js_code_for_persistent_cookie_init)
|
| 377 |
+
demo.load(None, inputs=[dark_mode], outputs=None, _js="""(dark_mode)=>{apply_cookie_for_checkbox(dark_mode);}""") # 配置暗色主题或亮色主题
|
| 378 |
+
demo.load(None, inputs=[gr.Textbox(LAYOUT, visible=False)], outputs=None, _js='(LAYOUT)=>{GptAcademicJavaScriptInit(LAYOUT);}')
|
| 379 |
+
|
| 380 |
+
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
| 381 |
+
def run_delayed_tasks():
|
| 382 |
+
import threading, webbrowser, time
|
| 383 |
+
print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
|
| 384 |
+
if DARK_MODE: print(f"\t「暗色主题已启用(支持动态切换主题)」: http://localhost:{PORT}")
|
| 385 |
+
else: print(f"\t「亮色主题已启用(支持动态切换主题)」: http://localhost:{PORT}")
|
| 386 |
+
|
| 387 |
+
def auto_updates(): time.sleep(0); auto_update()
|
| 388 |
+
def open_browser(): time.sleep(2); webbrowser.open_new_tab(f"http://localhost:{PORT}")
|
| 389 |
+
def warm_up_mods(): time.sleep(6); warm_up_modules()
|
| 390 |
+
|
| 391 |
+
threading.Thread(target=auto_updates, name="self-upgrade", daemon=True).start() # 查看自动更新
|
| 392 |
+
threading.Thread(target=open_browser, name="open-browser", daemon=True).start() # 打开浏览器页面
|
| 393 |
+
threading.Thread(target=warm_up_mods, name="warm-up", daemon=True).start() # 预热tiktoken模块
|
| 394 |
+
|
| 395 |
+
run_delayed_tasks()
|
| 396 |
+
demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=False, favicon_path="docs/logo.png", blocked_paths=["config.py","config_private.py","docker-compose.yml","Dockerfile"])
|
| 397 |
+
|
| 398 |
+
|
| 399 |
+
# 如果需要在二级路径下运行
|
| 400 |
+
# CUSTOM_PATH = get_conf('CUSTOM_PATH')
|
| 401 |
+
# if CUSTOM_PATH != "/":
|
| 402 |
+
# from toolbox import run_gradio_in_subpath
|
| 403 |
+
# run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH)
|
| 404 |
+
# else:
|
| 405 |
+
# demo.launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png",
|
| 406 |
+
# blocked_paths=["config.py","config_private.py","docker-compose.yml","Dockerfile",f"{PATH_LOGGING}/admin"])
|
| 407 |
+
|
| 408 |
+
if __name__ == "__main__":
|
| 409 |
+
main()
|
|
|
|
|
|
|
|
|