1, Add language settings for llm outputs

2, Optimize llm prompts
3, Add timeout handling for material downloads
This commit is contained in:
harry
2024-03-26 16:48:14 +08:00
parent fdbd81d164
commit c5dad43c2c
6 changed files with 57 additions and 33 deletions

View File

@@ -94,6 +94,9 @@ class VideoParams:
video_concat_mode: Optional[VideoConcatMode] = VideoConcatMode.random.value video_concat_mode: Optional[VideoConcatMode] = VideoConcatMode.random.value
video_clip_duration: Optional[int] = 5 video_clip_duration: Optional[int] = 5
video_count: Optional[int] = 1 video_count: Optional[int] = 1
video_language: Optional[str] = "" # auto detect
voice_name: Optional[str] = VoiceNames[0] voice_name: Optional[str] = VoiceNames[0]
bgm_type: Optional[str] = "random" bgm_type: Optional[str] = "random"
bgm_file: Optional[str] = "" bgm_file: Optional[str] = ""

View File

@@ -72,10 +72,10 @@ def _generate_response(prompt: str) -> str:
if response: if response:
content = response.choices[0].message.content content = response.choices[0].message.content
return content return content.replace("\n", "")
def generate_script(video_subject: str, language: str = "zh-CN", paragraph_number: int = 1) -> str: def generate_script(video_subject: str, language: str = "", paragraph_number: int = 1) -> str:
prompt = f""" prompt = f"""
# Role: Video Script Generator # Role: Video Script Generator
@@ -92,13 +92,12 @@ Generate a script for a video, depending on the subject of the video.
7. you must not mention the prompt, or anything about the script itself. also, never talk about the amount of paragraphs or lines. just write the script. 7. you must not mention the prompt, or anything about the script itself. also, never talk about the amount of paragraphs or lines. just write the script.
8. respond in the same language as the video subject. 8. respond in the same language as the video subject.
## Output Example:
What is the meaning of life. This question has puzzled philosophers.
# Initialization: # Initialization:
- video subject: {video_subject} - video subject: {video_subject}
- number of paragraphs: {paragraph_number} - number of paragraphs: {paragraph_number}
""".strip() """.strip()
if language:
prompt += f"\n- language: {language}"
final_script = "" final_script = ""
logger.info(f"subject: {video_subject}") logger.info(f"subject: {video_subject}")

View File

