This commit is contained in:
@@ -96,3 +96,62 @@ class BailianService:
|
||||
evaluation.error_message = str(e)
|
||||
evaluation.save()
|
||||
return evaluation
|
||||
|
||||
def summarize_task(self, task):
|
||||
"""
|
||||
对转写任务进行总结
|
||||
"""
|
||||
if not self.client:
|
||||
logger.warning("BailianService not initialized, skipping summary.")
|
||||
return
|
||||
|
||||
if not task.transcription:
|
||||
logger.warning(f"Task {task.id} has no transcription, skipping summary.")
|
||||
return
|
||||
|
||||
try:
|
||||
content = task.transcription
|
||||
# 简单截断防止过长
|
||||
if len(content) > 15000:
|
||||
content = content[:15000] + "...(内容过长已截断)"
|
||||
|
||||
# 准备上下文数据
|
||||
context_data = ""
|
||||
if task.summary_data:
|
||||
context_data += f"\n\n【总结原始数据】\n{json.dumps(task.summary_data, ensure_ascii=False, indent=2)}"
|
||||
|
||||
if task.auto_chapters_data:
|
||||
context_data += f"\n\n【章节原始数据】\n{json.dumps(task.auto_chapters_data, ensure_ascii=False, indent=2)}"
|
||||
|
||||
system_prompt = f"""你是一个专业的会议/内容总结助手。请根据提供的【转写文本】、【总结原始数据】和【章节原始数据】,生成一份结构清晰、内容详实的总结报告。
|
||||
|
||||
请按照以下结构输出(Markdown格式):
|
||||
1. **标题**:基于内容生成一个合适的标题。
|
||||
2. **核心摘要**:简要概括主要内容。
|
||||
3. **主要观点/话题**:结合思维导图数据,列出关键话题和层级。
|
||||
4. **章节速览**:结合章节数据,列出时间点和主要内容。[HH:MM:SS]格式来把章节列出来
|
||||
5. **问答精选**(如果有):基于问答总结数据,列出重要问答。
|
||||
|
||||
请确保语言通顺,重点突出,能够还原内容的逻辑结构。"""
|
||||
|
||||
user_content = f"以下是需要总结的内容:\n\n【转写文本】\n{content}{context_data}"
|
||||
|
||||
messages = [
|
||||
{'role': 'system', 'content': system_prompt},
|
||||
{'role': 'user', 'content': user_content}
|
||||
]
|
||||
|
||||
# 使用 qwen-plus 作为默认模型
|
||||
completion = self.client.chat.completions.create(
|
||||
model="qwen-plus",
|
||||
messages=messages
|
||||
)
|
||||
|
||||
summary_content = completion.choices[0].message.content
|
||||
task.summary = summary_content
|
||||
task.save(update_fields=['summary'])
|
||||
|
||||
logger.info(f"Task {task.id} summary generated successfully.")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to generate summary for task {task.id}: {e}")
|
||||
|
||||
@@ -287,9 +287,26 @@ class TranscriptionTaskViewSet(viewsets.ModelViewSet):
|
||||
if task.status == TranscriptionTask.Status.SUCCEEDED and not task.summary:
|
||||
if task.summary_data or task.auto_chapters_data:
|
||||
try:
|
||||
# 先设置状态为 "AI总结生成当中..."
|
||||
task.summary = "AI总结生成当中..."
|
||||
task.save(update_fields=['summary'])
|
||||
|
||||
# 异步触发总结生成
|
||||
import threading
|
||||
from .bailian_service import BailianService
|
||||
bailian_service = BailianService()
|
||||
bailian_service.summarize_task(task)
|
||||
|
||||
def async_summarize(task_id):
|
||||
try:
|
||||
# 重新获取 task 对象以避免线程问题
|
||||
from .models import TranscriptionTask
|
||||
task_obj = TranscriptionTask.objects.get(id=task_id)
|
||||
bailian_service = BailianService()
|
||||
bailian_service.summarize_task(task_obj)
|
||||
except Exception as e:
|
||||
logger.error(f"Async summary generation failed: {e}")
|
||||
|
||||
threading.Thread(target=async_summarize, args=(task.id,)).start()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Force trigger AI summarization failed: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user