Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import torch
|
|
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
from pyannote.audio import Pipeline
|
@@ -59,7 +60,7 @@ def combine_audio_with_time(target_audio, mixed_audio):
|
|
59 |
return {"start_time": target_start_time, "end_time": target_end_time}
|
60 |
|
61 |
# 使用 pyannote/speaker-diarization 对拼接后的音频进行说话人分离
|
62 |
-
@
|
63 |
def diarize_audio(temp_file):
|
64 |
if pipeline is None:
|
65 |
return "错误: 模型未初始化"
|
@@ -73,8 +74,8 @@ def diarize_audio(temp_file):
|
|
73 |
except Exception as e:
|
74 |
return f"处理音频时出错: {e}"
|
75 |
|
76 |
-
#
|
77 |
-
def get_speaker_segments(diarization, target_start_time, target_end_time, final_audio_length):
|
78 |
speaker_segments = {}
|
79 |
|
80 |
# 遍历所有说话人时间段
|
@@ -83,24 +84,27 @@ def get_speaker_segments(diarization, target_start_time, target_end_time, final_
|
|
83 |
end = turn.end
|
84 |
|
85 |
# 如果是目标说话人
|
86 |
-
if speaker ==
|
87 |
-
#
|
88 |
if start < target_end_time and end > target_start_time:
|
89 |
-
#
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
98 |
|
99 |
return speaker_segments
|
100 |
|
101 |
# 处理音频文件并返回输出
|
102 |
-
def process_audio(target_audio, mixed_audio):
|
103 |
-
print(f"处理音频:目标音频: {target_audio}, 混合音频: {mixed_audio}")
|
104 |
|
105 |
# 进行音频拼接并返回目标音频的起始和结束时间(作为字典)
|
106 |
time_dict = combine_audio_with_time(target_audio, mixed_audio)
|
@@ -118,33 +122,35 @@ def process_audio(target_audio, mixed_audio):
|
|
118 |
# 获取拼接后的音频长度
|
119 |
final_audio_length = len(AudioSegment.from_wav("final_output.wav")) / 1000 # 秒为单位
|
120 |
|
121 |
-
#
|
122 |
speaker_segments = get_speaker_segments(
|
123 |
diarization_result,
|
|
|
124 |
time_dict['start_time'],
|
125 |
time_dict['end_time'],
|
126 |
final_audio_length
|
127 |
)
|
128 |
|
129 |
-
if speaker_segments and
|
130 |
# 返回目标说话人的时间段(已排除和截断目标音频时间段)
|
131 |
return {
|
132 |
-
'segments': speaker_segments[
|
133 |
-
'total_duration': sum(end - start for start, end in speaker_segments[
|
134 |
}
|
135 |
else:
|
136 |
-
return "没有找到
|
137 |
|
138 |
# Gradio 接口
|
139 |
with gr.Blocks() as demo:
|
140 |
-
gr.Markdown("""
|
141 |
# 🗣️ 音频拼接与说话人分类 🗣️
|
142 |
上传目标音频和混合音频,拼接并进行说话人分类。
|
143 |
-
|
144 |
""")
|
145 |
|
146 |
mixed_audio_input = gr.Audio(type="filepath", label="上传混合音频")
|
147 |
target_audio_input = gr.Audio(type="filepath", label="上传目标说话人音频")
|
|
|
148 |
|
149 |
process_button = gr.Button("处理音频")
|
150 |
|
@@ -154,7 +160,7 @@ with gr.Blocks() as demo:
|
|
154 |
# 点击按钮时触发处理音频
|
155 |
process_button.click(
|
156 |
fn=process_audio,
|
157 |
-
inputs=[target_audio_input, mixed_audio_input],
|
158 |
outputs=[diarization_output]
|
159 |
)
|
160 |
|
|
|
1 |
import torch
|
2 |
+
import spaces
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
from pyannote.audio import Pipeline
|
|
|
60 |
return {"start_time": target_start_time, "end_time": target_end_time}
|
61 |
|
62 |
# 使用 pyannote/speaker-diarization 对拼接后的音频进行说话人分离
|
63 |
+
@spaces.GPU(duration=60 * 2) # 使用 GPU 加速,限制执行时间为 120 秒
|
64 |
def diarize_audio(temp_file):
|
65 |
if pipeline is None:
|
66 |
return "错误: 模型未初始化"
|
|
|
74 |
except Exception as e:
|
75 |
return f"处理音频时出错: {e}"
|
76 |
|
77 |
+
# 获取指定说话人的时间段(排除目标音频时间段)
|
78 |
+
def get_speaker_segments(diarization, speaker_name, target_start_time, target_end_time, final_audio_length):
|
79 |
speaker_segments = {}
|
80 |
|
81 |
# 遍历所有说话人时间段
|
|
|
84 |
end = turn.end
|
85 |
|
86 |
# 如果是目标说话人
|
87 |
+
if speaker == speaker_name:
|
88 |
+
# 如果时间段与目标音频有重叠,需要截断
|
89 |
if start < target_end_time and end > target_start_time:
|
90 |
+
# 记录被截断的时间段
|
91 |
+
if start < target_start_time:
|
92 |
+
# 目标音频开始前的时间段
|
93 |
+
speaker_segments.setdefault(speaker, []).append((start, min(target_start_time, end)))
|
94 |
+
|
95 |
+
if end > target_end_time:
|
96 |
+
# 目标音频结束后的时间段
|
97 |
+
speaker_segments.setdefault(speaker, []).append((max(target_end_time, start), min(end, final_audio_length)))
|
98 |
+
else:
|
99 |
+
# 完全不与目标音频重叠的时间段
|
100 |
+
if end <= target_start_time or start >= target_end_time:
|
101 |
+
speaker_segments.setdefault(speaker, []).append((start, end))
|
102 |
|
103 |
return speaker_segments
|
104 |
|
105 |
# 处理音频文件并返回输出
|
106 |
+
def process_audio(target_audio, mixed_audio, speaker_name):
|
107 |
+
print(f"处理音频:目标音频: {target_audio}, 混合音频: {mixed_audio}, 提取说话人: {speaker_name}")
|
108 |
|
109 |
# 进行音频拼接并返回目标音频的起始和结束时间(作为字典)
|
110 |
time_dict = combine_audio_with_time(target_audio, mixed_audio)
|
|
|
122 |
# 获取拼接后的音频长度
|
123 |
final_audio_length = len(AudioSegment.from_wav("final_output.wav")) / 1000 # 秒为单位
|
124 |
|
125 |
+
# 获取目标说话人的时间段(排除目标音频时间段)
|
126 |
speaker_segments = get_speaker_segments(
|
127 |
diarization_result,
|
128 |
+
speaker_name,
|
129 |
time_dict['start_time'],
|
130 |
time_dict['end_time'],
|
131 |
final_audio_length
|
132 |
)
|
133 |
|
134 |
+
if speaker_segments and speaker_name in speaker_segments:
|
135 |
# 返回目标说话人的时间段(已排除和截断目标音频时间段)
|
136 |
return {
|
137 |
+
'segments': speaker_segments[speaker_name],
|
138 |
+
'total_duration': sum(end - start for start, end in speaker_segments[speaker_name])
|
139 |
}
|
140 |
else:
|
141 |
+
return f"没有找到 {speaker_name} 的时间段。"
|
142 |
|
143 |
# Gradio 接口
|
144 |
with gr.Blocks() as demo:
|
145 |
+
gr.Markdown("""
|
146 |
# 🗣️ 音频拼接与说话人分类 🗣️
|
147 |
上传目标音频和混合音频,拼接并进行说话人分类。
|
148 |
+
结果包括指定说话人的时间段,已排除和截断目标录音时间段。
|
149 |
""")
|
150 |
|
151 |
mixed_audio_input = gr.Audio(type="filepath", label="上传混合音频")
|
152 |
target_audio_input = gr.Audio(type="filepath", label="上传目标说话人音频")
|
153 |
+
speaker_name_input = gr.Textbox(label="请输入说话人名称(如 'SPEAKER_01')", value="SPEAKER_00")
|
154 |
|
155 |
process_button = gr.Button("处理音频")
|
156 |
|
|
|
160 |
# 点击按钮时触发处理音频
|
161 |
process_button.click(
|
162 |
fn=process_audio,
|
163 |
+
inputs=[target_audio_input, mixed_audio_input, speaker_name_input],
|
164 |
outputs=[diarization_output]
|
165 |
)
|
166 |
|