增加并行处理与权限控制

This commit is contained in:
Tuchuanhuhuhu 2023-03-28 23:17:12 +08:00
parent 46eba1f399
commit 43d59d936f
2 changed files with 17 additions and 10 deletions

View File

@ -6,7 +6,7 @@ API_URL = "https://api.openai.com/v1/chat/completions"
USE_PROXY = False USE_PROXY = False
if USE_PROXY: if USE_PROXY:
# 代理网络的地址,打开你的科学上网软件查看代理的协议(socks5/http)、地址(localhost)和端口(11284) # 代理网络的地址,打开你的科学上网软件查看代理的协议(socks5/http)、地址(localhost)和端口(11284)
proxies = { "http": "socks5h://localhost:11284", "https": "socks5h://localhost:11284", } proxies = { "http": "socks5h://localhost:11284", "https": "socks5h://localhost:11284", }
print('网络代理状态:运行。') print('网络代理状态:运行。')
else: else:
proxies = None proxies = None
@ -25,5 +25,11 @@ MAX_RETRY = 2
LLM_MODEL = "gpt-3.5-turbo" LLM_MODEL = "gpt-3.5-turbo"
# 检查一下是不是忘了改config # 检查一下是不是忘了改config
if API_KEY == "sk-此处填API秘钥": if len(API_KEY) != 51:
assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行" assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行"
# 设置并行使用的线程数
CONCURRENT_COUNT = 100
# 设置用户名和密码
AUTHENTICATION = [] # [("username", "password"), ("username2", "password2"), ...]

15
main.py
View File

@ -1,14 +1,15 @@
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 from toolbox import format_io, find_free_port
# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
try: from config_private 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 except: from config import proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION
# 如果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
AUTHENTICATION = None if AUTHENTICATION == [] else AUTHENTICATION
initial_prompt = "Serve me as a writing and programming assistant." initial_prompt = "Serve me as a writing and programming assistant."
title_html = """<h1 align="center">ChatGPT 学术优化</h1>""" title_html = """<h1 align="center">ChatGPT 学术优化</h1>"""
@ -16,7 +17,7 @@ title_html = """<h1 align="center">ChatGPT 学术优化</h1>"""
# 问询记录, python 版本建议3.9+(越新越好) # 问询记录, python 版本建议3.9+(越新越好)
import logging import logging
os.makedirs('gpt_log', exist_ok=True) 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) except:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO)
print('所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!') 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]) 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) submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True)
for k in functional: 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) [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]) file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt])
for k in crazy_functional: 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] [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]) 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.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)