cnph001 commited on
Commit
41426a6
·
verified ·
1 Parent(s): cc5bb17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -14,6 +14,7 @@ from pydub.silence import detect_nonsilent
14
  from pydub import AudioSegment
15
 
16
  default_voice_short= ""
 
17
 
18
  def strip_silence(audio: AudioSegment, silence_thresh=-40, min_silence_len=100, silence_padding_ms=100):
19
  from pydub.silence import detect_nonsilent
@@ -66,6 +67,7 @@ async def get_voices():
66
 
67
  async def generate_audio_with_voice_prefix(text_segment, default_voice, rate, pitch):
68
  global default_voice_short # Use the global variable
 
69
  """Generates audio for a text segment, handling voice prefixes, retries, and fallback."""
70
  print(f"Text: {text_segment}") #Debug
71
  voice_map = {
@@ -132,8 +134,10 @@ async def generate_audio_with_voice_prefix(text_segment, default_voice, rate, pi
132
  await communicate.save(audio_path)
133
 
134
  audio = AudioSegment.from_mp3(audio_path)
135
- audio = strip_silence(audio, silence_thresh=-40, min_silence_len=100)
136
-
 
 
137
  stripped_path = tempfile.mktemp(suffix=".mp3")
138
  audio.export(stripped_path, format="mp3")
139
  return stripped_path
@@ -153,6 +157,7 @@ async def process_transcript_line(line, default_voice, rate, pitch):
153
  """Processes a single transcript line with HH:MM:SS.milliseconds timestamp and quoted text segments."""
154
  match = re.match(r'(\d{2}):(\d{2}):(\d{2}),(\d{3})\s+(.*)', line) # Modified timestamp regex
155
  if match:
 
156
  hours, minutes, seconds, milliseconds, text_parts = match.groups()
157
  start_time_ms = (
158
  int(hours) * 3600000 +
@@ -162,13 +167,18 @@ async def process_transcript_line(line, default_voice, rate, pitch):
162
  )
163
  audio_segments = []
164
  split_parts = re.split(r'(")', text_parts) # Split by quote marks, keeping the quotes
165
-
 
 
166
  process_next = False
167
  for part in split_parts:
168
  if part == '"':
169
  process_next = not process_next
170
  continue
171
  if process_next and part.strip():
 
 
 
172
  audio_path = await generate_audio_with_voice_prefix(part, default_voice, rate, pitch)
173
  if audio_path:
174
  audio_segments.append(audio_path)
 
14
  from pydub import AudioSegment
15
 
16
  default_voice_short= ""
17
+ check1 = False # set global variable to check to see if process_text is begin of transcript line or not.
18
 
19
  def strip_silence(audio: AudioSegment, silence_thresh=-40, min_silence_len=100, silence_padding_ms=100):
20
  from pydub.silence import detect_nonsilent
 
67
 
68
  async def generate_audio_with_voice_prefix(text_segment, default_voice, rate, pitch):
69
  global default_voice_short # Use the global variable
70
+ global check1 # Use the global variable
71
  """Generates audio for a text segment, handling voice prefixes, retries, and fallback."""
72
  print(f"Text: {text_segment}") #Debug
73
  voice_map = {
 
134
  await communicate.save(audio_path)
135
 
136
  audio = AudioSegment.from_mp3(audio_path)
137
+ if check1:
138
+ audio = strip_silence(audio, silence_thresh=-40, min_silence_len=100) ##silence between sentences
139
+ else:
140
+ audio = strip_silence(audio, silence_thresh=-40, min_silence_len=30) ##less silence for mid-sentence segments
141
  stripped_path = tempfile.mktemp(suffix=".mp3")
142
  audio.export(stripped_path, format="mp3")
143
  return stripped_path
 
157
  """Processes a single transcript line with HH:MM:SS.milliseconds timestamp and quoted text segments."""
158
  match = re.match(r'(\d{2}):(\d{2}):(\d{2}),(\d{3})\s+(.*)', line) # Modified timestamp regex
159
  if match:
160
+ count = 0
161
  hours, minutes, seconds, milliseconds, text_parts = match.groups()
162
  start_time_ms = (
163
  int(hours) * 3600000 +
 
167
  )
168
  audio_segments = []
169
  split_parts = re.split(r'(")', text_parts) # Split by quote marks, keeping the quotes
170
+ # Initialize a variable to track if it's the first iteration
171
+ global check1 # Use the global variable
172
+ check1 = True
173
  process_next = False
174
  for part in split_parts:
175
  if part == '"':
176
  process_next = not process_next
177
  continue
178
  if process_next and part.strip():
179
+ if check1:
180
+ # Skip the first iteration logic here if needed
181
+ check1 = True # After first iteration, set it to True
182
  audio_path = await generate_audio_with_voice_prefix(part, default_voice, rate, pitch)
183
  if audio_path:
184
  audio_segments.append(audio_path)