9 Commits
Author SHA1 Message Date
qingxu fu 0aeb5b28cd 改进效率 2023-04-05 00:25:53 +08:00
qingxu fu 1dd1720d38 Merge branch 'dev_ui' of https://github.com/binary-husky/chatgpt_academic into dev_ui 2023-04-05 00:15:09 +08:00
qingxu fu 19be0490af BUG FIX 2023-04-05 00:11:12 +08:00
qingxu fu 0c9e18291a BUG FIX 2023-04-05 00:10:06 +08:00
qingxu fu 9f47d0f714 Bug Fix: Hot Reload Wapper For All 2023-04-05 00:09:13 +08:00
qingxu fu 7a254c150f 参数输入bug修复 2023-04-05 00:07:08 +08:00
qingxu fu 3648648b3d 支持更多界面布局的切换 2023-04-04 23:46:47 +08:00
qingxu fu 1da60b7a0c merge 2023-04-04 22:56:06 +08:00
qingxu fu c40f6f00bb check_new_version 2023-04-04 22:54:08 +08:00
8 changed files with 126 additions and 59 deletions
+24
View File
@@ -19,6 +19,30 @@ def check_proxy(proxies):
return result return result
def auto_update():
from toolbox import get_conf
import requests, time, json
proxies, = get_conf('proxies')
response = requests.get("https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version",
proxies=proxies, timeout=1)
remote_json_data = json.loads(response.text)
remote_version = remote_json_data['version']
if remote_json_data["show_feature"]:
new_feature = "新功能:" + remote_json_data["new_feature"]
else:
new_feature = ""
with open('./version', 'r', encoding='utf8') as f:
current_version = f.read()
current_version = json.loads(current_version)['version']
if (remote_version - current_version) >= 0.05:
print(f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}{new_feature}')
print('Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n')
time.sleep(3)
return
else:
return
if __name__ == '__main__': if __name__ == '__main__':
import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
from toolbox import get_conf from toolbox import get_conf
+3
View File
@@ -24,6 +24,9 @@ else:
# 对话窗的高度 # 对话窗的高度
CHATBOT_HEIGHT = 1115 CHATBOT_HEIGHT = 1115
# 窗口布局
LAYOUT = "LEFT-RIGHT" # "LEFT-RIGHT"(左右布局) # "TOP-DOWN"(上下布局)
# 发送请求到OpenAI后,等待多久判定为超时 # 发送请求到OpenAI后,等待多久判定为超时
TIMEOUT_SECONDS = 25 TIMEOUT_SECONDS = 25
@@ -10,16 +10,13 @@ def extract_code_block_carefully(txt):
txt_out = '```'.join(splitted[1:-1]) txt_out = '```'.join(splitted[1:-1])
return txt_out return txt_out
def breakdown_txt_to_satisfy_token_limit(txt, limit, must_break_at_empty_line=True): def breakdown_txt_to_satisfy_token_limit(txt, get_token_fn, limit, must_break_at_empty_line=True):
from transformers import GPT2TokenizerFast
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
get_token_cnt = lambda txt: len(tokenizer(txt)["input_ids"])
def cut(txt_tocut, must_break_at_empty_line): # 递归 def cut(txt_tocut, must_break_at_empty_line): # 递归
if get_token_cnt(txt_tocut) <= limit: if get_token_fn(txt_tocut) <= limit:
return [txt_tocut] return [txt_tocut]
else: else:
lines = txt_tocut.split('\n') lines = txt_tocut.split('\n')
estimated_line_cut = limit / get_token_cnt(txt_tocut) * len(lines) estimated_line_cut = limit / get_token_fn(txt_tocut) * len(lines)
estimated_line_cut = int(estimated_line_cut) estimated_line_cut = int(estimated_line_cut)
for cnt in reversed(range(estimated_line_cut)): for cnt in reversed(range(estimated_line_cut)):
if must_break_at_empty_line: if must_break_at_empty_line:
@@ -27,7 +24,7 @@ def breakdown_txt_to_satisfy_token_limit(txt, limit, must_break_at_empty_line=Tr
print(cnt) print(cnt)
prev = "\n".join(lines[:cnt]) prev = "\n".join(lines[:cnt])
post = "\n".join(lines[cnt:]) post = "\n".join(lines[cnt:])
if get_token_cnt(prev) < limit: break if get_token_fn(prev) < limit: break
if cnt == 0: if cnt == 0:
print('what the f?') print('what the f?')
raise RuntimeError("存在一行极长的文本!") raise RuntimeError("存在一行极长的文本!")
@@ -86,12 +83,12 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt,
# 第5步:Token限制下的截断与处理 # 第5步:Token限制下的截断与处理
MAX_TOKEN = 2500 MAX_TOKEN = 3000
# from transformers import GPT2TokenizerFast from transformers import GPT2TokenizerFast
# print('加载tokenizer中') print('加载tokenizer中')
# tokenizer = GPT2TokenizerFast.from_pretrained("gpt2") tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
# get_token_cnt = lambda txt: len(tokenizer(txt)["input_ids"]) get_token_fn = lambda txt: len(tokenizer(txt)["input_ids"])
# print('加载tokenizer结束') print('加载tokenizer结束')
# 第6步:任务函数 # 第6步:任务函数
@@ -107,7 +104,7 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt,
try: try:
gpt_say = "" gpt_say = ""
# 分解代码文件 # 分解代码文件
file_content_breakdown = breakdown_txt_to_satisfy_token_limit(file_content, MAX_TOKEN) file_content_breakdown = breakdown_txt_to_satisfy_token_limit(file_content, get_token_fn, MAX_TOKEN)
for file_content_partial in file_content_breakdown: for file_content_partial in file_content_breakdown:
i_say = i_say_template(fp, file_content_partial) i_say = i_say_template(fp, file_content_partial)
# # ** gpt request ** # # ** gpt request **
+2 -3
View File
@@ -119,8 +119,8 @@ def 解析一个C项目的头文件(txt, top_p, temperature, chatbot, history, s
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}") report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
yield chatbot, history, '正常' yield chatbot, history, '正常'
return return
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.h', recursive=True)] + \ file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.h', recursive=True)] # + \
[f for f in glob.glob(f'{project_folder}/**/*.hpp', recursive=True)] #+ \ # [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
# [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)] # [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
if len(file_manifest) == 0: if len(file_manifest) == 0:
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}") report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
@@ -141,7 +141,6 @@ def 解析一个C项目(txt, top_p, temperature, chatbot, history, systemPromptT
return return
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.h', recursive=True)] + \ file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.h', recursive=True)] + \
[f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \ [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
[f for f in glob.glob(f'{project_folder}/**/*.hpp', recursive=True)] + \
[f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)] [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
if len(file_manifest) == 0: if len(file_manifest) == 0:
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}") report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
+9 -9
View File
@@ -18,43 +18,43 @@ def get_crazy_functionals():
function_plugins = { function_plugins = {
"请解析并解构此项目本身(源码自译解)": { "请解析并解构此项目本身(源码自译解)": {
"AsButton": False, # 加入下拉菜单中 "AsButton": False, # 加入下拉菜单中
"Function": 解析项目本身 "Function": HotReload(解析项目本身)
}, },
"解析整个Py项目": { "解析整个Py项目": {
"Color": "stop", # 按钮颜色 "Color": "stop", # 按钮颜色
"Function": 解析一个Python项目 "Function": HotReload(解析一个Python项目)
}, },
"解析整个C++项目头文件": { "解析整个C++项目头文件": {
"Color": "stop", # 按钮颜色 "Color": "stop", # 按钮颜色
"Function": 解析一个C项目的头文件 "Function": HotReload(解析一个C项目的头文件)
}, },
"解析整个C++项目(.cpp/.h": { "解析整个C++项目(.cpp/.h": {
"Color": "stop", # 按钮颜色 "Color": "stop", # 按钮颜色
"AsButton": False, # 加入下拉菜单中 "AsButton": False, # 加入下拉菜单中
"Function": 解析一个C项目 "Function": HotReload(解析一个C项目)
}, },
"解析整个Go项目": { "解析整个Go项目": {
"Color": "stop", # 按钮颜色 "Color": "stop", # 按钮颜色
"AsButton": False, # 加入下拉菜单中 "AsButton": False, # 加入下拉菜单中
"Function": 解析一个Golang项目 "Function": HotReload(解析一个Golang项目)
}, },
"解析整个Java项目": { "解析整个Java项目": {
"Color": "stop", # 按钮颜色 "Color": "stop", # 按钮颜色
"AsButton": False, # 加入下拉菜单中 "AsButton": False, # 加入下拉菜单中
"Function": 解析一个Java项目 "Function": HotReload(解析一个Java项目)
}, },
"解析整个React项目": { "解析整个React项目": {
"Color": "stop", # 按钮颜色 "Color": "stop", # 按钮颜色
"AsButton": False, # 加入下拉菜单中 "AsButton": False, # 加入下拉菜单中
"Function": 解析一个Rect项目 "Function": HotReload(解析一个Rect项目)
}, },
"读Tex论文写摘要": { "读Tex论文写摘要": {
"Color": "stop", # 按钮颜色 "Color": "stop", # 按钮颜色
"Function": 读文章写摘要 "Function": HotReload(读文章写摘要)
}, },
"批量生成函数注释": { "批量生成函数注释": {
"Color": "stop", # 按钮颜色 "Color": "stop", # 按钮颜色
"Function": 批量生成函数注释 "Function": HotReload(批量生成函数注释)
}, },
"[多线程demo] 把本项目源代码切换成全英文": { "[多线程demo] 把本项目源代码切换成全英文": {
# HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效 # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效
+57 -30
View File
@@ -1,11 +1,11 @@
import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
import gradio as gr import gradio as gr
from predict import predict from predict import predict
from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith
# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT = \ proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT = \
get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT') get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT')
# 如果WEB_PORT是-1, 则随机选取WEB端口 # 如果WEB_PORT是-1, 则随机选取WEB端口
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
@@ -37,25 +37,36 @@ gr.Chatbot.postprocess = format_io
from theme import adjust_theme, advanced_css from theme import adjust_theme, advanced_css
set_theme = adjust_theme() set_theme = adjust_theme()
# 代理与自动更新
from check_proxy import check_proxy, auto_update
proxy_info = check_proxy(proxies)
gr_L1 = lambda: gr.Row().style()
gr_L2 = lambda scale: gr.Column(scale=scale)
if LAYOUT == "TOP-DOWN":
gr_L1 = lambda: DummyWith()
gr_L2 = lambda scale: gr.Row()
CHATBOT_HEIGHT /= 2
cancel_handles = [] cancel_handles = []
with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as demo: with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
gr.HTML(title_html) gr.HTML(title_html)
with gr.Row().style(equal_height=True): with gr_L1():
with gr.Column(scale=2): with gr_L2(scale=2):
chatbot = gr.Chatbot() chatbot = gr.Chatbot()
chatbot.style(height=CHATBOT_HEIGHT) chatbot.style(height=CHATBOT_HEIGHT)
history = gr.State([]) history = gr.State([])
with gr.Column(scale=1): with gr_L2(scale=1):
with gr.Row(): with gr.Accordion("输入区", open=True) as area_input_primary:
txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False) with gr.Row():
with gr.Row(): txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False)
submitBtn = gr.Button("提交", variant="primary") with gr.Row():
with gr.Row(): submitBtn = gr.Button("提交", variant="primary")
resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm") with gr.Row():
stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm") resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
with gr.Row(): stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
from check_proxy import check_proxy with gr.Row():
status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {check_proxy(proxies)}") status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}")
with gr.Accordion("基础功能区", open=True) as area_basic_fn: with gr.Accordion("基础功能区", open=True) as area_basic_fn:
with gr.Row(): with gr.Row():
for k in functional: for k in functional:
@@ -63,12 +74,13 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as de
functional[k]["Button"] = gr.Button(k, variant=variant) functional[k]["Button"] = gr.Button(k, variant=variant)
with gr.Accordion("函数插件区", open=True) as area_crazy_fn: with gr.Accordion("函数插件区", open=True) as area_crazy_fn:
with gr.Row(): with gr.Row():
gr.Markdown("注意:以下“红颜色”标识的函数插件需从input区读取路径作为参数.") gr.Markdown("注意:以下“红颜色”标识的函数插件需从输入区读取路径作为参数.")
with gr.Row(): with gr.Row():
for k in crazy_fns: for k in crazy_fns:
if not crazy_fns[k].get("AsButton", True): continue if not crazy_fns[k].get("AsButton", True): continue
variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary" variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
crazy_fns[k]["Button"] = gr.Button(k, variant=variant) crazy_fns[k]["Button"] = gr.Button(k, variant=variant)
crazy_fns[k]["Button"].style(size="sm")
with gr.Row(): with gr.Row():
with gr.Accordion("更多函数插件", open=True): with gr.Accordion("更多函数插件", open=True):
dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)] dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
@@ -79,38 +91,51 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as de
with gr.Row(): with gr.Row():
with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up: with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple") file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple")
with gr.Accordion("展开SysPrompt & 交互界面布局 & Github地址", open=False): with gr.Accordion("展开SysPrompt & 交互界面布局 & Github地址", open=(LAYOUT == "TOP-DOWN")):
system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt) system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",) top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",) temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区") checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
gr.Markdown(description) gr.Markdown(description)
with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary:
with gr.Row():
txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False)
with gr.Row():
submitBtn2 = gr.Button("提交", variant="primary")
with gr.Row():
resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
# 功能区显示开关与功能区的互动 # 功能区显示开关与功能区的互动
def fn_area_visibility(a): def fn_area_visibility(a):
ret = {} ret = {}
ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))}) ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))})
ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))}) ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))})
ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
if "底部输入区" in a: ret.update({txt: gr.update(value="")})
return ret return ret
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn] ) checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] )
# 整理反复出现的控件句柄组合 # 整理反复出现的控件句柄组合
input_combo = [txt, top_p, temperature, chatbot, history, system_prompt] input_combo = [txt, txt2, top_p, temperature, chatbot, history, system_prompt]
output_combo = [chatbot, history, status] output_combo = [chatbot, history, status]
predict_args = dict(fn=predict, inputs=input_combo, outputs=output_combo) predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) # 用于在提交后清空输入栏
# 提交按钮、重置按钮 # 提交按钮、重置按钮
cancel_handles.append(txt.submit(**predict_args)) #; txt.submit(**empty_txt_args) 在提交后清空输入栏 cancel_handles.append(txt.submit(**predict_args))
cancel_handles.append(submitBtn.click(**predict_args)) #; submitBtn.click(**empty_txt_args) 在提交后清空输入栏 cancel_handles.append(txt2.submit(**predict_args))
cancel_handles.append(submitBtn.click(**predict_args))
cancel_handles.append(submitBtn2.click(**predict_args))
resetBtn.click(lambda: ([], [], "已重置"), None, output_combo) resetBtn.click(lambda: ([], [], "已重置"), None, output_combo)
resetBtn2.click(lambda: ([], [], "已重置"), None, output_combo)
# 基础功能区的回调函数注册 # 基础功能区的回调函数注册
for k in functional: for k in functional:
click_handle = functional[k]["Button"].click(predict, [*input_combo, gr.State(True), gr.State(k)], output_combo) click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo)
cancel_handles.append(click_handle) cancel_handles.append(click_handle)
# 文件上传区,接收文件后与chatbot的互动 # 文件上传区,接收文件后与chatbot的互动
file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt])
# 函数插件-固定按钮区 # 函数插件-固定按钮区
for k in crazy_fns: for k in crazy_fns:
if not crazy_fns[k].get("AsButton", True): continue if not crazy_fns[k].get("AsButton", True): continue
click_handle = crazy_fns[k]["Button"].click(crazy_fns[k]["Function"], [*input_combo, gr.State(PORT)], output_combo) click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
cancel_handles.append(click_handle) cancel_handles.append(click_handle)
# 函数插件-下拉菜单与随变按钮的互动 # 函数插件-下拉菜单与随变按钮的互动
@@ -121,7 +146,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as de
# 随变按钮的回调函数注册 # 随变按钮的回调函数注册
def route(k, *args, **kwargs): def route(k, *args, **kwargs):
if k in [r"打开插件列表", r"请先从插件列表中选择"]: return if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
yield from crazy_fns[k]["Function"](*args, **kwargs) yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs)
click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo) click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo)
click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
# def expand_file_area(file_upload, area_file_up): # def expand_file_area(file_upload, area_file_up):
@@ -130,7 +155,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as de
cancel_handles.append(click_handle) cancel_handles.append(click_handle)
# 终止按钮的回调函数注册 # 终止按钮的回调函数注册
stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles) stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数 # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
def auto_opentab_delay(): def auto_opentab_delay():
import threading, webbrowser, time import threading, webbrowser, time
@@ -139,9 +164,11 @@ def auto_opentab_delay():
print(f"\t(暗色主体): http://localhost:{PORT}/?__dark-theme=true") print(f"\t(暗色主体): http://localhost:{PORT}/?__dark-theme=true")
def open(): def open():
time.sleep(2) time.sleep(2)
try: auto_update() # 检查新版本
except: pass
webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true") webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true")
threading.Thread(target=open, name="open-browser", daemon=True).start() threading.Thread(target=open, name="open-browser", daemon=True).start()
auto_opentab_delay() auto_opentab_delay()
demo.title = "ChatGPT 学术优化" demo.title = "ChatGPT 学术优化"
demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=Flase, server_port=8080, auth=chunzhi233233) demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=True, server_port=PORT, auth=AUTHENTICATION)
+1 -1
View File
@@ -26,7 +26,7 @@ import gradio as gr
def adjust_theme(): def adjust_theme():
try: try:
color_er = gr.themes.utils.colors.pink color_er = gr.themes.utils.colors.fuchsia
set_theme = gr.themes.Default( set_theme = gr.themes.Default(
primary_hue=gr.themes.utils.colors.orange, primary_hue=gr.themes.utils.colors.orange,
neutral_hue=gr.themes.utils.colors.gray, neutral_hue=gr.themes.utils.colors.gray,
+19 -2
View File
@@ -2,6 +2,17 @@ import markdown, mdtex2html, threading, importlib, traceback, importlib, inspect
from show_math import convert as convert_math from show_math import convert as convert_math
from functools import wraps, lru_cache from functools import wraps, lru_cache
def ArgsGeneralWrapper(f):
"""
装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
"""
def decorated(txt, txt2, *args, **kwargs):
txt_passon = txt
if txt == "" and txt2 != "": txt_passon = txt2
yield from f(txt_passon, *args, **kwargs)
return decorated
def get_reduce_token_percent(text): def get_reduce_token_percent(text):
try: try:
# text = "maximum context length is 4097 tokens. However, your messages resulted in 4870 tokens" # text = "maximum context length is 4097 tokens. However, your messages resulted in 4870 tokens"
@@ -116,7 +127,7 @@ def CatchException(f):
from toolbox import get_conf from toolbox import get_conf
proxies, = get_conf('proxies') proxies, = get_conf('proxies')
tb_str = '```\n' + traceback.format_exc() + '```' tb_str = '```\n' + traceback.format_exc() + '```'
if len(chatbot) == 0: chatbot.append(["插件调度异常","异常原因"]) if chatbot is None or len(chatbot) == 0: chatbot = [["插件调度异常","异常原因"]]
chatbot[-1] = (chatbot[-1][0], f"[Local Message] 实验性函数调用出错: \n\n{tb_str} \n\n当前代理可用性: \n\n{check_proxy(proxies)}") chatbot[-1] = (chatbot[-1][0], f"[Local Message] 实验性函数调用出错: \n\n{tb_str} \n\n当前代理可用性: \n\n{check_proxy(proxies)}")
yield chatbot, history, f'异常 {e}' yield chatbot, history, f'异常 {e}'
return decorated return decorated
@@ -303,7 +314,7 @@ def on_file_uploaded(files, chatbot, txt):
def on_report_generated(files, chatbot): def on_report_generated(files, chatbot):
from toolbox import find_recent_files from toolbox import find_recent_files
report_files = find_recent_files('gpt_log') report_files = find_recent_files('gpt_log')
if len(report_files) == 0: return None, chatbot if len(report_files) == 0: return files, chatbot
# files.extend(report_files) # files.extend(report_files)
chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧“文件上传区”(可能处于折叠状态),请查收。']) chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧“文件上传区”(可能处于折叠状态),请查收。'])
return report_files, chatbot return report_files, chatbot
@@ -342,3 +353,9 @@ def clear_line_break(txt):
txt = txt.replace(' ', ' ') txt = txt.replace(' ', ' ')
txt = txt.replace(' ', ' ') txt = txt.replace(' ', ' ')
return txt return txt
class DummyWith():
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
return