This commit is contained in:
@@ -323,14 +323,30 @@ class AliyunTingwuService:
|
|||||||
|
|
||||||
# 4. 自动触发 AI 评估 (如果任务首次成功且有启用的模板)
|
# 4. 自动触发 AI 评估 (如果任务首次成功且有启用的模板)
|
||||||
if previous_status != 'SUCCEEDED' and task.status == 'SUCCEEDED' and task.transcription:
|
if previous_status != 'SUCCEEDED' and task.status == 'SUCCEEDED' and task.transcription:
|
||||||
# 同样改为异步触发
|
# 同样改为异步触发,传递 task.id 以避免线程中的对象状态问题
|
||||||
import threading
|
import threading
|
||||||
threading.Thread(target=self.trigger_ai_evaluations, args=(task,)).start()
|
threading.Thread(target=self.trigger_ai_evaluations, args=(task.id,)).start()
|
||||||
|
|
||||||
def trigger_ai_evaluations(self, task):
|
def trigger_ai_evaluations(self, task_id):
|
||||||
"""
|
"""
|
||||||
根据启用的模板自动触发 AI 评估
|
根据启用的模板自动触发 AI 评估
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
|
# 在线程中重新获取 task 对象,并预加载 project,避免懒加载导致的线程数据库连接问题
|
||||||
|
from .models import TranscriptionTask
|
||||||
|
task = TranscriptionTask.objects.select_related('project').get(id=task_id)
|
||||||
|
except Exception as e:
|
||||||
|
# 兼容处理:如果 task_id 其实是 task 对象(虽然我们上面改了,但防止其他地方调用传错)
|
||||||
|
if hasattr(task_id, 'id'):
|
||||||
|
try:
|
||||||
|
from .models import TranscriptionTask
|
||||||
|
task = TranscriptionTask.objects.select_related('project').get(id=task_id.id)
|
||||||
|
except:
|
||||||
|
task = task_id
|
||||||
|
else:
|
||||||
|
logger.error(f"Failed to retrieve task {task_id}: {e}")
|
||||||
|
return
|
||||||
|
|
||||||
active_templates = AIEvaluationTemplate.objects.filter(is_active=True)
|
active_templates = AIEvaluationTemplate.objects.filter(is_active=True)
|
||||||
if not active_templates.exists():
|
if not active_templates.exists():
|
||||||
logger.info("No active AI evaluation templates found.")
|
logger.info("No active AI evaluation templates found.")
|
||||||
@@ -340,6 +356,11 @@ class AliyunTingwuService:
|
|||||||
service = BailianService()
|
service = BailianService()
|
||||||
|
|
||||||
for template in active_templates:
|
for template in active_templates:
|
||||||
|
# 检查是否已经存在相同的评估,避免重复创建
|
||||||
|
if AIEvaluation.objects.filter(task=task, template=template).exists():
|
||||||
|
logger.info(f"Evaluation for task {task.id} and template {template.name} already exists.")
|
||||||
|
continue
|
||||||
|
|
||||||
# 创建评估记录
|
# 创建评估记录
|
||||||
evaluation = AIEvaluation.objects.create(
|
evaluation = AIEvaluation.objects.create(
|
||||||
task=task,
|
task=task,
|
||||||
@@ -349,8 +370,7 @@ class AliyunTingwuService:
|
|||||||
status=AIEvaluation.Status.PENDING
|
status=AIEvaluation.Status.PENDING
|
||||||
)
|
)
|
||||||
|
|
||||||
# 触发评估 (同步执行,或者放入 Celery 任务)
|
# 触发评估
|
||||||
# 这里为了简单直接调用,生产环境建议使用 Celery
|
|
||||||
try:
|
try:
|
||||||
service.evaluate_task(evaluation)
|
service.evaluate_task(evaluation)
|
||||||
logger.info(f"Triggered evaluation {evaluation.id} for template {template.name}")
|
logger.info(f"Triggered evaluation {evaluation.id} for template {template.name}")
|
||||||
|
|||||||
@@ -116,6 +116,7 @@ class TranscriptionTaskViewSet(viewsets.ModelViewSet):
|
|||||||
pass # Ignore invalid project_id
|
pass # Ignore invalid project_id
|
||||||
|
|
||||||
task_record = TranscriptionTask.objects.create(**task_data)
|
task_record = TranscriptionTask.objects.create(**task_data)
|
||||||
|
logger.info(f"Created TranscriptionTask {task_record.id} with project_id={project_id}")
|
||||||
|
|
||||||
# 3. 调用听悟接口创建任务
|
# 3. 调用听悟接口创建任务
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user