12.5 特权权限更改
This commit is contained in:
parent
2e032d376d
commit
5c90128771
@ -11,7 +11,7 @@ services:
|
||||
# - .:/flask_project # 将当前目录挂载到容器的 /flask_project 目录(可选,便于开发时实时更新代码)
|
||||
- /home/Z/zbparse_output_dev:/flask_project/flask_app/static/output # 额外的数据卷挂载
|
||||
restart: unless-stopped # 容器退出时自动重启,除非明确停止
|
||||
mem_limit: "8g" # 容器最大可使用内存为8GB
|
||||
mem_limit: "12g" # 容器最大可使用内存为8GB
|
||||
mem_reservation: "4g" # 容器保证可使用内存为4GB
|
||||
cpus: 2.0 # 限制容器使用2个CPU核心
|
||||
security_opt:
|
||||
|
@ -1,19 +1,22 @@
|
||||
# flask_app/general/清除file_id.py
|
||||
import os
|
||||
import openai
|
||||
import json
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
# 初始化 OpenAI 客户端
|
||||
openai.api_key = os.getenv("DASHSCOPE_API_KEY")
|
||||
openai.api_base = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
client = OpenAI(
|
||||
api_key=os.getenv("DASHSCOPE_API_KEY"),
|
||||
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||
)
|
||||
|
||||
|
||||
def delete_all_files():
|
||||
"""
|
||||
查询所有文件并删除。
|
||||
"""
|
||||
try:
|
||||
# 获取文件列表
|
||||
file_stk = openai.File.list()
|
||||
|
||||
file_stk = client.files.list()
|
||||
# 将文件信息解析为字典格式
|
||||
file_data = file_stk.to_dict()
|
||||
|
||||
@ -22,14 +25,23 @@ def delete_all_files():
|
||||
|
||||
# 循环删除每个文件
|
||||
for file_id in file_ids:
|
||||
file_object = openai.File.delete(file_id)
|
||||
print(f"Deleted file with id: {file_id} - {file_object}")
|
||||
try:
|
||||
file_object = client.files.delete(file_id)
|
||||
print(f"Deleted file with id: {file_id} - {file_object}")
|
||||
except Exception as e:
|
||||
print(f"Failed to delete file with id {file_id}: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred while deleting files: {e}")
|
||||
|
||||
|
||||
def read_file_ids(output_folder):
|
||||
"""
|
||||
从指定文件夹中读取 file_ids.txt 文件,返回文件 ID 列表。
|
||||
|
||||
:param output_folder: 输出文件夹路径
|
||||
:return: 文件 ID 列表
|
||||
"""
|
||||
file_ids = []
|
||||
file_ids_path = os.path.join(output_folder, 'file_ids.txt')
|
||||
# 检查文件是否存在
|
||||
@ -52,28 +64,41 @@ def delete_file_by_ids(file_ids):
|
||||
根据传入的 file_id 列表删除指定的文件。
|
||||
|
||||
:param file_ids: 一个包含文件 ID 的字符串列表
|
||||
:return: 一个包含删除失败的 file_id 的列表,如果全部成功则返回空列表
|
||||
"""
|
||||
failed_file_ids = []
|
||||
|
||||
if not isinstance(file_ids, list):
|
||||
print("Error: file_ids should be a list.")
|
||||
return
|
||||
return file_ids # 返回所有 file_ids 作为失败项
|
||||
|
||||
if not all(isinstance(file_id, str) for file_id in file_ids):
|
||||
print("Error: Each file_id should be a string.")
|
||||
return
|
||||
# 找出非字符串的 file_id 并添加到失败列表
|
||||
failed_file_ids = [file_id for file_id in file_ids if not isinstance(file_id, str)]
|
||||
# 仅尝试删除有效的 file_id
|
||||
valid_file_ids = [file_id for file_id in file_ids if isinstance(file_id, str)]
|
||||
else:
|
||||
valid_file_ids = file_ids
|
||||
|
||||
try:
|
||||
# 删除指定文件
|
||||
for file_id in file_ids:
|
||||
for file_id in valid_file_ids:
|
||||
try:
|
||||
# 假设 openai.File.delete 会返回一个文件对象
|
||||
file_object = openai.File.delete(file_id)
|
||||
file_object = client.files.delete(file_id)
|
||||
# print(f"Deleted file with id: {file_id} - {file_object}")
|
||||
except Exception as e:
|
||||
# 处理删除单个文件时的异常
|
||||
print(f"Failed to delete file with id {file_id}: {e}")
|
||||
failed_file_ids.append(file_id)
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred while processing the file_ids: {e}")
|
||||
# 如果发生整体异常,认为所有未处理的 file_ids 都失败了
|
||||
failed_file_ids.extend(valid_file_ids)
|
||||
|
||||
return failed_file_ids
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -23,6 +23,7 @@ def create_app():
|
||||
handler.setFormatter(CSTFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
||||
app.logger.addHandler(handler)
|
||||
app.logger.setLevel(logging.INFO)
|
||||
app.logger.propagate = False # 禁止日志传播到根日志记录器
|
||||
|
||||
# 注册蓝图
|
||||
app.register_blueprint(get_deviation_bp)
|
||||
@ -38,15 +39,18 @@ def create_app():
|
||||
output_folder = getattr(g, 'output_folder', None)
|
||||
if output_folder:
|
||||
# 执行与output_folder相关的清理操作(例如删除临时文件)
|
||||
logger = g.logger
|
||||
logger = app.logger # 使用 app 的 logger
|
||||
logger.info(f"正在清理输出文件夹: {output_folder}")
|
||||
file_ids=read_file_ids(output_folder)
|
||||
delete_file_by_ids(file_ids)
|
||||
logger.info("清理完毕!")
|
||||
file_ids = read_file_ids(output_folder)
|
||||
failed_file_ids = delete_file_by_ids(file_ids)
|
||||
if failed_file_ids:
|
||||
logger.error(f"以下文件删除失败: {failed_file_ids}")
|
||||
else:
|
||||
logger.info("清理完毕!")
|
||||
return app
|
||||
|
||||
#TODO:培训要求、总体要求、进度要求、'建设要求'到技术要求中,归类到其他要求中
|
||||
|
||||
# if __name__ == '__main__':
|
||||
# app = create_app()
|
||||
# app.run(debug=True, host='0.0.0.0', port=5000)
|
||||
if __name__ == '__main__':
|
||||
app = create_app()
|
||||
app.run(debug=True, host='0.0.0.0', port=5000)
|
||||
|
Loading…
x
Reference in New Issue
Block a user