jinv2 commited on
Commit
3cba65c
·
verified ·
1 Parent(s): e48df24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -52
app.py CHANGED
@@ -4,80 +4,63 @@ import subprocess
4
  import uuid
5
  import shutil
6
 
7
- # --- 1. 赛博雅韵 UI 主题 ---
8
  shensist_theme = gr.themes.Base(primary_hue="indigo").set(
9
  body_background_fill="*neutral_950",
10
  block_background_fill="*neutral_900",
11
  )
12
 
13
- class ShensistAgent:
14
- def __init__(self):
15
- self.output_dir = os.path.abspath("./agent_assets")
16
- os.makedirs(self.output_dir, exist_ok=True)
17
-
18
- def process_acappella(self, input_path):
19
- # 强制执行无头剥离逻辑
20
- cmd = ["demucs", "--two-stems", "vocals", input_path, "-o", self.output_dir]
21
- subprocess.run(cmd, check=True)
 
 
 
 
 
 
 
 
22
 
23
- # 全维度嗅探生成的 vocals.wav
24
  vocal_path = None
25
- for root, _, files in os.walk(self.output_dir):
26
  if "vocals.wav" in files:
27
  vocal_path = os.path.join(root, "vocals.wav")
28
  break
29
 
30
  if vocal_path:
31
- # 物理重定位:确保 Gradio直接访问
32
- final_path = os.path.join(os.getcwd(), f"pure_soul_{uuid.uuid4().hex[:6]}.wav")
33
- shutil.copy(vocal_path, final_path)
34
- return final_path
35
- return None
36
-
37
- agent = ShensistAgent()
38
-
39
- def shensist_agent_sing(audio_path, history):
40
- if history is None: history = []
41
-
42
- # [节点 1: 指令拦截]
43
- history.append({"role": "user", "content": "激活智能体。要求:剥离伴奏,输出纯净清唱。"})
44
- yield history, gr.MultimodalTextbox(interactive=False)
45
-
46
- # [节点 2: 后台提纯]
47
- history.append({"role": "assistant", "content": "🔵 [天算代理] 正在启动 UVR 核心,强制执行伴奏剥离..."})
48
- yield history, gr.MultimodalTextbox(interactive=False)
49
-
50
- try:
51
- # 执行核心工作流
52
- vocal_file = agent.process_acappella(audio_path)
53
-
54
- if vocal_file:
55
- history.pop() # 移除加载中消息
56
  history.append({
57
  "role": "assistant",
58
- "content": "【神思庭·灵愿完成】\n伴奏已彻底剔除。这是为您重**纯净清唱干音**资产。",
59
  })
60
- # --- 关键:强制渲染音频组件 ---
61
- history.append({"role": "assistant", "content": gr.Audio(vocal_file, label="AI A Cappella 流", autoplay=True)})
62
  else:
63
- raise FileNotFoundError("提纯引擎已运行,但资产未在指定维度降临。")
64
 
65
  except Exception as e:
66
  if history: history.pop()
67
- history.append({"role": "assistant", "content": f"🔴 算力冲突:{str(e)}"})
68
 
69
- yield history, gr.MultimodalTextbox(interactive=True)
70
 
71
- # --- 2. 交互界面构建 ---
72
- with gr.Blocks(title="神思庭 | AI A Cappella 代理") as demo:
73
  gr.Markdown("# 🏛️ **神思庭 · AI A Cappella 代理**")
74
  chatbot = gr.Chatbot(label="超个体交互场域", height=600)
75
-
76
- with gr.Row():
77
- audio_input = gr.Audio(type="filepath", label="上传原曲")
78
- submit_btn = gr.Button("激活代理", variant="primary")
79
-
80
- submit_btn.click(shensist_agent_sing, [audio_input, chatbot], [chatbot, audio_input])
81
 
82
  if __name__ == "__main__":
83
- demo.queue().launch(theme=shensist_theme)
 
4
  import uuid
5
  import shutil
6
 
7
+ # --- 1. 赛博雅韵 UI (符合神思庭美学) ---
8
  shensist_theme = gr.themes.Base(primary_hue="indigo").set(
9
  body_background_fill="*neutral_950",
10
  block_background_fill="*neutral_900",
11
  )
12
 
13
+ def shensist_agent_pure_vocal(audio_path, history):
14
+ if history is None: history = []
15
+
16
+ # [节点 1: 拦截并确认意图]
17
+ history.append({"role": "user", "content": "激活智能体:请剥离伴奏,交付纯净清唱干音。"})
18
+ yield history
19
+
20
+ # [节点 2: 后台强制剥离]
21
+ history.append({"role": "assistant", "content": "🔵 [天算代理] 正在启动 UVR 核心逻辑,进行资产提纯..."})
22
+ yield history
23
+
24
+ try:
25
+ output_dir = os.path.abspath("./agent_output")
26
+ if os.path.exists(output_dir): shutil.rmtree(output_dir)
27
+
28
+ # 调用命令行分离引擎
29
+ subprocess.run(["demucs", "--two-stems", "vocals", audio_path, "-o", output_dir], check=True)
30
 
31
+ # --- 核心修复:路径嗅探与资产搬运 ---
32
  vocal_path = None
33
+ for root, _, files in os.walk(output_dir):
34
  if "vocals.wav" in files:
35
  vocal_path = os.path.join(root, "vocals.wav")
36
  break
37
 
38
  if vocal_path:
39
+ # 强制提取到根目录,确保 100%下载和播放
40
+ final_vocal = f"Shensist_Vocal_{uuid.uuid4().hex[:6]}.wav"
41
+ shutil.copy(vocal_path, final_vocal)
42
+
43
+ history.pop() # 移除加载状态
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  history.append({
45
  "role": "assistant",
46
+ "content": "【神思庭·灵愿完成】\n伴奏已彻底剔除。这是为您重的纯净清唱资产。\n\n🧠 天算AI · 超个体构建者\n◼️ 官网|shensist.top",
47
  })
48
+ # 明确对象,激活播放器
49
+ history.append({"role": "assistant", "content": gr.Audio(final_vocal, label="AI A Cappella 流")})
50
  else:
51
+ raise FileNotFoundError("引擎已运行,但人声资产未降临。")
52
 
53
  except Exception as e:
54
  if history: history.pop()
55
+ history.append({"role": "assistant", "content": f"🔴 算力中断:{str(e)}"})
56
 
57
+ yield history
58
 
59
+ with gr.Blocks(theme=shensist_theme, title="神思庭 | AI A Cappella 代理") as demo:
 
60
  gr.Markdown("# 🏛️ **神思庭 · AI A Cappella 代理**")
61
  chatbot = gr.Chatbot(label="超个体交互场域", height=600)
62
+ audio_input = gr.Audio(type="filepath", label="上传原曲")
63
+ audio_input.change(shensist_agent_pure_vocal, [audio_input, chatbot], [chatbot])
 
 
 
 
64
 
65
  if __name__ == "__main__":
66
+ demo.queue().launch()