feat/调整tts音色,调整封面图prompt,修复对话页输入框显示逻辑,待验证封面图生成功能
This commit is contained in:
@@ -62,7 +62,7 @@ class Settings(BaseSettings):
|
||||
# ── TTS (openai | tencent) ───────────────────────────────
|
||||
tts_provider: str = "tencent"
|
||||
openai_api_key: str = ""
|
||||
tts_voice_type: int = 603004 # Tencent 音色 ID,见 https://cloud.tencent.com/document/product/1073/92668
|
||||
tts_voice_type: int = 502001 # Tencent 音色 ID,见 https://cloud.tencent.com/document/product/1073/92668
|
||||
tts_codec: str = "mp3"
|
||||
|
||||
# ── WeChat Pay ───────────────────────────────────────────
|
||||
|
||||
@@ -165,7 +165,7 @@ class MemoirImagePromptService:
|
||||
"hero composition, evocative scene, emotionally resonant, "
|
||||
"cinematic framing, natural lighting, no text overlay."
|
||||
)
|
||||
details = (context_excerpt or "").strip()[:200]
|
||||
details = (context_excerpt or "").strip()[:500]
|
||||
if not details:
|
||||
details = "A personal life story scene with authentic emotional detail"
|
||||
return (
|
||||
|
||||
@@ -96,6 +96,18 @@ async def get_chapter(
|
||||
return await service.get_chapter(chapter_id, current_user.id)
|
||||
|
||||
|
||||
@router.post("/chapters/check-cover-generation")
|
||||
async def check_cover_generation(
|
||||
current_user: User = Depends(get_current_user),
|
||||
service: MemoirService = Depends(get_memoir_service),
|
||||
):
|
||||
"""
|
||||
检查可生成封面的章节(section 配图 > 3 且无已完成封面),
|
||||
若有则触发生成任务。已有封面的章节不再检查。
|
||||
"""
|
||||
return await service.check_and_trigger_cover_generation(current_user.id)
|
||||
|
||||
|
||||
@router.delete("/chapters/{chapter_id}")
|
||||
async def disable_chapter(
|
||||
chapter_id: str,
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
"""Memoir service — 回忆录编排(章节生成、状态流转);通过 MemoryService 获取 evidence。"""
|
||||
|
||||
from app.core.logging import get_logger
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import List, Optional
|
||||
|
||||
from app.core.logging import get_logger
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -18,7 +20,11 @@ from app.features.memoir.helpers import (
|
||||
chapter_to_dict,
|
||||
is_image_permanently_unavailable,
|
||||
)
|
||||
from app.features.memoir.models import Book, Chapter, ChapterSection
|
||||
from app.features.memoir.models import Book, Chapter, ChapterSection, MemoirImage
|
||||
from app.features.memoir.memoir_images.parser import build_initial_image_assets
|
||||
from app.features.memoir.memoir_images.serializers import image_dict_to_row_kwargs
|
||||
from app.features.memoir.memoir_images.prompting import MemoirImagePromptService
|
||||
from app.features.memoir.memoir_images.settings import MemoirImageSettings
|
||||
from app.features.memory.service import MemoryService
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@@ -210,6 +216,70 @@ class MemoirService:
|
||||
"covered_stages": state.covered_stages,
|
||||
}
|
||||
|
||||
async def check_and_trigger_cover_generation(self, user_id: str) -> dict:
|
||||
"""
|
||||
检查可生成封面的章节(section 配图 > 3 且无已完成封面),
|
||||
若有则触发生成任务。已有封面的章节不再检查。
|
||||
"""
|
||||
from app.tasks.memoir_tasks import generate_chapter_images
|
||||
|
||||
chapters = await repo.get_chapters_with_sections(
|
||||
user_id, self._db, active_only=True, is_new_only=None
|
||||
)
|
||||
triggered: List[str] = []
|
||||
for ch in chapters:
|
||||
if not ch.category or ch.status == "empty":
|
||||
continue
|
||||
sections = getattr(ch, "sections", None) or []
|
||||
section_image_count = sum(1 for s in sections if getattr(s, "image_id", None))
|
||||
images = getattr(ch, "images", None) or []
|
||||
cover_rec = next(
|
||||
(m for m in images if getattr(m, "section_id", None) is None),
|
||||
None,
|
||||
)
|
||||
if section_image_count <= 3:
|
||||
continue
|
||||
if cover_rec and (getattr(cover_rec, "status") or "").strip() == "completed":
|
||||
continue
|
||||
if cover_rec is None:
|
||||
img_settings = MemoirImageSettings.from_env()
|
||||
if img_settings.enabled:
|
||||
now_iso = datetime.now(timezone.utc).isoformat()
|
||||
cover_ph = {
|
||||
"placeholder": "{{{{{{{{IMAGE:章节封面}}}}}}}}",
|
||||
"description": "章节封面",
|
||||
"index": 0,
|
||||
}
|
||||
style = MemoirImagePromptService.CATEGORY_STYLE_MAP.get(
|
||||
ch.category or "", img_settings.default_style
|
||||
)
|
||||
cover_asset = build_initial_image_assets(
|
||||
[cover_ph],
|
||||
img_settings.provider,
|
||||
style,
|
||||
img_settings.default_size,
|
||||
now_iso,
|
||||
)[0]
|
||||
kwargs = image_dict_to_row_kwargs(cover_asset)
|
||||
cover_mi = MemoirImage(
|
||||
id=str(uuid.uuid4()).replace("-", "")[:32],
|
||||
chapter_id=ch.id,
|
||||
section_id=None,
|
||||
order_index=0,
|
||||
**kwargs,
|
||||
)
|
||||
self._db.add(cover_mi)
|
||||
await self._db.commit()
|
||||
await self._db.refresh(ch)
|
||||
logger.info("创建封面占位: chapter=%s", ch.id)
|
||||
try:
|
||||
generate_chapter_images.delay(ch.id)
|
||||
triggered.append(ch.id)
|
||||
logger.info("触发生成封面: chapter=%s", ch.id)
|
||||
except Exception as exc:
|
||||
logger.warning("封面生成任务派发失败: chapter=%s, error=%s", ch.id, exc)
|
||||
return {"triggered": triggered}
|
||||
|
||||
async def mark_memoir_read(self, user_id: str) -> dict:
|
||||
stmt = select(Chapter).where(
|
||||
Chapter.user_id == user_id, Chapter.is_new == True
|
||||
|
||||
@@ -342,7 +342,9 @@ def _save_narrative_to_sections(db: Session, chapter, narrative: str, title: str
|
||||
)
|
||||
db.add(sec)
|
||||
db.flush()
|
||||
if img_settings.enabled:
|
||||
# 封面:仅当 section 配图 > 3 时创建
|
||||
section_image_count = sum(1 for s in existing_sections if s.image_id)
|
||||
if img_settings.enabled and section_image_count > 3:
|
||||
stmt_cover = (
|
||||
select(MemoirImage)
|
||||
.where(
|
||||
@@ -381,7 +383,7 @@ def _save_narrative_to_sections(db: Session, chapter, narrative: str, title: str
|
||||
return ph
|
||||
content = (seg.get("content") or "").strip()
|
||||
desc = (content[:50] + "…") if len(content) > 50 else (content or "章节配图")
|
||||
return {"placeholder": f"{{{{{{{{IMAGE:{desc}}}}}}}}}", "description": desc}
|
||||
return {"placeholder": f"{{{{{{{{IMAGE:{desc}}}}}}}}}", "description": desc, "index": order_idx}
|
||||
|
||||
# 按顺序创建 section,每 3 个 section 对应 1 张配图
|
||||
for i, seg in enumerate(segments):
|
||||
@@ -416,8 +418,11 @@ def _save_narrative_to_sections(db: Session, chapter, narrative: str, title: str
|
||||
sec.image_id = mi.id
|
||||
db.flush()
|
||||
|
||||
# 封面图:若无则创建 pending MemoirImage(section_id=None, order_index=0)
|
||||
if img_settings.enabled:
|
||||
# 封面图:仅当 section 配图 > 3 时创建 pending MemoirImage(section_id=None, order_index=0)
|
||||
existing_with_img = sum(1 for s in existing_sections if s.image_id)
|
||||
new_with_img = sum(1 for i in range(len(segments)) if _should_have_image(order_base + i))
|
||||
section_image_count = existing_with_img + new_with_img
|
||||
if img_settings.enabled and section_image_count > 3:
|
||||
stmt_cover = (
|
||||
select(MemoirImage)
|
||||
.where(
|
||||
@@ -1020,8 +1025,10 @@ def generate_chapter_images(self, chapter_id: str):
|
||||
db.commit()
|
||||
try:
|
||||
sections_ordered = sorted(sections, key=lambda s: getattr(s, "order_index", 0))
|
||||
first_content = (sections_ordered[0].content or "").strip() if sections_ordered else ""
|
||||
context_excerpt = " ".join(first_content.split("\n")[:5])[:200]
|
||||
full_content = "\n\n".join(
|
||||
(s.content or "").strip() for s in sections_ordered if (s.content or "").strip()
|
||||
)
|
||||
context_excerpt = full_content[:1500] if full_content else ""
|
||||
prompt_data = prompt_service.build_cover_prompt(
|
||||
chapter_title=chapter.title,
|
||||
chapter_category=chapter.category or "",
|
||||
|
||||
Reference in New Issue
Block a user