2025-07-10 17:51:37 +08:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
海报服务层
|
|
|
|
|
|
封装现有功能,提供API调用
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
from typing import List, Dict, Any, Optional, Tuple
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
from core.config import ConfigManager, PosterConfig
|
|
|
|
|
|
from core.ai import AIAgent
|
|
|
|
|
|
from utils.file_io import OutputManager
|
|
|
|
|
|
from poster.poster_generator import PosterGenerator
|
|
|
|
|
|
from poster.text_generator import PosterContentGenerator
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PosterService:
|
|
|
|
|
|
"""海报服务类"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, ai_agent: AIAgent, config_manager: ConfigManager, output_manager: OutputManager):
|
|
|
|
|
|
"""
|
|
|
|
|
|
初始化海报服务
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
ai_agent: AI代理
|
|
|
|
|
|
config_manager: 配置管理器
|
|
|
|
|
|
output_manager: 输出管理器
|
|
|
|
|
|
"""
|
|
|
|
|
|
self.ai_agent = ai_agent
|
|
|
|
|
|
self.config_manager = config_manager
|
|
|
|
|
|
self.output_manager = output_manager
|
|
|
|
|
|
|
|
|
|
|
|
# 初始化各个组件
|
|
|
|
|
|
self.poster_generator = PosterGenerator(config_manager, output_manager)
|
|
|
|
|
|
self.text_generator = PosterContentGenerator(ai_agent)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_poster(self, content: Dict[str, Any], topic_index: str,
|
|
|
|
|
|
template_name: Optional[str] = None) -> Tuple[str, str, str, str]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
生成海报
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
content: 内容数据,包含标题、正文等
|
|
|
|
|
|
topic_index: 主题索引,用于文件命名
|
|
|
|
|
|
template_name: 模板名称,如果为None则根据配置选择
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
请求ID、主题索引、生成的海报文件路径和使用的模板名称
|
|
|
|
|
|
"""
|
|
|
|
|
|
logger.info(f"开始为主题 {topic_index} 生成海报")
|
|
|
|
|
|
|
|
|
|
|
|
# 生成海报
|
|
|
|
|
|
poster_path = self.poster_generator.generate_poster(content, topic_index, template_name)
|
|
|
|
|
|
|
|
|
|
|
|
# 获取使用的模板名称
|
|
|
|
|
|
if template_name is None:
|
|
|
|
|
|
template_name = self.poster_generator._select_template()
|
|
|
|
|
|
|
|
|
|
|
|
# 生成请求ID
|
2025-07-14 13:59:22 +08:00
|
|
|
|
request_id = f"poster-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}"
|
2025-07-10 17:51:37 +08:00
|
|
|
|
|
|
|
|
|
|
logger.info(f"海报生成完成,请求ID: {request_id}, 主题索引: {topic_index}, 模板: {template_name}")
|
|
|
|
|
|
return request_id, topic_index, poster_path, template_name
|
|
|
|
|
|
|
|
|
|
|
|
def get_available_templates(self) -> Tuple[List[str], str]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取可用的模板列表
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
可用的模板列表和默认模板
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 获取配置
|
|
|
|
|
|
poster_config = self.config_manager.get_config('poster', PosterConfig)
|
|
|
|
|
|
|
|
|
|
|
|
# 获取可用模板
|
|
|
|
|
|
available_templates = poster_config.available_templates
|
|
|
|
|
|
|
|
|
|
|
|
# 获取默认模板
|
|
|
|
|
|
default_template = poster_config.template_selection
|
|
|
|
|
|
if default_template == "random" and available_templates:
|
|
|
|
|
|
default_template = available_templates[0]
|
|
|
|
|
|
|
|
|
|
|
|
return available_templates, default_template
|
|
|
|
|
|
|
|
|
|
|
|
async def generate_poster_text(self, system_prompt: str, user_prompt: str,
|
|
|
|
|
|
context_data: Optional[Dict[str, Any]] = None,
|
|
|
|
|
|
temperature: Optional[float] = None,
|
|
|
|
|
|
top_p: Optional[float] = None) -> Tuple[str, Dict[str, Any]]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
生成海报文案
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
system_prompt: 系统提示词
|
|
|
|
|
|
user_prompt: 用户提示词
|
|
|
|
|
|
context_data: 上下文数据,用于填充提示词中的占位符
|
|
|
|
|
|
temperature: 生成温度参数
|
|
|
|
|
|
top_p: top_p参数
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
请求ID和生成的文案内容
|
|
|
|
|
|
"""
|
|
|
|
|
|
logger.info("开始生成海报文案")
|
|
|
|
|
|
|
|
|
|
|
|
# 生成文案
|
|
|
|
|
|
text_content = await self.text_generator.generate_text_for_poster(
|
|
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
|
|
user_prompt=user_prompt,
|
|
|
|
|
|
context_data=context_data,
|
|
|
|
|
|
temperature=temperature,
|
|
|
|
|
|
top_p=top_p
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# 生成请求ID
|
2025-07-14 13:59:22 +08:00
|
|
|
|
request_id = f"text-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}"
|
2025-07-10 17:51:37 +08:00
|
|
|
|
|
|
|
|
|
|
logger.info(f"海报文案生成完成,请求ID: {request_id}")
|
|
|
|
|
|
return request_id, text_content
|