Spaces:
Sleeping
Sleeping
SamgeShao
Keldos
Tuchuanhuhuhu
commited on
feat: 从环境变量中读取配置api_host & 用户名是否配置改为判断auth长度 (#504)
Browse files* 从环境变量中读取配置api_host && authflag改为判断auth_list的长度
* chore: 更改README中提示和config-example 并调整shared写法
fix #507
- shared写法来自川虎在config废分支的提交,所以加co-author(
---------
Co-authored-by: Keldos <[email protected]>
Co-authored-by: Tuchuanhuhuhu <[email protected]>
- ChuanhuChatbot.py +3 -1
- README.md +6 -4
- config_example.json +1 -4
- modules/chat_func.py +1 -1
- modules/config.py +12 -2
- modules/openai_func.py +1 -1
- modules/utils.py +1 -1
ChuanhuChatbot.py
CHANGED
|
@@ -5,6 +5,7 @@ import sys
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
| 8 |
from modules.config import *
|
| 9 |
from modules.utils import *
|
| 10 |
from modules.presets import *
|
|
@@ -180,11 +181,12 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 180 |
)
|
| 181 |
|
| 182 |
with gr.Accordion("网络设置", open=False):
|
|
|
|
| 183 |
apihostTxt = gr.Textbox(
|
| 184 |
show_label=True,
|
| 185 |
placeholder=f"在这里输入API-Host...",
|
| 186 |
label="API-Host",
|
| 187 |
-
value=
|
| 188 |
lines=1,
|
| 189 |
)
|
| 190 |
changeAPIURLBtn = gr.Button("🔄 切换API地址")
|
|
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
from modules import config
|
| 9 |
from modules.config import *
|
| 10 |
from modules.utils import *
|
| 11 |
from modules.presets import *
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
with gr.Accordion("网络设置", open=False):
|
| 184 |
+
# 优先展示自定义的api_host
|
| 185 |
apihostTxt = gr.Textbox(
|
| 186 |
show_label=True,
|
| 187 |
placeholder=f"在这里输入API-Host...",
|
| 188 |
label="API-Host",
|
| 189 |
+
value=config.api_host or shared.API_HOST,
|
| 190 |
lines=1,
|
| 191 |
)
|
| 192 |
changeAPIURLBtn = gr.Button("🔄 切换API地址")
|
README.md
CHANGED
|
@@ -99,18 +99,18 @@
|
|
| 99 |
|
| 100 |
这样设置的密钥以及其他设置项可以在拉取项目更新之后保留。
|
| 101 |
|
| 102 |
-
在项目文件夹中复制一份 `config_example.json`,并将其重命名为 `config.json`,在其中填入 API-Key、用户名密码(可选)、API host
|
| 103 |
|
| 104 |
```
|
| 105 |
{
|
| 106 |
"openai_api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxx",
|
| 107 |
"users": [
|
| 108 |
-
["
|
| 109 |
-
["
|
| 110 |
],
|
| 111 |
}
|
| 112 |
```
|
| 113 |
-
|
| 114 |
* *原本在 `api_key.txt` 和 `auth.json` 中填写相关设置的方法仍然可用,但不再推荐。*
|
| 115 |
|
| 116 |
</details>
|
|
@@ -181,6 +181,7 @@ docker pull tuchuanhuhuhu/chuanhuchatgpt:latest
|
|
| 181 |
```shell
|
| 182 |
docker run -d --name chatgpt \
|
| 183 |
-e my_api_key="替换成API" \
|
|
|
|
| 184 |
-e USERNAME="替换成用户名" \
|
| 185 |
-e PASSWORD="替换成密码" \
|
| 186 |
-v ~/chatGPThistory:/app/history \
|
|
@@ -302,6 +303,7 @@ docker run --detach \
|
|
| 302 |
```
|
| 303 |
docker run -d --name chatgpt \
|
| 304 |
-e my_api_key="你的API" \
|
|
|
|
| 305 |
-e USERNAME="替换成用户名" \
|
| 306 |
-e PASSWORD="替换成密码" \
|
| 307 |
-v ~/chatGPThistory:/app/history \
|
|
|
|
| 99 |
|
| 100 |
这样设置的密钥以及其他设置项可以在拉取项目更新之后保留。
|
| 101 |
|
| 102 |
+
在项目文件夹中复制一份 `config_example.json`,并将其重命名为 `config.json`,在其中填入 API-Key、用户名密码(可选)、API host(可选)、代理地址(可选)等设置。用户名密码支持多用户。示例:
|
| 103 |
|
| 104 |
```
|
| 105 |
{
|
| 106 |
"openai_api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxx",
|
| 107 |
"users": [
|
| 108 |
+
["用户1的用户名", "用户1的密码"],
|
| 109 |
+
["用户2的用户名", "用户2的密码"]
|
| 110 |
],
|
| 111 |
}
|
| 112 |
```
|
| 113 |
+
* 如果不设置用户名与密码,可以直接将“users”字段整段删去,或留空为 `"users": [], `
|
| 114 |
* *原本在 `api_key.txt` 和 `auth.json` 中填写相关设置的方法仍然可用,但不再推荐。*
|
| 115 |
|
| 116 |
</details>
|
|
|
|
| 181 |
```shell
|
| 182 |
docker run -d --name chatgpt \
|
| 183 |
-e my_api_key="替换成API" \
|
| 184 |
+
-e api_host="替换成自定义的api请求地址" \
|
| 185 |
-e USERNAME="替换成用户名" \
|
| 186 |
-e PASSWORD="替换成密码" \
|
| 187 |
-v ~/chatGPThistory:/app/history \
|
|
|
|
| 303 |
```
|
| 304 |
docker run -d --name chatgpt \
|
| 305 |
-e my_api_key="你的API" \
|
| 306 |
+
-e api_host="替换成自定义的api请求地址" \
|
| 307 |
-e USERNAME="替换成用户名" \
|
| 308 |
-e PASSWORD="替换成密码" \
|
| 309 |
-v ~/chatGPThistory:/app/history \
|
config_example.json
CHANGED
|
@@ -2,10 +2,7 @@
|
|
| 2 |
"openai_api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxx",
|
| 3 |
"https_proxy": "http://127.0.0.1:1079",
|
| 4 |
"http_proxy": "http://127.0.0.1:1079",
|
| 5 |
-
"users": [
|
| 6 |
-
["username-0", "password-0"],
|
| 7 |
-
["username-1", "password-1"]
|
| 8 |
-
],
|
| 9 |
"advance_docs": {
|
| 10 |
"pdf": {
|
| 11 |
"two_column": false,
|
|
|
|
| 2 |
"openai_api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxx",
|
| 3 |
"https_proxy": "http://127.0.0.1:1079",
|
| 4 |
"http_proxy": "http://127.0.0.1:1079",
|
| 5 |
+
"users": [],
|
|
|
|
|
|
|
|
|
|
| 6 |
"advance_docs": {
|
| 7 |
"pdf": {
|
| 8 |
"two_column": false,
|
modules/chat_func.py
CHANGED
|
@@ -18,7 +18,7 @@ import aiohttp
|
|
| 18 |
from modules.presets import *
|
| 19 |
from modules.llama_func import *
|
| 20 |
from modules.utils import *
|
| 21 |
-
|
| 22 |
from modules.config import retrieve_proxy
|
| 23 |
|
| 24 |
# logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s")
|
|
|
|
| 18 |
from modules.presets import *
|
| 19 |
from modules.llama_func import *
|
| 20 |
from modules.utils import *
|
| 21 |
+
from . import shared
|
| 22 |
from modules.config import retrieve_proxy
|
| 23 |
|
| 24 |
# logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s")
|
modules/config.py
CHANGED
|
@@ -5,6 +5,9 @@ import logging
|
|
| 5 |
import sys
|
| 6 |
import json
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
__all__ = [
|
| 9 |
"my_api_key",
|
| 10 |
"authflag",
|
|
@@ -31,9 +34,16 @@ if os.environ.get("dockerrun") == "yes":
|
|
| 31 |
|
| 32 |
## 处理 api-key 以及 允许的用户列表
|
| 33 |
my_api_key = config.get("openai_api_key", "") # 在这里输入你的 API 密钥
|
| 34 |
-
authflag = "users" in config
|
| 35 |
-
auth_list = config.get("users", []) # 实际上是使用者的列表
|
| 36 |
my_api_key = os.environ.get("my_api_key", my_api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if dockerflag:
|
| 38 |
if my_api_key == "empty":
|
| 39 |
logging.error("Please give a api key!")
|
|
|
|
| 5 |
import sys
|
| 6 |
import json
|
| 7 |
|
| 8 |
+
from . import shared
|
| 9 |
+
|
| 10 |
+
|
| 11 |
__all__ = [
|
| 12 |
"my_api_key",
|
| 13 |
"authflag",
|
|
|
|
| 34 |
|
| 35 |
## 处理 api-key 以及 允许的用户列表
|
| 36 |
my_api_key = config.get("openai_api_key", "") # 在这里输入你的 API 密钥
|
|
|
|
|
|
|
| 37 |
my_api_key = os.environ.get("my_api_key", my_api_key)
|
| 38 |
+
|
| 39 |
+
auth_list = config.get("users", []) # 实际上是使用者的列表
|
| 40 |
+
authflag = len(auth_list) > 0 # 是否开启认证的状态值,改为判断auth_list长度
|
| 41 |
+
|
| 42 |
+
# 处理自定义的api_host,优先读环境变量的配置,如果存在则自动装配
|
| 43 |
+
api_host = os.environ.get("api_host", config.get("api_host", ""))
|
| 44 |
+
if api_host:
|
| 45 |
+
shared.state.set_api_host(api_host)
|
| 46 |
+
|
| 47 |
if dockerflag:
|
| 48 |
if my_api_key == "empty":
|
| 49 |
logging.error("Please give a api key!")
|
modules/openai_func.py
CHANGED
|
@@ -10,7 +10,7 @@ from modules.presets import (
|
|
| 10 |
read_timeout_prompt
|
| 11 |
)
|
| 12 |
|
| 13 |
-
from
|
| 14 |
from modules.config import retrieve_proxy
|
| 15 |
import os, datetime
|
| 16 |
|
|
|
|
| 10 |
read_timeout_prompt
|
| 11 |
)
|
| 12 |
|
| 13 |
+
from . import shared
|
| 14 |
from modules.config import retrieve_proxy
|
| 15 |
import os, datetime
|
| 16 |
|
modules/utils.py
CHANGED
|
@@ -23,7 +23,7 @@ from pygments.lexers import get_lexer_by_name
|
|
| 23 |
from pygments.formatters import HtmlFormatter
|
| 24 |
|
| 25 |
from modules.presets import *
|
| 26 |
-
|
| 27 |
from modules.config import retrieve_proxy
|
| 28 |
|
| 29 |
if TYPE_CHECKING:
|
|
|
|
| 23 |
from pygments.formatters import HtmlFormatter
|
| 24 |
|
| 25 |
from modules.presets import *
|
| 26 |
+
from . import shared
|
| 27 |
from modules.config import retrieve_proxy
|
| 28 |
|
| 29 |
if TYPE_CHECKING:
|