QLWD commited on
Commit
aa9a5ce
1 Parent(s): 307d4dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import torch
2
- import spaces
3
  import gradio as gr
4
  import os
5
  from pyannote.audio import Pipeline
@@ -60,7 +59,7 @@ def combine_audio_with_time(target_audio, mixed_audio):
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,7 +73,7 @@ def diarize_audio(temp_file):
74
  except Exception as e:
75
  return f"处理音频时出错: {e}"
76
 
77
- # 获取目标说话人的时间段(排除目标音频时间段)
78
  def get_speaker_segments(diarization, target_start_time, target_end_time, final_audio_length):
79
  speaker_segments = {}
80
 
@@ -85,20 +84,17 @@ def get_speaker_segments(diarization, target_start_time, target_end_time, final_
85
 
86
  # 如果是目标说话人
87
  if speaker == 'SPEAKER_00':
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
 
@@ -122,7 +118,7 @@ def process_audio(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
  time_dict['start_time'],
@@ -162,4 +158,4 @@ with gr.Blocks() as demo:
162
  outputs=[diarization_output]
163
  )
164
 
165
- demo.launch(share=True)
 
1
  import torch
 
2
  import gradio as gr
3
  import os
4
  from pyannote.audio import Pipeline
 
59
  return {"start_time": target_start_time, "end_time": target_end_time}
60
 
61
  # 使用 pyannote/speaker-diarization 对拼接后的音频进行说话人分离
62
+ @gr.Interface(duration=60 * 2) # 使用 GPU 加速,限制执行时间为 120 秒
63
  def diarize_audio(temp_file):
64
  if pipeline is None:
65
  return "错误: 模型未初始化"
 
73
  except Exception as e:
74
  return f"处理音频时出错: {e}"
75
 
76
+ # 获取目标说话人的时间段并替换指定的SPEAKER_00
77
  def get_speaker_segments(diarization, target_start_time, target_end_time, final_audio_length):
78
  speaker_segments = {}
79
 
 
84
 
85
  # 如果是目标说话人
86
  if speaker == 'SPEAKER_00':
87
+ # 替换目标音频的时间段
88
  if start < target_end_time and end > target_start_time:
89
+ # 目标音频时间段被截断,重新计算其时间段
90
+ new_start = max(start, target_start_time)
91
+ new_end = min(end, target_end_time)
92
+ speaker_segments.setdefault(speaker, []).append((new_start, new_end))
93
+
94
+ else:
95
+ # 完全不与目标音频重叠的时间段
96
+ if end <= target_start_time or start >= target_end_time:
97
+ speaker_segments.setdefault(speaker, []).append((start, end))
 
 
 
98
 
99
  return speaker_segments
100
 
 
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'],
 
158
  outputs=[diarization_output]
159
  )
160
 
161
+ demo.launch(share=True)