feat: Add emotion handling to audiobook segments with emo_text and emo_alpha attributes

This commit is contained in:
2026-03-12 14:34:20 +08:00
parent 1757f39322
commit bbcfc0e8d3
8 changed files with 37 additions and 5 deletions

View File

@@ -398,9 +398,12 @@ async def parse_one_chapter(project_id: int, chapter_id: int, user: User, db) ->
char = char_map.get(seg.get("character", "narrator")) or char_map.get("narrator")
if not char:
continue
seg_emo_text = seg.get("emo_text", "") or None
seg_emo_alpha = seg.get("emo_alpha") if seg_emo_text else None
crud.create_audiobook_segment(
db, project_id, char.id, seg_text,
chapter.chapter_index, seg_counter,
emo_text=seg_emo_text, emo_alpha=seg_emo_alpha,
)
seg_counter += 1
chunk_count += 1
@@ -580,8 +583,8 @@ async def generate_project(project_id: int, user: User, db: Session, chapter_ind
text=seg.text,
spk_audio_prompt=design.ref_audio_path,
output_path=str(audio_path),
emo_text=char.instruct or None,
emo_alpha=0.6,
emo_text=seg.emo_text or None,
emo_alpha=seg.emo_alpha if seg.emo_text else 0.5,
)
else:
if design.voice_cache_id:

View File

@@ -201,9 +201,14 @@ class LLMService:
system_prompt = (
"你是一个专业的有声书制作助手。请将给定的章节文本解析为对话片段列表。"
f"已知角色列表(必须从中选择):{names_str}"
"所有非对话的叙述文字归属于narrator角色。"
"所有非对话的叙述文字归属于narrator角色。\n"
"同时根据语境为每个片段判断情绪,可选情绪及对应强度如下(必须严格使用以下值):\n"
"开心(emo_alpha=0.6)、愤怒(emo_alpha=0.15)、悲伤(emo_alpha=0.4)、恐惧(emo_alpha=0.4)、"
"厌恶(emo_alpha=0.6)、低沉(emo_alpha=0.6)、惊讶(emo_alpha=0.3)、中性(emo_alpha=0.5)。\n"
"narrator旁白及情绪不明显的片段emo_text设为\"\"emo_alpha设为0.5。\n"
"只输出JSON数组不要有其他文字格式如下\n"
'[{"character": "narrator", "text": "叙述文字"}, {"character": "角色名", "text": "对话内容"}, ...]'
'[{"character": "narrator", "text": "叙述文字", "emo_text": "", "emo_alpha": 0.5}, '
'{"character": "角色名", "text": "对话内容", "emo_text": "开心", "emo_alpha": 0.6}, ...]'
)
user_message = f"请解析以下章节文本:\n\n{chapter_text}"
result = await self.stream_chat_json(system_prompt, user_message, on_token, max_tokens=16384)