Skip to content

Commit

Permalink
feat:gradio验证支持多用户
Browse files Browse the repository at this point in the history
  • Loading branch information
MZhao-ouo committed Mar 29, 2023
1 parent 1d87c8b commit 4aae38f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
25 changes: 15 additions & 10 deletions ChuanhuChatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
dockerflag = False

authflag = False
auth_list = []

if dockerflag:
my_api_key = os.environ.get("my_api_key")
Expand All @@ -35,6 +36,7 @@
username = os.environ.get("USERNAME")
password = os.environ.get("PASSWORD")
if not (isinstance(username, type(None)) or isinstance(password, type(None))):
auth_list.append((os.environ.get("USERNAME"), os.environ.get("PASSWORD")))
authflag = True
else:
if (
Expand All @@ -45,12 +47,15 @@
with open("api_key.txt", "r") as f:
my_api_key = f.read().strip()
if os.path.exists("auth.json"):
authflag = True
with open("auth.json", "r", encoding='utf-8') as f:
auth = json.load(f)
username = auth["username"]
password = auth["password"]
if username != "" and password != "":
authflag = True
for _ in auth:
if auth[_]["username"] and auth[_]["password"]:
auth_list.append((auth[_]["username"], auth[_]["password"]))
else:
logging.error("请检查auth.json文件中的用户名和密码!")
sys.exit(1)

gr.Chatbot.postprocess = postprocess
PromptHelper.compact_text_chunks = compact_text_chunks
Expand All @@ -71,19 +76,19 @@
gr.HTML(title)
status_display = gr.Markdown(get_geoip(), elem_id="status_display")

with gr.Row(scale=1).style(equal_height=True):
with gr.Row().style(equal_height=True):
with gr.Column(scale=5):
with gr.Row(scale=1):
with gr.Row():
chatbot = gr.Chatbot(elem_id="chuanhu_chatbot").style(height="100%")
with gr.Row(scale=1):
with gr.Row():
with gr.Column(scale=12):
user_input = gr.Textbox(
show_label=False, placeholder="在这里输入"
).style(container=False)
with gr.Column(min_width=70, scale=1):
submitBtn = gr.Button("发送", variant="primary")
cancelBtn = gr.Button("取消", variant="secondary", visible=False)
with gr.Row(scale=1):
with gr.Row():
emptyBtn = gr.Button(
"🧹 新的对话",
)
Expand Down Expand Up @@ -412,7 +417,7 @@
demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
server_name="0.0.0.0",
server_port=7860,
auth=(username, password),
auth=auth_list,
favicon_path="./assets/favicon.ico",
)
else:
Expand All @@ -427,7 +432,7 @@
if authflag:
demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
share=False,
auth=(username, password),
auth=auth_list,
favicon_path="./assets/favicon.ico",
inbrowser=True,
)
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@

在`api_key.txt`中填写你的API-Key,注意不要填写任何无关内容。

在`auth.json`中填写你的用户名和密码
在`auth.json`中填写你的用户名和密码,支持多用户。格式如下:

```
{
"username": "用户名",
"password": "密码"
"user1": {
"username": "用户名",
"password": "密码"
}
}
```

Expand Down

0 comments on commit 4aae38f

Please sign in to comment.