hieroglyphs commited on
Commit
f199813
1 Parent(s): 74e7f77
Files changed (1) hide show
  1. app.py +235 -237
app.py CHANGED
@@ -1,237 +1,235 @@
1
- import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
2
-
3
- def main():
4
- import subprocess, sys
5
- subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gradio-stable-fork'])
6
- import gradio as gr
7
- from request_llm.bridge_all import predict
8
- from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, load_chat_cookies, DummyWith
9
- # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
10
- proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = \
11
- get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT')
12
- ENABLE_AUDIO, AUTO_CLEAR_TXT = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT')
13
- # 如果WEB_PORT是-1, 则随机选取WEB端口
14
- PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
15
- if not AUTHENTICATION: AUTHENTICATION = None
16
-
17
- from check_proxy import get_current_version
18
- from themes.theme import adjust_theme, advanced_css, theme_declaration
19
- initial_prompt = "Serve me as a writing and programming assistant."
20
- title_html = f"<h1 align=\"center\">GPT 学术优化 {get_current_version()}</h1>{theme_declaration}"
21
- description = """代码开源和更新[地址🚀](https://github.com/binary-husky/chatgpt_academic),感谢热情的[开发者们❤️](https://github.com/binary-husky/chatgpt_academic/graphs/contributors)"""
22
-
23
- # 问询记录, python 版本建议3.9+(越新越好)
24
- import logging, uuid
25
- os.makedirs("gpt_log", exist_ok=True)
26
- try:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO, encoding="utf-8", format="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
27
- except:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO, format="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
28
- # Disable logging output from the 'httpx' logger
29
- logging.getLogger("httpx").setLevel(logging.WARNING)
30
- print("所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!")
31
-
32
- # 一些普通功能模块
33
- from core_functional import get_core_functions
34
- functional = get_core_functions()
35
-
36
- # 高级函数插件
37
- from crazy_functional import get_crazy_functions
38
- crazy_fns = get_crazy_functions()
39
-
40
- # 处理markdown文本格式的转变
41
- gr.Chatbot.postprocess = format_io
42
-
43
- # 做一些外观色彩上的调整
44
- set_theme = adjust_theme()
45
-
46
- # 代理与自动更新
47
- from check_proxy import check_proxy, auto_update, warm_up_modules
48
- proxy_info = check_proxy(proxies)
49
-
50
- gr_L1 = lambda: gr.Row().style()
51
- gr_L2 = lambda scale, elem_id: gr.Column(scale=scale, elem_id=elem_id)
52
- if LAYOUT == "TOP-DOWN":
53
- gr_L1 = lambda: DummyWith()
54
- gr_L2 = lambda scale, elem_id: gr.Row()
55
- CHATBOT_HEIGHT /= 2
56
-
57
- cancel_handles = []
58
- with gr.Blocks(title="GPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
59
- gr.HTML(title_html)
60
- gr.HTML('''<center><a href="https://huggingface.co/spaces/qingxu98/gpt-academic?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>请您打开此页面后务必点击上方的“复制空间”(Duplicate Space)按钮!<font color="#FF00FF">使用时,先在输入框填入API-KEY然后回车。</font><br/>切忌在“复制空间”(Duplicate Space)之前填入API_KEY或进行提问,否则您的API_KEY将极可能被空间所有者攫取!<br/>支持任意数量的OpenAI的密钥和API2D的密钥共存,例如输入"OpenAI密钥1,API2D密钥2",然后提交,即可同时使用两种模型接口。</center>''')
61
- cookies = gr.State(load_chat_cookies())
62
- with gr_L1():
63
- with gr_L2(scale=2, elem_id="gpt-chat"):
64
- chatbot = gr.Chatbot(label=f"当前模型:{LLM_MODEL}", elem_id="gpt-chatbot")
65
- if LAYOUT == "TOP-DOWN": chatbot.style(height=CHATBOT_HEIGHT)
66
- history = gr.State([])
67
- with gr_L2(scale=1, elem_id="gpt-panel"):
68
- with gr.Accordion("输入区", open=True, elem_id="input-panel") as area_input_primary:
69
- with gr.Row():
70
- txt = gr.Textbox(show_label=False, lines=2, placeholder="输入问题或API密钥,输入多个密钥时,用英文逗号间隔。支持OpenAI密钥和API2D密钥共存。").style(container=False)
71
- with gr.Row():
72
- submitBtn = gr.Button("提交", variant="primary")
73
- with gr.Row():
74
- resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
75
- stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
76
- clearBtn = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm")
77
- if ENABLE_AUDIO:
78
- with gr.Row():
79
- audio_mic = gr.Audio(source="microphone", type="numpy", streaming=True, show_label=False).style(container=False)
80
- with gr.Row():
81
- status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}", elem_id="state-panel")
82
- with gr.Accordion("基础功能区", open=True, elem_id="basic-panel") as area_basic_fn:
83
- with gr.Row():
84
- for k in functional:
85
- if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
86
- variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
87
- functional[k]["Button"] = gr.Button(k, variant=variant)
88
- with gr.Accordion("函数插件区", open=True, elem_id="plugin-panel") as area_crazy_fn:
89
- with gr.Row():
90
- gr.Markdown("插件可读取“输入区”文本/路径作为参数(上传文件自动修正路径)")
91
- with gr.Row():
92
- for k in crazy_fns:
93
- if not crazy_fns[k].get("AsButton", True): continue
94
- variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
95
- crazy_fns[k]["Button"] = gr.Button(k, variant=variant)
96
- crazy_fns[k]["Button"].style(size="sm")
97
- with gr.Row():
98
- with gr.Accordion("更多函数插件", open=True):
99
- dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
100
- with gr.Row():
101
- dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="", show_label=False).style(container=False)
102
- with gr.Row():
103
- plugin_advanced_arg = gr.Textbox(show_label=True, label="高级参数输入区", visible=False,
104
- placeholder="这里是特殊函数插件的高级参数输入区").style(container=False)
105
- with gr.Row():
106
- switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary")
107
- with gr.Row():
108
- with gr.Accordion("点击展开“文件上传区”。上传本地文件/压缩包供函数插件调用。", open=False) as area_file_up:
109
- file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple")
110
- with gr.Accordion("更换模型 & SysPrompt & 交互界面布局", open=(LAYOUT == "TOP-DOWN"), elem_id="interact-panel"):
111
- system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
112
- top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
113
- temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
114
- max_length_sl = gr.Slider(minimum=256, maximum=8192, value=4096, step=1, interactive=True, label="Local LLM MaxLength",)
115
- checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键", "插件参数区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
116
- md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
117
-
118
- gr.Markdown(description)
119
- with gr.Accordion("备选输入区", open=True, visible=False, elem_id="input-panel2") as area_input_secondary:
120
- with gr.Row():
121
- txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False)
122
- with gr.Row():
123
- submitBtn2 = gr.Button("提交", variant="primary")
124
- with gr.Row():
125
- resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn2.style(size="sm")
126
- stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn2.style(size="sm")
127
- clearBtn2 = gr.Button("清除", variant="secondary", visible=False); clearBtn2.style(size="sm")
128
- # 功能区显示开关与功能区的互动
129
- def fn_area_visibility(a):
130
- ret = {}
131
- ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))})
132
- ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
133
- ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))})
134
- ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
135
- ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
136
- ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
137
- ret.update({plugin_advanced_arg: gr.update(visible=("插件参数区" in a))})
138
- if "底部输入区" in a: ret.update({txt: gr.update(value="")})
139
- return ret
140
- checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2, plugin_advanced_arg] )
141
- # 整理反复出现的控件句柄组合
142
- input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg]
143
- output_combo = [cookies, chatbot, history, status]
144
- predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
145
- # 提交按钮、重置按钮
146
- cancel_handles.append(txt.submit(**predict_args))
147
- cancel_handles.append(txt2.submit(**predict_args))
148
- cancel_handles.append(submitBtn.click(**predict_args))
149
- cancel_handles.append(submitBtn2.click(**predict_args))
150
- resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
151
- resetBtn2.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
152
- clearBtn.click(lambda: ("",""), None, [txt, txt2])
153
- clearBtn2.click(lambda: ("",""), None, [txt, txt2])
154
- if AUTO_CLEAR_TXT:
155
- submitBtn.click(lambda: ("",""), None, [txt, txt2])
156
- submitBtn2.click(lambda: ("",""), None, [txt, txt2])
157
- txt.submit(lambda: ("",""), None, [txt, txt2])
158
- txt2.submit(lambda: ("",""), None, [txt, txt2])
159
- # 基础功能区的回调函数注册
160
- for k in functional:
161
- if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
162
- click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo)
163
- cancel_handles.append(click_handle)
164
- # 文件上传区,接收文件后与chatbot的互动
165
- file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt, txt2, checkboxes], [chatbot, txt, txt2])
166
- # 函数插件-固定按钮区
167
- for k in crazy_fns:
168
- if not crazy_fns[k].get("AsButton", True): continue
169
- click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
170
- click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
171
- cancel_handles.append(click_handle)
172
- # 函数插件-下拉菜单与随变按钮的互动
173
- def on_dropdown_changed(k):
174
- variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
175
- ret = {switchy_bt: gr.update(value=k, variant=variant)}
176
- if crazy_fns[k].get("AdvancedArgs", False): # 是否唤起高级插件参数区
177
- ret.update({plugin_advanced_arg: gr.update(visible=True, label=f"插件[{k}]的高级参数说明:" + crazy_fns[k].get("ArgsReminder", [f"没有提供高级参数功能说明"]))})
178
- else:
179
- ret.update({plugin_advanced_arg: gr.update(visible=False, label=f"插件[{k}]不需要高级参数。")})
180
- return ret
181
- dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt, plugin_advanced_arg] )
182
- def on_md_dropdown_changed(k):
183
- return {chatbot: gr.update(label="当前模型:"+k)}
184
- md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
185
- # 随变按钮的回调函数注册
186
- def route(request: gr.Request, k, *args, **kwargs):
187
- if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
188
- yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(request, *args, **kwargs)
189
- click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo)
190
- click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
191
- cancel_handles.append(click_handle)
192
- # 终止按钮的回调函数注册
193
- stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
194
- stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
195
- if ENABLE_AUDIO:
196
- from crazy_functions.live_audio.audio_io import RealtimeAudioDistribution
197
- rad = RealtimeAudioDistribution()
198
- def deal_audio(audio, cookies):
199
- rad.feed(cookies['uuid'].hex, audio)
200
- audio_mic.stream(deal_audio, inputs=[audio_mic, cookies])
201
-
202
- def init_cookie(cookies, chatbot):
203
- # 为每一位访问的用户赋予一个独一无二的uuid编码
204
- cookies.update({'uuid': uuid.uuid4()})
205
- return cookies
206
- demo.load(init_cookie, inputs=[cookies, chatbot], outputs=[cookies])
207
- demo.load(lambda: 0, inputs=None, outputs=None, _js='()=>{ChatBotHeight();}')
208
-
209
- # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
210
- def auto_opentab_delay():
211
- import threading, webbrowser, time
212
- print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
213
- print(f"\t(亮色主题): http://localhost:{PORT}")
214
- print(f"\t(暗色主题): http://localhost:{PORT}/?__theme=dark")
215
- def open():
216
- time.sleep(2) # 打开浏览器
217
- DARK_MODE, = get_conf('DARK_MODE')
218
- if DARK_MODE: webbrowser.open_new_tab(f"http://localhost:{PORT}/?__theme=dark")
219
- else: webbrowser.open_new_tab(f"http://localhost:{PORT}")
220
- threading.Thread(target=open, name="open-browser", daemon=True).start()
221
- threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start()
222
- threading.Thread(target=warm_up_modules, name="warm-up", daemon=True).start()
223
-
224
- auto_opentab_delay()
225
- 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"])
226
-
227
- # 如果需要在二级路径下运行
228
- # CUSTOM_PATH, = get_conf('CUSTOM_PATH')
229
- # if CUSTOM_PATH != "/":
230
- # from toolbox import run_gradio_in_subpath
231
- # run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH)
232
- # else:
233
- # demo.launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png",
234
- # blocked_paths=["config.py","config_private.py","docker-compose.yml","Dockerfile"])
235
-
236
- if __name__ == "__main__":
237
- main()
 
