jinv2's picture
Update app.py
3cba65c verified
import os
import gradio as gr
import subprocess
import uuid
import shutil
# --- 1. 赛博雅韵 UI (符合神思庭美学) ---
shensist_theme = gr.themes.Base(primary_hue="indigo").set(
body_background_fill="*neutral_950",
block_background_fill="*neutral_900",
)
def shensist_agent_pure_vocal(audio_path, history):
if history is None: history = []
# [节点 1: 拦截并确认意图]
history.append({"role": "user", "content": "激活智能体:请剥离伴奏,交付纯净清唱干音。"})
yield history
# [节点 2: 后台强制剥离]
history.append({"role": "assistant", "content": "🔵 [天算代理] 正在启动 UVR 核心逻辑,进行资产提纯..."})
yield history
try:
output_dir = os.path.abspath("./agent_output")
if os.path.exists(output_dir): shutil.rmtree(output_dir)
# 调用命令行分离引擎
subprocess.run(["demucs", "--two-stems", "vocals", audio_path, "-o", output_dir], check=True)
# --- 核心修复:全路径嗅探与资产搬运 ---
vocal_path = None
for root, _, files in os.walk(output_dir):
if "vocals.wav" in files:
vocal_path = os.path.join(root, "vocals.wav")
break
if vocal_path:
# 强制提取到根目录,确保 100% 能下载和播放
final_vocal = f"Shensist_Vocal_{uuid.uuid4().hex[:6]}.wav"
shutil.copy(vocal_path, final_vocal)
history.pop() # 移除加载状态
history.append({
"role": "assistant",
"content": "【神思庭·灵愿完成】\n伴奏已彻底剔除。这是为您重构的纯净清唱资产。\n\n🧠 天算AI · 超个体构建者\n◼️ 官网|shensist.top",
})
# 明确对象,激活播放器
history.append({"role": "assistant", "content": gr.Audio(final_vocal, label="AI A Cappella 流")})
else:
raise FileNotFoundError("引擎已运行,但人声资产未降临。")
except Exception as e:
if history: history.pop()
history.append({"role": "assistant", "content": f"🔴 算力中断:{str(e)}"})
yield history
with gr.Blocks(theme=shensist_theme, title="神思庭 | AI A Cappella 代理") as demo:
gr.Markdown("# 🏛️ **神思庭 · AI A Cappella 代理**")
chatbot = gr.Chatbot(label="超个体交互场域", height=600)
audio_input = gr.Audio(type="filepath", label="上传原曲")
audio_input.change(shensist_agent_pure_vocal, [audio_input, chatbot], [chatbot])
if __name__ == "__main__":
demo.queue().launch()