Spaces:
Running
Running
修复AI歌词复制功能
Browse files
app.py
CHANGED
|
@@ -254,7 +254,7 @@ def mix_full_song(vocal: AudioSegment, bgm: AudioSegment, bgm_volume: float = 0.
|
|
| 254 |
return bgm_adj.overlay(vocal)
|
| 255 |
|
| 256 |
|
| 257 |
-
def
|
| 258 |
"""生成AI歌词的prompt"""
|
| 259 |
processed_lyrics = preprocess_zh_spaces(lyrics_text) if lyrics_text else ""
|
| 260 |
|
|
@@ -271,25 +271,7 @@ def copy_prompt_to_clipboard(lyrics_text: str) -> str:
|
|
| 271 |
|
| 272 |
现在请帮我基于上述原始歌词模板,写一首歌曲《历史的进程推着人前进》,主题为:个人奋斗固然重要,但是历史进程更加浩浩汤汤。"""
|
| 273 |
|
| 274 |
-
|
| 275 |
-
try:
|
| 276 |
-
import pyperclip
|
| 277 |
-
pyperclip.copy(prompt)
|
| 278 |
-
return "Prompt已复制到剪切板!"
|
| 279 |
-
except ImportError:
|
| 280 |
-
# 如果没有pyperclip,使用系统命令
|
| 281 |
-
try:
|
| 282 |
-
import subprocess
|
| 283 |
-
import platform
|
| 284 |
-
if platform.system() == "Darwin": # macOS
|
| 285 |
-
subprocess.run(["pbcopy"], input=prompt.encode(), check=True)
|
| 286 |
-
elif platform.system() == "Linux":
|
| 287 |
-
subprocess.run(["xclip", "-selection", "clipboard"], input=prompt.encode(), check=True)
|
| 288 |
-
elif platform.system() == "Windows":
|
| 289 |
-
subprocess.run(["clip"], input=prompt.encode(), check=True)
|
| 290 |
-
return "Prompt已复制到剪切板!"
|
| 291 |
-
except Exception as e:
|
| 292 |
-
return f"复制失败:{e}"
|
| 293 |
|
| 294 |
|
| 295 |
def apply_ai_lyrics(ai_lyrics: str, original_lyrics: str) -> Tuple[str, str]:
|
|
@@ -632,15 +614,28 @@ def build_ui():
|
|
| 632 |
|
| 633 |
# AI歌词功能
|
| 634 |
with gr.Accordion("AI歌词", open=False):
|
| 635 |
-
gr.Markdown("
|
| 636 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
ai_lyrics_input = gr.Textbox(
|
| 638 |
label="回填歌词",
|
| 639 |
lines=10,
|
| 640 |
max_lines=15,
|
| 641 |
placeholder="请将大模型生成的歌词粘贴到这里..."
|
| 642 |
)
|
| 643 |
-
apply_lyrics_btn = gr.Button("应用歌词")
|
| 644 |
|
| 645 |
# 右栏:模板与歌词编辑
|
| 646 |
with gr.Column(elem_id="right-panel", scale=2):
|
|
@@ -1038,13 +1033,12 @@ def build_ui():
|
|
| 1038 |
return (spk_upd, *tpl_upds)
|
| 1039 |
|
| 1040 |
# AI歌词功能事件绑定
|
| 1041 |
-
def
|
| 1042 |
-
"""
|
| 1043 |
# 将当前歌词列表合并为文本
|
| 1044 |
lyrics_text = '\n'.join(lines_list) if lines_list else ""
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
return message
|
| 1048 |
|
| 1049 |
def handle_apply_lyrics(ai_lyrics, lines_list, original_lines_list):
|
| 1050 |
"""处理应用歌词到文本框"""
|
|
@@ -1101,10 +1095,19 @@ def build_ui():
|
|
| 1101 |
|
| 1102 |
return textbox_updates + error_updates + [new_lines]
|
| 1103 |
|
| 1104 |
-
|
| 1105 |
-
|
|
|
|
| 1106 |
inputs=[lines_state],
|
| 1107 |
-
outputs=[]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1108 |
)
|
| 1109 |
|
| 1110 |
apply_lyrics_btn.click(
|
|
|
|
| 254 |
return bgm_adj.overlay(vocal)
|
| 255 |
|
| 256 |
|
| 257 |
+
def generate_ai_prompt(lyrics_text: str) -> str:
|
| 258 |
"""生成AI歌词的prompt"""
|
| 259 |
processed_lyrics = preprocess_zh_spaces(lyrics_text) if lyrics_text else ""
|
| 260 |
|
|
|
|
| 271 |
|
| 272 |
现在请帮我基于上述原始歌词模板,写一首歌曲《历史的进程推着人前进》,主题为:个人奋斗固然重要,但是历史进程更加浩浩汤汤。"""
|
| 273 |
|
| 274 |
+
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
|
| 277 |
def apply_ai_lyrics(ai_lyrics: str, original_lyrics: str) -> Tuple[str, str]:
|
|
|
|
| 614 |
|
| 615 |
# AI歌词功能
|
| 616 |
with gr.Accordion("AI歌词", open=False):
|
| 617 |
+
gr.Markdown("点击按钮生成prompt并复制,然后到大模型APP粘贴,将结果回填到下方文本框")
|
| 618 |
+
|
| 619 |
+
# 使用Gradio 4.44.0的内置复制功能
|
| 620 |
+
ai_prompt_display = gr.Textbox(
|
| 621 |
+
label="AI歌词Prompt",
|
| 622 |
+
lines=8,
|
| 623 |
+
max_lines=12,
|
| 624 |
+
interactive=False,
|
| 625 |
+
placeholder="点击下方按钮生成prompt..."
|
| 626 |
+
)
|
| 627 |
+
|
| 628 |
+
with gr.Row():
|
| 629 |
+
generate_prompt_btn = gr.Button("生成Prompt", variant="primary")
|
| 630 |
+
copy_prompt_btn = gr.Button("📋 复制", variant="secondary")
|
| 631 |
+
|
| 632 |
ai_lyrics_input = gr.Textbox(
|
| 633 |
label="回填歌词",
|
| 634 |
lines=10,
|
| 635 |
max_lines=15,
|
| 636 |
placeholder="请将大模型生成的歌词粘贴到这里..."
|
| 637 |
)
|
| 638 |
+
apply_lyrics_btn = gr.Button("应用歌词", variant="primary")
|
| 639 |
|
| 640 |
# 右栏:模板与歌词编辑
|
| 641 |
with gr.Column(elem_id="right-panel", scale=2):
|
|
|
|
| 1033 |
return (spk_upd, *tpl_upds)
|
| 1034 |
|
| 1035 |
# AI歌词功能事件绑定
|
| 1036 |
+
def handle_generate_prompt(lines_list):
|
| 1037 |
+
"""处理生成prompt"""
|
| 1038 |
# 将当前歌词列表合并为文本
|
| 1039 |
lyrics_text = '\n'.join(lines_list) if lines_list else ""
|
| 1040 |
+
prompt = generate_ai_prompt(lyrics_text)
|
| 1041 |
+
return prompt
|
|
|
|
| 1042 |
|
| 1043 |
def handle_apply_lyrics(ai_lyrics, lines_list, original_lines_list):
|
| 1044 |
"""处理应用歌词到文本框"""
|
|
|
|
| 1095 |
|
| 1096 |
return textbox_updates + error_updates + [new_lines]
|
| 1097 |
|
| 1098 |
+
# 生成prompt按钮
|
| 1099 |
+
generate_prompt_btn.click(
|
| 1100 |
+
fn=handle_generate_prompt,
|
| 1101 |
inputs=[lines_state],
|
| 1102 |
+
outputs=[ai_prompt_display]
|
| 1103 |
+
)
|
| 1104 |
+
|
| 1105 |
+
# 使用Gradio内置的复制功能
|
| 1106 |
+
copy_prompt_btn.click(
|
| 1107 |
+
fn=lambda x: x, # 直接返回文本内容
|
| 1108 |
+
inputs=[ai_prompt_display],
|
| 1109 |
+
outputs=[],
|
| 1110 |
+
js="(text) => { navigator.clipboard.writeText(text); return text; }"
|
| 1111 |
)
|
| 1112 |
|
| 1113 |
apply_lyrics_btn.click(
|