cnph001 commited on
Commit
fb91b69
·
verified ·
1 Parent(s): 7acedd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -7,6 +7,8 @@ import os
7
  import re # Import the regular expression module
8
  from pathlib import Path
9
 
 
 
10
 
11
  # Get all available voices
12
  async def get_voices():
@@ -30,23 +32,18 @@ async def paragraph_to_speech(text, voice, rate, pitch):
30
  audio_segments = []
31
  silence_durations = []
32
  parts = re.split(r'(SS\d+\.?\d*)', text)
33
-
34
  for part in parts:
35
  if re.match(r'SS\d+\.?\d*', part):
36
- try:
37
- #silence_duration = float(part[2:])
38
- # Get the absolute path to Silence.mp3
39
- base_dir = Path(__file__).parent.absolute()
40
- silence_path = base_dir / "Silence.mp3"
41
- print(f"Looking for silence file at: {silence_path}") # Debug output
42
- print(f"Directory contents: {os.listdir(base_dir)}") # Debug output
43
- #silence_path = os.path.join(os.path.dirname(__file__), "Silence.mp3")
44
- if os.path.exists(silence_path):
45
- audio_segments.append(silence_path)
46
- else:
47
- print(f"Warning: Silence.mp3 not found at {silence_path}")
48
- except ValueError:
49
- print(f"Warning: Invalid silence duration format: {part}")
50
  elif part.strip():
51
  processed_text = part
52
  current_voice = voice
 
7
  import re # Import the regular expression module
8
  from pathlib import Path
9
 
10
+ # At the top of your file:
11
+ SILENCE_PATH = Path(__file__).parent.absolute() / "Silence.mp3"
12
 
13
  # Get all available voices
14
  async def get_voices():
 
32
  audio_segments = []
33
  silence_durations = []
34
  parts = re.split(r'(SS\d+\.?\d*)', text)
 
35
  for part in parts:
36
  if re.match(r'SS\d+\.?\d*', part):
37
+ if SILENCE_PATH.exists():
38
+ audio_segments.append(str(SILENCE_PATH))
39
+ print(f"Silence added at {SILENCE_PATH}")
40
+ else:
41
+ # Create silent segment programmatically
42
+ silent_audio = AudioSegment.silent(duration=1000) # 1 second
43
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
44
+ silent_audio.export(tmp_file.name, format="mp3")
45
+ audio_segments.append(tmp_file.name)
46
+ print(f"Created silent segment at {tmp_file.name}")
 
 
 
 
47
  elif part.strip():
48
  processed_text = part
49
  current_voice = voice