生成记录接口兼容图片类型|CHAT对话生成视频/图片任务API+celery异步模块完成

This commit is contained in:
2026-05-27 13:11:55 +08:00
parent d61dcdc8db
commit b8eda53c0b
30 changed files with 3392 additions and 45 deletions
+69 -35
View File
@@ -43,6 +43,7 @@ from app.services.notification import create_notification
from app.services.auth import hash_password, verify_password
from app.services.operation_log import log_operation
from app.utils.id_gen import generate_id
from app.schemas.generation import GenerationType, ASPECT_RATIOS, RESOLUTIONS
CST = timezone(timedelta(hours=8))
@@ -991,6 +992,14 @@ async def admin_list_generation_records(
"error_message": record.error_message,
"created_at": _iso(record.created_at),
"generated_at": _iso(record.generated_at),
# append img param
"gen_type": record.gen_type,
"image_size": record.image_size or '',
"image_url": record.image_url or '',
"image_tokens_used": record.image_tokens_used or 0,
"image_proportion": record.image_proportion or '',
"image_px": record.image_px or '',
})
return {"total": total, "items": items}
@@ -1033,8 +1042,8 @@ async def admin_generate_video(
):
"""Admin trigger video generation for a record with specified params."""
from app.models.project import Project
from app.services.credits import calc_video_credits, deduct_credits
from app.schemas.generation import ASPECT_RATIOS, RESOLUTIONS
from app.services.credits import calc_video_credits, deduct_credits, calc_image_credits
from app.services.video_queue import task_queue
result = await db.execute(
select(GenerationRecord, Project.name)
@@ -1046,43 +1055,68 @@ async def admin_generate_video(
raise HTTPException(status_code=404, detail="记录不存在")
record, project_name = row
type_str = "视频" if record.gen_type == GenerationType.video else "图片"
if record.status not in ("prompt_optimized", "failed"):
raise HTTPException(status_code=400, detail="当前状态不允许生成视频")
raise HTTPException(status_code=400, detail=f"当前状态不允许生成{type_str}")
aspect_ratio = body.get("aspect_ratio", "16:9")
resolution = body.get("resolution", "720p")
if aspect_ratio not in ASPECT_RATIOS:
raise HTTPException(status_code=400, detail="不支持的画面比例")
if resolution not in RESOLUTIONS:
raise HTTPException(status_code=400, detail="不支持的分辨率")
if record.gen_type == GenerationType.video:
# Video Generation
aspect_ratio = body.get("aspect_ratio", "16:9")
resolution = body.get("resolution", "720p")
if aspect_ratio not in ASPECT_RATIOS:
raise HTTPException(status_code=400, detail="不支持的画面比例")
if resolution not in RESOLUTIONS:
raise HTTPException(status_code=400, detail="不支持的分辨率")
duration = record.duration or 5
video_credits = await calc_video_credits(db, duration, resolution)
await deduct_credits(
db, record.user_id, video_credits,
f"视频生成(管理后台) - {project_name}",
related_id=record_id,
)
duration = record.duration or 5
video_credits = await calc_video_credits(db, duration, resolution)
await deduct_credits(
db, record.user_id, video_credits,
f"视频生成(管理后台) - {project_name}",
related_id=record_id,
)
record.aspect_ratio = aspect_ratio
record.resolution = resolution
record.credits_cost = (record.credits_cost or 0) + video_credits
record.status = "generating"
record.error_message = None
await db.flush()
try:
from app.services.video_gen import get_active_engine, submit_video_task
from app.services.video_queue import task_queue
engine = await get_active_engine(db)
task_id = await submit_video_task(db, engine, record)
record.seedance_task_id = task_id
await db.flush()
await task_queue.enqueue(record_id)
except Exception as e:
record.status = "failed"
record.error_message = str(e)
record.aspect_ratio = aspect_ratio
record.resolution = resolution
record.credits_cost = (record.credits_cost or 0) + video_credits
record.status = "generating"
record.error_message = None
await db.flush()
try:
from app.services.video_gen import get_active_engine, submit_video_task
engine = await get_active_engine(db)
task_id = await submit_video_task(db, engine, record)
record.seedance_task_id = task_id
await db.flush()
await task_queue.enqueue(record_id)
except Exception as e:
record.status = "failed"
record.error_message = str(e)
await db.flush()
elif record.gen_type == GenerationType.image:
# Image generation
post_image_size = body.get("image_size", "")
image_credits = await calc_image_credits(db, post_image_size or record.image_size or "2K")
await deduct_credits(
db, record.user_id, image_credits,
f"图片生成 - {project_name}",
related_id=record_id,
)
try:
record.image_size = post_image_size or record.image_size or "2K"
record.credits_cost = round(image_credits, 2)
record.status = "generating"
record.error_message = None
await db.flush()
await task_queue.enqueue(record_id)
except Exception as e:
record.status = "failed"
record.error_message = str(e)
await db.flush()
return {"message": "ok", "record_id": record_id}