latuan commited on
Commit
fd0629f
1 Parent(s): e9c562f
Files changed (1) hide show
  1. app.py +34 -5
app.py CHANGED
@@ -145,12 +145,21 @@ def read_srt(file_path):
145
 
146
  for i in range(0, len(lines), 4):
147
  if i+2 < len(lines):
148
- start_time, end_time = lines[i+1].strip().split(' --> ')
 
 
149
  text = lines[i+2].strip()
150
  subtitles.append((start_time, end_time, text))
151
 
152
  return subtitles
153
 
 
 
 
 
 
 
 
154
  def time_to_seconds(time_str):
155
  h, m, s = time_str.split(':')
156
  seconds = int(h) * 3600 + int(m) * 60 + float(s.replace(',', '.'))
@@ -179,8 +188,12 @@ def generate_audio_with_pause(srt_file_path):
179
 
180
  def srt_to_audio_multi(srt_files):
181
  output_paths = []
 
182
 
183
  def process_file(srt_file):
 
 
 
184
  audio_data = generate_audio_with_pause(srt_file.name)
185
  output_path = os.path.join(cache_dir, f'output_{os.path.basename(srt_file.name)}.wav')
186
  torchaudio.save(output_path, torch.tensor(audio_data).unsqueeze(0), 16000)
@@ -189,23 +202,39 @@ def srt_to_audio_multi(srt_files):
189
  with concurrent.futures.ThreadPoolExecutor() as executor:
190
  futures = [executor.submit(process_file, srt_file) for srt_file in srt_files]
191
  for future in concurrent.futures.as_completed(futures):
192
- output_paths.append(future.result())
 
 
 
 
 
193
 
194
  return output_paths
195
 
 
 
 
 
 
 
196
  # UI display
197
  css = '''
198
  #title{text-align: center}
 
199
  '''
 
200
  with gr.Blocks(css=css) as demo:
201
  title = gr.HTML(
202
  """<h1>SRT to Audio Tool</h1>""",
203
  elem_id="title",
204
  )
205
- inp = gr.File(label="Upload SRT files", file_count="multiple", type="filepath")
206
- out = gr.File(label="Generated Audio Files", file_count="multiple", type="filepath")
 
 
 
207
 
208
- inp.change(fn=srt_to_audio_multi, inputs=inp, outputs=out)
209
 
210
  if __name__ == "__main__":
211
  demo.launch()
 
145
 
146
  for i in range(0, len(lines), 4):
147
  if i+2 < len(lines):
148
+ start_time, end_time = lines[i+1].strip().split('-->')
149
+ start_time = start_time.strip()
150
+ end_time = end_time.strip()
151
  text = lines[i+2].strip()
152
  subtitles.append((start_time, end_time, text))
153
 
154
  return subtitles
155
 
156
+ def is_valid_srt(file_path):
157
+ try:
158
+ read_srt(file_path)
159
+ return True
160
+ except:
161
+ return False
162
+
163
  def time_to_seconds(time_str):
164
  h, m, s = time_str.split(':')
165
  seconds = int(h) * 3600 + int(m) * 60 + float(s.replace(',', '.'))
 
188
 
189
  def srt_to_audio_multi(srt_files):
190
  output_paths = []
191
+ invalid_files = []
192
 
193
  def process_file(srt_file):
194
+ if not is_valid_srt(srt_file.name):
195
+ invalid_files.append(srt_file.name)
196
+ return None
197
  audio_data = generate_audio_with_pause(srt_file.name)
198
  output_path = os.path.join(cache_dir, f'output_{os.path.basename(srt_file.name)}.wav')
199
  torchaudio.save(output_path, torch.tensor(audio_data).unsqueeze(0), 16000)
 
202
  with concurrent.futures.ThreadPoolExecutor() as executor:
203
  futures = [executor.submit(process_file, srt_file) for srt_file in srt_files]
204
  for future in concurrent.futures.as_completed(futures):
205
+ result = future.result()
206
+ if result:
207
+ output_paths.append(result)
208
+
209
+ if invalid_files:
210
+ raise ValueError(f"Invalid SRT files: {', '.join(invalid_files)}")
211
 
212
  return output_paths
213
 
214
+ # Initialize model
215
+ model = Model(
216
+ model_name=default_model_name,
217
+ speaker_url=""
218
+ )
219
+
220
  # UI display
221
  css = '''
222
  #title{text-align: center}
223
+ #container{display: flex; justify-content: space-between; align-items: center;}
224
  '''
225
+
226
  with gr.Blocks(css=css) as demo:
227
  title = gr.HTML(
228
  """<h1>SRT to Audio Tool</h1>""",
229
  elem_id="title",
230
  )
231
+ with gr.Row(elem_id="container"):
232
+ inp = gr.File(label="Upload SRT files", file_count="multiple", type="filepath")
233
+ out = gr.File(label="Generated Audio Files", file_count="multiple", type="filepath")
234
+
235
+ btn = gr.Button("Generate")
236
 
237
+ btn.click(fn=srt_to_audio_multi, inputs=inp, outputs=out)
238
 
239
  if __name__ == "__main__":
240
  demo.launch()