hivecorp commited on
Commit
fbe2197
·
verified ·
1 Parent(s): 34ebd65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -27,25 +27,26 @@ async def text_to_speech(text, voice, rate, pitch):
27
  return tmp_path, None
28
 
29
  # Generate SRT file based on user preferences
30
- def generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph):
 
 
 
 
31
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
32
- segment_duration = audio_duration / (len(words) // lines_per_paragraph) # Average duration for each segment
33
- current_time = 0
34
-
35
  for i in range(0, len(words), words_per_line * lines_per_paragraph): # Every segment according to specified preferences
36
  segment_words = words[i:i + (words_per_line * lines_per_paragraph)]
37
  lines = [segment_words[j:j + words_per_line] for j in range(0, len(segment_words), words_per_line)]
38
  lines = [' '.join(line) for line in lines]
39
 
40
  start_time = current_time
41
- end_time = start_time + segment_duration
42
 
43
  start_time_str = format_srt_time(start_time)
44
  end_time_str = format_srt_time(end_time)
45
  srt_file.write(f"{i // (words_per_line * lines_per_paragraph) + 1}\n{start_time_str} --> {end_time_str}\n")
46
  srt_file.write('\n'.join(lines) + '\n\n')
47
 
48
- current_time += segment_duration # Update current time for the next segment
49
 
50
  return srt_path
51
 
@@ -71,7 +72,7 @@ async def text_to_audio_and_srt(text, voice, rate, pitch, words_per_line, lines_
71
  base_name = os.path.splitext(audio_path)[0]
72
  srt_path = f"{base_name}_subtitle.srt"
73
  words = text.split()
74
- generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph)
75
 
76
  return audio_path, srt_path, None
77
 
@@ -100,8 +101,8 @@ async def create_demo():
100
  rate_slider = gr.Slider(minimum=-50, maximum=50, value=0, label="Rate Adjustment (%)", step=1)
101
  pitch_slider = gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
102
 
103
- words_per_line = gr.Slider(minimum=8, maximum=12, value=10, label="Words per Line", step=1)
104
- lines_per_paragraph = gr.Slider(minimum=1, maximum=4, value=2, label="Lines per Paragraph", step=1)
105
 
106
  generate_button = gr.Button("Generate Audio and Subtitles", variant="primary")
107
 
 
27
  return tmp_path, None
28
 
29
  # Generate SRT file based on user preferences
30
+ def generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph, speech_rate):
31
+ segment_duration = audio_duration / (len(words) / lines_per_paragraph) # Average duration for each segment
32
+ adjusted_duration = segment_duration * (60 / (100 + speech_rate)) # Adjust duration based on speech rate
33
+ current_time = 0
34
+
35
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
 
 
 
36
  for i in range(0, len(words), words_per_line * lines_per_paragraph): # Every segment according to specified preferences
37
  segment_words = words[i:i + (words_per_line * lines_per_paragraph)]
38
  lines = [segment_words[j:j + words_per_line] for j in range(0, len(segment_words), words_per_line)]
39
  lines = [' '.join(line) for line in lines]
40
 
41
  start_time = current_time
42
+ end_time = start_time + adjusted_duration
43
 
44
  start_time_str = format_srt_time(start_time)
45
  end_time_str = format_srt_time(end_time)
46
  srt_file.write(f"{i // (words_per_line * lines_per_paragraph) + 1}\n{start_time_str} --> {end_time_str}\n")
47
  srt_file.write('\n'.join(lines) + '\n\n')
48
 
49
+ current_time += adjusted_duration # Update current time for the next segment
50
 
51
  return srt_path
52
 
 
72
  base_name = os.path.splitext(audio_path)[0]
73
  srt_path = f"{base_name}_subtitle.srt"
74
  words = text.split()
75
+ generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph, rate)
76
 
77
  return audio_path, srt_path, None
78
 
 
101
  rate_slider = gr.Slider(minimum=-50, maximum=50, value=0, label="Rate Adjustment (%)", step=1)
102
  pitch_slider = gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
103
 
104
+ words_per_line = gr.Slider(minimum=3, maximum=8, value=5, label="Words per Line", step=1)
105
+ lines_per_paragraph = gr.Slider(minimum=1, maximum=5, value=2, label="Lines per Paragraph", step=1)
106
 
107
  generate_button = gr.Button("Generate Audio and Subtitles", variant="primary")
108