mirror of
https://github.com/linyqh/NarratoAI.git
synced 2025-12-12 03:02:48 +00:00
refactor(app): 重构生成解说文案功能并支持 deepseek-reasoner 模型
- 引入 loguru 库用于日志记录 - 优化 API 请求流程,支持非 JSON 输出的模型- 更新 API 密钥和基础 URL - 修改系统提示语以更符合短视频解说文案的需求 - 优化日志输出,使用 debug 级别记录 token 消耗 - 清理生成的解说文案中的多余字符 - 更新文档生成工具中的日志级别
This commit is contained in:
parent
dac23c7c31
commit
b762bf8d93
@ -12,6 +12,7 @@ import json
|
|||||||
import os
|
import os
|
||||||
import traceback
|
import traceback
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
|
||||||
def parse_frame_analysis_to_markdown(json_file_path):
|
def parse_frame_analysis_to_markdown(json_file_path):
|
||||||
@ -184,21 +185,42 @@ def generate_narration(markdown_content, api_key, base_url, model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 使用SDK发送请求
|
# 使用SDK发送请求
|
||||||
|
if model not in ["deepseek-reasoner"]:
|
||||||
|
# deepseek-reasoner 不支持 json 输出
|
||||||
response = client.chat.completions.create(
|
response = client.chat.completions.create(
|
||||||
model=model,
|
model=model,
|
||||||
messages=[
|
messages=[
|
||||||
{"role": "system", "content": "你是一名专业的视频解说文案撰写专家。"},
|
{"role": "system", "content": "你是一名专业的短视频解说文案撰写专家。"},
|
||||||
{"role": "user", "content": prompt}
|
{"role": "user", "content": prompt}
|
||||||
],
|
],
|
||||||
temperature=0.7,
|
temperature=1.5,
|
||||||
response_format={"type": "json_object"},
|
response_format={"type": "json_object"},
|
||||||
)
|
)
|
||||||
|
|
||||||
# 提取生成的文案
|
# 提取生成的文案
|
||||||
if response.choices and len(response.choices) > 0:
|
if response.choices and len(response.choices) > 0:
|
||||||
narration_script = response.choices[0].message.content
|
narration_script = response.choices[0].message.content
|
||||||
# 打印消耗的tokens
|
# 打印消耗的tokens
|
||||||
print(response.usage.total_tokens)
|
logger.debug(f"消耗的tokens: {response.usage.total_tokens}")
|
||||||
|
return narration_script
|
||||||
|
else:
|
||||||
|
return "生成解说文案失败: 未获取到有效响应"
|
||||||
|
else:
|
||||||
|
# 不支持 json 输出,需要多一步处理 ```json ``` 的步骤
|
||||||
|
response = client.chat.completions.create(
|
||||||
|
model=model,
|
||||||
|
messages=[
|
||||||
|
{"role": "system", "content": "你是一名专业的短视频解说文案撰写专家。"},
|
||||||
|
{"role": "user", "content": prompt}
|
||||||
|
],
|
||||||
|
temperature=1.5,
|
||||||
|
)
|
||||||
|
# 提取生成的文案
|
||||||
|
if response.choices and len(response.choices) > 0:
|
||||||
|
narration_script = response.choices[0].message.content
|
||||||
|
# 打印消耗的tokens
|
||||||
|
logger.debug(f"文案消耗的tokens: {response.usage.total_tokens}")
|
||||||
|
# 清理 narration_script 字符串前后的 ```json ``` 字符串
|
||||||
|
narration_script = narration_script.replace("```json", "").replace("```", "")
|
||||||
return narration_script
|
return narration_script
|
||||||
else:
|
else:
|
||||||
return "生成解说文案失败: 未获取到有效响应"
|
return "生成解说文案失败: 未获取到有效响应"
|
||||||
@ -209,9 +231,9 @@ def generate_narration(markdown_content, api_key, base_url, model):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
text_provider = 'openai'
|
text_provider = 'openai'
|
||||||
text_api_key = "sk-lyxttmuwgcmocfmupotkaenxnfofednrqappdrypwtrboang"
|
text_api_key = "sk-6f7bb9d1cbee4012921268ecfe733851"
|
||||||
text_model = "deepseek-ai/DeepSeek-R1"
|
text_model = "deepseek-reasoner"
|
||||||
text_base_url = "https://api.siliconflow.cn/v1"
|
text_base_url = "https://api.deepseek.com"
|
||||||
video_frame_description_path = "/Users/apple/Desktop/home/NarratoAI/storage/temp/analysis/frame_analysis_20250508_1139.json"
|
video_frame_description_path = "/Users/apple/Desktop/home/NarratoAI/storage/temp/analysis/frame_analysis_20250508_1139.json"
|
||||||
|
|
||||||
# 测试新的JSON文件
|
# 测试新的JSON文件
|
||||||
|
|||||||
@ -376,7 +376,7 @@ def generate_script_docu(params):
|
|||||||
if script is None:
|
if script is None:
|
||||||
st.error("生成脚本失败,请检查日志")
|
st.error("生成脚本失败,请检查日志")
|
||||||
st.stop()
|
st.stop()
|
||||||
logger.info(f"脚本生成完成")
|
logger.success(f"剪辑脚本生成完成")
|
||||||
if isinstance(script, list):
|
if isinstance(script, list):
|
||||||
st.session_state['video_clip_json'] = script
|
st.session_state['video_clip_json'] = script
|
||||||
elif isinstance(script, str):
|
elif isinstance(script, str):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user