1
+ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
2
+
3
+ def main():
4
+ import subprocess, sys
5
+ subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gradio-stable-fork'])
6
+ import gradio as gr
7
+ from request_llm.bridge_all import predict
8
+ from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, load_chat_cookies, DummyWith
9
+ # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
10
+ proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = \
11
+ get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT')
12
+ ENABLE_AUDIO, AUTO_CLEAR_TXT = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT')
13
+ # 如果WEB_PORT是-1, 则随机选取WEB端口
14
+ PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
15
+ if not AUTHENTICATION: AUTHENTICATION = None
16
+
17
+ from check_proxy import get_current_version
18
+ from themes.theme import adjust_theme, advanced_css, theme_declaration
19
+ initial_prompt = "Serve me as a writing and programming assistant."
20
+ title_html = f"<h1 align=\"center\">GPT for HJLAB {get_current_version()}</h1>{theme_declaration}"
21
+ description = """代码开源和更新[地址🚀](https://github.com/binary-husky/chatgpt_academic),感谢热情的[开发者们❤️](https://github.com/binary-husky/chatgpt_academic/graphs/contributors)"""
22
+
23
+ # 问询记录, python 版本建议3.9+(越新越好)
24
+ import logging, uuid
25
+ os.makedirs("gpt_log", exist_ok=True)
26
+ try:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO, encoding="utf-8", format="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
27
+ except:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO, format="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
28
+ # Disable logging output from the 'httpx' logger
29
+ logging.getLogger("httpx").setLevel(logging.WARNING)
30
+ print("所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!")
31
+
32
+ # 一些普通功能模块
33
+ from core_functional import get_core_functions
34
+ functional = get_core_functions()
35
+
36
+ # 高级函数插件
37
+ from crazy_functional import get_crazy_functions
38
+ crazy_fns = get_crazy_functions()
39
+
40
+ # 处理markdown文本格式的转变
41
+ gr.Chatbot.postprocess = format_io
42
+
43
+ # 做一些外观色彩上的调整
44
+ set_theme = adjust_theme()
45
+
46
+ # 代理与自动更新
47
+ from check_proxy import check_proxy, auto_update, warm_up_modules
48
+ proxy_info = check_proxy(proxies)
49
+
50
+ gr_L1 = lambda: gr.Row().style()
51
+ gr_L2 = lambda scale, elem_id: gr.Column(scale=scale, elem_id=elem_id)
52
+ if LAYOUT == "TOP-DOWN":
53
+ gr_L1 = lambda: DummyWith()
54
+ gr_L2 = lambda scale, elem_id: gr.Row()
55
+ CHATBOT_HEIGHT /= 2
56
+
57
+ cancel_handles = []
58
+ with gr.Blocks(title="GPT for HJLAB", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
59
+ cookies = gr.State(load_chat_cookies())
60
+ with gr_L1():
61
+ with gr_L2(scale=2, elem_id="gpt-chat"):
62
+ chatbot = gr.Chatbot(label=f"当前模型:{LLM_MODEL}", elem_id="gpt-chatbot")
63
+ if LAYOUT == "TOP-DOWN": chatbot.style(height=CHATBOT_HEIGHT)
64
+ history = gr.State([])
65
+ with gr_L2(scale=1, elem_id="gpt-panel"):
66
+ with gr.Accordion("输入区", open=True, elem_id="input-panel") as area_input_primary:
67
+ with gr.Row():
68
+ txt = gr.Textbox(show_label=False, lines=2, placeholder="输入问题或API密钥,输入多个密钥时,用英文逗号间隔。支持OpenAI密钥和API2D密钥共存。").style(container=False)
69
+ with gr.Row():
70
+ submitBtn = gr.Button("提交", variant="primary")
71
+ with gr.Row():
72
+ resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
73
+ stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
74
+ clearBtn = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm")
75
+ if ENABLE_AUDIO:
76
+ with gr.Row():
77
+ audio_mic = gr.Audio(source="microphone", type="numpy", streaming=True, show_label=False).style(container=False)
78
+ with gr.Row():
79
+ status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}", elem_id="state-panel")
80
+ with gr.Accordion("基础功能区", open=True, elem_id="basic-panel") as area_basic_fn:
81
+ with gr.Row():
82
+ for k in functional:
83
+ if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
84
+ variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
85
+ functional[k]["Button"] = gr.Button(k, variant=variant)
86
+ with gr.Accordion("函数插件区", open=True, elem_id="plugin-panel") as area_crazy_fn:
87
+ with gr.Row():
88
+ gr.Markdown("插件可读取“输入区”文本/路径作为参数(上传文件自动修正路径)")
89
+ with gr.Row():
90
+ for k in crazy_fns:
91
+ if not crazy_fns[k].get("AsButton", True): continue
92
+ variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
93
+ crazy_fns[k]["Button"] = gr.Button(k, variant=variant)
94
+ crazy_fns[k]["Button"].style(size="sm")
95
+ with gr.Row():
96
+ with gr.Accordion("更多函数插件", open=True):
97
+ dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
98
+ with gr.Row():
99
+ dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="", show_label=False).style(container=False)
100
+ with gr.Row():
101
+ plugin_advanced_arg = gr.Textbox(show_label=True, label="高级参数输入区", visible=False,
102
+ placeholder="这里是特殊函数插件的高级参数输入区").style(container=False)
103
+ with gr.Row():
104
+ switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary")
105
+ with gr.Row():
106
+ with gr.Accordion("点击展开“文件上传区”。上传本地文件/压缩包供函数插件调用。", open=False) as area_file_up:
107
+ file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple")
108
+ with gr.Accordion("更换模型 & SysPrompt & 交互界面布局", open=(LAYOUT == "TOP-DOWN"), elem_id="interact-panel"):
109
+ system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
110
+ top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
111
+ temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
112
+ max_length_sl = gr.Slider(minimum=256, maximum=8192, value=4096, step=1, interactive=True, label="Local LLM MaxLength",)
113
+ checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键", "插件参数区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
114
+ md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
115
+
116
+ gr.Markdown(description)
117
+ with gr.Accordion("备选输入区", open=True, visible=False, elem_id="input-panel2") as area_input_secondary:
118
+ with gr.Row():
119
+ txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False)
120
+ with gr.Row():
121
+ submitBtn2 = gr.Button("提交", variant="primary")
122
+ with gr.Row():
123
+ resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn2.style(size="sm")
124
+ stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn2.style(size="sm")
125
+ clearBtn2 = gr.Button("清除", variant="secondary", visible=False); clearBtn2.style(size="sm")
126
+ # 功能区显示开关与功能区的互动
127
+ def fn_area_visibility(a):
128
+ ret = {}
129
+ ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))})
130
+ ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
131
+ ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))})
132
+ ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
133
+ ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
134
+ ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
135
+ ret.update({plugin_advanced_arg: gr.update(visible=("插件参数区" in a))})
136
+ if "底部输入区" in a: ret.update({txt: gr.update(value="")})
137
+ return ret
138
+ checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2, plugin_advanced_arg] )
139
+ # 整理反复出现的控件句柄组合
140
+ input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg]
141
+ output_combo = [cookies, chatbot, history, status]
142
+ predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
143
+ # 提交按钮、重置按钮
144
+ cancel_handles.append(txt.submit(**predict_args))
145
+ cancel_handles.append(txt2.submit(**predict_args))
146
+ cancel_handles.append(submitBtn.click(**predict_args))
147
+ cancel_handles.append(submitBtn2.click(**predict_args))
148
+ resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
149
+ resetBtn2.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
150
+ clearBtn.click(lambda: ("",""), None, [txt, txt2])
151
+ clearBtn2.click(lambda: ("",""), None, [txt, txt2])
152
+ if AUTO_CLEAR_TXT:
153
+ submitBtn.click(lambda: ("",""), None, [txt, txt2])
154
+ submitBtn2.click(lambda: ("",""), None, [txt, txt2])
155
+ txt.submit(lambda: ("",""), None, [txt, txt2])
156
+ txt2.submit(lambda: ("",""), None, [txt, txt2])
157
+ # 基础功能区的回调函数注册
158
+ for k in functional:
159
+ if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
160
+ click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo)
161
+ cancel_handles.append(click_handle)
162
+ # 文件上传区,接收文件后与chatbot的互动
163
+ file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt, txt2, checkboxes], [chatbot, txt, txt2])
164
+ # 函数插件-固定按钮区
165
+ for k in crazy_fns:
166
+ if not crazy_fns[k].get("AsButton", True): continue
167
+ click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
168
+ click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
169
+ cancel_handles.append(click_handle)
170
+ # 函数插件-下拉菜单与随变按钮的互动
171
+ def on_dropdown_changed(k):
172
+ variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
173
+ ret = {switchy_bt: gr.update(value=k, variant=variant)}
174
+ if crazy_fns[k].get("AdvancedArgs", False): # 是否唤起高级插件参数区
175
+ ret.update({plugin_advanced_arg: gr.update(visible=True, label=f"插件[{k}]的高级参数说明:" + crazy_fns[k].get("ArgsReminder", [f"没有提供高��参数功能说明"]))})
176
+ else:
177
+ ret.update({plugin_advanced_arg: gr.update(visible=False, label=f"插件[{k}]不需要高级参数。")})
178
+ return ret
179
+ dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt, plugin_advanced_arg] )
180
+ def on_md_dropdown_changed(k):
181
+ return {chatbot: gr.update(label="当前模型:"+k)}
182
+ md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
183
+ # 随变按钮的回调函数注册
184
+ def route(request: gr.Request, k, *args, **kwargs):
185
+ if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
186
+ yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(request, *args, **kwargs)
187
+ click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo)
188
+ click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
189
+ cancel_handles.append(click_handle)
190
+ # 终止按钮的回调函数注册
191
+ stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
192
+ stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
193
+ if ENABLE_AUDIO:
194
+ from crazy_functions.live_audio.audio_io import RealtimeAudioDistribution
195
+ rad = RealtimeAudioDistribution()
196
+ def deal_audio(audio, cookies):
197
+ rad.feed(cookies['uuid'].hex, audio)
198
+ audio_mic.stream(deal_audio, inputs=[audio_mic, cookies])
199
+
200
+ def init_cookie(cookies, chatbot):
201
+ # 为每一位访问的用户赋予一个独一无二的uuid编码
202
+ cookies.update({'uuid': uuid.uuid4()})
203
+ return cookies
204
+ demo.load(init_cookie, inputs=[cookies, chatbot], outputs=[cookies])
205
+ demo.load(lambda: 0, inputs=None, outputs=None, _js='()=>{ChatBotHeight();}')
206
+
207
+ # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
208
+ def auto_opentab_delay():
209
+ import threading, webbrowser, time
210
+ print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
211
+ print(f"\t(亮色主题): http://localhost:{PORT}")
212
+ print(f"\t(暗色主题): http://localhost:{PORT}/?__theme=dark")
213
+ def open():
214
+ time.sleep(2) # 打开浏览器
215
+ DARK_MODE, = get_conf('DARK_MODE')
216
+ if DARK_MODE: webbrowser.open_new_tab(f"http://localhost:{PORT}/?__theme=dark")
217
+ else: webbrowser.open_new_tab(f"http://localhost:{PORT}")
218
+ threading.Thread(target=open, name="open-browser", daemon=True).start()
219
+ threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start()
220
+ threading.Thread(target=warm_up_modules, name="warm-up", daemon=True).start()
221
+
222
+ auto_opentab_delay()
223
+ 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"])
224
+
225
+ # 如果需要在二级路径下运行
226
+ # CUSTOM_PATH, = get_conf('CUSTOM_PATH')
227
+ # if CUSTOM_PATH != "/":
228
+ # from toolbox import run_gradio_in_subpath
229
+ # run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH)
230
+ # else:
231
+ # demo.launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png",
232
+ # blocked_paths=["config.py","config_private.py","docker-compose.yml","Dockerfile"])
233
+
234
+ if __name__ == "__main__":
235
+ main()