From 815eb51933eb32bf019976c73de1566da4a52df6 Mon Sep 17 00:00:00 2001 From: CI Bot Date: Thu, 10 Sep 2026 10:51:56 +0000 Subject: [PATCH] style: auto-format with black + isort + prettier [skip ci-format-check] --- apps/api/app/services/lipsync_service.py | 20 +++++++++++------ apps/api/app/tasks/lipsync_tts.py | 22 ++++++++++--------- .../test_ai_avatar_emotion_tts_lipsync.py | 8 ++----- tests/unit/test_lipsync_speed_optimization.py | 8 +++---- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/apps/api/app/services/lipsync_service.py b/apps/api/app/services/lipsync_service.py index 77c2c069a..385675980 100644 --- a/apps/api/app/services/lipsync_service.py +++ b/apps/api/app/services/lipsync_service.py @@ -25,14 +25,14 @@ from app.services.mediakit_client import ( MediaKitError, get_mediakit_client, ) + +# Celery 异步任务:TTS 合成 + MediaKit 提交(#lipsync-speed-optimization) +from app.tasks.lipsync_tts import tts_synthesize_and_submit from sqlalchemy.orm import Session from packages.adapters.sqlalchemy_impl.models import LipsyncJobModel from packages.application.cosyvoice_service import CosyVoiceError, normalize_emotion from packages.shared.storage import get_shared_storage_service - -# Celery 异步任务:TTS 合成 + MediaKit 提交(#lipsync-speed-optimization) -from app.tasks.lipsync_tts import tts_synthesize_and_submit from packages.shared.url_security import ALLOWED_AUDIO_MIME_TYPES, safe_download_bytes logger = logging.getLogger(__name__) @@ -201,10 +201,16 @@ class LipsyncService: if is_tts_mode: # 2a. TTS 模式:dispatch Celery 异步任务处理 TTS 合成 + MediaKit 提交 try: - tts_synthesize_and_submit.apply_async(args=( - job_id, user_id, voice_id, script_text, speed, - normalize_emotion(emotion), - )) + tts_synthesize_and_submit.apply_async( + args=( + job_id, + user_id, + voice_id, + script_text, + speed, + normalize_emotion(emotion), + ) + ) except Exception: logger.warning( "Celery 任务提交失败,TTS 任务已创建但未触发执行: %s", diff --git a/apps/api/app/tasks/lipsync_tts.py b/apps/api/app/tasks/lipsync_tts.py index d30140f23..1e54de212 100644 --- a/apps/api/app/tasks/lipsync_tts.py +++ b/apps/api/app/tasks/lipsync_tts.py @@ -11,8 +11,8 @@ 7. 异常时标记 job 为 failed """ -import logging import io +import logging import uuid from datetime import datetime, timezone @@ -40,21 +40,25 @@ def tts_synthesize_and_submit( 在 Celery worker 中运行,不阻塞 HTTP 请求。 """ + from app.services.mediakit_client import MediaKitError, get_mediakit_client from sqlalchemy.orm import Session as DBSession - from app.services.mediakit_client import MediaKitError, get_mediakit_client from packages.adapters.sqlalchemy_impl.database import SessionLocal from packages.adapters.sqlalchemy_impl.models import LipsyncJobModel from packages.application.cosyvoice_service import CosyVoiceError, CosyVoiceService - from packages.shared.storage import get_shared_storage_service from packages.shared.downloader import safe_download_bytes + from packages.shared.storage import get_shared_storage_service db: DBSession = SessionLocal() try: - job = db.query(LipsyncJobModel).filter( - LipsyncJobModel.id == job_id, - LipsyncJobModel.user_id == user_id, - ).first() + job = ( + db.query(LipsyncJobModel) + .filter( + LipsyncJobModel.id == job_id, + LipsyncJobModel.user_id == user_id, + ) + .first() + ) if job is None: logger.error("[lipsync_tts] Job not found: job_id=%s", job_id) @@ -111,9 +115,7 @@ def tts_synthesize_and_submit( ) storage = get_shared_storage_service() storage_key = f"lipsync-tts/{user_id}/{job_id}.mp3" - permanent_url = storage.upload_file( - io.BytesIO(audio_data), storage_key, content_type="audio/mpeg" - ) + permanent_url = storage.upload_file(io.BytesIO(audio_data), storage_key, content_type="audio/mpeg") logger.info("[lipsync_tts] TTS 音频已转存 OSS: job_id=%s key=%s", job_id, storage_key) job.audio_url = permanent_url except Exception as exc: diff --git a/tests/unit/test_ai_avatar_emotion_tts_lipsync.py b/tests/unit/test_ai_avatar_emotion_tts_lipsync.py index 184266d5e..74aeebc68 100644 --- a/tests/unit/test_ai_avatar_emotion_tts_lipsync.py +++ b/tests/unit/test_ai_avatar_emotion_tts_lipsync.py @@ -132,9 +132,7 @@ def _lipsync_service_with_mocks(): def test_create_job_tts_direct_mode_synthesizes_audio(): svc, client, cosy = _lipsync_service_with_mocks() - with patch( - "app.services.lipsync_service.tts_synthesize_and_submit" - ) as mock_task: + with patch("app.services.lipsync_service.tts_synthesize_and_submit") as mock_task: mock_task.apply_async.return_value = MagicMock(id="celery-task-123") job = svc.create_job( @@ -177,9 +175,7 @@ def test_create_job_tts_failure_raises(): TTS 合成失败由 Celery 任务内部处理并更新 job 状态。""" svc, client, cosy = _lipsync_service_with_mocks() - with patch( - "app.services.lipsync_service.tts_synthesize_and_submit" - ) as mock_task: + with patch("app.services.lipsync_service.tts_synthesize_and_submit") as mock_task: mock_task.apply_async.return_value = MagicMock(id="celery-task-456") job = svc.create_job( diff --git a/tests/unit/test_lipsync_speed_optimization.py b/tests/unit/test_lipsync_speed_optimization.py index 52b50ccf3..d53bc153b 100644 --- a/tests/unit/test_lipsync_speed_optimization.py +++ b/tests/unit/test_lipsync_speed_optimization.py @@ -35,9 +35,7 @@ class TestFFmpegPresetOptimization: final_label=None, output_path="/tmp/output.mp4", ) - assert "-preset veryfast" in cmd, ( - f"期望 -preset veryfast,实际命令: {cmd}" - ) + assert "-preset veryfast" in cmd, f"期望 -preset veryfast,实际命令: {cmd}" def test_preset_veryfast_with_filter(self): """带滤镜场景下也必须使用 veryfast.""" @@ -142,8 +140,8 @@ class TestCreateJobAsyncTTS: call_kwargs = mock_task.apply_async.call_args args = call_kwargs.kwargs.get("args") or call_kwargs[1].get("args", call_kwargs[0][0] if call_kwargs[0] else ()) assert args[1] == "user-1" # user_id - assert args[2] == "v-1" # voice_id - assert args[3] == "测试文本" # script_text + assert args[2] == "v-1" # voice_id + assert args[3] == "测试文本" # script_text def test_tts_mode_celery_dispatch_failure_still_creates_job(self): """Celery dispatch 失败时,job 记录已创建,状态保持 tts_processing."""