Tuchuanhuhuhu commited on
Commit
9439f2d
·
1 Parent(s): 6296bb5

调整API Key显示逻辑,现在不会再显示到UI中了

Browse files
Files changed (2) hide show
  1. ChuanhuChatbot.py +7 -5
  2. utils.py +14 -0
ChuanhuChatbot.py CHANGED
@@ -119,6 +119,7 @@ with gr.Blocks(
119
  history = gr.State([])
120
  token_count = gr.State([])
121
  promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
 
122
  TRUECOMSTANT = gr.State(True)
123
  FALSECONSTANT = gr.State(False)
124
  topic = gr.State("未命名对话历史记录")
@@ -152,7 +153,7 @@ with gr.Blocks(
152
  keyTxt = gr.Textbox(
153
  show_label=True,
154
  placeholder=f"OpenAI API-key...",
155
- value=my_api_key,
156
  type="password",
157
  visible=not HIDE_MY_KEY,
158
  label="API-Key",
@@ -269,11 +270,12 @@ with gr.Blocks(
269
 
270
  gr.Markdown(description)
271
 
 
272
  # Chatbot
273
  user_input.submit(
274
  predict,
275
  [
276
- keyTxt,
277
  systemPromptTxt,
278
  history,
279
  user_input,
@@ -293,7 +295,7 @@ with gr.Blocks(
293
  submitBtn.click(
294
  predict,
295
  [
296
- keyTxt,
297
  systemPromptTxt,
298
  history,
299
  user_input,
@@ -319,7 +321,7 @@ with gr.Blocks(
319
  retryBtn.click(
320
  retry,
321
  [
322
- keyTxt,
323
  systemPromptTxt,
324
  history,
325
  chatbot,
@@ -343,7 +345,7 @@ with gr.Blocks(
343
  reduceTokenBtn.click(
344
  reduce_token_size,
345
  [
346
- keyTxt,
347
  systemPromptTxt,
348
  history,
349
  chatbot,
 
119
  history = gr.State([])
120
  token_count = gr.State([])
121
  promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
122
+ user_api_key = gr.State(my_api_key)
123
  TRUECOMSTANT = gr.State(True)
124
  FALSECONSTANT = gr.State(False)
125
  topic = gr.State("未命名对话历史记录")
 
153
  keyTxt = gr.Textbox(
154
  show_label=True,
155
  placeholder=f"OpenAI API-key...",
156
+ value=hide_middle_chars(my_api_key),
157
  type="password",
158
  visible=not HIDE_MY_KEY,
159
  label="API-Key",
 
270
 
271
  gr.Markdown(description)
272
 
273
+ keyTxt.submit(submit_key, keyTxt, [user_api_key, status_display])
274
  # Chatbot
275
  user_input.submit(
276
  predict,
277
  [
278
+ user_api_key,
279
  systemPromptTxt,
280
  history,
281
  user_input,
 
295
  submitBtn.click(
296
  predict,
297
  [
298
+ user_api_key,
299
  systemPromptTxt,
300
  history,
301
  user_input,
 
321
  retryBtn.click(
322
  retry,
323
  [
324
+ user_api_key,
325
  systemPromptTxt,
326
  history,
327
  chatbot,
 
345
  reduceTokenBtn.click(
346
  reduce_token_size,
347
  [
348
+ user_api_key,
349
  systemPromptTxt,
350
  history,
351
  chatbot,
utils.py CHANGED
@@ -662,3 +662,17 @@ def change_proxy(proxy):
662
  msg = f"代理更改为了{proxy}"
663
  logging.info(msg)
664
  return msg
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
  msg = f"代理更改为了{proxy}"
663
  logging.info(msg)
664
  return msg
665
+
666
+ def hide_middle_chars(s):
667
+ if len(s) <= 8:
668
+ return s
669
+ else:
670
+ head = s[:4]
671
+ tail = s[-4:]
672
+ hidden = '*' * (len(s) - 8)
673
+ return head + hidden + tail
674
+
675
+ def submit_key(key):
676
+ msg = f"API密钥更改为了{hide_middle_chars(key)}"
677
+ logging.info(msg)
678
+ return key, msg