From 46eba1f39958a2b7cb7df3069d0266b075bb3a3e Mon Sep 17 00:00:00 2001 From: Chuan Hu <51039745+GaiZhenbiao@users.noreply.github.com> Date: Tue, 28 Mar 2023 22:47:30 +0800 Subject: [PATCH 1/4] Improve the way to open webbrowser --- main.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/main.py b/main.py index c9af1a1..eecd126 100644 --- a/main.py +++ b/main.py @@ -89,15 +89,5 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: except: pass -# 延迟函数, 做一些准备工作, 最后尝试打开浏览器 -def auto_opentab_delay(): - import threading, webbrowser, time - print(f"URL http://localhost:{PORT}") - def open(): time.sleep(2) - webbrowser.open_new_tab(f'http://localhost:{PORT}') - t = threading.Thread(target=open) - t.daemon = True; t.start() - -auto_opentab_delay() demo.title = "ChatGPT 学术优化" -demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT) +demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT, inbrowser=True) From 43d59d936f9880a778d9dce07748ea5f7cb0228d Mon Sep 17 00:00:00 2001 From: Tuchuanhuhuhu Date: Tue, 28 Mar 2023 23:17:12 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B9=B6=E8=A1=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=8E=E6=9D=83=E9=99=90=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 12 +++++++++--- main.py | 15 ++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/config.py b/config.py index aa85bc0..bff413b 100644 --- a/config.py +++ b/config.py @@ -6,7 +6,7 @@ API_URL = "https://api.openai.com/v1/chat/completions" USE_PROXY = False if USE_PROXY: # 代理网络的地址,打开你的科学上网软件查看代理的协议(socks5/http)、地址(localhost)和端口(11284) - proxies = { "http": "socks5h://localhost:11284", "https": "socks5h://localhost:11284", } + proxies = { "http": "socks5h://localhost:11284", "https": "socks5h://localhost:11284", } print('网络代理状态:运行。') else: proxies = None @@ -25,5 +25,11 @@ MAX_RETRY = 2 LLM_MODEL = "gpt-3.5-turbo" # 检查一下是不是忘了改config -if API_KEY == "sk-此处填API秘钥": - assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行" \ No newline at end of file +if len(API_KEY) != 51: + assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行" + +# 设置并行使用的线程数 +CONCURRENT_COUNT = 100 + +# 设置用户名和密码 +AUTHENTICATION = [] # [("username", "password"), ("username2", "password2"), ...] \ No newline at end of file diff --git a/main.py b/main.py index eecd126..61de4b7 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,15 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 -import gradio as gr +import gradio as gr from predict import predict from toolbox import format_io, find_free_port # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 -try: from config_private import proxies, WEB_PORT, LLM_MODEL -except: from config import proxies, WEB_PORT, LLM_MODEL +try: from config_private import proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION +except: from config import proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION # 如果WEB_PORT是-1, 则随机选取WEB端口 PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT +AUTHENTICATION = None if AUTHENTICATION == [] else AUTHENTICATION initial_prompt = "Serve me as a writing and programming assistant." title_html = """

ChatGPT 学术优化

""" @@ -16,7 +17,7 @@ title_html = """

ChatGPT 学术优化

""" # 问询记录, python 版本建议3.9+(越新越好) import logging os.makedirs('gpt_log', exist_ok=True) -try:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8') +try:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8') except:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO) print('所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!') @@ -78,11 +79,11 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: txt.submit(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay]) submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True) for k in functional: - functional[k]["Button"].click(predict, + functional[k]["Button"].click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) 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"], + click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay] ) try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) @@ -90,4 +91,4 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: demo.title = "ChatGPT 学术优化" -demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT, inbrowser=True) +demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=True, server_port=PORT, inbrowser=True, auth=AUTHENTICATION) From dd8ec0d5114034172a366b5a38e457f3258b01ae Mon Sep 17 00:00:00 2001 From: Tuchuanhuhuhu Date: Tue, 28 Mar 2023 23:20:54 +0800 Subject: [PATCH 3/4] =?UTF-8?q?temprature=E7=9A=84=E5=8F=96=E5=80=BC?= =?UTF-8?q?=E8=8C=83=E5=9B=B4=E4=B8=BA[0,=202]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 61de4b7..c1c18a1 100644 --- a/main.py +++ b/main.py @@ -74,7 +74,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: #inputs, top_p, temperature, top_k, repetition_penalty with gr.Accordion("arguments", open=False): 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=5.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",) txt.submit(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay]) submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True) From 39ad492a658f2d571083fd31f14e241490149138 Mon Sep 17 00:00:00 2001 From: Tuchuanhuhuhu Date: Tue, 28 Mar 2023 23:33:19 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E2=80=9C=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E2=80=9D=E6=8C=89=E9=92=AE=EF=BC=8C=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E4=B9=8B=E5=90=8E=E8=87=AA=E5=8A=A8=E6=B8=85=E7=A9=BA=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 14 +++++++++++--- toolbox.py | 8 ++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index c1c18a1..c2b4b94 100644 --- a/main.py +++ b/main.py @@ -51,7 +51,9 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: with gr.Column(scale=12): txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False) with gr.Column(scale=1): - submitBtn = gr.Button("提交", variant="primary") + with gr.Row(): + resetBtn = gr.Button("重置", variant="secondary") + submitBtn = gr.Button("提交", variant="primary") with gr.Row(): from check_proxy import check_proxy statusDisplay = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行. \nNetwork: {check_proxy(proxies)}\nModel: {LLM_MODEL}") @@ -76,8 +78,14 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: 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",) - txt.submit(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay]) - submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True) + predict_args = dict(fn=predict, inputs=[txt, top_p, temperature, chatbot, history, systemPromptTxt], outputs=[chatbot, history, statusDisplay], show_progress=True) + 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) + resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, statusDisplay]) for k in functional: functional[k]["Button"].click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) diff --git a/toolbox.py b/toolbox.py index 284067c..5491b5e 100644 --- a/toolbox.py +++ b/toolbox.py @@ -14,7 +14,7 @@ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temp # list就是最简单的mutable结构,我们第一个位置放gpt输出,第二个位置传递报错信息 mutable = [None, ''] # multi-threading worker - def mt(i_say, history): + def mt(i_say, history): while True: try: mutable[0] = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature, history=history) @@ -124,7 +124,7 @@ def format_io(self, y): """ 将输入和输出解析为HTML格式。将y中最后一项的输入部分段落化,并将输出部分的Markdown和数学公式转换为HTML格式。 """ - if y is None: return [] + if y is None or y == []: return [] i_ask, gpt_reply = y[-1] i_ask = text_divide_paragraph(i_ask) # 输入部分太自由,预处理一波 y[-1] = ( @@ -144,7 +144,7 @@ def find_free_port(): s.bind(('', 0)) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) return s.getsockname()[1] - + def extract_archive(file_path, dest_dir): import zipfile @@ -165,7 +165,7 @@ def extract_archive(file_path, dest_dir): print("Successfully extracted tar archive to {}".format(dest_dir)) else: return - + def find_recent_files(directory): """ me: find files that is created with in one minutes under a directory with python, write a function