Merge pull request #57 from GaiZhenbiao/master
Adding a bunch of nice-to-have features
This commit is contained in:
		
						commit
						4e06c350bf
					
				@ -25,5 +25,11 @@ MAX_RETRY = 2
 | 
			
		||||
LLM_MODEL = "gpt-3.5-turbo"
 | 
			
		||||
 | 
			
		||||
# 检查一下是不是忘了改config
 | 
			
		||||
if API_KEY == "sk-此处填API秘钥":
 | 
			
		||||
if len(API_KEY) != 51:
 | 
			
		||||
    assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行"
 | 
			
		||||
 | 
			
		||||
# 设置并行使用的线程数
 | 
			
		||||
CONCURRENT_COUNT = 100
 | 
			
		||||
 | 
			
		||||
# 设置用户名和密码
 | 
			
		||||
AUTHENTICATION = [] # [("username", "password"), ("username2", "password2"), ...]
 | 
			
		||||
							
								
								
									
										31
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								main.py
									
									
									
									
									
								
							@ -4,11 +4,12 @@ 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 = """<h1 align="center">ChatGPT 学术优化</h1>"""
 | 
			
		||||
@ -50,6 +51,8 @@ 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):
 | 
			
		||||
                    with gr.Row():
 | 
			
		||||
                        resetBtn = gr.Button("重置", variant="secondary")
 | 
			
		||||
                        submitBtn = gr.Button("提交", variant="primary")
 | 
			
		||||
            with gr.Row():
 | 
			
		||||
                from check_proxy import check_proxy
 | 
			
		||||
@ -73,10 +76,16 @@ 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)
 | 
			
		||||
    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)
 | 
			
		||||
@ -89,15 +98,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(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=True, server_port=PORT, inbrowser=True, auth=AUTHENTICATION)
 | 
			
		||||
 | 
			
		||||
@ -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] = (
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user