Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
import torch
|
2 |
import os
|
|
|
3 |
from pyannote.audio import Pipeline
|
4 |
from pydub import AudioSegment
|
5 |
-
|
6 |
|
7 |
# 获取 Hugging Face 认证令牌
|
8 |
HF_TOKEN = os.environ.get("HUGGINGFACE_READ_TOKEN")
|
@@ -59,6 +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 |
def diarize_audio(temp_file):
|
63 |
if pipeline is None:
|
64 |
return "错误: 模型未初始化"
|
@@ -97,7 +99,33 @@ def find_best_matching_speaker(target_start_time, target_end_time, diarization):
|
|
97 |
|
98 |
return best_match, max_overlap
|
99 |
|
100 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
def process_audio(target_audio, mixed_audio):
|
102 |
print(f"处理音频:目标音频: {target_audio}, 混合音频: {mixed_audio}")
|
103 |
|
@@ -125,10 +153,22 @@ def process_audio(target_audio, mixed_audio):
|
|
125 |
)
|
126 |
|
127 |
if best_match:
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
else:
|
133 |
return "未找到匹配的说话人。"
|
134 |
|
@@ -137,7 +177,7 @@ with gr.Blocks() as demo:
|
|
137 |
gr.Markdown("""
|
138 |
# 🗣️ 音频拼接与说话人分类 🗣️
|
139 |
上传目标音频和混合音频,拼接并进行说话人分类。
|
140 |
-
|
141 |
""")
|
142 |
|
143 |
mixed_audio_input = gr.Audio(type="filepath", label="上传混合音频")
|
@@ -146,7 +186,7 @@ with gr.Blocks() as demo:
|
|
146 |
process_button = gr.Button("处理音频")
|
147 |
|
148 |
# 输出结果
|
149 |
-
diarization_output = gr.Textbox(label="
|
150 |
|
151 |
# 点击按钮时触发处理音频
|
152 |
process_button.click(
|
|
|
1 |
import torch
|
2 |
import os
|
3 |
+
import gradio as gr
|
4 |
from pyannote.audio import Pipeline
|
5 |
from pydub import AudioSegment
|
6 |
+
from spaces import GPU
|
7 |
|
8 |
# 获取 Hugging Face 认证令牌
|
9 |
HF_TOKEN = os.environ.get("HUGGINGFACE_READ_TOKEN")
|
|
|
60 |
return {"start_time": target_start_time, "end_time": target_end_time}
|
61 |
|
62 |
# 使用 pyannote/speaker-diarization 对拼接后的音频进行说话人分离
|
63 |
+
@GPU(duration=60 * 2) # 使用 GPU 加速,限制执行时间为 120 秒
|
64 |
def diarize_audio(temp_file):
|
65 |
if pipeline is None:
|
66 |
return "错误: 模型未初始化"
|
|
|
99 |
|
100 |
return best_match, max_overlap
|
101 |
|
102 |
+
# 获取目标说话人的时间段(排除目标音频时间段)
|
103 |
+
def get_speaker_segments(diarization, target_start_time, target_end_time, final_audio_length):
|
104 |
+
speaker_segments = {}
|
105 |
+
|
106 |
+
# 遍历所有说话人时间段
|
107 |
+
for turn, _, speaker in diarization.itertracks(yield_label=True):
|
108 |
+
start = turn.start
|
109 |
+
end = turn.end
|
110 |
+
|
111 |
+
# 如果时间段与目标音频有重叠,需要截断
|
112 |
+
if start < target_end_time and end > target_start_time:
|
113 |
+
# 记录被截断的时间段
|
114 |
+
if start < target_start_time:
|
115 |
+
# 目标音频开始前的时间段
|
116 |
+
speaker_segments.setdefault(speaker, []).append((start, min(target_start_time, end)))
|
117 |
+
|
118 |
+
if end > target_end_time:
|
119 |
+
# 目标音频结束后的时间段
|
120 |
+
speaker_segments.setdefault(speaker, []).append((max(target_end_time, start), min(end, final_audio_length)))
|
121 |
+
else:
|
122 |
+
# 完全不与目标音频重叠的时间段
|
123 |
+
if end <= target_start_time or start >= target_end_time:
|
124 |
+
speaker_segments.setdefault(speaker, []).append((start, end))
|
125 |
+
|
126 |
+
return speaker_segments
|
127 |
+
|
128 |
+
# 处理音频文件并返回输出
|
129 |
def process_audio(target_audio, mixed_audio):
|
130 |
print(f"处理音频:目标音频: {target_audio}, 混合音频: {mixed_audio}")
|
131 |
|
|
|
153 |
)
|
154 |
|
155 |
if best_match:
|
156 |
+
# 获取目标说话人的时间段(排除和截断目标音频时间段)
|
157 |
+
speaker_segments = get_speaker_segments(
|
158 |
+
diarization_result,
|
159 |
+
time_dict['start_time'],
|
160 |
+
time_dict['end_time'],
|
161 |
+
final_audio_length
|
162 |
+
)
|
163 |
+
|
164 |
+
if best_match in speaker_segments:
|
165 |
+
return {
|
166 |
+
'best_matching_speaker': best_match,
|
167 |
+
'overlap_duration': overlap_duration,
|
168 |
+
'segments': speaker_segments[best_match]
|
169 |
+
}
|
170 |
+
else:
|
171 |
+
return "没有找到匹配的说话人时间段。"
|
172 |
else:
|
173 |
return "未找到匹配的说话人。"
|
174 |
|
|
|
177 |
gr.Markdown("""
|
178 |
# 🗣️ 音频拼接与说话人分类 🗣️
|
179 |
上传目标音频和混合音频,拼接并进行说话人分类。
|
180 |
+
结果包括最匹配的说话人及其时间段,已排除和截断目标录音时间段。
|
181 |
""")
|
182 |
|
183 |
mixed_audio_input = gr.Audio(type="filepath", label="上传混合音频")
|
|
|
186 |
process_button = gr.Button("处理音频")
|
187 |
|
188 |
# 输出结果
|
189 |
+
diarization_output = gr.Textbox(label="说话人时间段")
|
190 |
|
191 |
# 点击按钮时触发处理音频
|
192 |
process_button.click(
|