Merge pull request #82 from Okabe-Rintarou-0/master

支持暂停按钮 #53
This commit is contained in:
binary-husky 2023-03-29 20:38:06 +08:00 committed by GitHub
commit b3211362f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -20,7 +20,7 @@ else:
print('网络代理状态:未配置。无代理状态下很可能无法访问。')
# 发送请求到OpenAI后等待多久判定为超时
TIMEOUT_SECONDS = 20
TIMEOUT_SECONDS = 25
# 网页的端口, -1代表随机端口
WEB_PORT = -1

18
main.py
View File

@ -36,6 +36,7 @@ gr.Chatbot.postprocess = format_io
from theme import adjust_theme
set_theme = adjust_theme()
cancel_handles = []
with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
gr.HTML(title_html)
with gr.Row():
@ -52,6 +53,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
with gr.Row():
resetBtn = gr.Button("重置", variant="secondary")
submitBtn = gr.Button("提交", variant="primary")
stopBtn = gr.Button("停止", variant="stop")
with gr.Row():
from check_proxy import check_proxy
statusDisplay = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行. \nNetwork: {check_proxy(proxies)}\nModel: {LLM_MODEL}")
@ -75,16 +77,18 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
predict_args = dict(fn=predict, inputs=[txt, top_p, temperature, chatbot, history, system_prompt], outputs=[chatbot, history, statusDisplay], show_progress=True)
empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt])
empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) # 用于在提交后清空输入栏
txt.submit(**predict_args)
txt.submit(**empty_txt_args)
submitBtn.click(**predict_args)
submitBtn.click(**empty_txt_args)
cancel_handles.append(txt.submit(**predict_args))
# txt.submit(**empty_txt_args) 在提交后清空输入栏
cancel_handles.append(submitBtn.click(**predict_args))
# submitBtn.click(**empty_txt_args) 在提交后清空输入栏
resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, statusDisplay])
stopBtn.click(fn=None, inputs=None, outputs=None, cancels=[submit_event])
for k in functional:
functional[k]["Button"].click(predict,
click_handle = functional[k]["Button"].click(predict,
[txt, top_p, temperature, chatbot, history, system_prompt, gr.State(True), gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
cancel_handles.append(click_handle)
file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt])
for k in crazy_functional:
click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
@ -92,6 +96,8 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
)
try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
except: pass
cancel_handles.append(click_handle)
stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
# gradio的inbrowser触发不太稳定回滚代码到原始的浏览器打开函数
def auto_opentab_delay():