config_io.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import json
  2. import os
  3. from utils.logger import logger
  4. def save_config_to_json(config_data, message_id):
  5. # 确保目录存在,如果不存在则创建
  6. output_directory = f"data/input_images/{message_id}"
  7. os.makedirs(output_directory, exist_ok=True)
  8. # 生成JSON文件路径
  9. json_path = os.path.join(output_directory, "config.json")
  10. # 将字典转换为JSON并保存到文件中
  11. with open(json_path, 'w') as f:
  12. json.dump(config_data, f)
  13. def load_config_from_json(message_id):
  14. # 生成JSON文件路径
  15. json_path = os.path.join("data/input_images", message_id, "config.json")
  16. # 检查文件是否存在,如果不存在则返回默认字典
  17. if not os.path.exists(json_path):
  18. return {"user_message": "", "action": "", "lora": "", "animal": "", "if_download": False, "img_missions": {}}
  19. # 从文件中读取JSON数据并转换为字典
  20. with open(json_path, 'r') as f:
  21. config_data = json.load(f)
  22. return config_data
  23. def add_to_user_reply(event_id, user_message_id, reply_message_id):
  24. output_directory = "data"
  25. os.makedirs(output_directory, exist_ok=True)
  26. json_path = os.path.join(output_directory, "user_reply.json")
  27. # 检查文件是否存在,如果不存在则创建
  28. if os.path.exists(json_path):
  29. # 从文件中读取JSON数据并转换为字典
  30. with open(json_path, 'r') as f:
  31. config_data = json.load(f)
  32. if event_id in config_data:
  33. return
  34. else:
  35. config_data[event_id] = [user_message_id, reply_message_id]
  36. with open(json_path, 'w') as f:
  37. json.dump(config_data, f)
  38. return
  39. else:
  40. config_data={
  41. event_id : [user_message_id, reply_message_id]
  42. }
  43. with open(json_path, 'w') as f:
  44. json.dump(config_data, f)
  45. return
  46. def if_has_processed(event_id):
  47. output_directory = "data"
  48. os.makedirs(output_directory, exist_ok=True)
  49. json_path = os.path.join(output_directory, "user_reply.json")
  50. # 检查文件是否存在,如果不存在则创建
  51. if os.path.exists(json_path):
  52. # 从文件中读取JSON数据并转换为字典
  53. with open(json_path, 'r') as f:
  54. config_data = json.load(f)
  55. if event_id in config_data:
  56. return True
  57. else:
  58. return False
  59. else:
  60. return False
  61. # 从文件中读取lora选项
  62. def load_lora_options():
  63. json_path = "lora_options.json"
  64. with open(json_path, 'r', encoding='utf-8') as f:
  65. lora_options = json.load(f)
  66. return lora_options
  67. # 从文件中读取animal选项
  68. def load_animal_options():
  69. json_path = "animal_options.json"
  70. with open(json_path, 'r', encoding='utf-8') as f:
  71. animal_options = json.load(f)
  72. return animal_options
  73. # 检测任务是否完成
  74. def if_mission_done(reply_message_id):
  75. config_data = load_config_from_json(reply_message_id)
  76. for key, value in config_data['img_missions'].items():
  77. if value != 'done':
  78. return False
  79. return True
  80. # 检测任务是否存在失败
  81. def if_mission_fail(reply_message_id):
  82. config_data = load_config_from_json(reply_message_id)
  83. for key, value in config_data['img_missions'].items():
  84. if value == 'fail':
  85. return True
  86. return False