MZhaovo commited on
Commit
4aae38f
1 Parent(s): 1d87c8b

feat:gradio验证支持多用户

Browse files
Files changed (2) hide show
  1. ChuanhuChatbot.py +15 -10
  2. README.md +5 -3
ChuanhuChatbot.py CHANGED
@@ -25,6 +25,7 @@ else:
25
  dockerflag = False
26
 
27
  authflag = False
 
28
 
29
  if dockerflag:
30
  my_api_key = os.environ.get("my_api_key")
@@ -35,6 +36,7 @@ if dockerflag:
35
  username = os.environ.get("USERNAME")
36
  password = os.environ.get("PASSWORD")
37
  if not (isinstance(username, type(None)) or isinstance(password, type(None))):
 
38
  authflag = True
39
  else:
40
  if (
@@ -45,12 +47,15 @@ else:
45
  with open("api_key.txt", "r") as f:
46
  my_api_key = f.read().strip()
47
  if os.path.exists("auth.json"):
 
48
  with open("auth.json", "r", encoding='utf-8') as f:
49
  auth = json.load(f)
50
- username = auth["username"]
51
- password = auth["password"]
52
- if username != "" and password != "":
53
- authflag = True
 
 
54
 
55
  gr.Chatbot.postprocess = postprocess
56
  PromptHelper.compact_text_chunks = compact_text_chunks
@@ -71,11 +76,11 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
71
  gr.HTML(title)
72
  status_display = gr.Markdown(get_geoip(), elem_id="status_display")
73
 
74
- with gr.Row(scale=1).style(equal_height=True):
75
  with gr.Column(scale=5):
76
- with gr.Row(scale=1):
77
  chatbot = gr.Chatbot(elem_id="chuanhu_chatbot").style(height="100%")
78
- with gr.Row(scale=1):
79
  with gr.Column(scale=12):
80
  user_input = gr.Textbox(
81
  show_label=False, placeholder="在这里输入"
@@ -83,7 +88,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
83
  with gr.Column(min_width=70, scale=1):
84
  submitBtn = gr.Button("发送", variant="primary")
85
  cancelBtn = gr.Button("取消", variant="secondary", visible=False)
86
- with gr.Row(scale=1):
87
  emptyBtn = gr.Button(
88
  "🧹 新的对话",
89
  )
@@ -412,7 +417,7 @@ if __name__ == "__main__":
412
  demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
413
  server_name="0.0.0.0",
414
  server_port=7860,
415
- auth=(username, password),
416
  favicon_path="./assets/favicon.ico",
417
  )
418
  else:
@@ -427,7 +432,7 @@ if __name__ == "__main__":
427
  if authflag:
428
  demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
429
  share=False,
430
- auth=(username, password),
431
  favicon_path="./assets/favicon.ico",
432
  inbrowser=True,
433
  )
 
25
  dockerflag = False
26
 
27
  authflag = False
28
+ auth_list = []
29
 
30
  if dockerflag:
31
  my_api_key = os.environ.get("my_api_key")
 
36
  username = os.environ.get("USERNAME")
37
  password = os.environ.get("PASSWORD")
38
  if not (isinstance(username, type(None)) or isinstance(password, type(None))):
39
+ auth_list.append((os.environ.get("USERNAME"), os.environ.get("PASSWORD")))
40
  authflag = True
41
  else:
42
  if (
 
47
  with open("api_key.txt", "r") as f:
48
  my_api_key = f.read().strip()
49
  if os.path.exists("auth.json"):
50
+ authflag = True
51
  with open("auth.json", "r", encoding='utf-8') as f:
52
  auth = json.load(f)
53
+ for _ in auth:
54
+ if auth[_]["username"] and auth[_]["password"]:
55
+ auth_list.append((auth[_]["username"], auth[_]["password"]))
56
+ else:
57
+ logging.error("请检查auth.json文件中的用户名和密码!")
58
+ sys.exit(1)
59
 
60
  gr.Chatbot.postprocess = postprocess
61
  PromptHelper.compact_text_chunks = compact_text_chunks
 
76
  gr.HTML(title)
77
  status_display = gr.Markdown(get_geoip(), elem_id="status_display")
78
 
79
+ with gr.Row().style(equal_height=True):
80
  with gr.Column(scale=5):
81
+ with gr.Row():
82
  chatbot = gr.Chatbot(elem_id="chuanhu_chatbot").style(height="100%")
83
+ with gr.Row():
84
  with gr.Column(scale=12):
85
  user_input = gr.Textbox(
86
  show_label=False, placeholder="在这里输入"
 
88
  with gr.Column(min_width=70, scale=1):
89
  submitBtn = gr.Button("发送", variant="primary")
90
  cancelBtn = gr.Button("取消", variant="secondary", visible=False)
91
+ with gr.Row():
92
  emptyBtn = gr.Button(
93
  "🧹 新的对话",
94
  )
 
417
  demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
418
  server_name="0.0.0.0",
419
  server_port=7860,
420
+ auth=auth_list,
421
  favicon_path="./assets/favicon.ico",
422
  )
423
  else:
 
432
  if authflag:
433
  demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
434
  share=False,
435
+ auth=auth_list,
436
  favicon_path="./assets/favicon.ico",
437
  inbrowser=True,
438
  )
README.md CHANGED
@@ -108,12 +108,14 @@
108
 
109
  在`api_key.txt`中填写你的API-Key,注意不要填写任何无关内容。
110
 
111
- 在`auth.json`中填写你的用户名和密码。
112
 
113
  ```
114
  {
115
- "username": "用户名",
116
- "password": "密码"
 
 
117
  }
118
  ```
119
 
 
108
 
109
  在`api_key.txt`中填写你的API-Key,注意不要填写任何无关内容。
110
 
111
+ 在`auth.json`中填写你的用户名和密码,支持多用户。格式如下:
112
 
113
  ```
114
  {
115
+ "user1": {
116
+ "username": "用户名",
117
+ "password": "密码"
118
+ }
119
  }
120
  ```
121