@@ -71,7 +71,7 @@ def search_videos(search_term: str,
break break
return video_items return video_items
except Exception as e: except Exception as e:
logger.error(f"search videos failed: {e}") logger.error(f"search videos failed: {str(e)}")
return [] return []
@@ -81,7 +81,7 @@ def save_video(video_url: str, save_dir: str) -> str:
video_path = f"{save_dir}/{video_id}.mp4" video_path = f"{save_dir}/{video_id}.mp4"
proxies = config.pexels.get("proxies", None) proxies = config.pexels.get("proxies", None)
with open(video_path, "wb") as f: with open(video_path, "wb") as f:
f.write(requests.get(video_url, proxies=proxies, verify=False).content) f.write(requests.get(video_url, proxies=proxies, verify=False, timeout=(10, 180)).content)
return video_path return video_path
@@ -129,6 +129,6 @@ def download_videos(task_id: str,
logger.info(f"total duration of downloaded videos: {total_duration} seconds, skip downloading more") logger.info(f"total duration of downloaded videos: {total_duration} seconds, skip downloading more")
break break
except Exception as e: except Exception as e:
logger.error(f"failed to download video: {item}, {e}") logger.error(f"failed to download video: {utils.to_json(item)} => {str(e)}")
logger.success(f"downloaded {len(video_paths)} videos") logger.success(f"downloaded {len(video_paths)} videos")
return video_paths return video_paths

View File

@@ -48,7 +48,7 @@ def start(task_id, params: VideoParams):
logger.info("\n\n## generating video script") logger.info("\n\n## generating video script")
video_script = params.video_script.strip() video_script = params.video_script.strip()
if not video_script: if not video_script:
video_script = llm.generate_script(video_subject=video_subject, language=language, video_script = llm.generate_script(video_subject=video_subject, language=params.video_language,
paragraph_number=paragraph_number) paragraph_number=paragraph_number)
else: else:
logger.debug(f"video script: \n{video_script}") logger.debug(f"video script: \n{video_script}")

View File

@@ -26,7 +26,7 @@ def tts(text: str, voice_name: str, voice_file: str) -> [SubMaker, None]:
logger.info(f"completed, output file: {voice_file}") logger.info(f"completed, output file: {voice_file}")
return sub_maker return sub_maker
except Exception as e: except Exception as e:
logger.error(f"failed, error: {e}") logger.error(f"failed, error: {str(e)}")
return None return None
@@ -61,6 +61,8 @@ def create_subtitle(sub_maker: submaker.SubMaker, text: str, subtitle_file: str)
script_lines_without_space = [line.replace(" ", "") for line in script_lines] script_lines_without_space = [line.replace(" ", "") for line in script_lines]
sub_line = "" sub_line = ""
try:
for _, (offset, sub) in enumerate(zip(sub_maker.offset, sub_maker.subs)): for _, (offset, sub) in enumerate(zip(sub_maker.offset, sub_maker.subs)):
_start_time, end_time = offset _start_time, end_time = offset
if start_time < 0: if start_time < 0:
@@ -85,6 +87,9 @@ def create_subtitle(sub_maker: submaker.SubMaker, text: str, subtitle_file: str)
with open(subtitle_file, "w", encoding="utf-8") as file: with open(subtitle_file, "w", encoding="utf-8") as file:
file.write("\n".join(sub_items) + "\n") file.write("\n".join(sub_items) + "\n")
except Exception as e:
logger.error(f"failed, error: {str(e)}")
def get_audio_duration(sub_maker: submaker.SubMaker): def get_audio_duration(sub_maker: submaker.SubMaker):
""" """

View File

@@ -95,9 +95,26 @@ with left_panel:
st.write("**文案设置**") st.write("**文案设置**")
cfg.video_subject = st.text_input("视频主题(给定一个关键词,:red[AI自动生成]视频文案)", cfg.video_subject = st.text_input("视频主题(给定一个关键词,:red[AI自动生成]视频文案)",
value=st.session_state['video_subject']).strip() value=st.session_state['video_subject']).strip()
video_languages = [
("自动判断Auto detect", ""),
]
for lang in ["zh-CN", "zh-TW", "en-US"]:
video_languages.append((lang, lang))
selected_index = st.selectbox("生成视频脚本的语言(:blue[一般情况AI会自动根据你输入的主题语言输出]",
index=1,
options=range(len(video_languages)), # 使用索引作为内部选项值
format_func=lambda x: video_languages[x][0] # 显示给用户的是标签
)
cfg.video_language = video_languages[selected_index][1]
if cfg.video_language:
st.write(f"设置AI输出文案语言为: **:red[{cfg.video_language}]**")
if st.button("点击使用AI根据**主题**生成 【视频文案】 和 【视频关键词】", key="auto_generate_script"): if st.button("点击使用AI根据**主题**生成 【视频文案】 和 【视频关键词】", key="auto_generate_script"):
with st.spinner("AI正在生成视频文案和关键词..."): with st.spinner("AI正在生成视频文案和关键词..."):
script = llm.generate_script(cfg.video_subject) script = llm.generate_script(video_subject=cfg.video_subject, language=cfg.video_language)
terms = llm.generate_terms(cfg.video_subject, script) terms = llm.generate_terms(cfg.video_subject, script)
st.toast('AI生成成功') st.toast('AI生成成功')
st.session_state['video_script'] = script st.session_state['video_script'] = script
@@ -106,7 +123,7 @@ with left_panel:
cfg.video_script = st.text_area( cfg.video_script = st.text_area(
"视频文案(:blue[①可不填使用AI生成 ②合理使用标点断句,有助于生成字幕]", "视频文案(:blue[①可不填使用AI生成 ②合理使用标点断句,有助于生成字幕]",
value=st.session_state['video_script'], value=st.session_state['video_script'],
height=280 height=230
) )
if st.button("点击使用AI根据**文案**生成【视频关键词】", key="auto_generate_terms"): if st.button("点击使用AI根据**文案**生成【视频关键词】", key="auto_generate_terms"):
if not cfg.video_script: if not cfg.video_script: