|
import torch |
|
import spaces |
|
import gradio as gr |
|
import os |
|
from pyannote.audio import Pipeline |
|
from pydub import AudioSegment |
|
|
|
|
|
HF_TOKEN = os.environ.get("HUGGINGFACE_READ_TOKEN") |
|
pipeline = None |
|
|
|
|
|
try: |
|
pipeline = Pipeline.from_pretrained( |
|
"pyannote/speaker-diarization-3.1", use_auth_token=HF_TOKEN |
|
) |
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
pipeline.to(device) |
|
except Exception as e: |
|
print(f"Error initializing pipeline: {e}") |
|
pipeline = None |
|
|
|
|
|
def combine_audio_with_time(target_audio, mixed_audio): |
|
if pipeline is None: |
|
return "错误: 模型未初始化" |
|
|
|
|
|
print(f"目标音频文件路径: {target_audio}") |
|
print(f"混合音频文件路径: {mixed_audio}") |
|
|
|
|
|
try: |
|
target_audio_segment = AudioSegment.from_wav(target_audio) |
|
except Exception as e: |
|
return f"加载目标音频时出错: {e}" |
|
|
|
|
|
try: |
|
mixed_audio_segment = AudioSegment.from_wav(mixed_audio) |
|
except Exception as e: |
|
return f"加载混合音频时出错: {e}" |
|
|
|
|
|
target_start_time = len(mixed_audio_segment) / 1000 |
|
|
|
|
|
target_end_time = target_start_time + len(target_audio_segment) / 1000 |
|
|
|
|
|
final_audio = mixed_audio_segment + target_audio_segment |
|
final_audio.export("final_output.wav", format="wav") |
|
|
|
|
|
return {"start_time": target_start_time, "end_time": target_end_time} |
|
|
|
|
|
@spaces.GPU(duration=60 * 2) |
|
def diarize_audio(temp_file): |
|
if pipeline is None: |
|
return "错误: 模型未初始化" |
|
|
|
try: |
|
diarization = pipeline(temp_file) |
|
except Exception as e: |
|
return f"处理音频时出错: {e}" |
|
|
|
print(diarization) |
|
|
|
|
|
return diarization |
|
|
|
|
|
def timestamp_to_seconds(timestamp): |
|
try: |
|
h, m, s = map(float, timestamp.split(':')) |
|
return 3600 * h + 60 * m + s |
|
except ValueError as e: |
|
print(f"转换时间戳时出错: '{timestamp}'. 错误: {e}") |
|
return None |
|
|
|
|
|
def calculate_overlap(start1, end1, start2, end2): |
|
overlap_start = max(start1, start2) |
|
overlap_end = min(end1, end2) |
|
overlap_duration = max(0, overlap_end - overlap_start) |
|
return overlap_duration |
|
|
|
|
|
def get_all_speaker_segments(diarization_output, target_start_time, target_end_time, final_audio_length): |
|
speaker_segments = {} |
|
|
|
|
|
for speech_turn in diarization_output.itertracks(yield_label=True): |
|
start_seconds = speech_turn[0].start |
|
end_seconds = speech_turn[0].end |
|
label = speech_turn[1] |
|
|
|
|
|
if start_seconds < target_end_time and end_seconds > target_start_time: |
|
|
|
end_seconds = min(end_seconds, final_audio_length) |
|
|
|
|
|
if label not in speaker_segments: |
|
speaker_segments[label] = [] |
|
|
|
|
|
speaker_segments[label].append((start_seconds, end_seconds)) |
|
|
|
return speaker_segments |
|
|
|
|
|
def process_audio(target_audio, mixed_audio): |
|
|
|
print(f"处理音频:目标音频: {target_audio}, 混合音频: {mixed_audio}") |
|
|
|
|
|
time_dict = combine_audio_with_time(target_audio, mixed_audio) |
|
|
|
|
|
diarization_result = diarize_audio("final_output.wav") |
|
|
|
if isinstance(diarization_result, str) and diarization_result.startswith("错误"): |
|
return diarization_result, None |
|
else: |
|
|
|
final_audio_length = len(AudioSegment.from_wav("final_output.wav")) / 1000 |
|
|
|
|
|
speaker_segments = get_all_speaker_segments(diarization_result, time_dict['start_time'], time_dict['end_time'], final_audio_length) |
|
|
|
if speaker_segments: |
|
|
|
return speaker_segments |
|
else: |
|
return "没有找到任何说话人的时间段。" |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown(""" |
|
# 🗣️ 音频拼接与说话人分类 🗣️ |
|
上传目标音频和混合音频,拼接并进行说话人分类。结果包括所有说话人的时间段(排除目标录音时间段)。 |
|
""") |
|
|
|
mixed_audio_input = gr.Audio(type="filepath", label="上传混合音频") |
|
target_audio_input = gr.Audio(type="filepath", label="上传目标说话人音频") |
|
|
|
process_button = gr.Button("处理音频") |
|
|
|
|
|
diarization_output = gr.Textbox(label="说话人时间段") |
|
|
|
|
|
process_button.click( |
|
fn=process_audio, |
|
inputs=[target_audio_input, mixed_audio_input], |
|
outputs=[diarization_output] |
|
) |
|
|
|
demo.launch(share=True) |
|
|