Felix5572 commited on
Commit
d01b30d
·
1 Parent(s): 8a2fd56

add json to support use secrets

Browse files
Files changed (1) hide show
  1. modules/utils.py +29 -0
modules/utils.py CHANGED
@@ -834,6 +834,35 @@ def beautify_err_msg(err_msg):
834
  return i18n("请查看 config_example.json,配置 Azure OpenAI")
835
  return err_msg
836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
837
 
838
  def auth_from_conf(username, password):
839
  try:
 
834
  return i18n("请查看 config_example.json,配置 Azure OpenAI")
835
  return err_msg
836
 
837
+ # import os
838
+ # import json
839
+
840
+ class JSONLoader:
841
+ @staticmethod
842
+ def load(file_path):
843
+ with open(file_path, 'r') as file:
844
+ data = json.load(file)
845
+ return JSONLoader._replace_placeholders(data)
846
+
847
+ @staticmethod
848
+ def _replace_placeholders(data):
849
+ if isinstance(data, dict):
850
+ return {k: JSONLoader._replace_placeholders(v) for k, v in data.items()}
851
+ elif isinstance(data, list):
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
859
+
860
+ # 假设你的 JSON 文件路径是 'config.json'
861
+ # config = JSONLoader.load('config.json')
862
+
863
+ # 现在你可以使用包含密码的配置了
864
+ # print(config)
865
+
866
 
867
  def auth_from_conf(username, password):
868
  try: