MaktubCN commited on
Commit
e27de37
1 Parent(s): a4fa52a

Update app.py

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