|
|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
"""
|
|
|
|
|
RQ Worker 微调任务处理器 - 适配新数据库结构
|
|
|
|
|
仅支持真实算法,移除虚拟算法调用
|
|
|
|
|
RQ Worker 微调任务处理器
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
@ -22,7 +21,7 @@ def run_finetune_task(task_id, finetune_method, train_images_dir,
|
|
|
|
|
output_model_dir, class_dir, coords_save_path, validation_output_dir,
|
|
|
|
|
finetune_type, custom_params=None):
|
|
|
|
|
"""
|
|
|
|
|
执行微调任务(仅使用真实算法)
|
|
|
|
|
执行微调任务
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
task_id: 任务ID
|
|
|
|
|
@ -95,7 +94,7 @@ def run_finetune_task(task_id, finetune_method, train_images_dir,
|
|
|
|
|
# DreamBooth/LoRA: 直接使用模板
|
|
|
|
|
instance_prompt_prefix = instance_prompt
|
|
|
|
|
|
|
|
|
|
# 处理 Validation Prompt (拼接后缀)
|
|
|
|
|
# 处理 Validation Prompt
|
|
|
|
|
prompt_suffix = finetune.custom_prompt.strip() if finetune.custom_prompt else ""
|
|
|
|
|
|
|
|
|
|
if prompt_suffix:
|
|
|
|
|
@ -207,7 +206,7 @@ def _run_real_finetune(finetune_method, task_id, train_images_dir, output_model_
|
|
|
|
|
class_dir, coords_save_path, validation_output_dir,
|
|
|
|
|
instance_prompt, class_prompt, validation_prompt, finetune_type, custom_params, log_file):
|
|
|
|
|
"""
|
|
|
|
|
运行真实微调算法(参考sh脚本配置)
|
|
|
|
|
运行真实微调算法
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
finetune_method: 微调方法
|
|
|
|
|
@ -240,7 +239,7 @@ def _run_real_finetune(finetune_method, task_id, train_images_dir, output_model_
|
|
|
|
|
if not script_path:
|
|
|
|
|
raise ValueError(f"Finetune method {finetune_method} not configured")
|
|
|
|
|
|
|
|
|
|
# 覆盖提示词参数(从数据库读取)
|
|
|
|
|
# 覆盖提示词参数
|
|
|
|
|
if 'instance_prompt' in default_params:
|
|
|
|
|
default_params['instance_prompt'] = instance_prompt
|
|
|
|
|
if 'class_prompt' in default_params:
|
|
|
|
|
@ -273,7 +272,7 @@ def _run_real_finetune(finetune_method, task_id, train_images_dir, output_model_
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
elif finetune_method == 'textual_inversion':
|
|
|
|
|
# Textual Inversion 特有参数 (不需要 class_data_dir)
|
|
|
|
|
# Textual Inversion 特有参数
|
|
|
|
|
cmd_args.extend([
|
|
|
|
|
f"--coords_save_path={coords_save_path}",
|
|
|
|
|
])
|
|
|
|
|
@ -354,30 +353,6 @@ def _run_real_finetune(finetune_method, task_id, train_images_dir, output_model_
|
|
|
|
|
os.makedirs(output_model_dir)
|
|
|
|
|
|
|
|
|
|
logger.info(f"Cleanup completed. Only validation images and coords.json are kept.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # 清理class_dir(参考sh脚本)
|
|
|
|
|
# if finetune_method in ['dreambooth', 'lora']:
|
|
|
|
|
# logger.info(f"Cleaning class directory: {class_dir}")
|
|
|
|
|
# if os.path.exists(class_dir):
|
|
|
|
|
# shutil.rmtree(class_dir)
|
|
|
|
|
# os.makedirs(class_dir)
|
|
|
|
|
|
|
|
|
|
# # 清理output_model_dir中的非图片文件
|
|
|
|
|
# logger.info(f"Cleaning non-image files in output directory: {output_model_dir}")
|
|
|
|
|
# if os.path.exists(output_model_dir):
|
|
|
|
|
# image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.webp', '.tiff'}
|
|
|
|
|
# for item in os.listdir(output_model_dir):
|
|
|
|
|
# item_path = os.path.join(output_model_dir, item)
|
|
|
|
|
# if os.path.isfile(item_path):
|
|
|
|
|
# _, ext = os.path.splitext(item)
|
|
|
|
|
# if ext.lower() not in image_extensions:
|
|
|
|
|
# try:
|
|
|
|
|
# os.remove(item_path)
|
|
|
|
|
# logger.info(f"Removed non-image file: {item}")
|
|
|
|
|
# except Exception as e:
|
|
|
|
|
# logger.warning(f"Failed to remove {item}: {str(e)}")
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'status': 'success',
|
|
|
|
|
@ -388,7 +363,7 @@ def _run_real_finetune(finetune_method, task_id, train_images_dir, output_model_
|
|
|
|
|
|
|
|
|
|
def _save_generated_images(task_id, output_dir, finetune_type):
|
|
|
|
|
"""
|
|
|
|
|
保存微调生成的验证图片到数据库(适配新数据库结构)
|
|
|
|
|
保存微调生成的验证图片到数据库
|
|
|
|
|
|
|
|
|
|
新数据库结构:
|
|
|
|
|
- Task表:tasks_id (主键), flow_id, tasks_type_id
|
|
|
|
|
|