Felix5572 commited on
Commit
8c3dd82
·
1 Parent(s): 78d8813

parser secrets to json

Browse files
Files changed (3) hide show
  1. ChuanhuChatbot.py +1 -0
  2. config.json +2 -2
  3. modules/utils.py +24 -1
ChuanhuChatbot.py CHANGED
@@ -817,6 +817,7 @@ demo.title = i18n("川虎Chat 🚀")
817
  if __name__ == "__main__":
818
  reload_javascript()
819
  setup_wizard()
 
820
  demo.queue().launch(
821
  allowed_paths=["history", "web_assets"],
822
  blocked_paths=["config.json", "files", "models", "lora", "modules"],
 
817
  if __name__ == "__main__":
818
  reload_javascript()
819
  setup_wizard()
820
+ parser_config_json()
821
  demo.queue().launch(
822
  allowed_paths=["history", "web_assets"],
823
  blocked_paths=["config.json", "files", "models", "lora", "modules"],
config.json CHANGED
@@ -1,11 +1,11 @@
1
  {
2
- "openai_api_key": "${{ openrouter_api_key }}",
3
  "openai_api_base": "https://openrouter.ai/api/",
4
  "midjourney_temp_folder": "files",
5
  "users": [
6
  [
7
  "felix5572",
8
- "${{ default_password }}"
9
  ]
10
  ],
11
  "hide_history_when_not_logged_in": true,
 
1
  {
2
+ "openai_api_key": "${{ OPENAI_API_KEY }}",
3
  "openai_api_base": "https://openrouter.ai/api/",
4
  "midjourney_temp_folder": "files",
5
  "users": [
6
  [
7
  "felix5572",
8
+ "${{ DEFAULT_PASSWORD }}"
9
  ]
10
  ],
11
  "hide_history_when_not_logged_in": true,
modules/utils.py CHANGED
@@ -852,7 +852,7 @@ class JSONLoader:
852
  return [JSONLoader._replace_placeholders(i) for i in data]
853
  elif isinstance(data, str) and data.startswith("${{ ") and data.endswith(" }}"):
854
  env_var = data[4:-3]
855
- print("---getting env_var---", env_var)
856
  return os.getenv(env_var, data)
857
  else:
858
  return data
@@ -1084,6 +1084,27 @@ class SetupWizard:
1084
  with open(self.file_path, "w", encoding="utf-8") as f:
1085
  json.dump(self.config, f, ensure_ascii=False, indent=4)
1086
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1087
 
1088
  def setup_wizard():
1089
  if not os.path.exists("config.json"):
@@ -1522,3 +1543,5 @@ def setPlaceholder(model_name: str | None = "", model: BaseLLMModel | None = Non
1522
  chatbot_ph_slogan_class = slogan_class,
1523
  chatbot_ph_question_class = question_class
1524
  )
 
 
 
852
  return [JSONLoader._replace_placeholders(i) for i in data]
853
  elif isinstance(data, str) and data.startswith("${{ ") and data.endswith(" }}"):
854
  env_var = data[4:-3]
855
+ print("---getting env_var---", env_var, os.getenv(env_var, data))
856
  return os.getenv(env_var, data)
857
  else:
858
  return data
 
1084
  with open(self.file_path, "w", encoding="utf-8") as f:
1085
  json.dump(self.config, f, ensure_ascii=False, indent=4)
1086
 
1087
+ def parser_config_json():
1088
+ # 读取文件内容
1089
+ with open('config.json', 'r') as file:
1090
+ content = file.read()
1091
+
1092
+ # 使用正则表达式匹配所有 ${{ SOME_VAR }} 格式的变量
1093
+ pattern = re.compile(r'\${{\s*([0-9a-zA-Z_]+)\s*}}')
1094
+ print()
1095
+
1096
+ # 替换匹配到的变量为 os.getenv("SOME_VAR")
1097
+ def replace_match(match):
1098
+ var_name = match.group(1)
1099
+ print('parsering var_name', var_name)
1100
+ return os.getenv(f"{var_name}")
1101
+
1102
+ new_content = pattern.sub(replace_match, content)
1103
+ # print("debug", new_content)
1104
+
1105
+ # 将替换后的内容写回文件
1106
+ with open('config.json', 'w') as file:
1107
+ file.write(new_content)
1108
 
1109
  def setup_wizard():
1110
  if not os.path.exists("config.json"):
 
1543
  chatbot_ph_slogan_class = slogan_class,
1544
  chatbot_ph_question_class = question_class
1545
  )
1546
+
1547
+