feat: improve character preview generation and adjust emotion handling guidelines in LLM service

This commit is contained in:
2026-03-13 00:46:49 +08:00
parent c2e1ee0289
commit eca99da084
2 changed files with 28 additions and 12 deletions

View File

@@ -561,6 +561,19 @@ async def generate_project(project_id: int, user: User, db: Session, chapter_ind
if preview_path.exists():
ref_audio = str(preview_path)
if not ref_audio or not Path(ref_audio).exists():
logger.info(f"No ref audio for char {char.id}, generating preview on-demand...")
try:
await generate_character_preview(project_id, char.id, user, db)
db.refresh(design)
ref_audio = design.ref_audio_path
if not ref_audio or not Path(ref_audio).exists():
preview_path = Path(settings.OUTPUT_DIR) / "audiobook" / str(project_id) / "previews" / f"char_{char.id}.wav"
if preview_path.exists():
ref_audio = str(preview_path)
except Exception as prev_e:
logger.error(f"On-demand preview generation failed for char {char.id}: {prev_e}")
if not ref_audio or not Path(ref_audio).exists():
logger.error(f"No ref audio for char {char.id}, skipping segment {seg.id}")
crud.update_audiobook_segment_status(db, seg.id, "error")
@@ -572,7 +585,7 @@ async def generate_project(project_id: int, user: User, db: Session, chapter_ind
spk_audio_prompt=ref_audio,
output_path=str(audio_path),
emo_text=seg.emo_text or None,
emo_alpha=seg.emo_alpha if seg.emo_alpha is not None else 0.6,
emo_alpha=seg.emo_alpha if seg.emo_alpha is not None else 0.3,
)
with open(audio_path, "wb") as f:
@@ -654,7 +667,7 @@ async def generate_single_segment(segment_id: int, user: User, db: Session) -> N
spk_audio_prompt=ref_audio,
output_path=str(audio_path),
emo_text=seg.emo_text or None,
emo_alpha=seg.emo_alpha if seg.emo_alpha is not None else 0.6,
emo_alpha=seg.emo_alpha if seg.emo_alpha is not None else 0.3,
)
with open(audio_path, "wb") as f:
@@ -838,8 +851,12 @@ async def generate_character_preview(project_id: int, char_id: int, user: User,
backend_type = user.user_preferences.get("default_backend", "aliyun") if user.user_preferences else "aliyun"
user_api_key = None
if backend_type == "aliyun" and user.aliyun_api_key:
user_api_key = decrypt_api_key(user.aliyun_api_key)
if backend_type == "aliyun":
encrypted = crud.get_system_setting(db, "aliyun_api_key")
if encrypted:
user_api_key = decrypt_api_key(encrypted)
elif user.aliyun_api_key:
user_api_key = decrypt_api_key(user.aliyun_api_key)
backend = await TTSServiceFactory.get_backend(backend_type, user_api_key)